summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Framework
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs46
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs35
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs74
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs35
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs41
5 files changed, 0 insertions, 231 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs
deleted file mode 100644
index ac2facec..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs
+++ /dev/null
@@ -1,46 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Framework
-{
- /// <summary>Base class for a field finder.</summary>
- public abstract class BaseFieldFinder : IInstructionFinder
- {
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public abstract string NounPhrase { get; }
-
-
- /*********
- ** 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);
-
- /// <summary>Whether an instruction is a static field reference.</summary>
- /// <param name="instruction">The IL instruction.</param>
- protected bool IsStaticField(Instruction instruction)
- {
- return instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld;
- }
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs
deleted file mode 100644
index b2c25587..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldRewriter.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Framework
-{
- /// <summary>Base class for a field rewriter.</summary>
- public abstract class BaseFieldRewriter : BaseFieldFinder, IInstructionRewriter
- {
- /*********
- ** Public methods
- *********/
- /// <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>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);
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs
deleted file mode 100644
index bb71a9d7..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodFinder.cs
+++ /dev/null
@@ -1,74 +0,0 @@
-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
- {
- /*********
- ** Accessors
- *********/
- /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary>
- public abstract string NounPhrase { get; }
-
-
- /*********
- ** 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
deleted file mode 100644
index 6af1a0e1..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
+++ /dev/null
@@ -1,35 +0,0 @@
-using Mono.Cecil;
-using Mono.Cecil.Cil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Framework
-{
- /// <summary>Base class for a method rewriter.</summary>
- public abstract class BaseMethodRewriter : BaseMethodFinder, IInstructionRewriter
- {
- /*********
- ** Public methods
- *********/
- /// <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)
- {
- MethodReference methodRef = (MethodReference)instruction.Operand;
- this.Rewrite(module, cil, instruction, methodRef, assemblyMap);
- }
-
-
- /*********
- ** Protected methods
- *********/
- /// <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);
- }
-}
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs
deleted file mode 100644
index 0307053f..00000000
--- a/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using System;
-using Mono.Cecil;
-
-namespace StardewModdingAPI.AssemblyRewriters.Framework
-{
- /// <summary>Provides helper methods for field rewriters.</summary>
- internal static class RewriteHelper
- {
- /*********
- ** Public methods
- *********/
- /// <summary>Get whether a type matches a type reference.</summary>
- /// <param name="type">The defined type.</param>
- /// <param name="reference">The type reference.</param>
- public static bool IsMatchingType(Type type, TypeReference reference)
- {
- // same namespace & name
- if (type.Namespace != reference.Namespace || type.Name != reference.Name)
- return false;
-
- // same generic parameters
- if (type.IsGenericType)
- {
- if (!reference.IsGenericInstance)
- return false;
-
- Type[] defGenerics = type.GetGenericArguments();
- TypeReference[] refGenerics = ((GenericInstanceType)reference).GenericArguments.ToArray();
- if (defGenerics.Length != refGenerics.Length)
- return false;
- for (int i = 0; i < defGenerics.Length; i++)
- {
- if (!RewriteHelper.IsMatchingType(defGenerics[i], refGenerics[i]))
- return false;
- }
- }
-
- return true;
- }
- }
-}