From c513bb011c858e59abbd77f75080f4a1d8b712f9 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 Sep 2017 22:52:52 -0400 Subject: pass mod metadata into rewriters (#347) --- .../Framework/ModLoading/AssemblyLoader.cs | 14 ++++++++------ .../Framework/ModLoading/Finders/EventFinder.cs | 6 ++++-- .../Framework/ModLoading/Finders/FieldFinder.cs | 6 ++++-- .../Framework/ModLoading/Finders/MethodFinder.cs | 6 ++++-- .../Framework/ModLoading/Finders/PropertyFinder.cs | 6 ++++-- .../Framework/ModLoading/Finders/TypeFinder.cs | 6 ++++-- .../Framework/ModLoading/IInstructionRewriter.cs | 6 ++++-- .../Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs | 3 ++- .../ModLoading/Rewriters/FieldToPropertyRewriter.cs | 3 ++- .../Framework/ModLoading/Rewriters/MethodParentRewriter.cs | 6 ++++-- .../ModLoading/Rewriters/TypeReferenceRewriter.cs | 6 ++++-- 11 files changed, 44 insertions(+), 24 deletions(-) (limited to 'src/StardewModdingAPI/Framework/ModLoading') diff --git a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs index 01dc602a..f9c43f1f 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/AssemblyLoader.cs @@ -53,11 +53,12 @@ namespace StardewModdingAPI.Framework.ModLoading } /// Preprocess and load an assembly. + /// The mod for which the assembly is being loaded. /// The assembly file path. /// Assume the mod is compatible, even if incompatible code is detected. /// Returns the rewrite metadata for the preprocessed assembly. /// An incompatible CIL instruction was found while rewriting the assembly. - public Assembly Load(string assemblyPath, bool assumeCompatible) + public Assembly Load(IModMetadata mod, string assemblyPath, bool assumeCompatible) { // get referenced local assemblies AssemblyParseResult[] assemblies; @@ -86,7 +87,7 @@ namespace StardewModdingAPI.Framework.ModLoading if (assembly.Status == AssemblyLoadStatus.AlreadyLoaded) continue; - bool changed = this.RewriteAssembly(assembly.Definition, assumeCompatible, logPrefix: " "); + bool changed = this.RewriteAssembly(mod, assembly.Definition, assumeCompatible, logPrefix: " "); if (changed) { if (!oneAssembly) @@ -173,12 +174,13 @@ namespace StardewModdingAPI.Framework.ModLoading ** Assembly rewriting ****/ /// Rewrite the types referenced by an assembly. + /// The mod for which the assembly is being loaded. /// The assembly to rewrite. /// Assume the mod is compatible, even if incompatible code is detected. /// A string to prefix to log messages. /// Returns whether the assembly was modified. /// An incompatible CIL instruction was found while rewriting the assembly. - private bool RewriteAssembly(AssemblyDefinition assembly, bool assumeCompatible, string logPrefix) + private bool RewriteAssembly(IModMetadata mod, AssemblyDefinition assembly, bool assumeCompatible, string logPrefix) { ModuleDefinition module = assembly.MainModule; HashSet loggedMessages = new HashSet(); @@ -211,7 +213,7 @@ namespace StardewModdingAPI.Framework.ModLoading // find (and optionally rewrite) incompatible instructions bool anyRewritten = false; - IInstructionRewriter[] rewriters = Constants.GetRewriters().ToArray(); + IInstructionRewriter[] rewriters = Constants.GetRewriters(this.Monitor).ToArray(); foreach (MethodDefinition method in this.GetMethods(module)) { // check method definition @@ -219,7 +221,7 @@ namespace StardewModdingAPI.Framework.ModLoading { try { - if (rewriter.Rewrite(module, method, this.AssemblyMap, platformChanged)) + if (rewriter.Rewrite(mod, module, method, this.AssemblyMap, platformChanged)) { this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {rewriter.NounPhrase}..."); anyRewritten = true; @@ -241,7 +243,7 @@ namespace StardewModdingAPI.Framework.ModLoading { try { - if (rewriter.Rewrite(module, cil, instruction, this.AssemblyMap, platformChanged)) + if (rewriter.Rewrite(mod, module, cil, instruction, this.AssemblyMap, platformChanged)) { this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {rewriter.NounPhrase}..."); anyRewritten = true; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs index ce234e39..ac5034c4 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/EventFinder.cs @@ -38,18 +38,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return false; } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -57,7 +59,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs index 2feaf2e6..008399d5 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/FieldFinder.cs @@ -38,18 +38,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return false; } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -57,7 +59,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs index c3bb36e3..2a6dc99e 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/MethodFinder.cs @@ -38,18 +38,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return false; } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -57,7 +59,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs index d1fed84b..a0ce1cbf 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/PropertyFinder.cs @@ -38,18 +38,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return false; } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -57,7 +59,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs index e67e6766..a3005c85 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Finders/TypeFinder.cs @@ -34,13 +34,14 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(method)) return false; @@ -49,6 +50,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -56,7 +58,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public virtual bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public virtual bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs index 9b35cdae..ce12c717 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/IInstructionRewriter.cs @@ -17,15 +17,17 @@ namespace StardewModdingAPI.Framework.ModLoading ** Methods *********/ /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); + bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -33,6 +35,6 @@ namespace StardewModdingAPI.Framework.ModLoading /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged); + bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged); } } diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs index 7b838f13..fb2a9a96 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs @@ -33,6 +33,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -40,7 +41,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public override bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs index 8ef14103..03d1f707 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs @@ -33,6 +33,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -40,7 +41,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public override bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs index 1b95b83b..1e116e1f 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs @@ -44,18 +44,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { return false; } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -63,7 +65,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction, platformChanged)) return false; diff --git a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs index 2c444b64..8db39cfe 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs @@ -33,13 +33,14 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// Rewrite a method definition for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The method definition to rewrite. /// Metadata for mapping assemblies to the current platform. /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public override bool Rewrite(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) + public override bool Rewrite(IModMetadata mod, ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) { bool rewritten = false; @@ -87,6 +88,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// Rewrite a CIL instruction for compatibility. + /// The mod to which the module belongs. /// The module being rewritten. /// The CIL rewriter. /// The instruction to rewrite. @@ -94,7 +96,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Whether the mod was compiled on a different platform. /// Returns whether the instruction was rewritten. /// The CIL instruction is not compatible, and can't be rewritten. - public override bool Rewrite(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + public override bool Rewrite(IModMetadata mod, ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) { if (!this.IsMatch(instruction) && !instruction.ToString().Contains(this.FromTypeName)) return false; -- cgit