From 3663f70603fae8ed34e1e0c7500adc2c899312a5 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 01:01:52 -0500 Subject: split IInstructionFinder from IInstructionRewriter (#247) --- .../Framework/BaseFieldFinder.cs | 32 +++++++++++ .../Framework/BaseFieldRewriter.cs | 18 +----- .../Framework/BaseMethodFinder.cs | 67 ++++++++++++++++++++++ .../Framework/BaseMethodRewriter.cs | 53 +---------------- 4 files changed, 101 insertions(+), 69 deletions(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs new file mode 100644 index 00000000..96e8b1c0 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs @@ -0,0 +1,32 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters.Framework +{ + /// Base class for a field finder. + public abstract class BaseFieldFinder : IInstructionFinder + { + /********* + ** Public methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + public bool IsMatch(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.IsMatch(instruction, (FieldReference)instruction.Operand, platformChanged); + } + + + /********* + ** 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 IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged); + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs index 7e01ca73..b2c25587 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs @@ -4,21 +4,11 @@ using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Framework { /// Base class for a field rewriter. - public abstract class BaseFieldRewriter : IInstructionRewriter + public abstract class BaseFieldRewriter : BaseFieldFinder, 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. @@ -34,12 +24,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /********* ** 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. diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs new file mode 100644 index 00000000..7526286a --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs @@ -0,0 +1,67 @@ +using System; +using System.Linq; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters.Framework +{ + /// Base class for a method finder. + public abstract class BaseMethodFinder : IInstructionFinder + { + /********* + ** Public methods + *********/ + /// Get whether a CIL instruction matches. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + public bool IsMatch(Instruction instruction, bool platformChanged) + { + if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt) + return false; // not a method reference + return this.IsMatch(instruction, (MethodReference)instruction.Operand, platformChanged); + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// The IL instruction. + /// The method reference. + /// Whether the mod was compiled on a different platform. + protected abstract bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged); + + /// Get whether a method definition matches the signature expected by a method reference. + /// The method definition. + /// The method reference. + protected bool HasMatchingSignature(MethodInfo definition, MethodReference reference) + { + // same name + if (definition.Name != reference.Name) + return false; + + // same arguments + ParameterInfo[] definitionParameters = definition.GetParameters(); + ParameterDefinition[] referenceParameters = reference.Parameters.ToArray(); + if (referenceParameters.Length != definitionParameters.Length) + return false; + for (int i = 0; i < referenceParameters.Length; i++) + { + if (!RewriteHelper.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) + return false; + } + return true; + } + + /// Get whether a type has a method whose signature matches the one expected by a method reference. + /// The type to check. + /// The method reference. + protected bool HasMatchingSignature(Type type, MethodReference reference) + { + return type + .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) + .Any(method => this.HasMatchingSignature(method, reference)); + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs index e53e5c56..6af1a0e1 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs @@ -1,27 +1,14 @@ -using System; -using System.Linq; -using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Framework { /// Base class for a method rewriter. - public abstract class BaseMethodRewriter : IInstructionRewriter + public abstract class BaseMethodRewriter : BaseMethodFinder, 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.Call && instruction.OpCode != OpCodes.Callvirt) - return false; // not a method reference - return this.ShouldRewrite(instruction, (MethodReference)instruction.Operand, platformChanged); - } - /// Rewrite a CIL instruction for compatibility. /// The module being rewritten. /// The CIL rewriter. @@ -37,12 +24,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /********* ** Protected methods *********/ - /// Get whether a method reference should be rewritten. - /// The IL instruction. - /// The method reference. - /// Whether the mod was compiled on a different platform. - protected abstract bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged); - /// Rewrite a method for compatibility. /// The module being rewritten. /// The CIL rewriter. @@ -50,37 +31,5 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /// The method reference invoked by the . /// Metadata for mapping assemblies to the current platform. protected abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap); - - /// Get whether a method definition matches the signature expected by a method reference. - /// The method definition. - /// The method reference. - protected bool HasMatchingSignature(MethodInfo definition, MethodReference reference) - { - // same name - if (definition.Name != reference.Name) - return false; - - // same arguments - ParameterInfo[] definitionParameters = definition.GetParameters(); - ParameterDefinition[] referenceParameters = reference.Parameters.ToArray(); - if (referenceParameters.Length != definitionParameters.Length) - return false; - for (int i = 0; i < referenceParameters.Length; i++) - { - if (!RewriteHelper.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) - return false; - } - return true; - } - - /// Get whether a type has a method whose signature matches the one expected by a method reference. - /// The type to check. - /// The method reference. - protected bool HasMatchingSignature(Type type, MethodReference reference) - { - return type - .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) - .Any(method => this.HasMatchingSignature(method, reference)); - } } } -- cgit