Browse Source

- Added colour logging to the logger

- Rewrote all logging to use the logger
- IPA should look for `Beat Saber.exe` if loaded without having the application dropped on it
refactor
artman41 6 years ago
parent
commit
b94588b9bd
11 changed files with 63 additions and 43 deletions
  1. +1
    -1
      IPA.Tests/ProgramTest.cs
  2. +1
    -1
      IPA/PatchContext.cs
  3. +10
    -4
      IPA/Program.cs
  4. +7
    -4
      IllusionInjector/CompositePlugin.cs
  5. +3
    -0
      IllusionInjector/IllusionInjector.csproj
  6. +0
    -6
      IllusionInjector/PluginComponent.cs
  7. +11
    -10
      IllusionInjector/PluginManager.cs
  8. +4
    -0
      IllusionPlugin/IllusionPlugin.csproj
  9. +26
    -17
      IllusionPlugin/Logger.cs
  10. BIN
      Libs/UnityEngine.CoreModule.dll
  11. BIN
      Libs/UnityEngine.dll

+ 1
- 1
IPA.Tests/ProgramTest.cs View File

@ -30,7 +30,7 @@ namespace IPA.Tests
public void CopiesCorrectly(string from, string to, string nativeFolder, bool isFlat, string[] expected) public void CopiesCorrectly(string from, string to, string nativeFolder, bool isFlat, string[] expected)
{ {
var outcome = Program.NativePluginInterceptor(new FileInfo(from), new FileInfo(to), new DirectoryInfo(nativeFolder), isFlat).Select(f => f.FullName).ToList();
var outcome = Program.NativePluginInterceptor(new FileInfo(from), new FileInfo(to), new DirectoryInfo(nativeFolder), isFlat, Program.Architecture.Unknown).Select(f => f.FullName).ToList();
var expectedPaths = expected.Select(e => new FileInfo(e)).Select(f => f.FullName).ToList(); var expectedPaths = expected.Select(e => new FileInfo(e)).Select(f => f.FullName).ToList();
Assert.Equal(expectedPaths, outcome); Assert.Equal(expectedPaths, outcome);


+ 1
- 1
IPA/PatchContext.cs View File

@ -52,7 +52,7 @@ namespace IPA
context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll"); context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll");
context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll"); context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll");
context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName); context.BackupPath = Path.Combine(Path.Combine(context.IPARoot, "Backups"), context.ProjectName);
string shortcutName = string.Format("{0} (Patch & Launch)", context.ProjectName);
string shortcutName = $"{context.ProjectName} (Patch & Launch)";
context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk"; context.ShortcutPath = Path.Combine(context.ProjectRoot, shortcutName) + ".lnk";
Directory.CreateDirectory(context.BackupPath); Directory.CreateDirectory(context.BackupPath);


+ 10
- 4
IPA/Program.cs View File

@ -21,16 +21,22 @@ namespace IPA
Unknown Unknown
} }
static void Main(string[] args)
{
static void Main(string[] args) {
PatchContext context;
Console.WriteLine($"{args[0]}");
if(args.Length < 1 || !args[0].EndsWith(".exe")) if(args.Length < 1 || !args[0].EndsWith(".exe"))
{ {
Fail("Drag an (executable) file on the exe!");
//Fail("Drag an (executable) file on the exe!");
context = PatchContext.Create(new [] {"./Beat Saber.exe"});
}
else {
context = PatchContext.Create(args);
} }
try try
{ {
var context = PatchContext.Create(args);
bool isRevert = args.Contains("--revert") || Keyboard.IsKeyDown(Keys.LMenu); bool isRevert = args.Contains("--revert") || Keyboard.IsKeyDown(Keys.LMenu);
// Sanitizing // Sanitizing
Validate(context); Validate(context);


+ 7
- 4
IllusionInjector/CompositePlugin.cs View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
using Logger = IllusionPlugin.Logger;
namespace IllusionInjector { namespace IllusionInjector {
public class CompositePlugin : IPlugin { public class CompositePlugin : IPlugin {
@ -11,6 +12,8 @@ namespace IllusionInjector {
private delegate void CompositeCall(IPlugin plugin); private delegate void CompositeCall(IPlugin plugin);
private Logger debugLogger => PluginManager.debugLogger;
public CompositePlugin(IEnumerable<IPlugin> plugins) { public CompositePlugin(IEnumerable<IPlugin> plugins) {
this.plugins = plugins; this.plugins = plugins;
} }
@ -29,7 +32,7 @@ namespace IllusionInjector {
plugin.OnSceneLoaded(scene, sceneMode); plugin.OnSceneLoaded(scene, sceneMode);
} }
catch (Exception ex) { catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
} }
} }
} }
@ -40,7 +43,7 @@ namespace IllusionInjector {
plugin.OnSceneUnloaded(scene); plugin.OnSceneUnloaded(scene);
} }
catch (Exception ex) { catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
} }
} }
} }
@ -51,7 +54,7 @@ namespace IllusionInjector {
plugin.OnActiveSceneChanged(prevScene, nextScene); plugin.OnActiveSceneChanged(prevScene, nextScene);
} }
catch (Exception ex) { catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
} }
} }
} }
@ -63,7 +66,7 @@ namespace IllusionInjector {
callback(plugin); callback(plugin);
} }
catch (Exception ex) { catch (Exception ex) {
Console.WriteLine("{0}: {1}", plugin.Name, ex);
debugLogger.Exception($"{plugin.Name}: {ex}");
} }
} }
} }


