diff options
4 files changed, 82 insertions, 19 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs new file mode 100644 index 00000000..31f9a40f --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/FieldReplaceRewriter.cs @@ -0,0 +1,78 @@ +using System; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// <summary>Rewrites references to one field with another.</summary> + public class FieldReplaceRewriter : BaseFieldRewriter + { + /********* + ** Properties + *********/ + /// <summary>The type whose field to which references should be rewritten.</summary> + private readonly Type Type; + + /// <summary>The field name to rewrite.</summary> + private readonly string FromFieldName; + + /// <summary>The new field name to reference.</summary> + private readonly string ToFieldName; + + + /********* + ** 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="type">The type whose field to which references should be rewritten.</param> + /// <param name="fromFieldName">The field name to rewrite.</param> + /// <param name="toFieldName">The new field name to reference.</param> + /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> + public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName, string nounPhrase = null) + { + this.Type = type; + this.FromFieldName = fromFieldName; + this.ToFieldName = toFieldName; + this.NounPhrase = nounPhrase ?? $"{type.Name}.{fromFieldName} 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 + fieldRef.DeclaringType.FullName == this.Type.FullName + && fieldRef.Name == this.FromFieldName; + } + + /// <summary>Rewrite a method for compatibility.</summary> + /// <param name="module">The module being rewritten.</param> + /// <param name="cil">The CIL rewriter.</param> + /// <param name="instruction">The instruction which references the field.</param> + /// <param name="fieldRef">The field reference invoked by the <paramref name="instruction"/>.</param> + /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> + protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) + { + FieldInfo field = this.Type.GetField(this.ToFieldName); + if(field == null) + throw new InvalidOperationException($"The {this.Type.FullName} class doesn't have a {this.ToFieldName} field."); + FieldReference newRef = module.Import(field); + cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 90e800d0..0136c39b 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -80,6 +80,7 @@ <Compile Include="Platform.cs" /> <Compile Include="PlatformAssemblyMap.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Rewriters\FieldReplaceRewriter.cs" /> <Compile Include="Rewriters\FieldToPropertyRewriter.cs" /> <Compile Include="Rewriters\MethodParentRewriter.cs" /> <Compile Include="Rewriters\Wrappers\SpriteBatchWrapper.cs" /> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 5fa25014..9a7fba84 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -143,8 +143,6 @@ namespace StardewModdingAPI return new IInstructionFinder[] { // changes in Stardew Valley 1.2 (that don't have rewriters) - new FieldFinder("StardewValley.Game1", "borderFont", isStatic: true), - new FieldFinder("StardewValley.Game1", "smoothFont", isStatic: true), new FieldFinder("StardewValley.Item", "set_Name", isStatic: false), // APIs removed in SMAPI 1.9 @@ -178,7 +176,9 @@ namespace StardewModdingAPI // Stardew Valley 1.2 new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.activeClickableMenu)), new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.gameMode)), - new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.player)) + new FieldToPropertyRewriter(typeof(Game1), nameof(Game1.player)), + new FieldReplaceRewriter(typeof(Game1), "borderFont", nameof(Game1.smallFont)), + new FieldReplaceRewriter(typeof(Game1), "smoothFont", nameof(Game1.smallFont)) }; } diff --git a/src/StardewModdingAPI/StardewModdingAPI.config.json b/src/StardewModdingAPI/StardewModdingAPI.config.json index 64b4fd53..da6d3730 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.config.json +++ b/src/StardewModdingAPI/StardewModdingAPI.config.json @@ -94,22 +94,6 @@ This file contains advanced configuration for SMAPI. You generally shouldn't cha "Notes": "Crashes with 'Method not found: Void StardewValley.Item.set_Name(System.String)'." }, { - "Name": "CJB Cheats Menu", - "ID": "CJBCheatsMenu", - "UpperVersion": "1.13", - "Compatibility": "AssumeBroken", - "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/4", - "Notes": "Uses removed Game1.borderFont." - }, - { - "Name": "CJB Item Spawner", - "ID": "CJBItemSpawner", - "UpperVersion": "1.6", - "Compatibility": "AssumeBroken", - "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/93", - "Notes": "Uses removed Game1.borderFont." - }, - { "Name": "Cooking Skill", "ID": "CookingSkill", "UpperVersion": "1.0.3", |