From 7b641d816466fe7d9229374c175f59ee32b8dc5c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 15:17:26 -0400 Subject: simplify CIL rewriter hierarchy --- .../Finders/EventFinder.cs | 15 +++++++------- .../Finders/FieldFinder.cs | 23 +++++++++------------- .../Finders/MethodFinder.cs | 15 +++++++------- 3 files changed, 25 insertions(+), 28 deletions(-) (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') 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 { /// Finds CIL instructions that reference a given event. - public sealed class EventFinder : BaseMethodFinder + public sealed class EventFinder : IInstructionFinder { /********* ** Properties @@ -21,7 +20,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -41,13 +40,15 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /********* ** Protected methods *********/ - /// Get whether a method reference should be rewritten. + /// Get whether a CIL instruction matches. /// The IL instruction. - /// The method reference. /// Whether the mod was compiled on a different platform. - 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 { /// Finds CIL instructions that reference a given field. - public sealed class FieldFinder : BaseFieldFinder + public class FieldFinder : IInstructionFinder { /********* ** Properties @@ -16,15 +15,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// The field name for which to find references. private readonly string FieldName; - /// Whether the field to match is static. - private readonly bool IsStatic; - /********* ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -33,27 +29,26 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// Construct an instance. /// The full type name for which to find references. /// The field name for which to find references. - /// Whether the field to match is static. - public FieldFinder(string fullTypeName, string fieldName, bool isStatic) + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + 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 *********/ - /// Get whether a field reference should be rewritten. + /// Get whether a CIL instruction matches. /// The IL instruction. - /// The field reference. /// Whether the mod was compiled on a different platform. - 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 { /// Finds CIL instructions that reference a given method. - public sealed class MethodFinder : BaseMethodFinder + public class MethodFinder : IInstructionFinder { /********* ** Properties @@ -21,7 +20,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders ** Accessors *********/ /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } + public string NounPhrase { get; } /********* @@ -41,13 +40,15 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /********* ** Protected methods *********/ - /// Get whether a method reference should be rewritten. + /// Get whether a CIL instruction matches. /// The IL instruction. - /// The method reference. /// Whether the mod was compiled on a different platform. - 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; } } -- cgit