From 9ff9d02db5255d5c9ddf9a63cde9e48b28a9eff7 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 29 Nov 2016 19:21:11 -0500 Subject: rewrite SpriteBatch.Begin calls for compatibility (#166) --- .../Rewriters/BaseMethodRewriter.cs | 92 ++++++++++++++++++++++ .../Rewriters/SpriteBatchRewriter.cs | 30 +++++++ .../StardewModdingAPI.AssemblyRewriters.csproj | 3 + .../Wrappers/CompatibleSpriteBatch.cs | 52 ++++++++++++ src/StardewModdingAPI/Constants.cs | 6 +- 5 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs create mode 100644 src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs new file mode 100644 index 00000000..1af6e6c4 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/BaseMethodRewriter.cs @@ -0,0 +1,92 @@ +using System; +using System.Linq; +using System.Reflection; +using Mono.Cecil; +using Mono.Cecil.Cil; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// Base class for a method rewriter. + public abstract class BaseMethodRewriter : IMethodRewriter + { + /********* + ** Public methods + *********/ + /// Get whether the given method reference can be rewritten. + /// The method reference. + public abstract 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. + public abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap); + + + /********* + ** Protected methods + *********/ + /// Get whether a method definition matches the signature expected by a method reference. + /// The method definition. + /// The method reference. + protected bool HasMatchingSignature(MethodInfo definition, MethodReference reference) + { + // same name + if (definition.Name != reference.Name) + return false; + + // same arguments + ParameterInfo[] definitionParameters = definition.GetParameters(); + ParameterDefinition[] referenceParameters = reference.Parameters.ToArray(); + if (referenceParameters.Length != definitionParameters.Length) + return false; + for (int i = 0; i < referenceParameters.Length; i++) + { + if (!this.IsMatchingType(definitionParameters[i].ParameterType, referenceParameters[i].ParameterType)) + return false; + } + return true; + } + + /// Get whether a type has a method whose signature matches the one expected by a method reference. + /// The type to check. + /// The method reference. + protected bool HasMatchingSignature(Type type, MethodReference reference) + { + return type + .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) + .Any(method => this.HasMatchingSignature(method, reference)); + } + + /// Get whether a type matches a type reference. + /// The defined type. + /// The type reference. + private bool IsMatchingType(Type type, TypeReference reference) + { + // same namespace & name + if (type.Namespace != reference.Namespace || type.Name != reference.Name) + return false; + + // same generic parameters + if (type.IsGenericType) + { + if (!reference.IsGenericInstance) + return false; + + Type[] defGenerics = type.GetGenericArguments(); + TypeReference[] refGenerics = ((GenericInstanceType)reference).GenericArguments.ToArray(); + if (defGenerics.Length != refGenerics.Length) + return false; + for (int i = 0; i < defGenerics.Length; i++) + { + if (!this.IsMatchingType(defGenerics[i], refGenerics[i])) + return false; + } + } + + return true; + } + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs new file mode 100644 index 00000000..1c0a5cf3 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/SpriteBatchRewriter.cs @@ -0,0 +1,30 @@ +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. + public class SpriteBatchRewriter : BaseMethodRewriter + { + /// Get whether the given method reference can be rewritten. + /// The method reference. + public override bool ShouldRewrite(MethodReference methodRef) + { + return methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName && this.HasMatchingSignature(typeof(CompatibleSpriteBatch), 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. + public override void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + { + methodRef.DeclaringType = module.Import(typeof(CompatibleSpriteBatch)); + } + } +} \ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj index b2533566..51a49da0 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -75,6 +75,9 @@ + + + diff --git a/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs b/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs new file mode 100644 index 00000000..e28d1a68 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Wrappers/CompatibleSpriteBatch.cs @@ -0,0 +1,52 @@ +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 diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 4f2f00a1..815e5427 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -4,6 +4,7 @@ using System.IO; using System.Linq; using System.Reflection; using StardewModdingAPI.AssemblyRewriters; +using StardewModdingAPI.AssemblyRewriters.Rewriters; using StardewValley; namespace StardewModdingAPI @@ -116,7 +117,10 @@ namespace StardewModdingAPI /// Get method rewriters which fix incompatible method calls in mod assemblies. internal static IEnumerable GetMethodRewriters() { - yield break; + return new[] + { + new SpriteBatchRewriter() + }; } -- cgit