From b8f17e6afb00b78ac31df98f7dc3bbe071a99e47 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 11 Mar 2018 19:10:08 -0400 Subject: update rewriters for Stardew Valley 1.3 (#453) --- .../Rewriters/FieldToPropertyRewriter.cs | 18 +++++-- .../Rewriters/StaticFieldToConstantRewriter.cs | 63 ++++++++++++++++++++++ src/SMAPI/Metadata/InstructionMetadata.cs | 37 +++++++++---- src/SMAPI/StardewModdingAPI.csproj | 1 + 4 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs index a20b8bee..b1fa377a 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs @@ -14,8 +14,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The type whose field to which references should be rewritten. private readonly Type Type; - /// The field name to rewrite. - private readonly string FieldName; + /// The property name. + private readonly string PropertyName; /********* @@ -24,13 +24,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Construct an instance. /// The type whose field to which references should be rewritten. /// The field name to rewrite. - public FieldToPropertyRewriter(Type type, string fieldName) + /// The property name (if different). + public FieldToPropertyRewriter(Type type, string fieldName, string propertyName) : base(type.FullName, fieldName, InstructionHandleResult.None) { this.Type = type; - this.FieldName = fieldName; + this.PropertyName = propertyName; } + /// Construct an instance. + /// The type whose field to which references should be rewritten. + /// The field name to rewrite. + public FieldToPropertyRewriter(Type type, string fieldName) + : this(type, fieldName, fieldName) { } + /// Perform the predefined logic for an instruction if applicable. /// The assembly module containing the instruction. /// The CIL processor. @@ -43,8 +50,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters return InstructionHandleResult.None; string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; - MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}")); + MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.PropertyName}")); cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); + return InstructionHandleResult.Rewritten; } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs new file mode 100644 index 00000000..5e12b46a --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs @@ -0,0 +1,63 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Finders; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Rewrites static field references into constant values. + /// The constant value type. + internal class StaticFieldToConstantRewriter : FieldFinder + { + /********* + ** Properties + *********/ + /// The constant value to replace with. + private readonly TValue Value; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type whose field to which references should be rewritten. + /// The field name to rewrite. + /// The constant value to replace with. + public StaticFieldToConstantRewriter(Type type, string fieldName, TValue value) + : base(type.FullName, fieldName, InstructionHandleResult.None) + { + this.Value = value; + } + + /// Perform the predefined logic for an instruction if applicable. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The instruction to handle. + /// Metadata for mapping assemblies to the current platform. + /// Whether the mod was compiled on a different platform. + public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + { + if (!this.IsMatch(instruction)) + return InstructionHandleResult.None; + + cil.Replace(instruction, this.CreateConstantInstruction(cil, this.Value)); + return InstructionHandleResult.Rewritten; + } + + + /********* + ** Private methods + *********/ + /// Create a CIL constant value instruction. + /// The CIL processor. + /// The constant value to set. + private Instruction CreateConstantInstruction(ILProcessor cil, object value) + { + if (typeof(TValue) == typeof(int)) + return cil.Create(OpCodes.Ldc_I4, (int)value); + if (typeof(TValue) == typeof(string)) + return cil.Create(OpCodes.Ldstr, (string)value); + throw new NotSupportedException($"Rewriting to constant values of type {typeof(TValue)} isn't currently supported."); + } + } +} diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index 4ed1e38e..603a43cb 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -6,6 +6,9 @@ using StardewModdingAPI.Framework.ModLoading; using StardewModdingAPI.Framework.ModLoading.Finders; using StardewModdingAPI.Framework.ModLoading.Rewriters; using StardewValley; +#if STARDEW_VALLEY_1_3 +using SObject = StardewValley.Object; +#endif namespace StardewModdingAPI.Metadata { @@ -31,10 +34,11 @@ namespace StardewModdingAPI.Metadata /**** ** rewrite CIL to fix incompatible code ****/ - // crossplatform + // rewrite for crossplatform compatibility new MethodParentRewriter(typeof(SpriteBatch), typeof(SpriteBatchMethods), onlyIfPlatformChanged: true), - // Stardew Valley 1.2 +#if !STARDEW_VALLEY_1_3 + // rewrite for Stardew Valley 1.2 new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.activeClickableMenu)), new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.currentMinigame)), new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.gameMode)), @@ -42,19 +46,33 @@ namespace StardewModdingAPI.Metadata new FieldReplaceRewriter(typeof(Game1), "borderFont", nameof(Game1.smallFont)), new FieldReplaceRewriter(typeof(Game1), "smoothFont", nameof(Game1.smallFont)), - // SMAPI 1.9 + // rewrite for SMAPI 1.9 new TypeReferenceRewriter("StardewModdingAPI.Inheritance.ItemStackChange", typeof(ItemStackChange)), +#endif - // SMAPI 2.0 - new VirtualEntryCallRemover(), // Mod.Entry changed from virtual to abstract in SMAPI 2.0, which breaks the few mods which called base.Entry() + // rewrite for SMAPI 2.0 + new VirtualEntryCallRemover(), + + // rewrite for Stardew Valley 1.3 +#if STARDEW_VALLEY_1_3 + new StaticFieldToConstantRewriter(typeof(Game1), "tileSize", Game1.tileSize), + new FieldToPropertyRewriter(typeof(Character), nameof(Character.name), nameof(Character.Name)), + new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.isFarm), nameof(GameLocation.IsFarm)), + new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.isOutdoors), nameof(GameLocation.isOutdoors)), + new FieldToPropertyRewriter(typeof(GameLocation), nameof(GameLocation.name), nameof(GameLocation.Name)), + new FieldToPropertyRewriter(typeof(Item), nameof(Item.category), nameof(Item.Category)), + new FieldToPropertyRewriter(typeof(SObject), nameof(SObject.quality), nameof(SObject.Quality)), + new FieldToPropertyRewriter(typeof(SObject), nameof(SObject.stack), nameof(SObject.Stack)), +#endif /**** ** detect incompatible code ****/ - // changes in Stardew Valley 1.2 (with no rewriters) + #if !STARDEW_VALLEY_1_3 + // detect changes in Stardew Valley 1.2 new FieldFinder("StardewValley.Item", "set_Name", InstructionHandleResult.NotCompatible), - // APIs removed in SMAPI 1.9 + // detect APIs removed in SMAPI 1.9 new TypeFinder("StardewModdingAPI.Advanced.ConfigFile", InstructionHandleResult.NotCompatible), new TypeFinder("StardewModdingAPI.Advanced.IConfigFile", InstructionHandleResult.NotCompatible), new TypeFinder("StardewModdingAPI.Entities.SPlayer", InstructionHandleResult.NotCompatible), @@ -71,7 +89,7 @@ namespace StardewModdingAPI.Metadata new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderHudEventNoCheck", InstructionHandleResult.NotCompatible), new EventFinder("StardewModdingAPI.Events.GraphicsEvents", "OnPreRenderGuiEventNoCheck", InstructionHandleResult.NotCompatible), - // APIs removed in SMAPI 2.0 + // detect APIs removed in SMAPI 2.0 new TypeFinder("StardewModdingAPI.Command", InstructionHandleResult.NotCompatible), new TypeFinder("StardewModdingAPI.Config", InstructionHandleResult.NotCompatible), new TypeFinder("StardewModdingAPI.Log", InstructionHandleResult.NotCompatible), @@ -93,8 +111,9 @@ namespace StardewModdingAPI.Metadata new PropertyFinder("StardewModdingAPI.Mod", "BaseConfigPath", InstructionHandleResult.NotCompatible), new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigFolder", InstructionHandleResult.NotCompatible), new PropertyFinder("StardewModdingAPI.Mod", "PerSaveConfigPath", InstructionHandleResult.NotCompatible), + #endif - // broken code + // detect broken code new ReferenceToMissingMemberFinder(this.ValidateReferencesToAssemblies), new ReferenceToMemberWithUnexpectedTypeFinder(this.ValidateReferencesToAssemblies), diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 42bcb9a8..1dc7740e 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -112,6 +112,7 @@ + -- cgit