diff options
9 files changed, 165 insertions, 171 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"; - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs deleted file mode 100644 index 5a768794..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseTypeFinder.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters.Framework -{ - /// <summary>Base class for a type reference finder.</summary> - public abstract class BaseTypeFinder : IInstructionFinder - { - /********* - ** Accessors - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public abstract string NounPhrase { get; } - - /// <summary>The full type name to match.</summary> - public abstract string FullTypeName { get; } - - - /********* - ** Public methods - *********/ - /// <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 virtual 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/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index d489eb34..5089318c 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -66,10 +66,8 @@ <Compile Include="..\GlobalAssemblyInfo.cs"> <Link>Properties\GlobalAssemblyInfo.cs</Link> </Compile> - <Compile Include="Finders\Game1_smoothFont_FieldFinder.cs" /> - <Compile Include="Finders\Game1_borderFont_FieldFinder.cs" /> - <Compile Include="Finders\SMAPI_Extensions_MethodFinder.cs" /> - <Compile Include="Framework\BaseTypeFinder.cs" /> + <Compile Include="Finders\GenericFieldFinder.cs" /> + <Compile Include="Finders\GenericMethodFinder.cs" /> <Compile Include="Framework\BaseFieldFinder.cs" /> <Compile Include="Framework\BaseMethodFinder.cs" /> <Compile Include="Finders\GenericTypeFinder.cs" /> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 1e1ca325..4c1c7a8e 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -142,12 +142,12 @@ namespace StardewModdingAPI return new IInstructionFinder[] { // changes in Stardew Valley 1.2 - new Game1_borderFont_FieldFinder(), - new Game1_smoothFont_FieldFinder(), + new GenericFieldFinder("Game1", "borderFont", isStatic: true), + new GenericFieldFinder("Game1", "smoothFont", isStatic: true), // APIs removed in SMAPI 1.9 - new SMAPI_Extensions_MethodFinder(), - new GenericTypeFinder("StardewModdingAPI.Inheritance.SGame", "obsolete StardewModdingAPI.SGame class") + new GenericTypeFinder("StardewModdingAPI.Extensions"), + new GenericTypeFinder("StardewModdingAPI.Inheritance.SGame") }; } |