From 40a90147420614d7d593b478fcf93b9be542c5b0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 9 Feb 2017 13:45:34 -0500 Subject: generalise CIL rewriters for reuse (#231) --- .../IInstructionRewriter.cs | 21 ++++++ .../IMethodRewriter.cs | 21 ------ .../Rewriters/BaseMethodRewriter.cs | 43 +++++++++--- .../Rewriters/SpriteBatchRewriter.cs | 80 +++++++++++++++++++--- .../StardewModdingAPI.AssemblyRewriters.csproj | 3 +- .../Wrappers/CompatibleSpriteBatch.cs | 52 -------------- 6 files changed, 128 insertions(+), 92 deletions(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs delete mode 100644 src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs (limited to 'src/StardewModdingAPI.AssemblyRewriters') diff --git a/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs new file mode 100644 index 00000000..5c24acb6 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/IInstructionRewriter.cs @@ -0,0 +1,21 @@ +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters +{ + /// Rewrites a CIL instruction for compatibility. + public interface IInstructionRewriter + { + /// Get whether a CIL instruction should be rewritten. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + bool ShouldRewrite(Instruction instruction, bool platformChanged); + + /// Rewrite a CIL instruction for compatibility. + /// The module being rewritten. + /// The CIL rewriter. + /// The instruction to rewrite. + /// Metadata for mapping assemblies to the current platform. + void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap); + } +} diff --git a/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs deleted file mode 100644 index 5cbb7e0d..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/IMethodRewriter.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.AssemblyRewriters -{ - /// Rewrites a method for compatibility. - public interface IMethodRewriter - { - /// Get whether the given method reference can be rewritten. - /// The method reference. - bool ShouldRewrite(MethodReference 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. - void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap); - } -} diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs index 1af6e6c4..e44acaf9 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs @@ -7,27 +7,52 @@ using Mono.Cecil.Cil; namespace StardewModdingAPI.AssemblyRewriters.Rewriters { /// Base class for a method rewriter. - public abstract class BaseMethodRewriter : IMethodRewriter + public abstract class BaseMethodRewriter : IInstructionRewriter { /********* ** Public methods *********/ - /// Get whether the given method reference can be rewritten. - /// The method reference. - public abstract bool ShouldRewrite(MethodReference methodRef); + /// Get whether a CIL instruction should be rewritten. + /// The IL instruction. + /// Whether the mod was compiled on a different platform. + public bool ShouldRewrite(Instruction instruction, bool platformChanged) + { + // ignore non-method-call instructions + if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt) + return false; - /// Rewrite a method for compatibility. + // check reference + MethodReference methodRef = (MethodReference)instruction.Operand; + return this.ShouldRewrite(methodRef, platformChanged); + } + + /// Rewrite a CIL instruction for compatibility. /// The module being rewritten. /// The CIL rewriter. - /// The instruction which calls the method. - /// The method reference invoked by the . + /// The instruction to rewrite. /// Metadata for mapping assemblies to the current platform. - public abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap); - + public void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap) + { + MethodReference methodRef = (MethodReference)instruction.Operand; + this.Rewrite(module, cil, instruction, methodRef, assemblyMap); + } /********* ** Protected methods *********/ + /// Get whether the given method reference can be rewritten. + /// The method reference. + /// Whether the mod was compiled on a different platform. + protected abstract bool ShouldRewrite(MethodReference methodRef, bool platformChanged); + + /// 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 abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap); + /// Get whether a method definition matches the signature expected by a method reference. /// The method definition. /// The method reference. diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs index 1c0a5cf3..f64bf768 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs @@ -1,30 +1,94 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.AssemblyRewriters.Wrappers; 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. + /// 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 the given method reference can be rewritten. /// The method reference. - public override bool ShouldRewrite(MethodReference methodRef) + /// Whether the mod was compiled on a different platform. + protected override bool ShouldRewrite(MethodReference methodRef, bool platformChanged) { - return methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName && this.HasMatchingSignature(typeof(CompatibleSpriteBatch), methodRef); + 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 . + /// The instruction which calls the method. + /// The method reference invoked by the . /// Metadata for mapping assemblies to the current platform. - public override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + protected override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap) { - methodRef.DeclaringType = module.Import(typeof(CompatibleSpriteBatch)); + 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 1e6caacc..01ca1d66 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -70,13 +70,12 @@ Properties\GlobalAssemblyInfo.cs - + - diff --git a/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs b/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs deleted file mode 100644 index e28d1a68..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; - -#pragma warning disable CS0109 // Member does not hide an inherited member; new keyword is not required -namespace StardewModdingAPI.AssemblyRewriters.Wrappers -{ - /// Wraps methods that are incompatible when converting compiled code between MonoGame and XNA. - public class CompatibleSpriteBatch : SpriteBatch - { - /********* - ** Public methods - *********/ - /// Construct an instance. - public CompatibleSpriteBatch(GraphicsDevice graphicsDevice) : base(graphicsDevice) { } - - /**** - ** MonoGame signatures - ****/ - 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 - ****/ - public new void Begin() - { - base.Begin(); - } - - public new void Begin(SpriteSortMode sortMode, BlendState blendState) - { - base.Begin(sortMode, blendState); - } - - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState); - } - - public new void Begin(SpriteSortMode sortMode, BlendState blendState, SamplerState samplerState, DepthStencilState depthStencilState, RasterizerState rasterizerState, Effect effect) - { - base.Begin(sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect); - } - - 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 -- cgit