+ 3
- 0
IllusionInjector/IllusionInjector.csproj View File

@ -39,6 +39,9 @@
<HintPath>..\Libs\UnityEngine.dll</HintPath> <HintPath>..\Libs\UnityEngine.dll</HintPath>
<Private>False</Private> <Private>False</Private>
</Reference> </Reference>
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Bootstrapper.cs" /> <Compile Include="Bootstrapper.cs" />


+ 0
- 6
IllusionInjector/PluginComponent.cs View File

@ -9,7 +9,6 @@ namespace IllusionInjector
public class PluginComponent : MonoBehaviour public class PluginComponent : MonoBehaviour
{ {
private CompositePlugin plugins; private CompositePlugin plugins;
private bool freshlyLoaded = false;
private bool quitting = false; private bool quitting = false;
public static PluginComponent Create() public static PluginComponent Create()
@ -31,10 +30,6 @@ namespace IllusionInjector
void Update() void Update()
{ {
if (freshlyLoaded)
{
freshlyLoaded = false;
}
plugins.OnUpdate(); plugins.OnUpdate();
} }
@ -70,7 +65,6 @@ namespace IllusionInjector
void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{ {
plugins.OnSceneLoaded(scene, sceneMode); plugins.OnSceneLoaded(scene, sceneMode);
freshlyLoaded = true;
} }
private void OnSceneUnloaded(Scene scene) { private void OnSceneUnloaded(Scene scene) {


+ 11
- 10
IllusionInjector/PluginManager.cs View File

@ -15,6 +15,8 @@ namespace IllusionInjector
{ {
private static List<IPlugin> _Plugins = null; private static List<IPlugin> _Plugins = null;
internal static readonly Logger debugLogger = new Logger("IllusionInjector");
/// <summary> /// <summary>
/// Gets the list of loaded plugins and loads them if necessary. /// Gets the list of loaded plugins and loads them if necessary.
/// </summary> /// </summary>
@ -38,7 +40,7 @@ namespace IllusionInjector
// Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL, // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
// so we need to resort to P/Invoke // so we need to resort to P/Invoke
string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath); string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
Console.WriteLine(exeName);
debugLogger.Log(exeName);
_Plugins = new List<IPlugin>(); _Plugins = new List<IPlugin>();
if (!Directory.Exists(pluginDirectory)) return; if (!Directory.Exists(pluginDirectory)) return;
@ -51,16 +53,15 @@ namespace IllusionInjector
// DEBUG // DEBUG
Console.WriteLine("Running on Unity {0}", UnityEngine.Application.unityVersion);
Console.WriteLine("-----------------------------");
Console.WriteLine("Loading plugins from {0} and found {1}", pluginDirectory, _Plugins.Count);
Console.WriteLine("-----------------------------");
debugLogger.Log($"Running on Unity {UnityEngine.Application.unityVersion}");
debugLogger.Log("-----------------------------");
debugLogger.Log($"Loading plugins from {pluginDirectory} and found {_Plugins.Count}");
debugLogger.Log("-----------------------------");
foreach (var plugin in _Plugins) foreach (var plugin in _Plugins)
{ {
Console.WriteLine(" {0}: {1}", plugin.Name, plugin.Version);
debugLogger.Log($"{plugin.Name}: {plugin.Version}");
} }
Console.WriteLine("-----------------------------");
debugLogger.Log("-----------------------------");
} }
private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName) private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName)
@ -94,7 +95,7 @@ namespace IllusionInjector
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("[WARN] Could not load plugin {0} in {1}! {2}", t.FullName, Path.GetFileName(file), e);
debugLogger.Exception($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
} }
} }
} }
@ -102,7 +103,7 @@ namespace IllusionInjector
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine("[ERROR] Could not load {0}! {1}", Path.GetFileName(file), e);
debugLogger.Error($"Could not load {Path.GetFileName(file)}! {e}");
} }
return plugins; return plugins;


+ 4
- 0
IllusionPlugin/IllusionPlugin.csproj View File

