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

Commit

Permalink
Pre-empt AssemblyLoad callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
misandrie committed Nov 30, 2023
1 parent 5ba5e69 commit 582e529
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 40 deletions.
6 changes: 6 additions & 0 deletions Marsey/FileHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Marsey.Preloader;
using Marsey.Subversion;
using Marsey.Serializer;
using Marsey.Stealthsey;

namespace Marsey;

Expand Down Expand Up @@ -79,6 +80,7 @@ public static void LoadAssemblies(string[]? path = null, bool marserializer = fa

if (files == null) return;


foreach (string file in files)
{
LoadExactAssembly(file);
Expand All @@ -91,6 +93,8 @@ public static void LoadAssemblies(string[]? path = null, bool marserializer = fa
/// <param name="file">path to dll file</param>
public static void LoadExactAssembly(string file)
{
Redial.Disable(); // Disable any AssemblyLoad callbacks found

try
{
Assembly assembly = Assembly.LoadFrom(file);
Expand All @@ -105,6 +109,8 @@ public static void LoadExactAssembly(string file)
{
MarseyLogger.Log(MarseyLogger.LogType.FATL, ex.Message);
}

Redial.Enable(); // Enable callbacks in case the game needs them
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Marsey/GameAssemblyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Threading;
using HarmonyLib;
using Marsey.Stealthsey;

namespace Marsey;

Expand Down
39 changes: 39 additions & 0 deletions Marsey/Handbrake/Manual.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Reflection;
using HarmonyLib;

namespace Marsey.Handbrake;

/// <summary>
/// In some cases we are required to patch functions publicly
/// </summary>
public static class Manual
{
public static void Prefix(MethodInfo method, MethodInfo prefix)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, prefix: prefix);
}

public static void Postfix(MethodInfo method, MethodInfo postfix)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, postfix: postfix);
}

public static void Transpiler(MethodInfo method, MethodInfo transpiler)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Patch(method, transpiler: transpiler);
}

/// <summary>
/// Removes **all** patches of type from a method
/// </summary>
/// <param name="method">target method that has patches applies</param>
/// <param name="patchType">type of patches</param>
public static void Unpatch(MethodInfo method, HarmonyPatchType patchType)
{
Harmony? harm = HarmonyManager.GetHarmony();
harm?.Unpatch(method, patchType);
}
}
28 changes: 0 additions & 28 deletions Marsey/Handbreak/Manual.cs

This file was deleted.

7 changes: 3 additions & 4 deletions Marsey/MarseyPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@ public void Boot()
{
// Preload marseypatches, if available
PreloadManager.Preload();
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Passed preload");


// Initialize subverter if enabled and present
if (MarseyVars.Subverter && Subverse.InitSubverter())
{
// Side-load custom code
Subverse.PatchSubverter();
}
MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Passed subverter");

// Manage game assemblies
GameAssemblyManager.GetGameAssemblies(out Assembly? clientAss, out Assembly? robustSharedAss, out Assembly? clientSharedAss);
AssemblyFieldHandler.SetAssemblies(clientAss, robustSharedAss, clientSharedAss);

// Prepare marseypatches
FileHandler.LoadAssemblies(marserializer: true, filename: "patches.marsey");
FileHandler.LoadAssemblies(marserializer: true, filename: PatchListManager.MarserializerFile);
List<MarseyPatch>? patches = PatchListManager.GetPatchList<MarseyPatch>();

if (patches == null) return;
Expand Down
4 changes: 2 additions & 2 deletions Marsey/Preloader/PreloadManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public static class PreloadManager
public static void Preload(string[]? path = null)
{
path ??= new[] { MarseyVars.MarseyPatchFolder };

List<string>? preloads = Marserializer.Deserialize(path, filename: MarserializerFile);

if (preloads == null) return;
if (preloads == null || preloads.Count == 0) return;

MarseyLogger.Log(MarseyLogger.LogType.INFO, $"Preloading {preloads.Count} patches");

Expand Down
10 changes: 5 additions & 5 deletions Marsey/Stealthsey/Hidesey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.Loader;
using Marsey.Handbreak;
using Marsey.Handbrake;
using Marsey.Subversion;

namespace Marsey.Stealthsey;
Expand All @@ -28,11 +28,11 @@ public static void Initialize()
.GetMethod("GetAssemblies", BindingFlags.Public | BindingFlags.Instance);
MethodInfo? postfix =
typeof(HideseyPatches)
.GetMethod("LieLoader", BindingFlags.Public | BindingFlags.Static);
.GetMethod("LieLoader", BindingFlags.Public | BindingFlags.Static)!;

if (target == null || postfix == null) return;
Manual.ManualPostfix(target, postfix);
if (target == null) return;

Manual.Postfix(target, postfix);

MarseyLogger.Log(MarseyLogger.LogType.DEBG, "Hidesey started.");
}
Expand Down
5 changes: 5 additions & 0 deletions Marsey/Stealthsey/HideseyPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public static void LieLoader(ref Assembly[] __result)
{
__result = Hidesey.LyingDomain(__result);
}

/// <summary>
/// This patch skips function execution
/// </summary>
public static bool Skip() => false;
}
67 changes: 67 additions & 0 deletions Marsey/Stealthsey/Redial.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Loader;
using HarmonyLib;
using Marsey.Handbrake;
using MonoMod.Utils;

namespace Marsey.Stealthsey;

/// <summary>
/// Manages AssemblyLoad event delegates
/// </summary>
public static class Redial
{
private static Delegate[]? _phonebook;

/// <summary>
/// Disables AssemblyLoad Callbacks in CurrentDomain
/// </summary>
public static void Disable()
{
MethodInfo? prefix =
typeof(HideseyPatches)
.GetMethod("Skip", BindingFlags.Public | BindingFlags.Static)!;

_phonebook = FillPhonebook();

if (_phonebook == null) return;

foreach (Delegate dial in _phonebook)
{
Manual.Prefix(dial.Method, prefix);
}
}

/// <summary>
/// Unpatches AssemblyLoad callbacks
/// </summary>
public static void Enable()
{
if (_phonebook == null) return;

foreach (Delegate dial in _phonebook)
{
Manual.Unpatch(dial.Method, HarmonyPatchType.Prefix);
}
}

/// <summary>
/// Returns a list of AssemblyLoad delegates
/// </summary>
private static Delegate[]? FillPhonebook()
{
FieldInfo? fInfo = typeof(AssemblyLoadContext).GetField("AssemblyLoad", BindingFlags.Static | BindingFlags.NonPublic);

MulticastDelegate? eventDelegate = (MulticastDelegate?)fInfo?.GetValue(AppDomain.CurrentDomain);

if (eventDelegate == null) return null;

Delegate[] delegates = eventDelegate.GetInvocationList();

if (delegates != _phonebook) MarseyLogger.Log(MarseyLogger.LogType.DEBG, $"Redialing {delegates.Length} delegates.");

return delegates;
}
}
2 changes: 1 addition & 1 deletion SS14.Loader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,4 @@ internal static int Main(string[] args)

return 0;
}
}
}

0 comments on commit 582e529

Please sign in to comment.