diff options
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters')
11 files changed, 122 insertions, 79 deletions
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 +{ + /// <summary>Base class for a field finder.</summary> + public abstract class BaseFieldFinder : IInstructionFinder + { + /********* + ** Public methods + *********/ + /// <summary>Get whether a CIL instruction matches.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + 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 + *********/ + /// <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 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 { /// <summary>Base class for a field rewriter.</summary> - public abstract class BaseFieldRewriter : IInstructionRewriter + public abstract class BaseFieldRewriter : BaseFieldFinder, 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> @@ -34,12 +24,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /********* ** 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> 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 +{ + /// <summary>Base class for a method finder.</summary> + public abstract class BaseMethodFinder : IInstructionFinder + { + /********* + ** Public methods + *********/ + /// <summary>Get whether a CIL instruction matches.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + 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 + *********/ + /// <summary>Get whether a method reference should be rewritten.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="methodRef">The method reference.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + protected abstract bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged); + + /// <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> + 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; + } + + /// <summary>Get whether a type has a method whose signature matches the one expected by a method reference.</summary> + /// <param name="type">The type to check.</param> + /// <param name="reference">The method reference.</param> + 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 { /// <summary>Base class for a method rewriter.</summary> - public abstract class BaseMethodRewriter : IInstructionRewriter + public abstract class BaseMethodRewriter : BaseMethodFinder, 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.Call && instruction.OpCode != OpCodes.Callvirt) - return false; // not a method reference - return this.ShouldRewrite(instruction, (MethodReference)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> @@ -37,12 +24,6 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /********* ** Protected methods *********/ - /// <summary>Get whether a method reference should be rewritten.</summary> - /// <param name="instruction">The IL instruction.</param> - /// <param name="methodRef">The method reference.</param> - /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - protected abstract bool ShouldRewrite(Instruction instruction, 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> @@ -50,37 +31,5 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /// <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> - 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; - } - - /// <summary>Get whether a type has a method whose signature matches the one expected by a method reference.</summary> - /// <param name="type">The type to check.</param> - /// <param name="reference">The method reference.</param> - 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/IInstructionFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/IInstructionFinder.cs new file mode 100644 index 00000000..47a4247b --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/IInstructionFinder.cs @@ -0,0 +1,13 @@ +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters +{ + /// <summary>Finds CIL instructions considered incompatible.</summary> + public interface IInstructionFinder + { + /// <summary>Get whether a CIL instruction matches.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + bool IsMatch(Instruction instruction, bool platformChanged); + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs index 5c24acb6..f99a0a5a 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs @@ -4,13 +4,8 @@ using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters { /// <summary>Rewrites a CIL instruction for compatibility.</summary> - public interface IInstructionRewriter + public interface IInstructionRewriter : IInstructionFinder { - /// <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> - bool ShouldRewrite(Instruction instruction, bool platformChanged); - /// <summary>Rewrite a CIL instruction for compatibility.</summary> /// <param name="module">The module being rewritten.</param> /// <param name="cil">The CIL rewriter.</param> diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs index 252fe6d0..a47b8410 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs @@ -19,7 +19,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.Crossplatform /// <param name="instruction">The IL instruction.</param> /// <param name="methodRef">The method reference.</param> /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - protected override bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged) + protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) { return platformChanged && methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs index 36634b28..66fe0369 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs @@ -18,7 +18,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 /// <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 override bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged) + protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs index e8cf3d13..9cb6e13d 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs @@ -18,7 +18,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 /// <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 override bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged) + protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs index fb87c19a..115e3d7d 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs @@ -18,7 +18,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 /// <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 override bool ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged) + protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index d5394d67..03ed6f3a 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -66,7 +66,10 @@ <Compile Include="..\GlobalAssemblyInfo.cs"> <Link>Properties\GlobalAssemblyInfo.cs</Link> </Compile> + <Compile Include="Framework\BaseFieldFinder.cs" /> + <Compile Include="Framework\BaseMethodFinder.cs" /> <Compile Include="IInstructionRewriter.cs" /> + <Compile Include="IInstructionFinder.cs" /> <Compile Include="Platform.cs" /> <Compile Include="PlatformAssemblyMap.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> |