-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
HookTest.cs
98 lines (86 loc) · 4.15 KB
/
HookTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma warning disable CS1720 // Expression will always cause a System.NullReferenceException because the type's default value is null
#pragma warning disable xUnit1013 // Public method should be marked as test
using Xunit;
using MonoMod.RuntimeDetour;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace MonoMod.UnitTest {
[Collection("RuntimeDetour")]
public class HookTest {
[Fact]
public void TestHooks() {
lock (TestObject.Lock) {
Console.WriteLine("Hooks: none");
TestObject.TestStep(5, 6, 8);
Console.WriteLine();
// Note: You only need GetTypeInfo() if you target .NET Standard 1.6
IDetour hookTestMethodA = new Hook(
typeof(TestObject).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public),
typeof(HookTest).GetMethod("TestMethod_A", BindingFlags.Static | BindingFlags.Public)
);
IDetour hookTestStaticMethodA = new Hook(
typeof(TestObject).GetMethod("TestStaticMethod", BindingFlags.Static | BindingFlags.Public),
typeof(HookTest).GetMethod("TestStaticMethod_A", BindingFlags.Static | BindingFlags.Public)
);
IDetour hookTestVoidMethodA = new Hook(
typeof(TestObject).GetMethod("TestVoidMethod", BindingFlags.Static | BindingFlags.Public),
typeof(HookTest).GetMethod("TestVoidMethod_A", BindingFlags.Static | BindingFlags.Public)
);
Console.WriteLine("Hooks: A");
TestObject.TestStep(42, 12, 1);
Console.WriteLine();
IDetour hookTestMethodB = new Hook(
typeof(TestObject).GetMethod("TestMethod", BindingFlags.Instance | BindingFlags.Public),
typeof(HookTest).GetMethod("TestMethod_B", BindingFlags.Static | BindingFlags.Public)
);
IDetour hookTestStaticMethodB = new Hook(
typeof(TestObject).GetMethod("TestStaticMethod", BindingFlags.Static | BindingFlags.Public),
new Func<Func<int, int, int>, int, int, int>((orig, a, b) => {
return orig(a, b) + 2;
})
);
IDetour hookTestVoidMethodB = new Hook(
typeof(TestObject).GetMethod("TestVoidMethod", BindingFlags.Static | BindingFlags.Public),
new Action<Action<int, int>, int, int>((orig, a, b) => {
Console.WriteLine("Hook B");
TestObject.VoidResult += 2;
orig(a, b);
})
);
Console.WriteLine("Hooks: A + B");
TestObject.TestStep(42 + 42, 12 + 2, 3);
Console.WriteLine();
hookTestMethodA.Undo();
hookTestStaticMethodA.Undo();
hookTestVoidMethodA.Undo();
Console.WriteLine("Hooks: B");
TestObject.TestStep(5 + 42, 6 + 2, 12);
Console.WriteLine();
hookTestMethodB.Undo();
hookTestStaticMethodB.Undo();
hookTestVoidMethodB.Undo();
Console.WriteLine("Detours: none");
TestObject.TestStep(5, 6, 8);
Console.WriteLine();
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestMethod_A(TestObject self, int a, int b) {
return 42;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestStaticMethod_A(int a, int b) {
return a * b * 2;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void TestVoidMethod_A(int a, int b) {
Console.WriteLine("Hook A");
TestObject.VoidResult += 1;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int TestMethod_B(Func<TestObject, int, int, int> orig, TestObject self, int a, int b) {
return orig(self, a, b) + 42;
}
}
}