diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 12:55:06 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-03-25 12:55:06 -0400 |
commit | d724f54f32c4de113700fd2173c070d6a992f66e (patch) | |
tree | 94646604aa8c0f800b89db817dd88f12e4660d0c /src | |
parent | 45d2ed705473f51935af269c852a94432968a15c (diff) | |
download | SMAPI-d724f54f32c4de113700fd2173c070d6a992f66e.tar.gz SMAPI-d724f54f32c4de113700fd2173c070d6a992f66e.tar.bz2 SMAPI-d724f54f32c4de113700fd2173c070d6a992f66e.zip |
replace SpriteBatch rewriter with a generic method mapper
Diffstat (limited to 'src')
5 files changed, 139 insertions, 107 deletions
diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs deleted file mode 100644 index 1459ff17..00000000 --- a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Crossplatform/SpriteBatch_MethodRewriter.cs +++ /dev/null @@ -1,104 +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.Crossplatform -{ - /// <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="WrapperMethods"/>, which redirects all method signatures to the proper compiled MonoGame/XNA method.</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 SpriteBatch_MethodRewriter : BaseMethodRewriter - { - /********* - ** Accessors - *********/ - /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> - public override string NounPhrase { get; } = $"{nameof(SpriteBatch)} methods"; - - - /********* - ** Protected methods - *********/ - /// <summary>Get whether a method reference should be rewritten.</summary> - /// <param name="instruction">The IL instruction.</param> - /// <param name="methodRef">The method reference.</param> - /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> - protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) - { - return platformChanged - && methodRef.DeclaringType.FullName == typeof(SpriteBatch).FullName - && this.HasMatchingSignature(typeof(SpriteBatch_MethodRewriter.WrapperMethods), 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="instruction">The instruction which calls the method.</param> - /// <param name="methodRef">The method 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, MethodReference methodRef, PlatformAssemblyMap assemblyMap) - { - methodRef.DeclaringType = module.Import(typeof(SpriteBatch_MethodRewriter.WrapperMethods)); - } - - - /********* - ** Wrapper methods - *********/ - /// <summary>Wraps <see cref="SpriteBatch"/> methods that are incompatible when converting compiled code between MonoGame and XNA.</summary> - public class WrapperMethods : SpriteBatch - { - /********* - ** Public methods - *********/ - /// <summary>Construct an instance.</summary> - 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/GenericMethodMapper.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs new file mode 100644 index 00000000..92ba3af1 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/GenericMethodMapper.cs @@ -0,0 +1,74 @@ +using System; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.AssemblyRewriters.Framework; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters +{ + /// <summary>Rewrites method references from one parent type to another if the signatures match.</summary> + public class GenericMethodMapper : BaseMethodRewriter + { + /********* + ** Properties + *********/ + /// <summary>The type whose methods to remap.</summary> + private readonly Type FromType; + + /// <summary>The type with methods to map to.</summary> + private readonly Type ToType; + + /// <summary>Whether to only rewrite references if loading the assembly on a different platform than it was compiled on.</summary> + private readonly bool OnlyIfPlatformChanged; + + + /********* + ** Accessors + *********/ + /// <summary>A brief noun phrase indicating what the instruction finder matches.</summary> + public override string NounPhrase { get; } + + + /********* + ** Public methods + *********/ + /// <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) + { + this.FromType = fromType; + this.ToType = toType; + this.NounPhrase = nounPhrase; + this.OnlyIfPlatformChanged = onlyIfPlatformChanged; + } + + + /********* + ** Protected methods + *********/ + /// <summary>Get whether a method reference should be rewritten.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="methodRef">The method reference.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + protected override bool IsMatch(Instruction instruction, MethodReference methodRef, bool platformChanged) + { + return + (!this.OnlyIfPlatformChanged || platformChanged) + && methodRef.DeclaringType.FullName == this.FromType.FullName + && this.HasMatchingSignature(this.ToType, 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="instruction">The instruction which calls the method.</param> + /// <param name="methodRef">The method 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, MethodReference methodRef, PlatformAssemblyMap assemblyMap) + { + methodRef.DeclaringType = module.Import(this.ToType); + } + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs new file mode 100644 index 00000000..ee68f1d5 --- /dev/null +++ b/src/StardewModdingAPI.AssemblyRewriters/Rewriters/Wrappers/SpriteBatchWrapper.cs @@ -0,0 +1,59 @@ +using System.Diagnostics.CodeAnalysis; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers +{ + /// <summary>Wraps <see cref="SpriteBatch"/> methods that are incompatible when converting compiled code between MonoGame and XNA.</summary> + public class SpriteBatchWrapper : SpriteBatch + { + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + public SpriteBatchWrapper(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 a3322e67..8c10aea7 100644 --- a/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj +++ b/src/StardewModdingAPI.AssemblyRewriters/StardewModdingAPI.AssemblyRewriters.csproj @@ -80,10 +80,11 @@ <Compile Include="Framework\BaseFieldRewriter.cs" /> <Compile Include="Framework\BaseMethodRewriter.cs" /> <Compile Include="Framework\RewriteHelper.cs" /> + <Compile Include="Rewriters\Wrappers\SpriteBatchWrapper.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\Crossplatform\SpriteBatch_MethodRewriter.cs" /> </ItemGroup> <ItemGroup> <None Include="packages.config" /> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 263d3352..a7e28213 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -3,10 +3,12 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; +using Microsoft.Xna.Framework.Graphics; using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.AssemblyRewriters.Finders; -using StardewModdingAPI.AssemblyRewriters.Rewriters.Crossplatform; +using StardewModdingAPI.AssemblyRewriters.Rewriters; using StardewModdingAPI.AssemblyRewriters.Rewriters.SDV1_2; +using StardewModdingAPI.AssemblyRewriters.Rewriters.Wrappers; using StardewValley; namespace StardewModdingAPI @@ -172,7 +174,7 @@ namespace StardewModdingAPI return new IInstructionRewriter[] { // crossplatform - new SpriteBatch_MethodRewriter(), + new GenericMethodMapper(typeof(SpriteBatch), typeof(SpriteBatchWrapper), $"{nameof(SpriteBatch)} methods", onlyIfPlatformChanged: true), // Stardew Valley 1.2 new Game1_ActiveClickableMenu_FieldRewriter(), |