diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 14:28:44 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 14:28:44 -0500 |
commit | 5f9c03a8a93d51202e737d1d32c21e4a2e55ff08 (patch) | |
tree | 0183d835a7be1b89091b51ac3a24fb389e656934 /src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs | |
parent | 74a56a7b3b3bde30fbcb711eaef977ad69601e03 (diff) | |
download | SMAPI-5f9c03a8a93d51202e737d1d32c21e4a2e55ff08.tar.gz SMAPI-5f9c03a8a93d51202e737d1d32c21e4a2e55ff08.tar.bz2 SMAPI-5f9c03a8a93d51202e737d1d32c21e4a2e55ff08.zip |
add field rewriter for the `Game1.activeClickableMenu` change in SDV 1.2 (#231)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs')
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs new file mode 100644 index 00000000..7e01ca73 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs @@ -0,0 +1,51 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters.Framework +{ + /// <summary>Base class for a field rewriter.</summary> + public abstract class BaseFieldRewriter : IInstructionRewriter + { + /********* + ** Public methods + *********/ + /// <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) + { + if (instruction.OpCode != OpCodes.Ldfld && instruction.OpCode != OpCodes.Ldsfld && instruction.OpCode != OpCodes.Stfld && instruction.OpCode != OpCodes.Stsfld) + return false; // not a field reference + return this.ShouldRewrite(instruction, (FieldReference)instruction.Operand, 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="instruction">The instruction to rewrite.</param> + /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> + public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap) + { + FieldReference fieldRef = (FieldReference)instruction.Operand; + this.Rewrite(module, cil, instruction, fieldRef, assemblyMap); + } + + + /********* + ** Protected methods + *********/ + /// <summary>Get whether a field reference should be rewritten.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="fieldRef">The field reference.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + protected abstract bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, 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 references the field.</param> + /// <param name="fieldRef">The field 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, FieldReference fieldRef, PlatformAssemblyMap assemblyMap); + } +} |