コード例 #1
0
ファイル: StaticPatches.cs プロジェクト: smile-qcr/Harmony-1
        public void TestMethod1()
        {
            var originalClass = typeof(Class1);

            Assert.IsNotNull(originalClass);
            var originalMethod = originalClass.GetMethod("Method1");

            Assert.IsNotNull(originalMethod);

            var patchClass = typeof(Class1Patch);
            var prefix     = patchClass.GetMethod("Prefix");
            var postfix    = patchClass.GetMethod("Postfix");
            var transpiler = patchClass.GetMethod("Transpiler");

            Assert.IsNotNull(prefix);
            Assert.IsNotNull(postfix);
            Assert.IsNotNull(transpiler);

            Class1Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.IsNotNull(instance);

            var patcher = instance.CreateProcessor(originalMethod);

            Assert.IsNotNull(patcher);
            patcher.AddPrefix(prefix);
            patcher.AddPostfix(postfix);
            patcher.AddTranspiler(transpiler);

            var originalMethodStartPre = Memory.GetMethodStart(originalMethod, out _);

            patcher.Patch();
            var originalMethodStartPost = Memory.GetMethodStart(originalMethod, out _);

            Assert.AreEqual(originalMethodStartPre, originalMethodStartPost);
            unsafe
            {
                var patchedCode = *(byte *)originalMethodStartPre;
                if (IntPtr.Size == sizeof(long))
                {
                    Assert.IsTrue(patchedCode == 0x48);
                }
                else
                {
                    Assert.IsTrue(patchedCode == 0x68);
                }
            }

            Class1.Method1();
            Assert.IsTrue(Class1Patch.prefixed, "Prefix was not executed");
            Assert.IsTrue(Class1Patch.originalExecuted, "Original was not executed");
            Assert.IsTrue(Class1Patch.postfixed, "Postfix was not executed");
        }
コード例 #2
0
        public void TestMethod1()
        {
            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");
            var realTranspiler = patchClass.GetMethod("Transpiler");

            Assert.IsNotNull(realPrefix);
            Assert.IsNotNull(realPostfix);
            Assert.IsNotNull(realTranspiler);

            Class1Patch._reset();

            MethodInfo prefixMethod;
            MethodInfo postfixMethod;
            MethodInfo transpilerMethod;

            PatchTools.GetPatches(typeof(Class1Patch), 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);

            Class1.Method1();
            Assert.IsTrue(Class1Patch.prefixed, "Prefix was not executed");
            Assert.IsTrue(Class1Patch.originalExecuted, "Original was not executed");
            Assert.IsTrue(Class1Patch.postfixed, "Postfix was not executed");
        }
コード例 #3
0
        public void TestMethod1()
        {
            var originalClass = typeof(Class1);

            Assert.IsNotNull(originalClass);
            var originalMethod = originalClass.GetMethod("Method1");

            Assert.IsNotNull(originalMethod);

            var patchClass = typeof(Class1Patch);
            var prefix     = patchClass.GetMethod("Prefix");
            var postfix    = patchClass.GetMethod("Postfix");
            var transpiler = patchClass.GetMethod("Transpiler");

            Assert.IsNotNull(prefix);
            Assert.IsNotNull(postfix);
            Assert.IsNotNull(transpiler);

            Class1Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.IsNotNull(instance);

            var patcher = new PatchProcessor(instance, originalMethod);

            Assert.IsNotNull(patcher);
            patcher.AddPrefix(prefix);
            patcher.AddPostfix(postfix);
            patcher.AddTranspiler(transpiler);

            patcher.Patch();
            // unsafe
            // {
            //     var patchedCode = *(byte*) originalMethodStartPre;
            //     if (IntPtr.Size == sizeof(long))
            //         Assert.IsTrue(patchedCode == 0x48);
            //     else
            //         Assert.IsTrue(patchedCode == 0x68);
            // }

            Class1.Method1();
            Assert.IsTrue(Class1Patch.prefixed, "Prefix was not executed");
            Assert.IsTrue(Class1Patch.originalExecuted, "Original was not executed");
            Assert.IsTrue(Class1Patch.postfixed, "Postfix was not executed");
        }
コード例 #4
0
ファイル: StaticPatches.cs プロジェクト: weimeittx/Harmony
        public void Test_Method1()
        {
            var originalClass = typeof(Class1);

            Assert.NotNull(originalClass);
            var originalMethod = originalClass.GetMethod("Method1");

            Assert.NotNull(originalMethod);

            var patchClass = typeof(Class1Patch);
            var prefix     = patchClass.GetMethod("Prefix");
            var postfix    = patchClass.GetMethod("Postfix");
            var transpiler = patchClass.GetMethod("Transpiler");

            Assert.NotNull(prefix);
            Assert.NotNull(postfix);
            Assert.NotNull(transpiler);

            Class1Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.NotNull(instance);

            var patcher = instance.CreateProcessor(originalMethod);

            Assert.NotNull(patcher);
            _ = patcher.AddPrefix(prefix);
            _ = patcher.AddPostfix(postfix);
            _ = patcher.AddTranspiler(transpiler);

            var originalMethodStartPre = Memory.GetMethodStart(originalMethod, out _);

            _ = patcher.Patch();
            var originalMethodStartPost = Memory.GetMethodStart(originalMethod, out _);

            Assert.AreEqual(originalMethodStartPre, originalMethodStartPost);

            Class1.Method1();
            Assert.True(Class1Patch.prefixed, "Prefix was not executed");
            Assert.True(Class1Patch.originalExecuted, "Original was not executed");
            Assert.True(Class1Patch.postfixed, "Postfix was not executed");
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: StaticPatches.cs プロジェクト: AdamDowns/Harmony
        public void TestMethod1()
        {
            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");
            var realTranspiler = patchClass.GetMethod("Transpiler");

            Assert.IsNotNull(realPrefix);
            Assert.IsNotNull(realPostfix);
            Assert.IsNotNull(realTranspiler);

            Class1Patch._reset();

            MethodInfo prefixMethod;
            MethodInfo postfixMethod;
            MethodInfo transpilerMethod;

            PatchTools.GetPatches(typeof(Class1Patch), 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);
                }
            }

            Class1.Method1();
            Assert.IsTrue(Class1Patch.prefixed);
            Assert.IsTrue(Class1Patch.originalExecuted);
            Assert.IsTrue(Class1Patch.postfixed);
        }