diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 15:17:26 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 15:17:26 -0400 |
commit | 7b641d816466fe7d9229374c175f59ee32b8dc5c (patch) | |
tree | 6ed0c1036071b1eba0ca181a2c9257fbfeeedde8 /src/StardewModdingAPI.AssemblyRewriters/Finders | |
parent | 267e2469da290cbdb93bd58cea0272de403bbdab (diff) | |
download | SMAPI-7b641d816466fe7d9229374c175f59ee32b8dc5c.tar.gz SMAPI-7b641d816466fe7d9229374c175f59ee32b8dc5c.tar.bz2 SMAPI-7b641d816466fe7d9229374c175f59ee32b8dc5c.zip |
simplify CIL rewriter hierarchy
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders')
3 files changed, 25 insertions, 28 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs index 359ca63e..9d0184c6 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs @@ -1,11 +1,10 @@ using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; namespace StardewModdingAPI.AssemblyRewriters.Finders { /// <summary>Finds CIL instructions that reference a given event.</summary> - public sealed class EventFinder : BaseMethodFinder + public sealed class EventFinder : IInstructionFinder { /********* ** Properties @@ -21,7 +20,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders ** Accessors *********/ /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -41,13 +40,15 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /********* ** Protected methods *********/ - /// <summary>Get whether a method reference should be rewritten.</summary> + /// <summary>Get whether a CIL instruction matches.</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 override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) + public bool IsMatch(Instruction instruction, bool platformChanged) { - return methodRef.DeclaringType.FullName == this.FullTypeName + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && methodRef.DeclaringType.FullName == this.FullTypeName && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName); } } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs index 516641f2..068119b8 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs @@ -1,11 +1,10 @@ using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; namespace StardewModdingAPI.AssemblyRewriters.Finders { /// <summary>Finds CIL instructions that reference a given field.</summary> - public sealed class FieldFinder : BaseFieldFinder + public class FieldFinder : IInstructionFinder { /********* ** Properties @@ -16,15 +15,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// <summary>The field name for which to find references.</summary> private readonly string FieldName; - /// <summary>Whether the field to match is static.</summary> - private readonly bool IsStatic; - /********* ** Accessors *********/ /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -33,27 +29,26 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// <summary>Construct an instance.</summary> /// <param name="fullTypeName">The full type name for which to find references.</param> /// <param name="fieldName">The field name for which to find references.</param> - /// <param name="isStatic">Whether the field to match is static.</param> - public FieldFinder(string fullTypeName, string fieldName, bool isStatic) + /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> + public FieldFinder(string fullTypeName, string fieldName, string nounPhrase = null) { this.FullTypeName = fullTypeName; this.FieldName = fieldName; - this.IsStatic = isStatic; - this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field"; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{fieldName} field"; } /********* ** Protected methods *********/ - /// <summary>Get whether a field reference should be rewritten.</summary> + /// <summary>Get whether a CIL instruction matches.</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 override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) + public bool IsMatch(Instruction instruction, bool platformChanged) { + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); return - this.IsStaticField(instruction) == this.IsStatic + fieldRef != null && fieldRef.DeclaringType.FullName == this.FullTypeName && fieldRef.Name == this.FieldName; } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs index 6c210d68..bea549ee 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs @@ -1,11 +1,10 @@ using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; namespace StardewModdingAPI.AssemblyRewriters.Finders { /// <summary>Finds CIL instructions that reference a given method.</summary> - public sealed class MethodFinder : BaseMethodFinder + public class MethodFinder : IInstructionFinder { /********* ** Properties @@ -21,7 +20,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders ** Accessors *********/ /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -41,13 +40,15 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /********* ** Protected methods *********/ - /// <summary>Get whether a method reference should be rewritten.</summary> + /// <summary>Get whether a CIL instruction matches.</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 override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) + public bool IsMatch(Instruction instruction, bool platformChanged) { - return methodRef.DeclaringType.FullName == this.FullTypeName + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + return + methodRef != null + && methodRef.DeclaringType.FullName == this.FullTypeName && methodRef.Name == this.MethodName; } } |