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 { diff --git a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs index f2074f22..ac2facec 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Framework/BaseFieldFinder.cs @@ -35,5 +35,12 @@ namespace StardewModdingAPI.AssemblyRewriters.Framework /// The field reference. /// Whether the mod was compiled on a different platform. protected abstract bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged); + + /// Whether an instruction is a static field reference. + /// The IL instruction. + protected bool IsStaticField(Instruction instruction) + { + return instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld; + } } } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs index bb49f16c..59a7c798 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return - (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field + this.IsStaticField(instruction) && fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.Name == nameof(Game1.activeClickableMenu); } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs index 4d84d9ac..c3da6863 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return - (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field + this.IsStaticField(instruction) && fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.Name == nameof(Game1.gameMode); } diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs index f43f5d57..91eae416 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 protected override bool IsMatch(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return - (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field + this.IsStaticField(instruction) && fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.Name == nameof(Game1.player); } diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 4d96d5d3..3f437ca0 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -66,6 +66,8 @@ Properties\GlobalAssemblyInfo.cs + + diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index f18e0792..563cdd52 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -141,6 +141,10 @@ namespace StardewModdingAPI { return new IInstructionFinder[] { + // changes in Stardew Valley 1.2 + new Game1_borderFont_FieldFinder(), + new Game1_smoothFont_FieldFinder(), + // APIs removed in SMAPI 1.9 new SMAPI_Extensions_MethodFinder() };