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

Commit

Permalink
Add support for patch-specific logging that is not a Console.Writelin…
Browse files Browse the repository at this point in the history
…e in the patch itself.
  • Loading branch information
misandrie committed Nov 9, 2023
1 parent 5c303d6 commit ee44f57
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public static void Boot(Assembly? robClientAssembly)

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

PatchAssemblyManager.InitLogger();

GameAssemblyManager.PatchProc();
}
}
36 changes: 30 additions & 6 deletions Marsey/PatchAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ public abstract class PatchAssemblyManager
/// Initializes a given assembly, validates its structure, and adds it to the list of patch assemblies
/// </summary>
/// <param name="assembly">The assembly to initialize</param>
/// <exception cref="Exception">Excepts if MarseyPatch is not present in the assembly</exception>
/// <exception cref="Exception">Excepts if "TargetAssembly" is present in the assembly MarseyPatch type"</exception>
/// <exception cref="Exception">Excepts if GetPatchAssemblyFields returns null</exception>
/// <exception cref="PatchAssemblyException">Excepts if MarseyPatch is not present in the assembly</exception>
/// <exception cref="PatchAssemblyException">Excepts if "TargetAssembly" is present in the assembly MarseyPatch type"</exception>
/// <exception cref="PatchAssemblyException">Excepts if GetPatchAssemblyFields returns null</exception>
public static void InitAssembly(Assembly assembly)
{
Type marseyPatchType = assembly.GetType("MarseyPatch") ?? throw new PatchAssemblyException("Loaded assembly does not have MarseyPatch type.");

if (marseyPatchType.GetField("TargetAssembly") != null) throw new PatchAssemblyException($"{assembly.FullName} cannot be loaded because it uses an outdated patch!");
Type marseyPatchType = assembly.GetType("MarseyPatch") ?? throw new PatchAssemblyException("Loaded assembly does not have MarseyPatch type. Most likely because MarseyPatch is under a namespace, or you provided a non-patch dll.");

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

Expand All @@ -51,6 +49,32 @@ 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.
/// </summary>
public static void InitLogger()
{
foreach (MarseyPatch patch in _patchAssemblies)
{
Assembly assembly = patch.Asm;

// Check for a logger class
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");
}
}
}

/// <summary>
/// Obtains fields for each of the game's assemblies.
/// Returns null if any of the fields is null.
Expand Down
28 changes: 28 additions & 0 deletions Marsey/Utility.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Reflection;

namespace Marsey;

Expand All @@ -11,8 +12,35 @@ public enum LogType
FATL,
DEBG
}

// Loader logs
public static void Log(LogType logType, string message)
{
Console.WriteLine($"[MARSEY] [{logType.ToString()}] {message}");
}

// Patch logs
public static void Log(AssemblyName asm, string message)
{
Console.WriteLine($"[{asm.Name}] {message}");
}

/// <summary>
/// Sets patch delegate to Utility::Log(AssemblyName, string)
/// Executed only by the Loader.
/// </summary>
/// <see cref="PatchAssemblyManager.InitLogger"/>
/// <param name="patch">Assembly from MarseyPatch</param>
public static void SetupLogger(Assembly patch)
{
Type marseyLoggerType = patch.GetType("MarseyLogger")!;

Type logDelegateType = marseyLoggerType.GetNestedType("Forward", BindingFlags.Public)!;

MethodInfo logMethod = typeof(Utility).GetMethod("Log", new []{typeof(AssemblyName), typeof(string)})!;

Delegate logDelegate = Delegate.CreateDelegate(logDelegateType, null, logMethod);

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

Check warning on line 44 in Marsey/Utility.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 44 in Marsey/Utility.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 44 in Marsey/Utility.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 44 in Marsey/Utility.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 44 in Marsey/Utility.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
}
}

0 comments on commit ee44f57

Please sign in to comment.