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 -------------- src/StardewModdingAPI.sln.DotSettings | 1 + src/StardewModdingAPI/Constants.cs | 4 +- src/StardewModdingAPI/Framework/AssemblyLoader.cs | 71 ++++++++++--------- 9 files changed, 168 insertions(+), 128 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') 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 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 @@  + HINT Field, Property, Event, Method Field, Property, Event, Method True 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); } - /// Get method rewriters which fix incompatible method calls in mod assemblies. - internal static IEnumerable GetMethodRewriters() + /// Get rewriters which fix incompatible CIL instructions in mod assemblies. + internal static IEnumerable 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 whether the assembly was modified. 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 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 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; } /// Get the correct reference to use for compatibility with the current platform. -- cgit