From 905386d4b615312aa9c313aabf10d0b7a27f1fb4 Mon Sep 17 00:00:00 2001 From: Kaede Date: Fri, 10 Nov 2023 23:39:29 +0200 Subject: [PATCH] Optional DEBUG logging and accepting patch messages --- Marsey/MarseyPatcher.cs | 6 +++- Marsey/MarseyVars.cs | 26 ++++++++++++-- Marsey/PatchAssemblyManager.cs | 7 +--- Marsey/Utility.cs | 35 +++++++++++++++++-- SS14.Launcher/Models/Connector.cs | 6 ++++ SS14.Launcher/Models/Data/CVars.cs | 10 ++++++ .../MainWindowTabs/OptionsTabViewModel.cs | 20 +++++++++++ .../Views/MainWindowTabs/OptionsTabView.xaml | 10 ++++++ SS14.Loader/Program.cs | 4 +-- 9 files changed, 109 insertions(+), 15 deletions(-) diff --git a/Marsey/MarseyPatcher.cs b/Marsey/MarseyPatcher.cs index 5d5a961..7dbed79 100644 --- a/Marsey/MarseyPatcher.cs +++ b/Marsey/MarseyPatcher.cs @@ -14,7 +14,9 @@ namespace Marsey; public class MarseyPatcher { /// - /// Boots up the patcher, executed by the loader assembly only. + /// Boots up the patcher + /// + /// Executed by the loader. /// /// Robust.Client assembly as *loaded* by the *loader* /// Excepts if Robust.Client assembly is null @@ -22,6 +24,8 @@ public static void Boot(Assembly? robClientAssembly) { if (robClientAssembly == null) throw new Exception("Robust.Client was null."); + Utility.SetupLogFlags(); + GameAssemblyManager.Init(new Harmony(MarseyVars.Identifier)); GameAssemblyManager.GetGameAssemblies(out var clientAss, out var robustSharedAss, out var clientSharedAss); diff --git a/Marsey/MarseyVars.cs b/Marsey/MarseyVars.cs index ae1eb69..8991ce3 100644 --- a/Marsey/MarseyVars.cs +++ b/Marsey/MarseyVars.cs @@ -2,12 +2,32 @@ namespace Marsey; public abstract class MarseyVars { - // Namespace identifier for Harmony + + /// + /// + /// Namespace identifier for Harmony + /// public const string Identifier = "com.validhunters.marseyloader"; - // Max amount of loops allowed to catch game assemblies + /// + /// Max amount of loops allowed to catch game assemblies + /// public const int MaxLoops = 50; - // Cooldown to try the loop again, in ms + /// + /// Cooldown to try the loop again, in ms + /// public const int LoopCooldown = 200; + + /// + /// Log DEBG messages + /// + /// + public static bool DebugAllowed; + + /// + /// Log messages sent from patches + /// + /// + public static bool PatchLogAllowed; } diff --git a/Marsey/PatchAssemblyManager.cs b/Marsey/PatchAssemblyManager.cs index bc2da21..dab89d3 100644 --- a/Marsey/PatchAssemblyManager.cs +++ b/Marsey/PatchAssemblyManager.cs @@ -51,7 +51,7 @@ public static void InitAssembly(Assembly assembly) /// /// Initializes logger class in patches that have it. /// Executed only by the loader. - /// MarseyLogger example can be found in the Rethemer MarseyPatch example. + /// MarseyLogger example can be found in the BasePatch MarseyPatch example. /// public static void InitLogger() { @@ -63,14 +63,9 @@ public static void InitLogger() Type? marseyLoggerType = assembly.GetType("MarseyLogger"); if (marseyLoggerType != null) - { - //Utility.Log(Utility.LogType.DEBG, $"{assembly.GetName().Name} has a MarseyLogger class"); Utility.SetupLogger(assembly); - } else - { Utility.Log(Utility.LogType.DEBG, $"{assembly.GetName().Name} has no MarseyLogger class"); - } } } diff --git a/Marsey/Utility.cs b/Marsey/Utility.cs index 5e0f136..e49c2fc 100644 --- a/Marsey/Utility.cs +++ b/Marsey/Utility.cs @@ -13,16 +13,28 @@ public enum LogType DEBG } - // Loader logs + /// + /// Log function used by the loader + /// + /// Log level + /// Log message public static void Log(LogType logType, string message) { + if (logType == LogType.DEBG && MarseyVars.DebugAllowed != true) + return; + Console.WriteLine($"[MARSEY] [{logType.ToString()}] {message}"); } - // Patch logs + /// + /// Log function used by patches + /// + /// Assembly name of patch + /// Log message public static void Log(AssemblyName asm, string message) { - Console.WriteLine($"[{asm.Name}] {message}"); + if (MarseyVars.PatchLogAllowed) + Console.WriteLine($"[{asm.Name}] {message}"); } /// @@ -43,4 +55,21 @@ public static void SetupLogger(Assembly patch) marseyLoggerType.GetField("logDelegate", BindingFlags.Public | BindingFlags.Static)!.SetValue(null, logDelegate); } + + /// + /// Checks loader environment variables, sets relevant flags in MarseyVars + /// + /// Executed only by the loader. + /// + public static void SetupLogFlags() + { + MarseyVars.PatchLogAllowed = CheckEnv("MARSEY_LOG_PATCHES"); + MarseyVars.DebugAllowed = CheckEnv("MARSEY_LOADER_DEBUG"); + } + + private static bool CheckEnv(string envName) + { + var envVar = Environment.GetEnvironmentVariable(envName)!; + return !string.IsNullOrEmpty(envVar) && bool.Parse(envVar); + } } diff --git a/SS14.Launcher/Models/Connector.cs b/SS14.Launcher/Models/Connector.cs index 1ba4722..85b8c4f 100644 --- a/SS14.Launcher/Models/Connector.cs +++ b/SS14.Launcher/Models/Connector.cs @@ -466,6 +466,12 @@ private async Task InstallContentBundleAsync( startInfo.RedirectStandardError = true; } + if (_cfg.GetCVar(CVars.LogLoaderDebug)) + EnvVar("MARSEY_LOADER_DEBUG", "true"); + + if (_cfg.GetCVar(CVars.LogPatches)) + EnvVar("MARSEY_LOG_PATCHES", "true"); + if (_cfg.GetCVar(CVars.DynamicPgo)) { Log.Debug("Dynamic PGO is enabled."); diff --git a/SS14.Launcher/Models/Data/CVars.cs b/SS14.Launcher/Models/Data/CVars.cs index c5a2b4c..2da665e 100644 --- a/SS14.Launcher/Models/Data/CVars.cs +++ b/SS14.Launcher/Models/Data/CVars.cs @@ -56,6 +56,16 @@ public static readonly CVarDef HasDismissedEarlyAccessWarning /// public static readonly CVarDef LogLauncherVerbose = CVarDef.Create("LogLauncherVerbose", false); + /// + /// Log messages coming from patches + /// + public static readonly CVarDef LogPatches = CVarDef.Create("LogPatches", true); + + /// + /// Log debug messages coming from loader + /// + public static readonly CVarDef LogLoaderDebug = CVarDef.Create("LogLoaderDebug", false); + /// /// Enable multi-account support on release builds. /// diff --git a/SS14.Launcher/ViewModels/MainWindowTabs/OptionsTabViewModel.cs b/SS14.Launcher/ViewModels/MainWindowTabs/OptionsTabViewModel.cs index ae7336d..803d789 100644 --- a/SS14.Launcher/ViewModels/MainWindowTabs/OptionsTabViewModel.cs +++ b/SS14.Launcher/ViewModels/MainWindowTabs/OptionsTabViewModel.cs @@ -84,6 +84,26 @@ public bool LogLauncherVerbose } } + public bool LogPatches + { + get => Cfg.GetCVar(CVars.LogPatches); + set + { + Cfg.SetCVar(CVars.LogPatches, value); + Cfg.CommitConfig(); + } + } + + public bool LogLoaderDebug + { + get => Cfg.GetCVar(CVars.LogLoaderDebug); + set + { + Cfg.SetCVar(CVars.LogLoaderDebug, value); + Cfg.CommitConfig(); + } + } + public bool DisableSigning { get => Cfg.GetCVar(CVars.DisableSigning); diff --git a/SS14.Launcher/Views/MainWindowTabs/OptionsTabView.xaml b/SS14.Launcher/Views/MainWindowTabs/OptionsTabView.xaml index dad0286..a8b4b6e 100644 --- a/SS14.Launcher/Views/MainWindowTabs/OptionsTabView.xaml +++ b/SS14.Launcher/Views/MainWindowTabs/OptionsTabView.xaml @@ -51,6 +51,16 @@ Text="For when the developers are *very* stumped with your problem. (requires launcher restart)" Margin="8" /> + Log Patches + + + Enable Loader Debug Logs + + diff --git a/SS14.Loader/Program.cs b/SS14.Loader/Program.cs index 7d8d40a..ca666be 100644 --- a/SS14.Loader/Program.cs +++ b/SS14.Loader/Program.cs @@ -61,8 +61,8 @@ private bool Run() SQLitePCL.Batteries_V2.Init(); - Thread t = new Thread(() => MarseyPatcher.Boot(clientAssembly)); - t.Start(); + // Start the MarseyPatcher + new Thread(() => MarseyPatcher.Boot(clientAssembly)).Start(); var launcher = Environment.GetEnvironmentVariable("SS14_LAUNCHER_PATH"); var redialApi = launcher != null ? new RedialApi(launcher) : null;