diff --git a/IPA.Tests/ProgramTest.cs b/IPA.Tests/ProgramTest.cs index c13b6c83..902199ce 100644 --- a/IPA.Tests/ProgramTest.cs +++ b/IPA.Tests/ProgramTest.cs @@ -30,7 +30,7 @@ namespace IPA.Tests 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(); Assert.Equal(expectedPaths, outcome); diff --git a/IPA/PatchContext.cs b/IPA/PatchContext.cs index 9867d210..1060a8ad 100644 --- a/IPA/PatchContext.cs +++ b/IPA/PatchContext.cs @@ -52,7 +52,7 @@ namespace IPA context.EngineFile = Path.Combine(context.ManagedPath, "UnityEngine.CoreModule.dll"); context.AssemblyFile = Path.Combine(context.ManagedPath, "Assembly-CSharp.dll"); 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"; Directory.CreateDirectory(context.BackupPath); diff --git a/IPA/Program.cs b/IPA/Program.cs index fc8ce4dd..31be7dfc 100644 --- a/IPA/Program.cs +++ b/IPA/Program.cs @@ -21,16 +21,22 @@ namespace IPA 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")) { - 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 { - var context = PatchContext.Create(args); + bool isRevert = args.Contains("--revert") || Keyboard.IsKeyDown(Keys.LMenu); // Sanitizing Validate(context); diff --git a/IllusionInjector/CompositePlugin.cs b/IllusionInjector/CompositePlugin.cs index 193b702f..f43d9c7f 100644 --- a/IllusionInjector/CompositePlugin.cs +++ b/IllusionInjector/CompositePlugin.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Text; using UnityEngine; using UnityEngine.SceneManagement; +using Logger = IllusionPlugin.Logger; namespace IllusionInjector { public class CompositePlugin : IPlugin { @@ -11,6 +12,8 @@ namespace IllusionInjector { private delegate void CompositeCall(IPlugin plugin); + private Logger debugLogger => PluginManager.debugLogger; + public CompositePlugin(IEnumerable plugins) { this.plugins = plugins; } @@ -29,7 +32,7 @@ namespace IllusionInjector { plugin.OnSceneLoaded(scene, sceneMode); } catch (Exception ex) { - Console.WriteLine("{0}: {1}", plugin.Name, ex); + debugLogger.Exception($"{plugin.Name}: {ex}"); } } } @@ -40,7 +43,7 @@ namespace IllusionInjector { plugin.OnSceneUnloaded(scene); } 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); } catch (Exception ex) { - Console.WriteLine("{0}: {1}", plugin.Name, ex); + debugLogger.Exception($"{plugin.Name}: {ex}"); } } } @@ -63,7 +66,7 @@ namespace IllusionInjector { callback(plugin); } catch (Exception ex) { - Console.WriteLine("{0}: {1}", plugin.Name, ex); + debugLogger.Exception($"{plugin.Name}: {ex}"); } } } diff --git a/IllusionInjector/IllusionInjector.csproj b/IllusionInjector/IllusionInjector.csproj index 6d15b941..98f3142b 100644 --- a/IllusionInjector/IllusionInjector.csproj +++ b/IllusionInjector/IllusionInjector.csproj @@ -39,6 +39,9 @@ ..\Libs\UnityEngine.dll False + + ..\Libs\UnityEngine.CoreModule.dll + diff --git a/IllusionInjector/PluginComponent.cs b/IllusionInjector/PluginComponent.cs index fb58d32e..63602464 100644 --- a/IllusionInjector/PluginComponent.cs +++ b/IllusionInjector/PluginComponent.cs @@ -9,7 +9,6 @@ namespace IllusionInjector public class PluginComponent : MonoBehaviour { private CompositePlugin plugins; - private bool freshlyLoaded = false; private bool quitting = false; public static PluginComponent Create() @@ -31,10 +30,6 @@ namespace IllusionInjector void Update() { - if (freshlyLoaded) - { - freshlyLoaded = false; - } plugins.OnUpdate(); } @@ -70,7 +65,6 @@ namespace IllusionInjector void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) { plugins.OnSceneLoaded(scene, sceneMode); - freshlyLoaded = true; } private void OnSceneUnloaded(Scene scene) { diff --git a/IllusionInjector/PluginManager.cs b/IllusionInjector/PluginManager.cs index 24cced6e..4cb5d3a5 100644 --- a/IllusionInjector/PluginManager.cs +++ b/IllusionInjector/PluginManager.cs @@ -15,6 +15,8 @@ namespace IllusionInjector { private static List _Plugins = null; + internal static readonly Logger debugLogger = new Logger("IllusionInjector"); + /// /// Gets the list of loaded plugins and loads them if necessary. /// @@ -38,7 +40,7 @@ namespace IllusionInjector // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL, // so we need to resort to P/Invoke string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath); - Console.WriteLine(exeName); + debugLogger.Log(exeName); _Plugins = new List(); if (!Directory.Exists(pluginDirectory)) return; @@ -51,16 +53,15 @@ namespace IllusionInjector // 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) { - - Console.WriteLine(" {0}: {1}", plugin.Name, plugin.Version); + debugLogger.Log($"{plugin.Name}: {plugin.Version}"); } - Console.WriteLine("-----------------------------"); + debugLogger.Log("-----------------------------"); } private static IEnumerable LoadPluginsFromFile(string file, string exeName) @@ -94,7 +95,7 @@ namespace IllusionInjector } 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) { - Console.WriteLine("[ERROR] Could not load {0}! {1}", Path.GetFileName(file), e); + debugLogger.Error($"Could not load {Path.GetFileName(file)}! {e}"); } return plugins; diff --git a/IllusionPlugin/IllusionPlugin.csproj b/IllusionPlugin/IllusionPlugin.csproj index 057d612b..ae6ce0cf 100644 --- a/IllusionPlugin/IllusionPlugin.csproj +++ b/IllusionPlugin/IllusionPlugin.csproj @@ -36,11 +36,15 @@ + + ..\Libs\UnityEngine.CoreModule.dll + + diff --git a/IllusionPlugin/Logger.cs b/IllusionPlugin/Logger.cs index 3c201ce0..036550f8 100644 --- a/IllusionPlugin/Logger.cs +++ b/IllusionPlugin/Logger.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Runtime.Remoting.Messaging; using System.Threading; using IllusionPlugin; @@ -11,6 +12,8 @@ namespace IllusionPlugin { private readonly Thread _watcherThread; private bool _threadRunning; + private string ModName; + private logMessage oldLog; struct logMessage { @@ -24,18 +27,18 @@ namespace IllusionPlugin { } enum WarningLevel { - Log, Error, Exception + Log, Error, Exception, Warning } - Logger() { + public Logger(string modName = "Default") { _logQueue = new Queue(); - _logFile = GetPath("Default"); + _logFile = GetPath(modName); _watcherThread = new Thread(QueueWatcher) {IsBackground = true}; _threadRunning = true; Start(); } - public Logger(IPlugin plugin) { + public Logger(IPlugin plugin) { _logQueue = new Queue(); _logFile = GetPath(plugin); _watcherThread = new Thread(QueueWatcher) {IsBackground = true}; @@ -45,22 +48,26 @@ namespace IllusionPlugin { public void Log(string msg) { 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) { 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) { 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() { _logFile.Create().Close(); - SetConsoleColour(WarningLevel.Log); while (_threadRunning) { if (_logQueue.Count > 0) { _watcherThread.IsBackground = false; @@ -70,8 +77,8 @@ namespace IllusionPlugin { if (d.Message == oldLog.Message) return; oldLog = d; 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(); } - void SetConsoleColour(WarningLevel level) { + ConsoleColor GetConsoleColour(WarningLevel level) { switch (level) { case WarningLevel.Log: - Console.ForegroundColor = ConsoleColor.Green; - break; + return ConsoleColor.Green; case WarningLevel.Error: - Console.ForegroundColor = ConsoleColor.Yellow; - break; + return ConsoleColor.Yellow; 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(string modName) { + ModName = modName; var logsDir = new DirectoryInfo($"./Logs/{modName}/{DateTime.Now:dd-MM-yy}"); logsDir.Create(); return new FileInfo($"{logsDir.FullName}/{logsDir.GetFiles().Length}.txt"); diff --git a/Libs/UnityEngine.CoreModule.dll b/Libs/UnityEngine.CoreModule.dll new file mode 100644 index 00000000..30f76f4c Binary files /dev/null and b/Libs/UnityEngine.CoreModule.dll differ diff --git a/Libs/UnityEngine.dll b/Libs/UnityEngine.dll index 6a4c68cb..2842b720 100644 Binary files a/Libs/UnityEngine.dll and b/Libs/UnityEngine.dll differ