Ejemplo n.º 1
0
        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");
        }