From 2e58f853d27cc200be5b34a1ebef5bbdb7703a49 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 25 Mar 2017 13:40:37 -0400 Subject: replace field-to-property rewriters with a generic rewriter --- .../Rewriters/GenericFieldToPropertyRewriter.cs | 70 ++++++++++++++++++++++ .../Rewriters/GenericMethodMapper.cs | 8 +-- .../Game1_ActiveClickableMenu_FieldRewriter.cs | 49 --------------- .../SDV1_2/Game1_GameMode_FieldRewriter.cs | 49 --------------- .../Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs | 49 --------------- .../StardewModdingAPI.AssemblyRewriters.csproj | 6 +- 6 files changed, 76 insertions(+), 155 deletions(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs new file mode 100644 index 00000000..f58bcfbb --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs @@ -0,0 +1,70 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// Rewrites field references into property references. + public class GenericFieldToPropertyRewriter : BaseFieldRewriter + { + /********* + ** Properties + *********/ + /// The type whose field to which references should be rewritten. + private readonly Type Type; + + /// The field name to rewrite. + private readonly string FieldName; + + + /********* + ** Accessors + *********/ + /// A brief noun phrase indicating what the instruction finder matches. + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The type whose field to which references should be rewritten. + /// The field name to rewrite. + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public GenericFieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null) + { + this.Type = type; + this.FieldName = fieldName; + this.NounPhrase = nounPhrase ?? $"{type.Name}.{fieldName} 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 + fieldRef.DeclaringType.FullName == this.Type.FullName + && fieldRef.Name == this.FieldName; + } + + /// 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 || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; + MethodReference propertyRef = module.Import(this.Type.GetMethod($"{methodPrefix}_{this.FieldName}")); + cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs index 92ba3af1..49e0aad7 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs @@ -34,13 +34,13 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters /// Construct an instance. /// The type whose methods to remap. /// The type with methods to map to. - /// A brief noun phrase indicating what the instruction finder matches. /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - public GenericMethodMapper(Type fromType, Type toType, string nounPhrase, bool onlyIfPlatformChanged = false) + /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). + public GenericMethodMapper(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null) { this.FromType = fromType; this.ToType = toType; - this.NounPhrase = nounPhrase; + this.NounPhrase = nounPhrase ?? $"{fromType.Name} methods"; this.OnlyIfPlatformChanged = onlyIfPlatformChanged; } @@ -71,4 +71,4 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters methodRef.DeclaringType = module.Import(this.ToType); } } -} \ No newline at end of file +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs deleted file mode 100644 index 59a7c798..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs +++ /dev/null @@ -1,49 +0,0 @@ -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 - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.activeClickableMenu)} 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 == 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)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs deleted file mode 100644 index c3da6863..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs +++ /dev/null @@ -1,49 +0,0 @@ -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_GameMode_FieldRewriter : BaseFieldRewriter - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.gameMode)} 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 == nameof(Game1.gameMode); - } - - /// 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.gameMode)}")); - cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs deleted file mode 100644 index 91eae416..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs +++ /dev/null @@ -1,49 +0,0 @@ -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_Player_FieldRewriter : BaseFieldRewriter - { - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the instruction finder matches. - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.player)} 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 == nameof(Game1.player); - } - - /// 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.player)}")); - cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 8c10aea7..8416aad7 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -80,11 +80,9 @@ - + - - - + -- cgit