summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-03-12 01:01:52 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-03-12 01:01:52 -0500
commit3663f70603fae8ed34e1e0c7500adc2c899312a5 (patch)
tree4df5bbdee93c16aaa50d3244a8149cc3356f472b /src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs
parent95e519796773a2ef17ec482e27cd4403282e2cd1 (diff)
downloadSMAPI-3663f70603fae8ed34e1e0c7500adc2c899312a5.tar.gz
SMAPI-3663f70603fae8ed34e1e0c7500adc2c899312a5.tar.bz2
SMAPI-3663f70603fae8ed34e1e0c7500adc2c899312a5.zip
split IInstructionFinder from IInstructionRewriter (#247)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs53
1 files changed, 1 insertions, 52 deletions
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));
- }
}
}