From 5f9c03a8a93d51202e737d1d32c21e4a2e55ff08 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 9 Feb 2017 14:28:44 -0500 Subject: add field rewriter for the `Game1.activeClickableMenu` change in SDV 1.2 (#231) --- .../Framework/BaseFieldRewriter.cs | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs') 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 +{ + /// Base class for a field rewriter. + public abstract class BaseFieldRewriter : IInstructionRewriter + { + /********* + ** Public methods + *********/ + /// Get whether a CIL instruction should be rewritten. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + 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); + } + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// Metadata for mapping assemblies to the current platform. + 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 + *********/ + /// Get whether a field reference should be rewritten. + /// The IL instruction. + /// The field reference. + /// Whether the mod was compiled on a different platform. + protected abstract bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged); + + /// Rewrite a method for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction which references the field. + /// The field reference invoked by the . + /// Metadata for mapping assemblies to the current platform. + protected abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap); + } +} -- cgit