diff options
| author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-12 18:25:29 -0400 | 
|---|---|---|
| committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-12 18:25:29 -0400 | 
| commit | ccc57935de9b75e17b9f531df87e2ac4dac43dfc (patch) | |
| tree | 907ba213250089a2833939822950ace659e56fce /src/StardewModdingAPI.AssemblyRewriters/Finders | |
| parent | a6ed67a9f763b5efab58589776e8eaa31a4f2dbc (diff) | |
| download | SMAPI-ccc57935de9b75e17b9f531df87e2ac4dac43dfc.tar.gz SMAPI-ccc57935de9b75e17b9f531df87e2ac4dac43dfc.tar.bz2 SMAPI-ccc57935de9b75e17b9f531df87e2ac4dac43dfc.zip | |
replace individual instruction finders with generic implementations (#247)
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters/Finders')
6 files changed, 159 insertions, 111 deletions
| 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 -{ -    /// <summary>Finds CIL instructions that reference the former <c>Game1.borderFont</c> field, which was removed in Stardew Valley 1.2.3–1.2.6.</summary> -    [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 -        *********/ -        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> -        public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.borderFont field"; - - -        /********* -        ** Protected methods -        *********/ -        /// <summary>Get whether a field reference should be rewritten.</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) -        { -            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 -{ -    /// <summary>Finds CIL instructions that reference the former <c>Game1.smoothFont</c> field, which was removed in Stardew Valley 1.2.3–1.2.6.</summary> -    [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 -        *********/ -        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> -        public override string NounPhrase { get; } = $"obsolete {nameof(Game1)}.smoothFont field"; - - -        /********* -        ** Protected methods -        *********/ -        /// <summary>Get whether a field reference should be rewritten.</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) -        { -            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 +{ +    /// <summary>Finds CIL instructions that reference a given field.</summary> +    public sealed class GenericFieldFinder : BaseFieldFinder +    { +        /********* +        ** Properties +        *********/ +        /// <summary>The full type name for which to find references.</summary> +        private readonly string FullTypeName; + +        /// <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 methods +        *********/ +        /// <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 GenericFieldFinder(string fullTypeName, string fieldName, bool isStatic) +        { +            this.FullTypeName = fullTypeName; +            this.FieldName = fieldName; +            this.IsStatic = isStatic; +            this.NounPhrase = $"obsolete {fullTypeName}.{fieldName} field"; +        } + + +        /********* +        ** Protected methods +        *********/ +        /// <summary>Get whether a field reference should be rewritten.</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) +        { +            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 +{ +    /// <summary>Finds CIL instructions that reference a given method.</summary> +    public sealed class GenericMethodFinder : BaseMethodFinder +    { +        /********* +        ** Properties +        *********/ +        /// <summary>The full type name for which to find references.</summary> +        private readonly string FullTypeName; + +        /// <summary>The method name for which to find references.</summary> +        private readonly string MethodName; + + +        /********* +        ** Accessors +        *********/ +        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> +        public override string NounPhrase { get; } + + +        /********* +        ** Public methods +        *********/ +        /// <summary>Construct an instance.</summary> +        /// <param name="fullTypeName">The full type name for which to find references.</param> +        /// <param name="methodName">The method name for which to find references.</param> +        public GenericMethodFinder(string fullTypeName, string methodName) +        { +            this.FullTypeName = fullTypeName; +            this.MethodName = methodName; +            this.NounPhrase = $"obsolete {fullTypeName}.{methodName} method"; +        } + + +        /********* +        ** Protected methods +        *********/ +        /// <summary>Get whether a method reference should be rewritten.</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) +        { +            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  { -    /// <summary>Base class for a type reference finder.</summary> -    public class GenericTypeFinder : BaseTypeFinder +    /// <summary>Finds CIL instructions that reference a given type.</summary> +    public sealed class GenericTypeFinder : IInstructionFinder      {          /*********          ** Accessors          *********/ -        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> -        public override string NounPhrase { get; } +        /// <summary>The full type name for which to find references.</summary> +        private readonly string FullTypeName; + -        /// <summary>The full type name to match.</summary> -        public override string FullTypeName { get; } +        /********* +        ** Accessors +        *********/ +        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> +        public string NounPhrase { get; }          /********* @@ -20,11 +26,39 @@ namespace StardewModdingAPI.AssemblyRewriters.Finders          *********/          /// <summary>Construct an instance.</summary>          /// <param name="fullTypeName">The full type name to match.</param> -        /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches.</param> -        public GenericTypeFinder(string fullTypeName, string nounPhrase) +        public GenericTypeFinder(string fullTypeName)          {              this.FullTypeName = fullTypeName; -            this.NounPhrase = nounPhrase; +            this.NounPhrase = $"obsolete {fullTypeName} type"; +        } + +        /// <summary>Get whether a CIL instruction matches.</summary> +        /// <param name="instruction">The IL instruction.</param> +        /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> +        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 -{ -    /// <summary>Finds CIL instructions that reference the former <c>StardewModdingAPI.Extensions</c> class, which was removed in SMAPI 1.9.</summary> -    [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 -        *********/ -        /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> -        public override string NounPhrase { get; } = "obsolete StardewModdingAPI.Extensions API"; - - -        /********* -        ** Protected methods -        *********/ -        /// <summary>Get whether a method reference should be rewritten.</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) -        { -            return methodRef.DeclaringType.FullName == "StardewModdingAPI.Extensions"; -        } -    } -} | 