@ -36,11 +36,15 @@
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\Libs\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="IEnhancedPlugin.cs" /> <Compile Include="IEnhancedPlugin.cs" />
<Compile Include="IniFile.cs" /> <Compile Include="IniFile.cs" />
<Compile Include="IPlugin.cs" /> <Compile Include="IPlugin.cs" />
<Compile Include="Logger.cs" />
<Compile Include="ModPrefs.cs" /> <Compile Include="ModPrefs.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>


+ 26
- 17
IllusionPlugin/Logger.cs View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Threading; using System.Threading;
using IllusionPlugin; using IllusionPlugin;
@ -11,6 +12,8 @@ namespace IllusionPlugin {
private readonly Thread _watcherThread; private readonly Thread _watcherThread;
private bool _threadRunning; private bool _threadRunning;
private string ModName;
private logMessage oldLog; private logMessage oldLog;
struct logMessage { struct logMessage {
@ -24,18 +27,18 @@ namespace IllusionPlugin {
} }
enum WarningLevel { enum WarningLevel {
Log, Error, Exception
Log, Error, Exception, Warning
} }
Logger() {
public Logger(string modName = "Default") {
_logQueue = new Queue<logMessage>(); _logQueue = new Queue<logMessage>();
_logFile = GetPath("Default");
_logFile = GetPath(modName);
_watcherThread = new Thread(QueueWatcher) {IsBackground = true}; _watcherThread = new Thread(QueueWatcher) {IsBackground = true};
_threadRunning = true; _threadRunning = true;
Start(); Start();
} }
public Logger(IPlugin plugin) {
public Logger(IPlugin plugin) {
_logQueue = new Queue<logMessage>(); _logQueue = new Queue<logMessage>();
_logFile = GetPath(plugin); _logFile = GetPath(plugin);
_watcherThread = new Thread(QueueWatcher) {IsBackground = true}; _watcherThread = new Thread(QueueWatcher) {IsBackground = true};
@ -45,22 +48,26 @@ namespace IllusionPlugin {
public void Log(string msg) { public void Log(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Log));
_logQueue.Enqueue(new logMessage($"[LOG @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Log));
} }
public void Error(string msg) { public void Error(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Error));
_logQueue.Enqueue(new logMessage($"[ERROR @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Error));
} }
public void Exception(string msg) { public void Exception(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!"); if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss}] {msg}", WarningLevel.Exception));
_logQueue.Enqueue(new logMessage($"[EXCEPTION @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Exception));
}
public void Warning(string msg) {
if(!_watcherThread.IsAlive) throw new Exception("Logger is Closed!");
_logQueue.Enqueue(new logMessage($"[WARNING @ {DateTime.Now:HH:mm:ss} | {ModName}] {msg}", WarningLevel.Warning));
} }
void QueueWatcher() { void QueueWatcher() {
_logFile.Create().Close(); _logFile.Create().Close();
SetConsoleColour(WarningLevel.Log);
while (_threadRunning) { while (_threadRunning) {
if (_logQueue.Count > 0) { if (_logQueue.Count > 0) {
_watcherThread.IsBackground = false; _watcherThread.IsBackground = false;
@ -70,8 +77,8 @@ namespace IllusionPlugin {
if (d.Message == oldLog.Message) return; if (d.Message == oldLog.Message) return;
oldLog = d; oldLog = d;
f.WriteLine(d.Message); f.WriteLine(d.Message);
if(d.WarningLevel != oldLog.WarningLevel) SetConsoleColour(d.WarningLevel);
Console.WriteLine(d);
Console.ForegroundColor = GetConsoleColour(d.WarningLevel);
Console.WriteLine(d.Message);
} }
} }
@ -87,22 +94,24 @@ namespace IllusionPlugin {
_watcherThread.Join(); _watcherThread.Join();
} }
void SetConsoleColour(WarningLevel level) {
ConsoleColor GetConsoleColour(WarningLevel level) {
switch (level) { switch (level) {
case WarningLevel.Log: case WarningLevel.Log:
Console.ForegroundColor = ConsoleColor.Green;
break;
return ConsoleColor.Green;
case WarningLevel.Error: case WarningLevel.Error:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
return ConsoleColor.Yellow;
case WarningLevel.Exception: case WarningLevel.Exception:
Console.ForegroundColor = ConsoleColor.Red;
break;
return ConsoleColor.Red;
case WarningLevel.Warning:
return ConsoleColor.Blue;
default:
return ConsoleColor.Gray;
} }
} }
FileInfo GetPath(IPlugin plugin) => GetPath(plugin.Name); FileInfo GetPath(IPlugin plugin) => GetPath(plugin.Name);
FileInfo GetPath(string modName) { FileInfo GetPath(string modName) {
ModName = modName;
var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}"); var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}");
logsDir.Create(); logsDir.Create();
return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt"); return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt");


BIN
Libs/UnityEngine.CoreModule.dll View File


BIN
Libs/UnityEngine.dll View File


Loading…
Cancel
Save