@@ -129,9 +129,9 @@ public override void HandleAddedToManager(PluginManager manager)
129129 base . HandleAddedToManager ( manager ) ;
130130
131131 // Subscribe us
132- foreach ( string hookname in Hooks . Keys )
132+ foreach ( string hookName in Hooks . Keys )
133133 {
134- Subscribe ( hookname ) ;
134+ Subscribe ( hookName ) ;
135135 }
136136
137137 try
@@ -149,8 +149,13 @@ public override void HandleAddedToManager(PluginManager manager)
149149 }
150150
151151 // Find all classes with the AutoPatch attribute and apply the patches
152- foreach ( Type nestedType in GetType ( ) . GetNestedTypes ( BindingFlags . DeclaredOnly | BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Static ) )
152+ Type [ ] nestedTypes = GetType ( ) . GetNestedTypes ( BindingFlags . DeclaredOnly | BindingFlags . Public |
153+ BindingFlags . NonPublic | BindingFlags . Static ) ;
154+
155+ int nestedTypeCount = nestedTypes . Length ;
156+ for ( int i = 0 ; i < nestedTypeCount ; i ++ )
153157 {
158+ Type nestedType = nestedTypes [ i ] ;
154159 object [ ] attr = nestedType . GetCustomAttributes ( typeof ( AutoPatchAttribute ) , false ) ;
155160 if ( attr . Length < 1 )
156161 {
@@ -159,22 +164,26 @@ public override void HandleAddedToManager(PluginManager manager)
159164
160165 try
161166 {
162- List < MethodInfo > harmonyMethods = HarmonyInstance . CreateClassProcessor ( nestedType ) ? . Patch ( ) ;
163-
167+ List < MethodInfo > ? harmonyMethods = HarmonyInstance . CreateClassProcessor ( nestedType ) ? . Patch ( ) ;
164168 if ( harmonyMethods == null || harmonyMethods . Count == 0 )
165169 {
166- Interface . Oxide . LogWarning ( $ "[{ Title } ] AutoPatch attribute found on '{ nestedType . Name } ' but no HarmonyPatch methods found. Skipping.") ;
170+ Interface . Oxide . LogWarning (
171+ $ "[{ Title } ] AutoPatch attribute found on '{ nestedType . Name } ' but no HarmonyPatch methods found. Skipping.") ;
167172 continue ;
168173 }
169174
170- foreach ( MethodInfo method in harmonyMethods )
175+ int harmonyMethodCount = harmonyMethods . Count ;
176+ for ( int j = 0 ; j < harmonyMethodCount ; j ++ )
171177 {
172- Interface . Oxide . LogInfo ( $ "[{ Title } ] Automatically Harmony patched '{ method ? . Name ?? "unknown" } ' method. ({ nestedType . Name } )") ;
178+ MethodInfo harmonyMethod = harmonyMethods [ j ] ;
179+ Interface . Oxide . LogInfo (
180+ $ "[{ Title } ] Automatically Harmony patched '{ harmonyMethod ? . Name ?? "unknown" } ' method. ({ nestedType . Name } )") ;
173181 }
174182 }
175183 catch ( Exception ex )
176184 {
177- Interface . Oxide . LogException ( $ "[{ Title } ] Failed to automatically Harmony patch '{ nestedType . Name } '", ex ) ;
185+ Interface . Oxide . LogException ( $ "[{ Title } ] Failed to automatically Harmony patch '{ nestedType . Name } '",
186+ ex ) ;
178187 }
179188 }
180189 }
@@ -211,19 +220,22 @@ protected void AddHookMethod(string name, MethodInfo method)
211220 /// <returns></returns>
212221 protected sealed override object OnCallHook ( string name , object [ ] args )
213222 {
214- object returnvalue = null ;
223+ object returnValue = null ;
215224 bool pooledArray = false ;
216225
217226 // Call all hooks that match the signature
218- foreach ( HookMethod h in FindHooks ( name , args ) )
227+ List < HookMethod > hookMethods = FindHooks ( name , args ) ;
228+ int hookMethodCount = hookMethods . Count ;
229+ for ( int i = 0 ; i < hookMethodCount ; i ++ )
219230 {
231+ HookMethod hookMethod = hookMethods [ i ] ;
220232 int received = args ? . Length ?? 0 ;
221233 object [ ] hookArgs ;
222234
223- if ( received != h . Parameters . Length )
235+ if ( received != hookMethod . Parameters . Length )
224236 {
225237 // The call argument count is different to the declared callback methods argument count
226- hookArgs = ObjectArrayPool . Take ( h . Parameters . Length ) ;
238+ hookArgs = ObjectArrayPool . Take ( hookMethod . Parameters . Length ) ;
227239 pooledArray = true ;
228240
229241 if ( received > 0 && hookArgs . Length > 0 )
@@ -237,7 +249,7 @@ protected sealed override object OnCallHook(string name, object[] args)
237249 // Create additional parameters for arguments excluded in this hook call
238250 for ( int n = received ; n < hookArgs . Length ; n ++ )
239251 {
240- ParameterInfo parameter = h . Parameters [ n ] ;
252+ ParameterInfo parameter = hookMethod . Parameters [ n ] ;
241253 if ( parameter . DefaultValue != null && parameter . DefaultValue != DBNull . Value )
242254 {
243255 // Use the default value that was provided by the method definition
@@ -258,7 +270,7 @@ protected sealed override object OnCallHook(string name, object[] args)
258270
259271 try
260272 {
261- returnvalue = InvokeMethod ( h , hookArgs ) ;
273+ returnValue = InvokeMethod ( hookMethod , hookArgs ) ;
262274 }
263275 catch ( TargetInvocationException exception )
264276 {
@@ -276,13 +288,13 @@ protected sealed override object OnCallHook(string name, object[] args)
276288 throw ;
277289 }
278290
279- if ( received != h . Parameters . Length )
291+ if ( received != hookMethod . Parameters . Length )
280292 {
281293 // A copy of the call arguments was used for this method call
282- for ( int n = 0 ; n < h . Parameters . Length ; n ++ )
294+ for ( int n = 0 ; n < hookMethod . Parameters . Length ; n ++ )
283295 {
284296 // Copy output values for out and by reference arguments back to the calling args
285- if ( h . Parameters [ n ] . IsOut || h . Parameters [ n ] . ParameterType . IsByRef )
297+ if ( hookMethod . Parameters [ n ] . IsOut || hookMethod . Parameters [ n ] . ParameterType . IsByRef )
286298 {
287299 args [ n ] = hookArgs [ n ] ;
288300 }
@@ -295,22 +307,23 @@ protected sealed override object OnCallHook(string name, object[] args)
295307 }
296308 }
297309
298- return returnvalue ;
310+ return returnValue ;
299311 }
300312
301313 protected List < HookMethod > FindHooks ( string name , object [ ] args )
302314 {
303315 // Get the full name of the hook `name(argument type 1, argument type 2, ..., argument type x)`
304316
305317 // Check the cache if we already found a match for this hook
306- List < HookMethod > methods = HooksCache . GetHookMethod ( name , args , out HookCache cache ) ;
307- if ( methods != null )
318+ List < HookMethod > hookMethods = HooksCache . GetHookMethod ( name , args , out HookCache cache ) ;
319+ if ( hookMethods != null )
308320 {
309- return methods ;
321+ return hookMethods ;
310322 }
323+
311324 List < HookMethod > matches = new List < HookMethod > ( ) ;
312325 // Get all hook methods that could match, return an empty list if none match
313- if ( ! Hooks . TryGetValue ( name , out methods ) )
326+ if ( ! Hooks . TryGetValue ( name , out hookMethods ) )
314327 {
315328 return matches ;
316329 }
@@ -319,13 +332,15 @@ protected List<HookMethod> FindHooks(string name, object[] args)
319332 HookMethod exactMatch = null ;
320333 HookMethod overloadedMatch = null ;
321334
322- foreach ( HookMethod h in methods )
335+ int hookMethodCount = hookMethods . Count ;
336+ for ( int i = 0 ; i < hookMethodCount ; i ++ )
323337 {
338+ HookMethod hookMethod = hookMethods [ i ] ;
324339 // A base hook should always have a matching signature either directly or through inheritance
325340 // and should always be called as core functionality depends on it.
326- if ( h . IsBaseHook )
341+ if ( hookMethod . IsBaseHook )
327342 {
328- matches . Add ( h ) ;
343+ matches . Add ( hookMethod ) ;
329344 continue ;
330345 }
331346
@@ -335,10 +350,10 @@ protected List<HookMethod> FindHooks(string name, object[] args)
335350
336351 bool pooledArray = false ;
337352
338- if ( received != h . Parameters . Length )
353+ if ( received != hookMethod . Parameters . Length )
339354 {
340355 // The call argument count is different to the declared callback methods argument count
341- hookArgs = ObjectArrayPool . Take ( h . Parameters . Length ) ;
356+ hookArgs = ObjectArrayPool . Take ( hookMethod . Parameters . Length ) ;
342357 pooledArray = true ;
343358
344359 if ( received > 0 && hookArgs . Length > 0 )
@@ -352,7 +367,7 @@ protected List<HookMethod> FindHooks(string name, object[] args)
352367 // Create additional parameters for arguments excluded in this hook call
353368 for ( int n = received ; n < hookArgs . Length ; n ++ )
354369 {
355- ParameterInfo parameter = h . Parameters [ n ] ;
370+ ParameterInfo parameter = hookMethod . Parameters [ n ] ;
356371 if ( parameter . DefaultValue != null && parameter . DefaultValue != DBNull . Value )
357372 {
358373 // Use the default value that was provided by the method definition
@@ -371,16 +386,16 @@ protected List<HookMethod> FindHooks(string name, object[] args)
371386 hookArgs = args ;
372387 }
373388
374- if ( h . HasMatchingSignature ( hookArgs , out bool isExactMatch ) )
389+ if ( hookMethod . HasMatchingSignature ( hookArgs , out bool isExactMatch ) )
375390 {
376391 if ( isExactMatch )
377392 {
378- exactMatch = h ;
393+ exactMatch = hookMethod ;
379394 break ;
380395 }
381396
382397 // Should we determine the level and call the closest overloaded match? Performance impact?
383- overloadedMatch = h ;
398+ overloadedMatch = hookMethod ;
384399 }
385400
386401 if ( pooledArray )
0 commit comments