Esempio 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 prefix     = patchClass.GetMethod("Prefix");

            Assert.IsNotNull(prefix);

            Class4Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.IsNotNull(instance);

            var patcher = instance.CreateProcessor(originalMethod);

            Assert.IsNotNull(patcher);
            patcher.AddPrefix(prefix);

            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);
                }
            }

            (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");
        }
Esempio n. 2
0
        public void TestMethod4()
        {
            var originalClass = typeof(Class4);

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

            Assert.IsNotNull(originalMethod);

            var patchClass = typeof(Class4Patch);
            var prefix     = patchClass.GetMethod("Prefix");

            Assert.IsNotNull(prefix);

            Class4Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.IsNotNull(instance);

            var patcher = new PatchProcessor(instance, originalMethod);

            Assert.IsNotNull(patcher);
            patcher.AddPrefix(prefix);

            patcher.Patch();
            // 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");
        }
Esempio n. 3
0
        public void Test_Method4()
        {
            var originalClass = typeof(Class4);

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

            Assert.NotNull(originalMethod);

            var patchClass = typeof(Class4Patch);
            var prefix     = patchClass.GetMethod("Prefix");

            Assert.NotNull(prefix);

            Class4Patch.ResetTest();

            var instance = new Harmony("test");

            Assert.NotNull(instance);

            var patcher = instance.CreateProcessor(originalMethod);

            Assert.NotNull(patcher);
            _ = patcher.AddPrefix(prefix);

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

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

            Assert.AreEqual(originalMethodStartPre, originalMethodStartPost);

            (new Class4()).Method4("foo");
            Assert.True(Class4Patch.prefixed, "Prefix was not executed");
            Assert.True(Class4Patch.originalExecuted, "Original was not executed");
            Assert.AreEqual(Class4Patch.senderValue, "foo");
        }