diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 14:02:43 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-02-09 14:02:43 -0500 |
commit | 74a56a7b3b3bde30fbcb711eaef977ad69601e03 (patch) | |
tree | f74cc4701b2fa26bd0a0fc637dee3a26965f7d85 /src | |
parent | 40a90147420614d7d593b478fcf93b9be542c5b0 (diff) | |
download | SMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.tar.gz SMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.tar.bz2 SMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.zip |
further generalise CIL rewriters for reuse (#231)
Diffstat (limited to 'src')
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs (renamed from src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs) | 38 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs | 41 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs | 3 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj | 3 |
4 files changed, 50 insertions, 35 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs index e44acaf9..c9729ee8 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseMethodRewriter.cs @@ -4,7 +4,7 @@ using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; -namespace StardewModdingAPI.AssemblyRewriters.Rewriters +namespace StardewModdingAPI.AssemblyRewriters.Framework { /// <summary>Base class for a method rewriter.</summary> public abstract class BaseMethodRewriter : IInstructionRewriter @@ -37,10 +37,11 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters this.Rewrite(module, cil, instruction, methodRef, assemblyMap); } + /********* ** Protected methods *********/ - /// <summary>Get whether the given method reference can be rewritten.</summary> + /// <summary>Get whether a method reference should be rewritten.</summary> /// <param name="methodRef">The method reference.</param> /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> protected abstract bool ShouldRewrite(MethodReference methodRef, bool platformChanged); @@ -69,7 +70,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters return false; for (int i = 0; i < referenceParameters.Length; i++) { - if (!this.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) + if (!RewriteHelper.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) return false; } return true; @@ -84,34 +85,5 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) .Any(method => this.HasMatchingSignature(method, reference)); } - - /// <summary>Get whether a type matches a type reference.</summary> - /// <param name="type">The defined type.</param> - /// <param name="reference">The type reference.</param> - private 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 (!this.IsMatchingType(defGenerics[i], refGenerics[i])) - return false; - } - } - - return true; - } } -}
\ No newline at end of file +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs new file mode 100644 index 00000000..0307053f --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs @@ -0,0 +1,41 @@ +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; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs index f64bf768..819578e0 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs @@ -3,6 +3,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Mono.Cecil; using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; namespace StardewModdingAPI.AssemblyRewriters.Rewriters { @@ -13,7 +14,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /********* ** Protected methods *********/ - /// <summary>Get whether the given method reference can be rewritten.</summary> + /// <summary>Get whether a method reference should be rewritten.</summary> /// <param name="methodRef">The method reference.</param> /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> protected override bool ShouldRewrite(MethodReference methodRef, bool platformChanged) diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 01ca1d66..0b549a86 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -74,7 +74,8 @@ <Compile Include="Platform.cs" /> <Compile Include="PlatformAssemblyMap.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> - <Compile Include="Rewriters\BaseMethodRewriter.cs" /> + <Compile Include="Framework\BaseMethodRewriter.cs" /> + <Compile Include="Framework\RewriteHelper.cs" /> <Compile Include="Rewriters\SpriteBatchRewriter.cs" /> </ItemGroup> <ItemGroup> |