Exemple #1
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 = 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");
        }
Exemple #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 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");
        }
Exemple #3
0
        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");
        }