diff --git a/Source/PatchingUtilities.cs b/Source/PatchingUtilities.cs
index a63d85d..a6535df 100644
--- a/Source/PatchingUtilities.cs
+++ b/Source/PatchingUtilities.cs
@@ -993,17 +993,27 @@ private static bool StopSecondHostCall()
/// Method or constructor to search for.
/// Method or constructor to replace with.
/// Method or constructor that is being patched, used for logging to provide information on which patched method had issues.
- /// Extra instructions to insert before the method or constructor is called.
+ /// Extra instructions to insert before the method or constructor is called.
+ /// Extra instructions to insert after the method or constructor is called.
/// The expected number of times the method should be replaced. Use -1 to to disable, or use -2 to expect unspecified amount (but more than 1 replacement).
/// The text that should appear before replacing a method. A first occurence of the method after this text will be replaced.
/// The text that excludes the next method from being patched. Will prevent skip patching the next time the method was going to be patched.
/// Modified enumeration of
- public static IEnumerable ReplaceMethod(this IEnumerable instr, MethodBase from, MethodBase to, MethodBase baseMethod = null, Func> extraInstructions = null, int expectedReplacements = -1, string targetText = null, string excludedText = null)
+ public static IEnumerable ReplaceMethod(this IEnumerable instr, MethodBase from, MethodBase to = null, MethodBase baseMethod = null, Func> extraInstructionsBefore = null, Func> extraInstructionsAfter = null, int expectedReplacements = -1, string targetText = null, string excludedText = null)
{
+ if (instr == null)
+ throw new ArgumentNullException(nameof(instr));
+ if (from == null)
+ Log.Error($"Call to {nameof(ReplaceMethod)} is meaningless as the target method is null for method {MethodName()}");
+ // Check if all meaningful arguments are null and provide a proper warning if they are.
+ if (to == null && extraInstructionsBefore == null && extraInstructionsAfter == null)
+ Log.Error($"Call to {nameof(ReplaceMethod)} is meaningless as no useful arguments were provided for method {MethodName()}");
+
// Check for text only if expected text isn't null
var isCorrectText = targetText == null;
var skipNextCall = false;
var replacedCount = 0;
+ var insertInstructionsAfter = false;
foreach (var ci in instr)
{
@@ -1029,24 +1039,40 @@ public static IEnumerable ReplaceMethod(this IEnumerable ExtraInstructions(CodeInstruction _) =>
new CodeInstruction(OpCodes.Ldloc_0),
];
- return instr.ReplaceMethod(target, replacement, baseMethod, ExtraInstructions, 1, "+");
+ return instr.ReplaceMethod(target, replacement, baseMethod, ExtraInstructions, null, 1, "+");
}
[MpCompatTranspiler(typeof(MagicCardUtility), nameof(MagicCardUtility.DrawLevelBar))]
@@ -859,7 +859,7 @@ private static IEnumerable UniversalReplaceGlobalLevelUpPlusBut
// Shouldn't happen
else throw new Exception($"Trying to apply transpiler ({nameof(UniversalReplaceLevelUpPlusButton)}) for an unsupported type ({baseMethod.DeclaringType.FullDescription()}).");
- return instr.ReplaceMethod(target, replacement, baseMethod, extraInstructions, expected, "+");
+ return instr.ReplaceMethod(target, replacement, baseMethod, extraInstructions, null, expected, "+");
}
#endregion
@@ -1123,9 +1123,9 @@ IEnumerable ExtraInstructions(CodeInstruction _) =>
];
// Replace the "TM_Learn" button to learn a power
- var replacedLearnButton = instr.ReplaceMethod(targetTextButton, textButtonReplacement, baseMethod, ExtraInstructions, 1, "TM_Learn", "TM_MCU_PointsToLearn");
+ var replacedLearnButton = instr.ReplaceMethod(targetTextButton, textButtonReplacement, baseMethod, ExtraInstructions, null, 1, "TM_Learn", "TM_MCU_PointsToLearn");
// Replace the image button to level-up a power
- return replacedLearnButton.ReplaceMethod(targetImageButton, imageButtonReplacement, baseMethod, ExtraInstructions, 1);
+ return replacedLearnButton.ReplaceMethod(targetImageButton, imageButtonReplacement, baseMethod, ExtraInstructions, null, 1);
}
#endregion
@@ -1370,7 +1370,7 @@ IEnumerable ExtraInstructions(CodeInstruction _) =>
];
// The "Apply" text isn't translated in the mod...
- return instr.ReplaceMethod(target, replacement, baseMethod, ExtraInstructions, 1, "Apply");
+ return instr.ReplaceMethod(target, replacement, baseMethod, ExtraInstructions, null, 1, "Apply");
}
#endregion