Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
Optional DEBUG logging and accepting patch messages
Browse files Browse the repository at this point in the history
  • Loading branch information
misandrie committed Nov 10, 2023
1 parent 3783bc6 commit 905386d
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 15 deletions.
6 changes: 5 additions & 1 deletion Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ namespace Marsey;
public class MarseyPatcher
{
/// <summary>
/// Boots up the patcher, executed by the loader assembly only.
/// Boots up the patcher
///
/// Executed by the loader.
/// </summary>
/// <param name="robClientAssembly">Robust.Client assembly as *loaded* by the *loader*</param>
/// <exception cref="Exception">Excepts if Robust.Client assembly is null</exception>
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);
Expand Down
26 changes: 23 additions & 3 deletions Marsey/MarseyVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@ namespace Marsey;

public abstract class MarseyVars
{
// Namespace identifier for Harmony

/// <summary>
///
/// Namespace identifier for Harmony
/// </summary>
public const string Identifier = "com.validhunters.marseyloader";

// Max amount of loops allowed to catch game assemblies
/// <summary>
/// Max amount of loops allowed to catch game assemblies
/// </summary>
public const int MaxLoops = 50;

// Cooldown to try the loop again, in ms
/// <summary>
/// Cooldown to try the loop again, in ms
/// </summary>
public const int LoopCooldown = 200;

/// <summary>
/// Log DEBG messages
/// <see cref="Utility.SetupLogFlags"/>
/// </summary>
public static bool DebugAllowed;

/// <summary>
/// Log messages sent from patches
/// <see cref="Utility.SetupLogFlags"/>
/// </summary>
public static bool PatchLogAllowed;
}
7 changes: 1 addition & 6 deletions Marsey/PatchAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void InitAssembly(Assembly assembly)
/// <summary>
/// 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.
/// </summary>
public static void InitLogger()
{
Expand All @@ -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");
}
}
}

Expand Down
35 changes: 32 additions & 3 deletions Marsey/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,28 @@ public enum LogType
DEBG
}

// Loader logs
/// <summary>
/// Log function used by the loader
/// </summary>
/// <param name="logType">Log level</param>
/// <param name="message">Log message</param>
public static void Log(LogType logType, string message)
{
if (logType == LogType.DEBG && MarseyVars.DebugAllowed != true)
return;

Console.WriteLine($"[MARSEY] [{logType.ToString()}] {message}");
}

// Patch logs
/// <summary>
/// Log function used by patches
/// </summary>
/// <param name="asm">Assembly name of patch</param>
/// <param name="message">Log message</param>
public static void Log(AssemblyName asm, string message)
{
Console.WriteLine($"[{asm.Name}] {message}");
if (MarseyVars.PatchLogAllowed)
Console.WriteLine($"[{asm.Name}] {message}");
}

/// <summary>
Expand All @@ -43,4 +55,21 @@ public static void SetupLogger(Assembly patch)

marseyLoggerType.GetField("logDelegate", BindingFlags.Public | BindingFlags.Static)!.SetValue(null, logDelegate);
}

/// <summary>
/// Checks loader environment variables, sets relevant flags in MarseyVars
///
/// Executed only by the loader.
/// </summary>
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);
}
}
6 changes: 6 additions & 0 deletions SS14.Launcher/Models/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,12 @@ private async Task<ContentLaunchInfo> 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.");
Expand Down
10 changes: 10 additions & 0 deletions SS14.Launcher/Models/Data/CVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public static readonly CVarDef<bool> HasDismissedEarlyAccessWarning
/// </summary>
public static readonly CVarDef<bool> LogLauncherVerbose = CVarDef.Create("LogLauncherVerbose", false);

/// <summary>
/// Log messages coming from patches
/// </summary>
public static readonly CVarDef<bool> LogPatches = CVarDef.Create("LogPatches", true);

/// <summary>
/// Log debug messages coming from loader
/// </summary>
public static readonly CVarDef<bool> LogLoaderDebug = CVarDef.Create("LogLoaderDebug", false);

/// <summary>
/// Enable multi-account support on release builds.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions SS14.Launcher/ViewModels/MainWindowTabs/OptionsTabViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions SS14.Launcher/Views/MainWindowTabs/OptionsTabView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
Text="For when the developers are *very* stumped with your problem. (requires launcher restart)"
Margin="8" />

<CheckBox VerticalAlignment="Center" Margin="4" IsChecked="{Binding LogPatches}">Log Patches</CheckBox>
<TextBlock VerticalAlignment="Center" TextWrapping="Wrap"
Text="Write MarseyLogger patch output to log"
Margin="8" />

<CheckBox VerticalAlignment="Center" Margin="4" IsChecked="{Binding LogLoaderDebug}">Enable Loader Debug Logs</CheckBox>
<TextBlock VerticalAlignment="Center" TextWrapping="Wrap"
Text="For when things stop making sense"
Margin="8" />

<TextBlock VerticalAlignment="Center" TextWrapping="Wrap"
Text="Set your current active account name to a funny"
Margin="8"/>
Expand Down
4 changes: 2 additions & 2 deletions SS14.Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 905386d

Please sign in to comment.