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

Commit

Permalink
Proper logging
Browse files Browse the repository at this point in the history
Uncaught assemblies get warned instead of causing the entire patcher to die

Marseyloader doesn't start catching assemblies if no patches are enabled.
  • Loading branch information
misandrie committed Nov 4, 2023
1 parent a361de4 commit 30ec509
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 16 deletions.
13 changes: 13 additions & 0 deletions Marsey/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
namespace Marsey;

public class PatchAssemblyException : Exception
{
public PatchAssemblyException(string message) : base(message) {}

public override string ToString()
{
Utility.Log(Utility.LogType.FATL, Message);
return base.ToString();
}
}
4 changes: 2 additions & 2 deletions Marsey/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public static void LoadAssemblies(string[]? path = null)
Assembly assembly = Assembly.LoadFrom(file);
PatchAssemblyManager.InitAssembly(assembly);
}
catch (Exception ex)
catch (PatchAssemblyException ex)
{
Console.WriteLine($"Failed to load assembly from {file}. Error: {ex.Message}");
Utility.Log(Utility.LogType.FATL, ex.Message);
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions Marsey/GameAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Marsey;
public class GameAssemblyManager
{
private static Harmony? _harmony;
private static readonly int MaxLoops = 50;

/// <summary>
/// Sets the _harmony field in the class.
Expand All @@ -30,7 +31,7 @@ public static void PatchProc()
{
foreach (MarseyPatch p in PatchAssemblyManager.GetPatchList())
{
Console.WriteLine($"[MARSEY] Patching {p.Asm.GetName()}");
Utility.Log(Utility.LogType.INFO, $"Patching {p.Asm.GetName()}");
_harmony.PatchAll(p.Asm);
}
}
Expand All @@ -40,19 +41,19 @@ public static void PatchProc()
/// Obtains game assemblies.
/// The function ends only when Robust.Shared,
/// Content.Client and Content.Shared are initialized by the game,
/// or 100 loops have been made without obtaining all the assemblies.
/// or $MaxLoops loops have been made without obtaining all the assemblies.
///
/// Executed only by the Loader.
/// </summary>
/// <exception cref="Exception">Excepts if manager couldn't get game assemblies after 100 loops.</exception>
/// <exception cref="Exception">Excepts if manager couldn't get game assemblies after $MaxLoops loops.</exception>
public static void GetGameAssemblies(out Assembly? clientAss, out Assembly? robustSharedAss, out Assembly? clientSharedAss)
{
clientAss = null;
robustSharedAss = null;
clientSharedAss = null;

int loops = 0;
while (robustSharedAss == null || clientAss == null || clientSharedAss == null && loops < 100)
while ((robustSharedAss == null || clientAss == null || clientSharedAss == null) && loops < MaxLoops)
{
Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies();
foreach (var e in asms)
Expand All @@ -79,9 +80,18 @@ public static void GetGameAssemblies(out Assembly? clientAss, out Assembly? robu
Thread.Sleep(200);
}

if (loops >= 100)
throw new Exception("Failed to receive assemblies within 100 loops.");
if (loops >= MaxLoops)
{
Utility.Log(Utility.LogType.WARN, "Total amount of loops exhausted.");

if (clientAss == null)
Utility.Log(Utility.LogType.WARN, "Content.Client assembly was not received.");
if (clientSharedAss == null)
Utility.Log(Utility.LogType.WARN, "Client.Shared assembly was not received.");
if (robustSharedAss == null)
Utility.Log(Utility.LogType.WARN, "Robust.Shared assembly was not received");
}
else
Console.WriteLine("[MARSEY] Received assemblies.");
Utility.Log(Utility.LogType.INFO, "Received assemblies.");
}
}
13 changes: 9 additions & 4 deletions Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ public void Boot(Assembly? robClientAssembly)

GameAssemblyManager.Init(new Harmony("com.validhunters.marseyloader"));

// Exception from this function halts execution.
// Patcher won't work if any of the 3 variables are null
FileHandler.LoadAssemblies(new []{"Marsey", "Enabled"});

// If no patches are enabled - don't bother doing anything with the game.
if (PatchAssemblyManager.GetPatchList().Count == 0)
{
Utility.Log(Utility.LogType.INFO, "No patches loaded. Not capturing game assemblies.");
return;
}

GameAssemblyManager.GetGameAssemblies(out var clientAss, out var robustSharedAss, out var clientSharedAss);

PatchAssemblyManager.SetAssemblies(robClientAssembly, clientAss, robustSharedAss, clientSharedAss);

FileHandler.LoadAssemblies(new []{"Marsey", "Enabled"});

GameAssemblyManager.PatchProc();
}
}
7 changes: 4 additions & 3 deletions Marsey/PatchAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public class PatchAssemblyManager
/// <exception cref="Exception">Excepts if GetPatchAssemblyFields returns null</exception>
public static void InitAssembly(Assembly assembly)
{
Type marseyPatchType = assembly.GetType("MarseyPatch") ?? throw new Exception("Loaded assembly does not have MarseyPatch type.");
Type marseyPatchType = assembly.GetType("MarseyPatch") ?? throw new PatchAssemblyException("Loaded assembly does not have MarseyPatch type.");

if (marseyPatchType.GetField("TargetAssembly") != null) throw new Exception($"{assembly.FullName} cannot be loaded because it uses an outdated patch!");
if (marseyPatchType.GetField("TargetAssembly") != null) throw new PatchAssemblyException($"{assembly.FullName} cannot be loaded because it uses an outdated patch!");

List<FieldInfo> targets = GetPatchAssemblyFields(marseyPatchType) ?? throw new Exception($"Couldn't get assembly fields on {assembly.FullName}.");
List<FieldInfo> targets = GetPatchAssemblyFields(marseyPatchType) ?? throw new PatchAssemblyException($"Couldn't get assembly fields on {assembly.FullName}.");

SetAssemblyTargets(targets);

Expand All @@ -48,6 +48,7 @@ public static void InitAssembly(Assembly assembly)
}

_patchAssemblies.Add(patch);

}

/// <summary>
Expand Down
18 changes: 18 additions & 0 deletions Marsey/Utility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace Marsey;

public class Utility
{
public enum LogType
{
INFO,
WARN,
FATL,
DEBG
}
public static void Log(LogType logType, string message)
{
Console.WriteLine($"[MARSEY] [{logType.ToString()}] {message}");
}
}

0 comments on commit 30ec509

Please sign in to comment.