public void MethodRestorationTest() { var originalMethod = typeof(RestoreableClass).GetMethod("Method2"); Assert.IsNotNull(originalMethod); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class2Patch), originalMethod, out prefixMethod, out postfixMethod, out transpilerMethod); var instance = HarmonyInstance.Create("test"); var patcher = new PatchProcessor(instance, originalMethod, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod), null); // Check if the class is clean before using it for patching Assert.AreEqual(null, instance.IsPatched(originalMethod), "Class already patched!"); var start = Memory.GetMethodStart(originalMethod); var oldBytes = new byte[12]; if (IntPtr.Size == sizeof(long)) { Marshal.Copy((IntPtr)start, oldBytes, 0, 12); } else { Marshal.Copy((IntPtr)start, oldBytes, 0, 6); } patcher.Patch(); patcher.Restore(); var newBytes = new byte[12]; if (IntPtr.Size == sizeof(long)) { Marshal.Copy((IntPtr)start, newBytes, 0, 12); } else { Marshal.Copy((IntPtr)start, newBytes, 0, 6); } for (int i = 0; i < oldBytes.Length; i++) { Assert.AreEqual(oldBytes[i], newBytes[i], string.Format("Byte {0} differs after restoration", i)); } Class2Patch._reset(); new RestoreableClass().Method2(); Assert.IsFalse(Class2Patch.prefixed); Assert.IsTrue(Class2Patch.originalExecuted); Assert.IsFalse(Class2Patch.postfixed); Assert.AreEqual(0, instance.IsPatched(originalMethod).Postfixes.Count); Assert.AreEqual(0, instance.IsPatched(originalMethod).Prefixes.Count); Assert.AreEqual(0, instance.IsPatched(originalMethod).Transpilers.Count); }
public static void HookFunctions() { hInstance = HarmonyInstance.Create("main"); hInstance.PatchAll(); Hooks.sendChat.spammer.Start(); //Spy MethodInfo originalMethod = typeof(Player).GetMethod("askScreenshot", BindingFlags.Public | BindingFlags.Instance); MethodInfo prefixMethod, postfixMethod, transpiler; PatchTools.GetPatches(typeof(Hooks.askScreenshot), out prefixMethod, out postfixMethod, out transpiler); PatchProcessor patcher = new PatchProcessor(hInstance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), null, null ); patcher.Patch(); }
public void TestMethod4() { var originalClass = typeof(Class4); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method4"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class4Patch); var realPrefix = patchClass.GetMethod("Prefix"); Assert.IsNotNull(realPrefix); Class4Patch._reset(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class4Patch), out prefixMethod, out postfixMethod, out transpilerMethod); Assert.AreSame(realPrefix, prefixMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); var patcher = new PatchProcessor(instance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), null, null); Assert.IsNotNull(patcher); var originalMethodStartPre = Memory.GetMethodStart(originalMethod, out var exception); patcher.Patch(); var originalMethodStartPost = Memory.GetMethodStart(originalMethod, out exception); Assert.AreEqual(originalMethodStartPre, originalMethodStartPost); unsafe { var patchedCode = *(byte *)originalMethodStartPre; if (IntPtr.Size == sizeof(long)) { Assert.IsTrue(patchedCode == 0x48); } else { Assert.IsTrue(patchedCode == 0x68); } } (new Class4()).Method4("foo"); Assert.IsTrue(Class4Patch.prefixed, "Prefix was not executed"); Assert.IsTrue(Class4Patch.originalExecuted, "Original was not executed"); Assert.AreEqual(Class4Patch.senderValue, "foo"); }
public void TestMethod2() { var originalClass = typeof(Class2); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method2"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class2Patch); var realPrefix = patchClass.GetMethod("Prefix"); var realPostfix = patchClass.GetMethod("Postfix"); var realTranspiler = patchClass.GetMethod("Transpiler"); Assert.IsNotNull(realPrefix); Assert.IsNotNull(realPostfix); Assert.IsNotNull(realTranspiler); Class2Patch._reset(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class2Patch), out prefixMethod, out postfixMethod, out transpilerMethod); Assert.AreSame(realPrefix, prefixMethod); Assert.AreSame(realPostfix, postfixMethod); Assert.AreSame(realTranspiler, transpilerMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); var patcher = new PatchProcessor(instance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod), new HarmonyMethod(transpilerMethod)); Assert.IsNotNull(patcher); var originalMethodStartPre = Memory.GetMethodStart(originalMethod, out var exception); patcher.Patch(); var originalMethodStartPost = Memory.GetMethodStart(originalMethod, out exception); Assert.AreEqual(originalMethodStartPre, originalMethodStartPost); new Class2().Method2(); Warn.If(Class1Patch.prefixed != true, "Prefix was not executed"); Warn.If(Class1Patch.originalExecuted != true, "Original was not executed"); Warn.If(Class1Patch.postfixed != true, "Postfix was not executed"); // Assert.IsTrue(Class1Patch.prefixed, "Prefix was not executed"); // Assert.IsTrue(Class1Patch.originalExecuted, "Original was not executed"); // Assert.IsTrue(Class1Patch.postfixed, "Postfix was not executed"); }
public void TestMethod6() { var originalClass = typeof(Class6); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method6"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class6Patch); var realPrefix = patchClass.GetMethod("Prefix"); Assert.IsNotNull(realPrefix); var instance6 = new Class6(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class6Patch), out prefixMethod, out postfixMethod, out transpilerMethod); Assert.AreSame(realPrefix, prefixMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); var patcher = new PatchProcessor(instance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), null); Assert.IsNotNull(patcher); patcher.Patch(); instance6.someFloat = 999; instance6.someString = "original"; instance6.someStruct = new Class6Struct() { d1 = 1, d2 = 2, d3 = 3 }; var res = instance6.Method6(); Assert.AreEqual(123, res.Item1); Assert.AreEqual("patched", res.Item2); Assert.AreEqual(10.0, res.Item3.d1); }
public void TestMethod7() { var originalClass = typeof(Class7); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method7"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class7Patch); var realPrefix = patchClass.GetMethod("Prefix"); Assert.IsNotNull(realPrefix); var instance6 = new Class6(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class7Patch), out prefixMethod, out postfixMethod, out transpilerMethod); Assert.AreSame(realPrefix, prefixMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); var patcher = new PatchProcessor(instance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), null); Assert.IsNotNull(patcher); patcher.Patch(); var instance7 = new Class7(); var result = instance7.Method7(); Assert.AreEqual(1, result.a); Assert.AreEqual(2, result.b); }
public void TestMethod5() { var originalClass = typeof(Class5); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method5"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class5Patch); var realPrefix = patchClass.GetMethod("Prefix"); Assert.IsNotNull(realPrefix); var realPostfix = patchClass.GetMethod("Postfix"); Assert.IsNotNull(realPostfix); Class5Patch._reset(); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class5Patch), out prefixMethod, out postfixMethod, out transpilerMethod); var patcher = new PatchProcessor(instance, new List <MethodBase> { originalMethod }, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod), null); Assert.IsNotNull(patcher); patcher.Patch(); (new Class5()).Method5("foo"); Assert.IsTrue(Class5Patch.prefixed, "Prefix was not executed"); Assert.IsTrue(Class5Patch.postfixed, "Prefix was not executed"); }
public void NUnitTestMethod1() { var originalClass = typeof(Class1); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method1"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class1Patch); var realPrefix = patchClass.GetMethod("Prefix"); var realPostfix = patchClass.GetMethod("Postfix"); Assert.IsNotNull(realPrefix); Assert.IsNotNull(realPostfix); Class1Patch._reset(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo infixMethod; PatchTools.GetPatches(typeof(Class1Patch), originalMethod, out prefixMethod, out postfixMethod, out infixMethod); Assert.AreSame(realPrefix, prefixMethod); Assert.AreSame(realPostfix, postfixMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); instance.Patch(originalMethod, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod)); Class1.Method1(); Assert.IsTrue(Class1Patch.prefixed); Assert.IsTrue(Class1Patch.originalExecuted); Assert.IsTrue(Class1Patch.postfixed); }
public void TestMethod2() { var originalClass = typeof(Class2); Assert.IsNotNull(originalClass); var originalMethod = originalClass.GetMethod("Method2"); Assert.IsNotNull(originalMethod); var patchClass = typeof(Class2Patch); var realPrefix = patchClass.GetMethod("Prefix"); var realPostfix = patchClass.GetMethod("Postfix"); var realTranspiler = patchClass.GetMethod("Transpiler"); Assert.IsNotNull(realPrefix); Assert.IsNotNull(realPostfix); Assert.IsNotNull(realTranspiler); Class2Patch._reset(); MethodInfo prefixMethod; MethodInfo postfixMethod; MethodInfo transpilerMethod; PatchTools.GetPatches(typeof(Class2Patch), originalMethod, out prefixMethod, out postfixMethod, out transpilerMethod); Assert.AreSame(realPrefix, prefixMethod); Assert.AreSame(realPostfix, postfixMethod); Assert.AreSame(realTranspiler, transpilerMethod); var instance = HarmonyInstance.Create("test"); Assert.IsNotNull(instance); var patcher = new PatchProcessor(instance, originalMethod, new HarmonyMethod(prefixMethod), new HarmonyMethod(postfixMethod), new HarmonyMethod(transpilerMethod)); Assert.IsNotNull(patcher); var originalMethodStartPre = Memory.GetMethodStart(originalMethod); patcher.Patch(); var originalMethodStartPost = Memory.GetMethodStart(originalMethod); Assert.AreEqual(originalMethodStartPre, originalMethodStartPost); unsafe { byte patchedCode = *(byte *)originalMethodStartPre; if (IntPtr.Size == sizeof(long)) { Assert.IsTrue(patchedCode == 0x48); } else { Assert.IsTrue(patchedCode == 0x68); } } new Class2().Method2(); Assert.IsTrue(Class2Patch.prefixed); Assert.IsTrue(Class2Patch.originalExecuted); Assert.IsTrue(Class2Patch.postfixed); }