@@ -16,6 +16,7 @@ public class HookGenerator : IIncrementalGenerator
1616{
1717 private const string NativeHookAttrName = "StArray.ModManager.Hooks.NativeHookAttribute" ;
1818 private const string Il2CppHookAttrName = "StArray.ModManager.Hooks.Il2CppHookAttribute" ;
19+ private const string MonoHookAttrName = "StArray.ModManager.Hooks.MonoHookAttribute" ;
1920
2021 public void Initialize ( IncrementalGeneratorInitializationContext context )
2122 {
@@ -58,6 +59,7 @@ private sealed class HookInfo
5859 {
5960 NativeHookAttrName => "NativeHook" ,
6061 Il2CppHookAttrName => "Il2CppHook" ,
62+ MonoHookAttrName => "MonoHook" ,
6163 _ => null ,
6264 } ;
6365 if ( attrType == null ) continue ;
@@ -127,7 +129,6 @@ private static void GenerateCode(SourceProductionContext ctx, Compilation compil
127129 var sf = Sanitize ( h . MethodName ) ;
128130 var pd = string . Join ( ", " , h . Parameters . Select ( p => $ "{ SimplifyType ( p . Type ) } @{ p . Name } ") ) ;
129131 var pn = string . Join ( ", " , h . Parameters . Select ( p => $ "@{ p . Name } ") ) ;
130- var pt = string . Join ( ", " , h . Parameters . Select ( p => SimplifyType ( p . Type ) ) ) ;
131132 var cv = GetConv ( h ) ;
132133 var ucoCallConv = GetUCOCallConv ( h ) ;
133134
@@ -140,20 +141,12 @@ private static void GenerateCode(SourceProductionContext ctx, Compilation compil
140141 sb . AppendLine ( $ " private static nint _{ sf } _origPtr;") ;
141142 sb . AppendLine ( $ " private static _{ sf } Delegate? _{ sf } _orig;") ;
142143
143- // ── 私有 wrapper(委托包装,作为 detour ) ──
144+ // ── 持久引用(防止 delegate 被 GC ) ──
144145 sb . AppendLine ( $ " private static _{ sf } Delegate? _{ sf } _wrap;") ;
145- sb . AppendLine ( ) ;
146- sb . AppendLine ( $ " private static { rt } _Wrap_{ sf } ({ pd } )") ;
147- sb . AppendLine ( " {" ) ;
148- if ( IsVoid ( h ) )
149- sb . AppendLine ( $ " { h . MethodName } ({ pn } );") ;
150- else
151- sb . AppendLine ( $ " return { h . MethodName } ({ pn } );") ;
152- sb . AppendLine ( " }" ) ;
153146
154147 // ── Install ──
155148 sb . AppendLine ( ) ;
156- sb . AppendLine ( $ " private static void Install_{ sf } ()") ;
149+ sb . AppendLine ( $ " private static bool Install_{ sf } ()") ;
157150 sb . AppendLine ( " {" ) ;
158151 if ( h . AttrType == "NativeHook" )
159152 {
@@ -166,15 +159,16 @@ private static void GenerateCode(SourceProductionContext ctx, Compilation compil
166159 var s = h . CtorArgs . Length > 1 ? h . CtorArgs [ 1 ] : "\" \" " ;
167160 sb . AppendLine ( $ " var t = global::StArray.ModManager.Runtime.HookHelper.GetFunction({ l } , { s } );") ;
168161 }
169- sb . AppendLine ( $ " if (t == nint.Zero) return;") ;
162+ sb . AppendLine ( $ " if (t == nint.Zero) return false ;") ;
170163 sb . AppendLine ( $ " _{ sf } _origPtr = t;") ;
171- sb . AppendLine ( $ " _{ sf } _wrap = _Wrap_ { sf } ;") ;
164+ sb . AppendLine ( $ " _{ sf } _wrap = { h . MethodName } ;") ;
172165 sb . AppendLine ( $ " var d = global::System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(_{ sf } _wrap);") ;
173166 sb . AppendLine ( $ " var o = global::StArray.ModManager.Runtime.HookHelper.Hook(t, d);") ;
174- sb . AppendLine ( $ " if (o != nint.Zero)") ;
175- sb . AppendLine ( $ " _{ sf } _orig = global::System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<_{ sf } Delegate>(o);") ;
167+ sb . AppendLine ( $ " if (o == nint.Zero) return false;") ;
168+ sb . AppendLine ( $ " _{ sf } _orig = global::System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<_{ sf } Delegate>(o);") ;
169+ sb . AppendLine ( " return true;" ) ;
176170 }
177- else // Il2CppHook
171+ else if ( h . AttrType == " Il2CppHook" )
178172 {
179173 var a = h . CtorArgs . Length > 0 ? h . CtorArgs [ 0 ] : "\" \" " ;
180174 var c = h . CtorArgs . Length > 1 ? h . CtorArgs [ 1 ] : "\" \" " ;
@@ -187,20 +181,53 @@ private static void GenerateCode(SourceProductionContext ctx, Compilation compil
187181 if ( n . Key is "ParameterCount" or "parameterCount" ) pc = n . Value ;
188182
189183 sb . AppendLine ( $ " var asm = global::StArray.ModManager.Il2Cpp.Il2CppAssembly.Get({ a } );") ;
190- sb . AppendLine ( $ " if (asm == null) return;") ;
184+ sb . AppendLine ( $ " if (asm == null) return false ;") ;
191185 sb . AppendLine ( $ " var cls = asm.GetClass(\" \" , { c } );") ;
192- sb . AppendLine ( $ " if (cls == null) return;") ;
186+ sb . AppendLine ( $ " if (cls == null) return false;") ;
187+ if ( ptn != null )
188+ sb . AppendLine ( $ " var method = cls.GetMethod({ m } , { ptn } );") ;
189+ else
190+ sb . AppendLine ( $ " var method = cls.GetMethod({ m } , { pc } );") ;
191+ sb . AppendLine ( $ " if (method == null) return false;") ;
192+ sb . AppendLine ( $ " var t = method.FunctionPtr;") ;
193+ sb . AppendLine ( $ " _{ sf } _origPtr = t;") ;
194+ sb . AppendLine ( $ " _{ sf } _wrap = { h . MethodName } ;") ;
195+ sb . AppendLine ( $ " var d = global::System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(_{ sf } _wrap);") ;
196+ sb . AppendLine ( $ " var o = global::StArray.ModManager.Runtime.HookHelper.Hook(t, d);") ;
197+ sb . AppendLine ( $ " if (o == nint.Zero) return false;") ;
198+ sb . AppendLine ( $ " _{ sf } _orig = global::System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<_{ sf } Delegate>(o);") ;
199+ sb . AppendLine ( " return true;" ) ;
200+ }
201+ else // MonoHook
202+ {
203+ var a = h . CtorArgs . Length > 0 ? h . CtorArgs [ 0 ] : "\" \" " ;
204+ var nsArg = h . CtorArgs . Length > 1 ? h . CtorArgs [ 1 ] : "\" \" " ;
205+ var c = h . CtorArgs . Length > 2 ? h . CtorArgs [ 2 ] : "\" \" " ;
206+ var m = h . CtorArgs . Length > 3 ? h . CtorArgs [ 3 ] : "\" \" " ;
207+ string ? ptn = null ;
208+ foreach ( var n in h . NamedArgs )
209+ if ( n . Key is "ParameterTypeNames" or "parameterTypeNames" ) ptn = n . Value ;
210+ string pc = "-1" ;
211+ foreach ( var n in h . NamedArgs )
212+ if ( n . Key is "ParameterCount" or "parameterCount" ) pc = n . Value ;
213+
214+ sb . AppendLine ( $ " var asm = global::StArray.ModManager.Mono.MonoAssembly.Get({ a } );") ;
215+ sb . AppendLine ( $ " if (asm == null) return false;") ;
216+ sb . AppendLine ( $ " var cls = asm.GetClass({ nsArg } , { c } );") ;
217+ sb . AppendLine ( $ " if (cls == null) return false;") ;
193218 if ( ptn != null )
194219 sb . AppendLine ( $ " var method = cls.GetMethod({ m } , { ptn } );") ;
195220 else
196221 sb . AppendLine ( $ " var method = cls.GetMethod({ m } , { pc } );") ;
197- sb . AppendLine ( $ " if (method == null) return;") ;
222+ sb . AppendLine ( $ " if (method == null) return false ;") ;
198223 sb . AppendLine ( $ " var t = method.FunctionPtr;") ;
199224 sb . AppendLine ( $ " _{ sf } _origPtr = t;") ;
225+ sb . AppendLine ( $ " _{ sf } _wrap = { h . MethodName } ;") ;
200226 sb . AppendLine ( $ " var d = global::System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(_{ sf } _wrap);") ;
201227 sb . AppendLine ( $ " var o = global::StArray.ModManager.Runtime.HookHelper.Hook(t, d);") ;
202- sb . AppendLine ( $ " if (o != nint.Zero)") ;
203- sb . AppendLine ( $ " _{ sf } _orig = global::System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<_{ sf } Delegate>(o);") ;
228+ sb . AppendLine ( $ " if (o == nint.Zero) return false;") ;
229+ sb . AppendLine ( $ " _{ sf } _orig = global::System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer<_{ sf } Delegate>(o);") ;
230+ sb . AppendLine ( " return true;" ) ;
204231 }
205232 sb . AppendLine ( " }" ) ;
206233
@@ -216,13 +243,12 @@ private static void GenerateCode(SourceProductionContext ctx, Compilation compil
216243 }
217244
218245 sb . AppendLine ( ) ;
219- sb . AppendLine ( " public static void InstallHooks()" ) ;
246+ sb . AppendLine ( " public static bool InstallHooks()" ) ;
220247 sb . AppendLine ( " {" ) ;
221- sb . AppendLine ( " foreach (var m in new global::System.Action[]" ) ;
222- sb . AppendLine ( " {" ) ;
248+ sb . AppendLine ( " bool ok = true;" ) ;
223249 foreach ( var h in group )
224- sb . AppendLine ( $ " Install_{ Sanitize ( h . MethodName ) } , ") ;
225- sb . AppendLine ( " }) m() ;" ) ;
250+ sb . AppendLine ( $ " ok &= Install_{ Sanitize ( h . MethodName ) } (); ") ;
251+ sb . AppendLine ( " return ok ;" ) ;
226252 sb . AppendLine ( " }" ) ;
227253 sb . AppendLine ( ) ;
228254 sb . AppendLine ( " public static void UninstallHooks()" ) ;
0 commit comments