From 9fab0bf58f7c8b7d38f026bde4230e39049c056b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 01:32:30 -0500 Subject: reject mods which reference obsolete StardewModdingAPI.Extensions class (#247) --- .../Finders/SMAPI_Extensions_MethodFinder.cs | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs new file mode 100644 index 00000000..f359b595 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs @@ -0,0 +1,31 @@ +using System.Diagnostics.CodeAnalysis; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Finders +{ + /// Matches CIL instructions that reference the former StardewModdingAPI.Extensions class, which was removed in SMAPI 1.9. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] + public class SMAPI_Extensions_MethodFinder : BaseMethodFinder + { + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } = "obsolete StardewModdingAPI.Extensions API"; + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// 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) + { + return methodRef.DeclaringType.FullName == "StardewModdingAPI.Extensions"; + } + } +} -- cgit From fae362723f1390cb7758bd151d50889cf6c999a9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 03:23:20 -0400 Subject: reject mods which reference obsolete Game1.borderFont and Game1.smoothFont fields (#247) --- .../Finders/Game1_borderFont_FieldFinder.cs | 35 ++++++++++++++++++++++ .../Finders/Game1_smoothFont_FieldFinder.cs | 35 ++++++++++++++++++++++ .../Finders/SMAPI_Extensions_MethodFinder.cs | 2 +- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs new file mode 100644 index 00000000..e25cc544 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs @@ -0,0 +1,35 @@ +using System.Diagnostics.CodeAnalysis; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; +using StardewValley; + +namespace StardewModdingAPI.AssemblyRewriters.Finders +{ + /// Finds CIL instructions that reference the former Game1.borderFont field, which was removed in Stardew Valley 1.2.3–1.2.6. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] + public class Game1_borderFont_FieldFinder : BaseFieldFinder + { + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.borderFont field"; + + + /********* + ** Protected methods + *********/ + /// Get whether a field reference should be rewritten. + /// 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) + { + return + this.IsStaticField(instruction) + && fieldRef.DeclaringType.FullName == typeof(Game1).FullName + && fieldRef.Name == "borderFont"; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs new file mode 100644 index 00000000..b852f10d --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs @@ -0,0 +1,35 @@ +using System.Diagnostics.CodeAnalysis; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; +using StardewValley; + +namespace StardewModdingAPI.AssemblyRewriters.Finders +{ + /// Finds CIL instructions that reference the former Game1.smoothFont field, which was removed in Stardew Valley 1.2.3–1.2.6. + [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] + public class Game1_smoothFont_FieldFinder : BaseFieldFinder + { + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.smoothFont field"; + + + /********* + ** Protected methods + *********/ + /// Get whether a field reference should be rewritten. + /// 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) + { + return + this.IsStaticField(instruction) + && fieldRef.DeclaringType.FullName == typeof(Game1).FullName + && fieldRef.Name == "smoothFont"; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs index f359b595..4abcbc13 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs @@ -5,7 +5,7 @@ using StardewModdingAPI.AssemblyRewriters.Framework; namespace StardewModdingAPI.AssemblyRewriters.Finders { - /// Matches CIL instructions that reference the former StardewModdingAPI.Extensions class, which was removed in SMAPI 1.9. + /// Finds CIL instructions that reference the former StardewModdingAPI.Extensions class, which was removed in SMAPI 1.9. [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] public class SMAPI_Extensions_MethodFinder : BaseMethodFinder { -- cgit From a12bcf3b7845b7c4541ca7539a4810a81b87e3ce Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 17:48:53 -0400 Subject: reject mods which reference obsolete SGame class (#247) --- .../Finders/GenericTypeFinder.cs | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs new file mode 100644 index 00000000..11ffa734 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs @@ -0,0 +1,30 @@ +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Finders +{ + /// Base class for a type reference finder. + public class GenericTypeFinder : BaseTypeFinder + { + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + /// The full type name to match. + public override string FullTypeName { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name to match. + /// A brief noun phrase indicating what the instruction finder matches. + public GenericTypeFinder(string fullTypeName, string nounPhrase) + { + this.FullTypeName = fullTypeName; + this.NounPhrase = nounPhrase; + } + } +} -- cgit From ccc57935de9b75e17b9f531df87e2ac4dac43dfc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 18:25:29 -0400 Subject: replace individual instruction finders with generic implementations (#247) --- .../Finders/Game1_borderFont_FieldFinder.cs | 35 ------------- .../Finders/Game1_smoothFont_FieldFinder.cs | 35 ------------- .../Finders/GenericFieldFinder.cs | 61 ++++++++++++++++++++++ .../Finders/GenericMethodFinder.cs | 54 +++++++++++++++++++ .../Finders/GenericTypeFinder.cs | 54 +++++++++++++++---- .../Finders/SMAPI_Extensions_MethodFinder.cs | 31 ----------- 6 files changed, 159 insertions(+), 111 deletions(-) delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs deleted file mode 100644 index e25cc544..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_borderFont_FieldFinder.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; -using StardewValley; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds CIL instructions that reference the former Game1.borderFont field, which was removed in Stardew Valley 1.2.3–1.2.6. - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] - public class Game1_borderFont_FieldFinder : BaseFieldFinder - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.borderFont field"; - - - /********* - ** Protected methods - *********/ - /// Get whether a field reference should be rewritten. - /// 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) - { - return - this.IsStaticField(instruction) - && fieldRef.DeclaringType.FullName == typeof(Game1).FullName - && fieldRef.Name == "borderFont"; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs deleted file mode 100644 index b852f10d..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/Game1_smoothFont_FieldFinder.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; -using StardewValley; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds CIL instructions that reference the former Game1.smoothFont field, which was removed in Stardew Valley 1.2.3–1.2.6. - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] - public class Game1_smoothFont_FieldFinder : BaseFieldFinder - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.smoothFont field"; - - - /********* - ** Protected methods - *********/ - /// Get whether a field reference should be rewritten. - /// 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) - { - return - this.IsStaticField(instruction) - && fieldRef.DeclaringType.FullName == typeof(Game1).FullName - && fieldRef.Name == "smoothFont"; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs new file mode 100644 index 00000000..056422a4 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs @@ -0,0 +1,61 @@ +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 GenericFieldFinder : BaseFieldFinder + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// 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 methods + *********/ + /// 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 GenericFieldFinder(string fullTypeName, string fieldName, bool isStatic) + { + this.FullTypeName = fullTypeName; + this.FieldName = fieldName; + this.IsStatic = isStatic; + this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a field reference should be rewritten. + /// 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) + { + return + this.IsStaticField(instruction) == this.IsStatic + && fieldRef.DeclaringType.FullName == this.FullTypeName + && fieldRef.Name == this.FieldName; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs new file mode 100644 index 00000000..f5443558 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs @@ -0,0 +1,54 @@ +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 GenericMethodFinder : BaseMethodFinder + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The method name for which to find references. + private readonly string MethodName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name for which to find references. + /// The method name for which to find references. + public GenericMethodFinder(string fullTypeName, string methodName) + { + this.FullTypeName = fullTypeName; + this.MethodName = methodName; + this.NounPhrase = $"obsolete {fullTypeName}.{methodName} method"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// 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) + { + return methodRef.DeclaringType.FullName == this.FullTypeName + && methodRef.Name == this.MethodName; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs index 11ffa734..1556cc3c 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs @@ -1,18 +1,24 @@ -using StardewModdingAPI.AssemblyRewriters.Framework; +using System.Linq; +using Mono.Cecil; +using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Finders { - /// Base class for a type reference finder. - public class GenericTypeFinder : BaseTypeFinder + /// Finds CIL instructions that reference a given type. + public sealed class GenericTypeFinder : IInstructionFinder { /********* ** Accessors *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } + /// The full type name for which to find references. + private readonly string FullTypeName; + - /// The full type name to match. - public override string FullTypeName { get; } + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } /********* @@ -20,11 +26,39 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders *********/ /// Construct an instance. /// The full type name to match. - /// A brief noun phrase indicating what the instruction finder matches. - public GenericTypeFinder(string fullTypeName, string nounPhrase) + public GenericTypeFinder(string fullTypeName) { this.FullTypeName = fullTypeName; - this.NounPhrase = nounPhrase; + this.NounPhrase = $"obsolete {fullTypeName} type"; + } + + /// Get whether a CIL instruction matches. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + public bool IsMatch(Instruction instruction, bool platformChanged) + { + string fullName = this.FullTypeName; + + // field reference + if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld) + { + FieldReference field = (FieldReference)instruction.Operand; + return + field.DeclaringType.FullName == fullName // field on target class + || field.FieldType.FullName == fullName; // field value is target class + } + + // method reference + if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt) + { + MethodReference method = (MethodReference)instruction.Operand; + return + method.DeclaringType.FullName == fullName // method on target class + || method.ReturnType.FullName == fullName // method returns target class + || method.Parameters.Any(p => p.ParameterType.FullName == fullName); // method parameters + } + + return false; } } } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs deleted file mode 100644 index 4abcbc13..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/SMAPI_Extensions_MethodFinder.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds CIL instructions that reference the former StardewModdingAPI.Extensions class, which was removed in SMAPI 1.9. - [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "This class is not meant to be used directly, and is deliberately named to make it easier to know what it changes at a glance.")] - public class SMAPI_Extensions_MethodFinder : BaseMethodFinder - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = "obsolete StardewModdingAPI.Extensions API"; - - - /********* - ** Protected methods - *********/ - /// Get whether a method reference should be rewritten. - /// 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) - { - return methodRef.DeclaringType.FullName == "StardewModdingAPI.Extensions"; - } - } -} -- cgit From ac19a1a85aac382271b01bfc801f7a293f8b0804 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 12 Mar 2017 19:05:31 -0400 Subject: add incompatibility finders for events removed in SMAPI 1.9 (#247) --- .../Finders/GenericEventFinder.cs | 54 ++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs new file mode 100644 index 00000000..c2a981e5 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs @@ -0,0 +1,54 @@ +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 GenericEventFinder : BaseMethodFinder + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The event name for which to find references. + private readonly string EventName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name for which to find references. + /// The event name for which to find references. + public GenericEventFinder(string fullTypeName, string eventName) + { + this.FullTypeName = fullTypeName; + this.EventName = eventName; + this.NounPhrase = $"obsolete {fullTypeName}.{eventName} event"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// 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) + { + return methodRef.DeclaringType.FullName == this.FullTypeName + && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName); + } + } +} -- cgit From 4d48bdfe7c3806ec1995cd499ca9382ace2d8a53 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 13:50:01 -0400 Subject: drop 'generic' prefix for rewriters since they're all generic now --- .../Finders/EventFinder.cs | 54 ++++++++++++++++++ .../Finders/FieldFinder.cs | 61 +++++++++++++++++++++ .../Finders/GenericEventFinder.cs | 54 ------------------ .../Finders/GenericFieldFinder.cs | 61 --------------------- .../Finders/GenericMethodFinder.cs | 54 ------------------ .../Finders/GenericTypeFinder.cs | 64 ---------------------- .../Finders/MethodFinder.cs | 54 ++++++++++++++++++ .../Finders/TypeFinder.cs | 64 ++++++++++++++++++++++ 8 files changed, 233 insertions(+), 233 deletions(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs new file mode 100644 index 00000000..359ca63e --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs @@ -0,0 +1,54 @@ +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The event name for which to find references. + private readonly string EventName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name for which to find references. + /// The event name for which to find references. + public EventFinder(string fullTypeName, string eventName) + { + this.FullTypeName = fullTypeName; + this.EventName = eventName; + this.NounPhrase = $"obsolete {fullTypeName}.{eventName} event"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// 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) + { + return 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 new file mode 100644 index 00000000..516641f2 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/FieldFinder.cs @@ -0,0 +1,61 @@ +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// 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 methods + *********/ + /// 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) + { + this.FullTypeName = fullTypeName; + this.FieldName = fieldName; + this.IsStatic = isStatic; + this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a field reference should be rewritten. + /// 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) + { + return + this.IsStaticField(instruction) == this.IsStatic + && fieldRef.DeclaringType.FullName == this.FullTypeName + && fieldRef.Name == this.FieldName; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs deleted file mode 100644 index c2a981e5..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericEventFinder.cs +++ /dev/null @@ -1,54 +0,0 @@ -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 GenericEventFinder : BaseMethodFinder - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The event name for which to find references. - private readonly string EventName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The full type name for which to find references. - /// The event name for which to find references. - public GenericEventFinder(string fullTypeName, string eventName) - { - this.FullTypeName = fullTypeName; - this.EventName = eventName; - this.NounPhrase = $"obsolete {fullTypeName}.{eventName} event"; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a method reference should be rewritten. - /// 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) - { - return methodRef.DeclaringType.FullName == this.FullTypeName - && (methodRef.Name == "add_" + this.EventName || methodRef.Name == "remove_" + this.EventName); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs deleted file mode 100644 index 056422a4..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericFieldFinder.cs +++ /dev/null @@ -1,61 +0,0 @@ -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 GenericFieldFinder : BaseFieldFinder - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// 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 methods - *********/ - /// 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 GenericFieldFinder(string fullTypeName, string fieldName, bool isStatic) - { - this.FullTypeName = fullTypeName; - this.FieldName = fieldName; - this.IsStatic = isStatic; - this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field"; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a field reference should be rewritten. - /// 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) - { - return - this.IsStaticField(instruction) == this.IsStatic - && fieldRef.DeclaringType.FullName == this.FullTypeName - && fieldRef.Name == this.FieldName; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs deleted file mode 100644 index f5443558..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericMethodFinder.cs +++ /dev/null @@ -1,54 +0,0 @@ -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 GenericMethodFinder : BaseMethodFinder - { - /********* - ** Properties - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - /// The method name for which to find references. - private readonly string MethodName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The full type name for which to find references. - /// The method name for which to find references. - public GenericMethodFinder(string fullTypeName, string methodName) - { - this.FullTypeName = fullTypeName; - this.MethodName = methodName; - this.NounPhrase = $"obsolete {fullTypeName}.{methodName} method"; - } - - - /********* - ** Protected methods - *********/ - /// Get whether a method reference should be rewritten. - /// 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) - { - return methodRef.DeclaringType.FullName == this.FullTypeName - && methodRef.Name == this.MethodName; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs deleted file mode 100644 index 1556cc3c..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/GenericTypeFinder.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Finders -{ - /// Finds CIL instructions that reference a given type. - public sealed class GenericTypeFinder : IInstructionFinder - { - /********* - ** Accessors - *********/ - /// The full type name for which to find references. - private readonly string FullTypeName; - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public string NounPhrase { get; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - /// The full type name to match. - public GenericTypeFinder(string fullTypeName) - { - this.FullTypeName = fullTypeName; - this.NounPhrase = $"obsolete {fullTypeName} type"; - } - - /// Get whether a CIL instruction matches. - /// The IL instruction. - /// Whether the mod was compiled on a different platform. - public bool IsMatch(Instruction instruction, bool platformChanged) - { - string fullName = this.FullTypeName; - - // field reference - if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld) - { - FieldReference field = (FieldReference)instruction.Operand; - return - field.DeclaringType.FullName == fullName // field on target class - || field.FieldType.FullName == fullName; // field value is target class - } - - // method reference - if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt) - { - MethodReference method = (MethodReference)instruction.Operand; - return - method.DeclaringType.FullName == fullName // method on target class - || method.ReturnType.FullName == fullName // method returns target class - || method.Parameters.Any(p => p.ParameterType.FullName == fullName); // method parameters - } - - return false; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs new file mode 100644 index 00000000..6c210d68 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs @@ -0,0 +1,54 @@ +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 + { + /********* + ** Properties + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + /// The method name for which to find references. + private readonly string MethodName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name for which to find references. + /// The method name for which to find references. + public MethodFinder(string fullTypeName, string methodName) + { + this.FullTypeName = fullTypeName; + this.MethodName = methodName; + this.NounPhrase = $"obsolete {fullTypeName}.{methodName} method"; + } + + + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// 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) + { + return methodRef.DeclaringType.FullName == this.FullTypeName + && methodRef.Name == this.MethodName; + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs new file mode 100644 index 00000000..ba8e7102 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs @@ -0,0 +1,64 @@ +using System.Linq; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters.Finders +{ + /// Finds CIL instructions that reference a given type. + public sealed class TypeFinder : IInstructionFinder + { + /********* + ** Accessors + *********/ + /// The full type name for which to find references. + private readonly string FullTypeName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The full type name to match. + public TypeFinder(string fullTypeName) + { + this.FullTypeName = fullTypeName; + this.NounPhrase = $"obsolete {fullTypeName} type"; + } + + /// Get whether a CIL instruction matches. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + public bool IsMatch(Instruction instruction, bool platformChanged) + { + string fullName = this.FullTypeName; + + // field reference + if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld) + { + FieldReference field = (FieldReference)instruction.Operand; + return + field.DeclaringType.FullName == fullName // field on target class + || field.FieldType.FullName == fullName; // field value is target class + } + + // method reference + if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt) + { + MethodReference method = (MethodReference)instruction.Operand; + return + method.DeclaringType.FullName == fullName // method on target class + || method.ReturnType.FullName == fullName // method returns target class + || method.Parameters.Any(p => p.ParameterType.FullName == fullName); // method parameters + } + + return false; + } + } +} -- cgit 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 From 06f5e92b88fac190f94690f1580775449014e411 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 22:08:48 -0400 Subject: minor cleanup --- .../Finders/EventFinder.cs | 5 +++-- .../Finders/MethodFinder.cs | 5 +++-- .../Finders/TypeFinder.cs | 25 +++++++++++----------- 3 files changed, 19 insertions(+), 16 deletions(-) (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs index 9d0184c6..848e54ff 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/EventFinder.cs @@ -29,11 +29,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// Construct an instance. /// The full type name for which to find references. /// The event name for which to find references. - public EventFinder(string fullTypeName, string eventName) + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public EventFinder(string fullTypeName, string eventName, string nounPhrase = null) { this.FullTypeName = fullTypeName; this.EventName = eventName; - this.NounPhrase = $"obsolete {fullTypeName}.{eventName} event"; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{eventName} event"; } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs index bea549ee..d174bacd 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/MethodFinder.cs @@ -29,11 +29,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders /// Construct an instance. /// The full type name for which to find references. /// The method name for which to find references. - public MethodFinder(string fullTypeName, string methodName) + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public MethodFinder(string fullTypeName, string methodName, string nounPhrase = null) { this.FullTypeName = fullTypeName; this.MethodName = methodName; - this.NounPhrase = $"obsolete {fullTypeName}.{methodName} method"; + this.NounPhrase = nounPhrase ?? $"{fullTypeName}.{methodName} method"; } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs index ba8e7102..8f492d5f 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Finders/TypeFinder.cs @@ -5,7 +5,7 @@ using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Finders { /// Finds CIL instructions that reference a given type. - public sealed class TypeFinder : IInstructionFinder + public class TypeFinder : IInstructionFinder { /********* ** Accessors @@ -26,10 +26,11 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders *********/ /// Construct an instance. /// The full type name to match. - public TypeFinder(string fullTypeName) + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public TypeFinder(string fullTypeName, string nounPhrase = null) { this.FullTypeName = fullTypeName; - this.NounPhrase = $"obsolete {fullTypeName} type"; + this.NounPhrase = nounPhrase ?? $"{fullTypeName} type"; } /// Get whether a CIL instruction matches. @@ -40,22 +41,22 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders string fullName = this.FullTypeName; // field reference - if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld) + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) { - FieldReference field = (FieldReference)instruction.Operand; return - field.DeclaringType.FullName == fullName // field on target class - || field.FieldType.FullName == fullName; // field value is target class + fieldRef.DeclaringType.FullName == fullName // field on target class + || fieldRef.FieldType.FullName == fullName;