From 388ef0a01205387c5ae56f0cdbe8a86a1febc3de Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 9 Feb 2017 22:56:42 -0500 Subject: reorganise rewriters (#231) --- .../Rewriters/ActiveClickableMenuRewriter.cs | 40 --------- .../Crossplatform/SpriteBatch_MethodRewriter.cs | 97 ++++++++++++++++++++++ .../Game1_ActiveClickableMenu_FieldRewriter.cs | 42 ++++++++++ .../Rewriters/SpriteBatchRewriter.cs | 96 --------------------- .../StardewModdingAPI.AssemblyRewriters.csproj | 4 +- src/StardewModdingAPI/Constants.cs | 10 ++- 6 files changed, 148 insertions(+), 141 deletions(-) delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs (limited to 'src') diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs deleted file mode 100644 index b8aef019..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/ActiveClickableMenuRewriter.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; -using StardewValley; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites field references to . - /// Stardew Valley changed the field to a property, which broke many mods that reference it. - public class ActiveClickableMenuRewriter : 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)); - } - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs new file mode 100644 index 00000000..252fe6d0 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs @@ -0,0 +1,97 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters.Crossplatform +{ + /// Rewrites references to to fix inconsistent method signatures between MonoGame and XNA. + /// MonoGame has one SpriteBatch.Begin method with optional arguments, but XNA has multiple method overloads. Incompatible method references are rewritten to use , which redirects all method signatures to the proper compiled MonoGame/XNA method. + [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 SpriteBatch_MethodRewriter : BaseMethodRewriter + { + /********* + ** Protected methods + *********/ + /// Get whether a method reference should be rewritten. + /// The IL instruction. + /// The method reference. + /// Whether the mod was compiled on a different platform. + protected override bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged) + { + return platformChanged + && methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName + && this.HasMatchingSignature(typeof(SpriteBatch_MethodRewriter.WrapperMethods), methodRef); + } + + /// Rewrite a method for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction which calls the method. + /// The method reference invoked by the . + /// Metadata for mapping assemblies to the current platform. + protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + { + methodRef.DeclaringType = module.Import(typeof(SpriteBatch_MethodRewriter.WrapperMethods)); + } + + + /********* + ** Wrapper methods + *********/ + /// Wraps methods that are incompatible when converting compiled code between MonoGame and XNA. + public class WrapperMethods : SpriteBatch + { + /********* + ** Public methods + *********/ + /// Construct an instance. + public WrapperMethods(GraphicsDevice graphicsDevice) : base(graphicsDevice) { } + + + /**** + ** MonoGame signatures + ****/ + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity); + } + + /**** + ** XNA signatures + ****/ + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin() + { + base.Begin(); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState) + { + base.Begin(sortMode, blendState); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect); + } + + [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] + public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) + { + base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); + } + } + } +} \ 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 new file mode 100644 index 00000000..36634b28 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SDV1_2/Game1_ActiveClickableMenu_FieldRewriter.cs @@ -0,0 +1,42 @@ +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)); + } + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs deleted file mode 100644 index 109a6a6e..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.Diagnostics.CodeAnalysis; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using Mono.Cecil; -using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Framework; - -namespace StardewModdingAPI.AssemblyRewriters.Rewriters -{ - /// Rewrites references to to fix inconsistent method signatures between MonoGame and XNA. - /// MonoGame has one SpriteBatch.Begin method with optional arguments, but XNA has multiple method overloads. Incompatible method references are rewritten to use , which redirects all method signatures to the proper compiled MonoGame/XNA method. - public class SpriteBatchRewriter : BaseMethodRewriter - { - /********* - ** Protected methods - *********/ - /// Get whether a method reference should be rewritten. - /// The IL instruction. - /// The method reference. - /// Whether the mod was compiled on a different platform. - protected override bool ShouldRewrite(Instruction instruction, MethodReference methodRef, bool platformChanged) - { - return platformChanged - && methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName - && this.HasMatchingSignature(typeof(SpriteBatchRewriter.WrapperMethods), methodRef); - } - - /// Rewrite a method for compatibility. - /// The module being rewritten. - /// The CIL rewriter. - /// The instruction which calls the method. - /// The method reference invoked by the . - /// Metadata for mapping assemblies to the current platform. - protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) - { - methodRef.DeclaringType = module.Import(typeof(SpriteBatchRewriter.WrapperMethods)); - } - - - /********* - ** Wrapper methods - *********/ - /// Wraps methods that are incompatible when converting compiled code between MonoGame and XNA. - public class WrapperMethods : SpriteBatch - { - /********* - ** Public methods - *********/ - /// Construct an instance. - public WrapperMethods(GraphicsDevice graphicsDevice) : base(graphicsDevice) { } - - - /**** - ** MonoGame signatures - ****/ - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Linux/Mac.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix? matrix) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, matrix ?? Matrix.Identity); - } - - /**** - ** XNA signatures - ****/ - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin() - { - base.Begin(); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState) - { - base.Begin(sortMode, blendState); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect); - } - - [SuppressMessage("ReSharper", "CS0109", Justification = "The 'new' modifier applies when compiled on Windows.")] - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect, Matrix transformMatrix) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, transformMatrix); - } - } - } -} \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index 4433131b..a58dc176 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -77,8 +77,8 @@ - - + + diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 57704def..cee33560 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -4,7 +4,8 @@ using System.IO; using System.Linq; using System.Reflection; using StardewModdingAPI.AssemblyRewriters; -using StardewModdingAPI.AssemblyRewriters.Rewriters; +using StardewModdingAPI.AssemblyRewriters.Rewriters.Crossplatform; +using StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2; using StardewValley; namespace StardewModdingAPI @@ -125,8 +126,11 @@ namespace StardewModdingAPI { return new IInstructionRewriter[] { - new SpriteBatchRewriter(), - new ActiveClickableMenuRewriter() + // crossplatform + new SpriteBatch_MethodRewriter(), + + // Stardew Valley 1.2 + new Game1_ActiveClickableMenu_FieldRewriter() }; } -- cgit