@@ -9,21 +9,18 @@ namespace Harmony
99public static class PatchTools
1010{
1111// this holds all the objects we want to keep alive so they don't get garbage-collected
12- static object [ ] objectReferences ;
13- [ MethodImpl ( MethodImplOptions . Synchronized ) ]
14- public static void KeepAliveForever ( object obj )
12+ static Dictionary < object , object > objectReferences = new Dictionary < object , object > ( ) ;
13+ public static void RememberObject ( object key , object value )
1514{
16- if ( objectReferences == null )
17- objectReferences = new object [ 0 ] ;
18- objectReferences . Add ( obj ) ;
15+ objectReferences [ key ] = value ;
1916}
2017
21- public static MethodInfo GetPatchMethod < T > ( Type patchType , string name , Type [ ] parameter )
18+ public static MethodInfo GetPatchMethod < T > ( Type patchType , string name , Type [ ] parameters = null )
2219{
2320var method = patchType . GetMethods ( AccessTools . all )
2421. FirstOrDefault ( m => m . GetCustomAttributes ( typeof ( T ) , true ) . Count ( ) > 0 ) ;
2522if ( method == null )
26- method = patchType . GetMethod ( name , AccessTools . all , null , parameter , null ) ;
23+ method = AccessTools . Method ( patchType , name , parameters ) ;
2724return method ;
2825}
2926
@@ -32,42 +29,10 @@ public static void GetPatches(Type patchType, MethodBase original, out MethodInf
3229var type = original . DeclaringType ;
3330var methodName = original . Name ;
3431
35- var parameters = original . GetParameters ( ) ;
36- var prefixParams = new List < Type > ( ) ;
37- var postfixParams = new List < Type > ( ) ;
38- if ( original . IsStatic == false )
39- {
40- prefixParams . Add ( type ) ;
41- postfixParams . Add ( type ) ;
42- }
43- var returnedType = AccessTools . GetReturnedType ( original ) ;
44- if ( returnedType != typeof ( void ) )
45- {
46- var retRef = returnedType . MakeByRefType ( ) ;
47- prefixParams . Add ( retRef ) ;
48- postfixParams . Add ( retRef ) ;
49- }
50- parameters . Do ( pi =>
51- {
52- var paramRef = pi . ParameterType . MakeByRefType ( ) ;
53- if ( pi . IsOut == false ) // prefix patches should not get out-parameters
54- prefixParams . Add ( paramRef ) ;
55- postfixParams . Add ( paramRef ) ;
56- } ) ;
57-
58- prefix = GetPatchMethod < HarmonyPrefix > ( patchType , "Prefix" , prefixParams . ToArray ( ) ) ;
59- postfix = GetPatchMethod < HarmonyPostfix > ( patchType , "Postfix" , postfixParams . ToArray ( ) ) ;
32+ prefix = GetPatchMethod < HarmonyPrefix > ( patchType , "Prefix" ) ;
33+ postfix = GetPatchMethod < HarmonyPostfix > ( patchType , "Postfix" ) ;
6034if ( prefix == null && postfix == null )
61- {
62- var prefixMethod = "Prefix(" + string . Join ( ", " , prefixParams . Select ( p => p . FullName ) . ToArray ( ) ) + ")" ;
63- var postfixMethod = "Postfix(" + string . Join ( ", " , postfixParams . Select ( p => p . FullName ) . ToArray ( ) ) + ")" ;
64- throw new MissingMethodException ( "No prefix/postfix patch for " + type . FullName + "." + methodName + "() found that matches " + prefixMethod + " or " + postfixMethod ) ;
65- }
66-
67- if ( prefix != null && prefix . ReturnType != typeof ( bool ) )
68- throw new MissingMethodException ( "Prefix() must return bool (return true to execute original method)" ) ;
69- if ( postfix != null && postfix . ReturnType != typeof ( void ) )
70- throw new MissingMethodException ( "Postfix() must not return anything" ) ;
35+ throw new MissingMethodException ( "No prefix/postfix patch for " + type . FullName + "." + methodName + "() found" ) ;
7136}
7237}
7338}
0 commit comments