@@ -208,7 +208,7 @@ private static bool IsMaxHandSizeToken(CodeInstruction ins)
208208 return HarmonyIl . LoadsInt32 ( ins , DefaultMaxHandSize ) ;
209209#else
210210 return HarmonyIl . LoadsInt32 ( ins , DefaultMaxHandSize )
211- || HarmonyIl . IsCallTo ( ins , MaxCardsInHandGetter ) ;
211+ || HarmonyIl . IsCall ( MaxCardsInHandGetter ) ( ins ) ;
212212#endif
213213 }
214214
@@ -259,7 +259,7 @@ private static IEnumerable<CodeInstruction> PlayerArg1Transpiler(IEnumerable<Cod
259259 private static IEnumerable < CodeInstruction > StateMachineTranspiler ( IEnumerable < CodeInstruction > instructions )
260260 {
261261 var rewriter = HarmonyIlRewriter . From ( instructions ) ;
262- var loadPlayer = FindStateMachinePlayerLoad ( rewriter . Code ) ;
262+ var loadPlayer = FindStateMachinePlayerLoad ( rewriter ) ;
263263 if ( loadPlayer == null )
264264 {
265265 RitsuLibFramework . Logger . Warn (
@@ -279,7 +279,7 @@ private static IEnumerable<CodeInstruction> StateMachineTranspiler(IEnumerable<C
279279 private static IEnumerable < CodeInstruction > CardOnPlayTranspiler ( IEnumerable < CodeInstruction > instructions )
280280 {
281281 var rewriter = HarmonyIlRewriter . From ( instructions ) ;
282- var loadCard = FindStateMachineCardLoad ( rewriter . Code ) ;
282+ var loadCard = FindStateMachineCardLoad ( rewriter ) ;
283283 if ( loadCard == null )
284284 {
285285 RitsuLibFramework . Logger . Warn (
@@ -296,35 +296,29 @@ private static IEnumerable<CodeInstruction> CardOnPlayTranspiler(IEnumerable<Cod
296296 return rewriter . InstructionsChecked ( "[MaxHandSize] Card OnPlay max-hand-size replacement" ) ;
297297 }
298298
299- private static IReadOnlyList < CodeInstruction > ? FindStateMachinePlayerLoad ( IReadOnlyList < CodeInstruction > code )
299+ private static IReadOnlyList < CodeInstruction > ? FindStateMachinePlayerLoad ( HarmonyIlRewriter rewriter )
300300 {
301- var pattern = HarmonyIlPattern . Sequence ( HarmonyIl . IsLdarg ( 0 ) , HarmonyIl . IsLdfld ( ) ) ;
302- foreach ( var match in pattern . FindAll ( code ) )
303- {
304- var fieldInstruction = match . InstructionAt ( code , 1 ) ;
305- if ( fieldInstruction . operand is not FieldInfo field || field . FieldType != typeof ( Player ) )
306- continue ;
307-
308- return [ match . InstructionAt ( code , 0 ) . Clone ( ) , fieldInstruction . Clone ( ) ] ;
309- }
301+ return FindStateMachineFieldLoad ( rewriter , field => field . FieldType == typeof ( Player ) ,
302+ "[MaxHandSize] state-machine Player field load" ) ;
303+ }
310304
311- return null ;
305+ private static IReadOnlyList < CodeInstruction > ? FindStateMachineCardLoad ( HarmonyIlRewriter rewriter )
306+ {
307+ return FindStateMachineFieldLoad ( rewriter , field => typeof ( CardModel ) . IsAssignableFrom ( field . FieldType ) ,
308+ "[MaxHandSize] state-machine CardModel field load" ) ;
312309 }
313310
314- private static IReadOnlyList < CodeInstruction > ? FindStateMachineCardLoad ( IReadOnlyList < CodeInstruction > code )
311+ private static IReadOnlyList < CodeInstruction > ? FindStateMachineFieldLoad (
312+ HarmonyIlRewriter rewriter ,
313+ Func < FieldInfo , bool > fieldPredicate ,
314+ string description )
315315 {
316316 var pattern = HarmonyIlPattern . Sequence ( HarmonyIl . IsLdarg ( 0 ) , HarmonyIl . IsLdfld ( ) ) ;
317- foreach ( var match in pattern . FindAll ( code ) )
318- {
319- var fieldInstruction = match . InstructionAt ( code , 1 ) ;
320- if ( fieldInstruction . operand is not FieldInfo field ||
321- ! typeof ( CardModel ) . IsAssignableFrom ( field . FieldType ) )
322- continue ;
323-
324- return [ match . InstructionAt ( code , 0 ) . Clone ( ) , fieldInstruction . Clone ( ) ] ;
325- }
326-
327- return null ;
317+ return ( from match in rewriter . FindMatches ( pattern , description ) . Items
318+ where fieldPredicate ( match . GetFieldOperand ( rewriter . Code , 1 ) )
319+ select ( IReadOnlyList < CodeInstruction > ? )
320+ [ match . InstructionAt ( rewriter . Code , 0 ) . Clone ( ) , match . InstructionAt ( rewriter . Code , 1 ) . Clone ( ) ] )
321+ . FirstOrDefault ( ) ;
328322 }
329323
330324 // ReSharper disable InconsistentNaming
@@ -371,7 +365,7 @@ private static IEnumerable<CodeInstruction> StartCardPlayTranspiler(IEnumerable<
371365
372366 private static bool ContainsCall ( IReadOnlyList < CodeInstruction > code , MethodInfo method )
373367 {
374- return code . Any ( instruction => HarmonyIl . IsCallTo ( instruction , method ) ) ;
368+ return code . Any ( HarmonyIl . IsCall ( method ) ) ;
375369 }
376370
377371 private static void WarnIfRewriteUnsatisfied ( HarmonyIlRewriteReport report )
0 commit comments