2
2
using HarmonyTests . Patching . Assets ;
3
3
using NUnit . Framework ;
4
4
using System ;
5
+ using System . IO ;
6
+ using System . Reflection ;
7
+ using System . Text ;
5
8
6
9
namespace HarmonyLibTests . Patching ;
7
10
11
+ using EIP = ExternalInstanceMethod_StringIsInterned_Patch ;
12
+
8
13
[ TestFixture ]
9
14
public class NativeDetourPatches : TestLogger
10
15
{
11
16
[ Test ]
12
- public void Test_PatchExternalMethod ( )
17
+ public void Test_PatchInstanceExternalMethod ( )
18
+ {
19
+ var target = typeof ( string ) . GetMethod ( "Intern" , BindingFlags . Instance | BindingFlags . NonPublic ) ;
20
+
21
+ if ( target == null )
22
+ Assert . Inconclusive ( "string.Intern is missing in current runtime" ) ;
23
+
24
+ #if ! NET35
25
+ if ( ( target . MethodImplementationFlags & MethodImplAttributes . InternalCall ) == 0 )
26
+ Assert . Inconclusive ( "string.Intern is not an InternalCall (extern) in current runtime " ) ;
27
+ #endif
28
+
29
+ if ( target . GetMethodBody ( ) != null )
30
+ Assert . Inconclusive ( "string.Intern has IL body in current runtime" ) ;
31
+
32
+ var str1 = new StringBuilder ( ) . Append ( 'o' ) . Append ( 'k' ) . Append ( '4' ) . Append ( '1' ) . ToString ( ) ;
33
+ Assert . IsNull ( string . IsInterned ( str1 ) ) ;
34
+ var internedStr1 = string . Intern ( str1 ) ;
35
+ Assert . AreEqual ( internedStr1 , string . IsInterned ( str1 ) ) ;
36
+
37
+ var instance = new Harmony ( "test-patch-external-instance-method" ) ;
38
+ Assert . NotNull ( instance , "Harmony instance" ) ;
39
+
40
+ instance . Patch ( target , transpiler : typeof ( EIP ) . Method ( "Transpiler" ) ) ;
41
+ var str2 = new StringBuilder ( ) . Append ( 'o' ) . Append ( 'k' ) . Append ( '4' ) . Append ( '2' ) . ToString ( ) ;
42
+ Assert . IsNull ( string . IsInterned ( str2 ) ) ;
43
+ var internedStr2 = string . Intern ( str2 ) ;
44
+ Assert . AreEqual ( internedStr2 , string . IsInterned ( str2 ) ) ;
45
+
46
+ instance . Patch ( target , prefix : typeof ( EIP ) . Method ( "Prefix" ) ) ;
47
+ Assert . AreEqual ( EIP . PrefixOutput , string . Intern ( EIP . PrefixInput ) ) ;
48
+
49
+ instance . Patch ( target , postfix : typeof ( EIP ) . Method ( "Postfix" ) ) ;
50
+ Assert . AreEqual ( EIP . PostfixOutput , string . Intern ( EIP . PostfixInput ) ) ;
51
+
52
+ instance . Patch ( target , transpiler : typeof ( EIP ) . Method ( "TranspilerThrow" ) ) ;
53
+ Assert . Throws ( EIP . TranspiledException , ( ) => string . Intern ( "does not matter" ) ) ;
54
+
55
+ instance . Patch ( target , finalizer : typeof ( EIP ) . Method ( "Finalizer" ) ) ;
56
+ Assert . AreEqual ( EIP . FinalizerOutput , string . Intern ( EIP . FinalizerInput ) ) ;
57
+
58
+ instance . UnpatchSelf ( ) ;
59
+ var str3 = new StringBuilder ( ) . Append ( 'o' ) . Append ( 'k' ) . Append ( '4' ) . Append ( '3' ) . ToString ( ) ;
60
+ Assert . IsNull ( string . IsInterned ( str3 ) ) ;
61
+ Assert . AreEqual ( internedStr1 , string . IsInterned ( str1 ) ) ;
62
+ Assert . AreEqual ( internedStr2 , string . IsInterned ( str2 ) ) ;
63
+ }
64
+
65
+ [ Test ]
66
+ public void Test_PatchStaticExternalMethod ( )
13
67
{
14
68
var target = SymbolExtensions . GetMethodInfo ( ( ) => Math . Cos ( 0 ) ) ;
15
69
@@ -20,22 +74,22 @@ public void Test_PatchExternalMethod()
20
74
var cos = Math . Cos ;
21
75
Assert . AreEqual ( 1d , cos ( 0d ) ) ;
22
76
23
- var instance = new Harmony ( "test-patch-external-method" ) ;
77
+ var instance = new Harmony ( "test-patch-external-static- method" ) ;
24
78
Assert . NotNull ( instance , "Harmony instance" ) ;
25
79
26
- instance . Patch ( target , transpiler : typeof ( ExternalMethod_Patch ) . Method ( "Transpiler" ) ) ;
80
+ instance . Patch ( target , transpiler : typeof ( ExternalStaticMethod_MathCos_Patch ) . Method ( "Transpiler" ) ) ;
27
81
Assert . AreEqual ( 1d , cos ( 0d ) ) ;
28
82
29
- instance . Patch ( target , prefix : typeof ( ExternalMethod_Patch ) . Method ( "Prefix" ) ) ;
83
+ instance . Patch ( target , prefix : typeof ( ExternalStaticMethod_MathCos_Patch ) . Method ( "Prefix" ) ) ;
30
84
Assert . AreEqual ( 1d , cos ( 2d ) ) ;
31
85
32
- instance . Patch ( target , postfix : typeof ( ExternalMethod_Patch ) . Method ( "Postfix" ) ) ;
86
+ instance . Patch ( target , postfix : typeof ( ExternalStaticMethod_MathCos_Patch ) . Method ( "Postfix" ) ) ;
33
87
Assert . AreEqual ( 2d , cos ( 0d ) ) ;
34
88
35
- instance . Patch ( target , transpiler : typeof ( ExternalMethod_Patch ) . Method ( "TranspilerThrow" ) ) ;
89
+ instance . Patch ( target , transpiler : typeof ( ExternalStaticMethod_MathCos_Patch ) . Method ( "TranspilerThrow" ) ) ;
36
90
Assert . Throws < UnauthorizedAccessException > ( ( ) => cos ( 0d ) ) ;
37
91
38
- instance . Patch ( target , finalizer : typeof ( ExternalMethod_Patch ) . Method ( "Finalizer" ) ) ;
92
+ instance . Patch ( target , finalizer : typeof ( ExternalStaticMethod_MathCos_Patch ) . Method ( "Finalizer" ) ) ;
39
93
Assert . AreEqual ( - 2d , cos ( 0d ) ) ;
40
94
41
95
instance . UnpatchSelf ( ) ;
0 commit comments