mirror of
https://github.com/pardeike/Harmony
synced 2026-06-08 16:40:38 +00:00
0b64f4c059
* Added simple tests for GetExecutingAssembly Better nullability checks for InternalPatches Minor formatting fixes * Added test for code inside the patched methods * More null checks - GetOriginalMethodFromStackframe could return null
62 lines
1.9 KiB
C#
62 lines
1.9 KiB
C#
using HarmonyLib;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reflection;
|
|
|
|
namespace TestLibrary;
|
|
|
|
public static class TestPatch
|
|
{
|
|
private static void Method(ref Assembly prefix, ref Assembly original, ref Assembly postfix)
|
|
{
|
|
original = Assembly.GetExecutingAssembly();
|
|
}
|
|
|
|
private static void Prefix(ref Assembly prefix)
|
|
{
|
|
prefix = Assembly.GetExecutingAssembly();
|
|
}
|
|
|
|
private static void Postfix(ref Assembly postfix)
|
|
{
|
|
postfix = Assembly.GetExecutingAssembly();
|
|
}
|
|
|
|
private static void Finalizer(Exception? __exception) { }
|
|
|
|
|
|
private static IEnumerable<CodeInstruction> EmptyTranspiler(IEnumerable<CodeInstruction> instructions) => instructions;
|
|
|
|
|
|
/// <summary>
|
|
/// Checks that Assembly.GetExecutingAssembly is not broken for non patched methods
|
|
/// </summary>
|
|
public static void TestGlobalPatch(out Assembly original, out Assembly patched)
|
|
{
|
|
original = Assembly.GetExecutingAssembly();
|
|
|
|
var instance = new Harmony("test");
|
|
instance.Patch(SymbolExtensions.GetMethodInfo((Assembly x) => Method(ref x, ref x, ref x)),
|
|
transpiler: SymbolExtensions.GetMethodInfo(() => EmptyTranspiler(null))
|
|
);
|
|
|
|
patched = Assembly.GetExecutingAssembly();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks that Assembly.GetExecutingAssembly is not broken for patched methods
|
|
/// </summary>
|
|
public static void TestPatching(out Assembly prefix, out Assembly original, out Assembly postfix)
|
|
{
|
|
var instance = new Harmony("test");
|
|
instance.Patch(SymbolExtensions.GetMethodInfo((Assembly x) => Method(ref x, ref x, ref x)),
|
|
prefix: SymbolExtensions.GetMethodInfo((Assembly x) => Prefix(ref x)),
|
|
postfix: SymbolExtensions.GetMethodInfo((Assembly x) => Postfix(ref x)),
|
|
transpiler: SymbolExtensions.GetMethodInfo(() => EmptyTranspiler(null)),
|
|
finalizer: SymbolExtensions.GetMethodInfo(() => Finalizer(null))
|
|
);
|
|
|
|
prefix = original = postfix = null;
|
|
Method(ref prefix, ref original, ref postfix);
|
|
}
|
|
} |