summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs43
1 files changed, 34 insertions, 9 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs
index 1af6e6c4..e44acaf9 100644
--- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs
+++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs
@@ -7,27 +7,52 @@ using Mono.Cecil.Cil;
namespace StardewModdingAPI.AssemblyRewriters.Rewriters
{
/// <summary>Base class for a method rewriter.</summary>
- public abstract class BaseMethodRewriter : IMethodRewriter
+ public abstract class BaseMethodRewriter : IInstructionRewriter
{
/*********
** Public methods
*********/
- /// <summary>Get whether the given method reference can be rewritten.</summary>
- /// <param name="methodRef">The method reference.</param>
- public abstract bool ShouldRewrite(MethodReference methodRef);
+ /// <summary>Get whether a CIL instruction should be rewritten.</summary>
+ /// <param name="instruction">The IL instruction.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ public bool ShouldRewrite(Instruction instruction, bool platformChanged)
+ {
+ // ignore non-method-call instructions
+ if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt)
+ return false;
- /// <summary>Rewrite a method for compatibility.</summary>
+ // check reference
+ MethodReference methodRef = (MethodReference)instruction.Operand;
+ return this.ShouldRewrite(methodRef, platformChanged);
+ }
+
+ /// <summary>Rewrite a CIL instruction for compatibility.</summary>
/// <param name="module">The module being rewritten.</param>
/// <param name="cil">The CIL rewriter.</param>
- /// <param name="callOp">The instruction which calls the method.</param>
- /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param>
+ /// <param name="instruction">The instruction to rewrite.</param>
/// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
- public abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap);
-
+ public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap)
+ {
+ MethodReference methodRef = (MethodReference)instruction.Operand;
+ this.Rewrite(module, cil, instruction, methodRef, assemblyMap);
+ }
/*********
** Protected methods
*********/
+ /// <summary>Get whether the given method reference can be rewritten.</summary>
+ /// <param name="methodRef">The method reference.</param>
+ /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param>
+ protected abstract bool ShouldRewrite(MethodReference methodRef, bool platformChanged);
+
+ /// <summary>Rewrite a method for compatibility.</summary>
+ /// <param name="module">The module being rewritten.</param>
+ /// <param name="cil">The CIL rewriter.</param>
+ /// <param name="instruction">The instruction which calls the method.</param>
+ /// <param name="methodRef">The method reference invoked by the <paramref name="instruction"/>.</param>
+ /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param>
+ protected abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap);
+
/// <summary>Get whether a method definition matches the signature expected by a method reference.</summary>
/// <param name="definition">The method definition.</param>
/// <param name="reference">The method reference.</param>