diff options
9 files changed, 168 insertions, 128 deletions
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 +{ + /// <summary>Rewrites a CIL instruction for compatibility.</summary> + public interface IInstructionRewriter + { + /// <summary>Get whether a CIL instruction should be rewritten.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + bool ShouldRewrite(Instruction instruction, bool platformChanged); + + /// <summary>Rewrite a CIL instruction for compatibility.</summary> + /// <param name="module">The module being rewritten.</param> + /// <param name="cil">The CIL rewriter.</param> + /// <param name="instruction">The instruction to rewrite.</param> + /// <param name="assemblyMap">Metadata for mapping assemblies to the current platform.</param> + 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 -{ - /// <summary>Rewrites a method for compatibility.</summary> - public interface IMethodRewriter - { - /// <summary>Get whether the given method reference can be rewritten.</summary> - /// <param name="methodRef">The method reference.</param> - 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> - 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 { /// <summary>Base class for a method rewriter.</summary> - public abstract class BaseMethodRewriter : IMethodRewriter + public abstract class BaseMethodRewriter : IInstructionRewriter { /********* ** 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>Get whether a CIL instruction should be rewritten.</summary> + /// <param name="instruction">The IL instruction.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + public bool ShouldRewrite(Instruction instruction, bool platformChanged) + { + // ignore non-method-call instructions + if (instruction.OpCode != OpCodes.Call && instruction.OpCode != OpCodes.Callvirt) + return false; - /// <summary>Rewrite a method for compatibility.</summary> + // check reference + MethodReference methodRef = (MethodReference)instruction.Operand; + return this.ShouldRewrite(methodRef, platformChanged); + } + + /// <summary>Rewrite a CIL instruction 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="instruction">The instruction to rewrite.</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); - + 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 *********/ + /// <summary>Get whether the given method reference can be rewritten.</summary> + /// <param name="methodRef">The method reference.</param> + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + protected abstract bool ShouldRewrite(MethodReference methodRef, bool platformChanged); + + /// <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 abstract void Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, MethodReference methodRef, PlatformAssemblyMap assemblyMap); + /// <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> 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 { /// <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> + /// <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> public class SpriteBatchRewriter : BaseMethodRewriter { + /********* + ** Protected methods + *********/ /// <summary>Get whether the given method reference can be rewritten.</summary> /// <param name="methodRef">The method reference.</param> - public override bool ShouldRewrite(MethodReference methodRef) + /// <param name="platformChanged">Whether the mod was compiled on a different platform.</param> + 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); } /// <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="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> - 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 + *********/ + /// <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/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 @@ <Compile Include="..\GlobalAssemblyInfo.cs"> <Link>Properties\GlobalAssemblyInfo.cs</Link> </Compile> - <Compile Include="IMethodRewriter.cs" /> + <Compile Include="IInstructionRewriter.cs" /> <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 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 -{ - /// <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.sln.DotSettings b/src/StardewModdingAPI.sln.DotSettings index 7ee9b76e..81b52fd4 100644 --- a/src/StardewModdingAPI.sln.DotSettings +++ b/src/StardewModdingAPI.sln.DotSettings @@ -1,4 +1,5 @@ <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantNameQualifier/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/StaticQualifier/STATIC_MEMBERS_QUALIFY_MEMBERS/@EntryValue">Field, Property, Event, Method</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ThisQualifier/INSTANCE_MEMBERS_QUALIFY_MEMBERS/@EntryValue">Field, Property, Event, Method</s:String> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 0965ffcd..f6afa95f 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -120,8 +120,8 @@ namespace StardewModdingAPI return new PlatformAssemblyMap(targetPlatform, removeAssemblyReferences, targetAssemblies); } - /// <summary>Get method rewriters which fix incompatible method calls in mod assemblies.</summary> - internal static IEnumerable<IMethodRewriter> GetMethodRewriters() + /// <summary>Get rewriters which fix incompatible CIL instructions in mod assemblies.</summary> + internal static IEnumerable<IInstructionRewriter> GetRewriters() { return new[] { diff --git a/src/StardewModdingAPI/Framework/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/AssemblyLoader.cs index 123211b9..d5e8f5ee 100644 --- a/src/StardewModdingAPI/Framework/AssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/AssemblyLoader.cs @@ -72,10 +72,10 @@ namespace StardewModdingAPI.Framework Assembly lastAssembly = null; foreach (AssemblyParseResult assembly in assemblies) { - this.Monitor.Log($"Loading {assembly.File.Name}...", LogLevel.Trace); bool changed = this.RewriteAssembly(assembly.Definition); if (changed) { + this.Monitor.Log($"Loading {assembly.File.Name} (rewritten in memory)...", LogLevel.Trace); using (MemoryStream outStream = new MemoryStream()) { assembly.Definition.Write(outStream); @@ -84,7 +84,10 @@ namespace StardewModdingAPI.Framework } } else + { + this.Monitor.Log($"Loading {assembly.File.Name}...", LogLevel.Trace); lastAssembly = Assembly.UnsafeLoadFrom(assembly.File.FullName); + } } // last assembly loaded is the root @@ -155,59 +158,59 @@ namespace StardewModdingAPI.Framework /// <returns>Returns whether the assembly was modified.</returns> private bool RewriteAssembly(AssemblyDefinition assembly) { - ModuleDefinition module = assembly.Modules.Single(); // technically an assembly can have multiple modules, but none of the build tools (including MSBuild) support it; simplify by assuming one module + ModuleDefinition module = assembly.MainModule; - // remove old assembly references - bool shouldRewrite = false; + // swap assembly references if needed (e.g. XNA => MonoGame) + bool platformChanged = false; for (int i = 0; i < module.AssemblyReferences.Count; i++) { + // remove old assembly reference if (this.AssemblyMap.RemoveNames.Any(name => module.AssemblyReferences[i].Name == name)) { - shouldRewrite = true; + platformChanged = true; module.AssemblyReferences.RemoveAt(i); i--; } } - if (!shouldRewrite) - return false; - - // add target assembly references - foreach (AssemblyNameReference target in this.AssemblyMap.TargetReferences.Values) - module.AssemblyReferences.Add(target); - - // rewrite type scopes to use target assemblies - IEnumerable<TypeReference> typeReferences = module.GetTypeReferences().OrderBy(p => p.FullName); - foreach (TypeReference type in typeReferences) - this.ChangeTypeScope(type); + if (platformChanged) + { + // add target assembly references + foreach (AssemblyNameReference target in this.AssemblyMap.TargetReferences.Values) + module.AssemblyReferences.Add(target); + + // rewrite type scopes to use target assemblies + IEnumerable<TypeReference> typeReferences = module.GetTypeReferences().OrderBy(p => p.FullName); + foreach (TypeReference type in typeReferences) + this.ChangeTypeScope(type); + } - // rewrite incompatible methods - IMethodRewriter[] methodRewriters = Constants.GetMethodRewriters().ToArray(); + // rewrite incompatible instructions + bool anyRewritten = false; + IInstructionRewriter[] rewriters = Constants.GetRewriters().ToArray(); foreach (MethodDefinition method in this.GetMethods(module)) { - // skip methods with no rewritable method - bool hasMethodToRewrite = method.Body.Instructions.Any(op => (op.OpCode == OpCodes.Call || op.OpCode == OpCodes.Callvirt) && methodRewriters.Any(rewriter => rewriter.ShouldRewrite((MethodReference)op.Operand))); - if (!hasMethodToRewrite) + // skip methods with no rewritable instructions + bool canRewrite = method.Body.Instructions.Any(op => rewriters.Any(rewriter => rewriter.ShouldRewrite(op, platformChanged))); + if (!canRewrite) continue; - // rewrite method references + // prepare method method.Body.SimplifyMacros(); ILProcessor cil = method.Body.GetILProcessor(); - Instruction[] instructions = cil.Body.Instructions.ToArray(); - foreach (Instruction op in instructions) + + // rewrite instructions + foreach (Instruction op in cil.Body.Instructions.ToArray()) { - if (op.OpCode == OpCodes.Call || op.OpCode == OpCodes.Callvirt) - { - IMethodRewriter rewriter = methodRewriters.FirstOrDefault(p => p.ShouldRewrite((MethodReference)op.Operand)); - if (rewriter != null) - { - MethodReference methodRef = (MethodReference)op.Operand; - rewriter.Rewrite(module, cil, op, methodRef, this.AssemblyMap); - } - } + IInstructionRewriter rewriter = rewriters.FirstOrDefault(p => p.ShouldRewrite(op, platformChanged)); + rewriter?.Rewrite(module, cil, op, this.AssemblyMap); } + + // finalise method method.Body.OptimizeMacros(); + anyRewritten = true; } - return true; + + return platformChanged || anyRewritten; } /// <summary>Get the correct reference to use for compatibility with the current platform.</summary> |