diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-29 19:21:11 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-29 19:21:11 -0500 |
commit | 9ff9d02db5255d5c9ddf9a63cde9e48b28a9eff7 (patch) | |
tree | b9f0ade847f2af574a2d68b8ba827e8fe87d1aa0 | |
parent | cc4d3c1cf8755467acc4c72d25bf1e03fd8c4cba (diff) | |
download | SMAPI-9ff9d02db5255d5c9ddf9a63cde9e48b28a9eff7.tar.gz SMAPI-9ff9d02db5255d5c9ddf9a63cde9e48b28a9eff7.tar.bz2 SMAPI-9ff9d02db5255d5c9ddf9a63cde9e48b28a9eff7.zip |
rewrite SpriteBatch.Begin calls for compatibility (#166)
5 files changed, 182 insertions, 1 deletions
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 +{ + /// <summary>Base class for a method rewriter.</summary> + public abstract class BaseMethodRewriter : IMethodRewriter + { + /********* + ** Public methods + *********/ + /// <summary>Get whether the given method reference can be rewritten.</summary> + /// <param name="methodRef">The method reference.</param> + public abstract bool ShouldRewrite(MethodReference methodRef); + + /// <summary>Rewrite a method for compatibility.</summary> + /// <param name="module">The module being rewritten.</param> + /// <param name="cil">The CIL rewriter.</param> + /// <param name="callOp">The instruction which calls the method.</param> + /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param> + /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> + public abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction callOp, MethodReference methodRef, PlatformAssemblyMap assemblyMap); + + + /********* + ** Protected methods + *********/ + /// <summary>Get whether a method definition matches the signature expected by a method reference.</summary> + /// <param name="definition">The method definition.</param> + /// <param name="reference">The method reference.</param> + 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; + } + + /// <summary>Get whether a type has a method whose signature matches the one expected by a method reference.</summary> + /// <param name="type">The type to check.</param> + /// <param name="reference">The method reference.</param> + protected bool HasMatchingSignature(Type type, MethodReference reference) + { + return type + .GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public) + .Any(method => this.HasMatchingSignature(method, reference)); + } + + /// <summary>Get whether a type matches a type reference.</summary> + /// <param name="type">The defined type.</param> + /// <param name="reference">The type reference.</param> + 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 +{ + /// <summary>Rewrites references to <see cref="SpriteBatch"/> to fix inconsistent method signatures between MonoGame and XNA.</summary> + /// <remarks>MonoGame has one <c>SpriteBatch.Begin</c> method with optional arguments, but XNA has multiple method overloads. Incompatible method references are rewritten to use <see cref="CompatibleSpriteBatch"/>, which redirects all method signatures to the proper compiled MonoGame/XNA method.</remarks> + public class SpriteBatchRewriter : BaseMethodRewriter + { + /// <summary>Get whether the given method reference can be rewritten.</summary> + /// <param name="methodRef">The method reference.</param> + public override bool ShouldRewrite(MethodReference methodRef) + { + return methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName && this.HasMatchingSignature(typeof(CompatibleSpriteBatch), methodRef); + } + + /// <summary>Rewrite a method for compatibility.</summary> + /// <param name="module">The module being rewritten.</param> + /// <param name="cil">The CIL rewriter.</param> + /// <param name="callOp">The instruction which calls the method.</param> + /// <param name="methodRef">The method reference invoked by the <paramref name="callOp"/>.</param> + /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> + 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 @@ <Compile Include="Platform.cs" /> <Compile Include="PlatformAssemblyMap.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> + <Compile Include="Rewriters\BaseMethodRewriter.cs" /> + <Compile Include="Rewriters\SpriteBatchRewriter.cs" /> + <Compile Include="Wrappers\CompatibleSpriteBatch.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> 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 +{ + /// <summary>Wraps <see cref="SpriteBatch"/> methods that are incompatible when converting compiled code between MonoGame and XNA.</summary> + public class CompatibleSpriteBatch : SpriteBatch + { + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + 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 /// <summary>Get method rewriters which fix incompatible method calls in mod assemblies.</summary> internal static IEnumerable<IMethodRewriter> GetMethodRewriters() { - yield break; + return new[] + { + new SpriteBatchRewriter() + }; } |