diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 13:40:37 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 13:40:37 -0400 |
commit | 2e58f853d27cc200be5b34a1ebef5bbdb7703a49 (patch) | |
tree | 450dc06275f595b8ffd9036f69679d06994fc7ed /src/StardewModdingAPI.AssemblyRewriters | |
parent | d724f54f32c4de113700fd2173c070d6a992f66e (diff) | |
download | SMAPI-2e58f853d27cc200be5b34a1ebef5bbdb7703a49.tar.gz SMAPI-2e58f853d27cc200be5b34a1ebef5bbdb7703a49.tar.bz2 SMAPI-2e58f853d27cc200be5b34a1ebef5bbdb7703a49.zip |
replace field-to-property rewriters with a generic rewriter
Diffstat (limited to 'src/StardewModdingAPI.AssemblyRewriters')
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs (renamed from src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs) | 47 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs | 8 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_GameMode_FieldRewriter.cs | 49 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_Player_FieldRewriter.cs | 49 | ||||
-rw-r--r-- | src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj | 6 |
5 files changed, 40 insertions, 119 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs index 59a7c798..f58bcfbb 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericFieldToPropertyRewriter.cs @@ -1,21 +1,43 @@ -using System.Diagnostics.CodeAnalysis; +using System; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.AssemblyRewriters.Framework; -using StardewValley; -namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 +namespace StardewModdingAPI.AssemblyRewriters.Rewriters { - /// <summary>Rewrites field references to <see cref="Game1.activeClickableMenu"/>.</summary> - /// <remarks>Stardew Valley changed the <see cref="Game1.activeClickableMenu"/> field to a property, which broke many mods that reference it.</remarks> - [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 + /// <summary>Rewrites field references into property references.</summary> + public class GenericFieldToPropertyRewriter : 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 FieldName; + + + /********* ** Accessors *********/ /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.activeClickableMenu)} field"; + 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="fieldName">The field name to rewrite.</param> + /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> + public GenericFieldToPropertyRewriter(Type type, string fieldName, string nounPhrase = null) + { + this.Type = type; + this.FieldName = fieldName; + this.NounPhrase = nounPhrase ?? $"{type.Name}.{fieldName} field"; + } /********* @@ -28,9 +50,8 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 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); + fieldRef.DeclaringType.FullName == this.Type.FullName + && fieldRef.Name == this.FieldName; } /// <summary>Rewrite a method for compatibility.</summary> @@ -41,8 +62,8 @@ namespace StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2 /// <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) { - string methodPrefix = instruction.OpCode == OpCodes.Ldsfld ? "get" : "set"; - MethodReference propertyRef = module.Import(typeof(Game1).GetMethod($"{methodPrefix}_{nameof(Game1.activeClickableMenu)}")); + 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 /// <summary>Construct an instance.</summary> /// <param name="fromType">The type whose methods to remap.</param> /// <param name="toType">The type with methods to map to.</param> - /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches.</param> /// <param name="onlyIfPlatformChanged">Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.</param> - public GenericMethodMapper(Type fromType, Type toType, string nounPhrase, bool onlyIfPlatformChanged = false) + /// <param name="nounPhrase">A brief noun phrase indicating what the instruction finder matches (or <c>null</c> to generate one).</param> + 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_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 -{ - /// <summary>Rewrites field references to <see cref="Game1.gameMode"/>.</summary> - /// <remarks>Stardew Valley changed the <see cref="Game1.gameMode"/> field to a property, which broke many mods that reference it.</remarks> - [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 - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.gameMode)} 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 - this.IsStaticField(instruction) - && fieldRef.DeclaringType.FullName == typeof(Game1).FullName - && fieldRef.Name == nameof(Game1.gameMode); - } - - /// <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) - { - 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 -{ - /// <summary>Rewrites field references to <see cref="Game1.player"/>.</summary> - /// <remarks>Stardew Valley changed the <see cref="Game1.player"/> field to a property, which broke many mods that reference it.</remarks> - [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 - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } = $"{nameof(Game1)}.{nameof(Game1.player)} 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 - this.IsStaticField(instruction) - && fieldRef.DeclaringType.FullName == typeof(Game1).FullName - && fieldRef.Name == nameof(Game1.player); - } - - /// <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) - { - 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 @@ <Compile Include="Framework\BaseFieldRewriter.cs" /> <Compile Include="Framework\BaseMethodRewriter.cs" /> <Compile Include="Framework\RewriteHelper.cs" /> - <Compile Include="Rewriters\Wrappers\SpriteBatchWrapper.cs" /> + <Compile Include="Rewriters\GenericFieldToPropertyRewriter.cs" /> <Compile Include="Rewriters\GenericMethodMapper.cs" /> - <Compile Include="Rewriters\SDV1_2\Game1_GameMode_FieldRewriter.cs" /> - <Compile Include="Rewriters\SDV1_2\Game1_Player_FieldRewriter.cs" /> - <Compile Include="Rewriters\SDV1_2\Game1_ActiveClickableMenu_FieldRewriter.cs" /> + <Compile Include="Rewriters\Wrappers\SpriteBatchWrapper.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> |