summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-02-09 14:02:43 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-02-09 14:02:43 -0500
commit74a56a7b3b3bde30fbcb711eaef977ad69601e03 (patch)
treef74cc4701b2fa26bd0a0fc637dee3a26965f7d85 /src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs
parent40a90147420614d7d593b478fcf93b9be542c5b0 (diff)
downloadSMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.tar.gz
SMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.tar.bz2
SMAPI-74a56a7b3b3bde30fbcb711eaef977ad69601e03.zip
further generalise CIL rewriters for reuse (#231)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs')
-rw-r--r--src/StardewModdingAPI.AssemblyRewriters/Framework/RewriteHelper.cs41
1 files changed, 41 insertions, 0 deletions
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;
+ }
+ }
+}