using System.Diagnostics.CodeAnalysis; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.AssemblyRewriters.Framework; using StardewValley; namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 { /// Rewrites field references to . /// Stardew Valley changed the field to a property, which broke many mods that reference it. [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_ActiveClickableMenu_FieldRewriter : BaseFieldRewriter { /********* ** 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 ShouldRewrite(Instruction instruction, FieldReference fieldRef, bool platformChanged) { return (instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Stsfld) // static field && fieldRef.DeclaringType.FullName == typeof(Game1).FullName && fieldRef.Name == nameof(Game1.activeClickableMenu); } /// Rewrite a method for compatibility. /// The module being rewritten. /// The CIL rewriter. /// The instruction which references the field. /// The field reference invoked by the . /// Metadata for mapping assemblies to the current platform. protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, FieldReference fieldRef, PlatformAssemblyMap assemblyMap) { string methodPrefix = instruction.OpCode == OpCodes.Ldsfld ? "get" : "set"; MethodReference propertyRef = module.Import(typeof(Game1).GetMethod($"{methodPrefix}_{nameof(Game1.activeClickableMenu)}")); cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); } } }