From 1838842bbc2db2d1049c193b8650bd101ba4858f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 19 May 2020 20:57:50 -0400 Subject: [PATCH] rewrite assembly rewriting, merge Harmony rewriters (#711) This reduces duplication, decouples it from the assembly loader, and makes it more flexible to handle Harmony rewriting. --- .../Framework/ModLoading/AssemblyLoader.cs | 102 ++++--- .../ModLoading/Finders/EventFinder.cs | 18 +- .../ModLoading/Finders/FieldFinder.cs | 31 +-- .../ModLoading/Finders/MethodFinder.cs | 18 +- .../ModLoading/Finders/PropertyFinder.cs | 18 +- ...ferenceToMemberWithUnexpectedTypeFinder.cs | 27 +- .../Finders/ReferenceToMissingMemberFinder.cs | 28 +- .../ModLoading/Finders/TypeAssemblyFinder.cs | 42 ++- .../ModLoading/Finders/TypeFinder.cs | 42 ++- .../Framework/BaseInstructionHandler.cs | 66 +++-- .../ModLoading/Framework/BaseTypeFinder.cs | 172 ------------ .../Framework/BaseTypeReferenceRewriter.cs | 209 -------------- .../ModLoading/Framework/RecursiveRewriter.cs | 260 ++++++++++++++++++ .../{ => Framework}/RewriteHelper.cs | 24 +- .../ModLoading/IInstructionHandler.cs | 35 +-- .../Rewriters/FieldReplaceRewriter.cs | 37 ++- .../Rewriters/FieldToPropertyRewriter.cs | 42 +-- .../Rewriters/Harmony1AssemblyRewriter.cs | 111 +++++--- .../Rewriters/MethodParentRewriter.cs | 40 ++- .../StaticFieldToConstantRewriter.cs | 35 ++- .../Rewriters/TypeReferenceRewriter.cs | 50 ++-- src/SMAPI/Metadata/InstructionMetadata.cs | 8 +- 22 files changed, 706 insertions(+), 709 deletions(-) delete mode 100644 src/SMAPI/Framework/ModLoading/Framework/BaseTypeFinder.cs delete mode 100644 src/SMAPI/Framework/ModLoading/Framework/BaseTypeReferenceRewriter.cs create mode 100644 src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs rename src/SMAPI/Framework/ModLoading/{ => Framework}/RewriteHelper.cs (84%) diff --git a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs index b95a45b5..d9b4af1b 100644 --- a/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs +++ b/src/SMAPI/Framework/ModLoading/AssemblyLoader.cs @@ -4,8 +4,8 @@ using System.IO; using System.Linq; using System.Reflection; using Mono.Cecil; -using Mono.Cecil.Cil; using StardewModdingAPI.Framework.Exceptions; +using StardewModdingAPI.Framework.ModLoading.Framework; using StardewModdingAPI.Metadata; using StardewModdingAPI.Toolkit.Framework.ModData; using StardewModdingAPI.Toolkit.Utilities; @@ -283,54 +283,32 @@ namespace StardewModdingAPI.Framework.ModLoading this.ChangeTypeScope(type); } - // find (and optionally rewrite) incompatible instructions - bool anyRewritten = false; - IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers(this.ParanoidMode).ToArray(); - foreach (TypeDefinition type in module.GetTypes()) - { - // check type definition - foreach (IInstructionHandler handler in handlers) + // find or rewrite code + IInstructionHandler[] handlers = new InstructionMetadata().GetHandlers(this.ParanoidMode, platformChanged).ToArray(); + RecursiveRewriter rewriter = new RecursiveRewriter( + module: module, + rewriteType: (type, replaceWith) => { - InstructionHandleResult result = handler.Handle(module, type, this.AssemblyMap, platformChanged); - this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename); - if (result == InstructionHandleResult.Rewritten) - anyRewritten = true; - } - - // check methods - foreach (MethodDefinition method in type.Methods.Where(p => p.HasBody)) - { - // check method definition + bool rewritten = false; foreach (IInstructionHandler handler in handlers) - { - InstructionHandleResult result = handler.Handle(module, method, this.AssemblyMap, platformChanged); - this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename); - if (result == InstructionHandleResult.Rewritten) - anyRewritten = true; - } - - // check CIL instructions - ILProcessor cil = method.Body.GetILProcessor(); - var instructions = cil.Body.Instructions; - // ReSharper disable once ForCanBeConvertedToForeach -- deliberate access by index so each handler sees replacements from previous handlers - for (int offset = 0; offset < instructions.Count; offset++) - { - Instruction instruction = instructions[offset]; - if (instruction.OpCode.Code == Code.Nop) - continue; - - foreach (IInstructionHandler handler in handlers) - { - InstructionHandleResult result = handler.Handle(module, cil, instruction, this.AssemblyMap, platformChanged); - this.ProcessInstructionHandleResult(mod, handler, result, loggedMessages, logPrefix, filename); - if (result == InstructionHandleResult.Rewritten) - { - instruction = instructions[offset]; - anyRewritten = true; - } - } - } + rewritten |= handler.Handle(module, type, replaceWith); + return rewritten; + }, + rewriteInstruction: (instruction, cil, replaceWith) => + { + bool rewritten = false; + foreach (IInstructionHandler handler in handlers) + rewritten |= handler.Handle(module, cil, instruction, replaceWith); + return rewritten; } + ); + bool anyRewritten = rewriter.RewriteModule(); + + // handle rewrite flags + foreach (IInstructionHandler handler in handlers) + { + foreach (var flag in handler.Flags) + this.ProcessInstructionHandleResult(mod, handler, flag, loggedMessages, logPrefix, filename); } return platformChanged || anyRewritten; @@ -345,49 +323,52 @@ namespace StardewModdingAPI.Framework.ModLoading /// The assembly filename for log messages. private void ProcessInstructionHandleResult(IModMetadata mod, IInstructionHandler handler, InstructionHandleResult result, HashSet loggedMessages, string logPrefix, string filename) { + // get message template + // ($phrase is replaced with the noun phrase or messages) + string template = null; switch (result) { case InstructionHandleResult.Rewritten: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Rewrote {filename} to fix {handler.NounPhrase}..."); + template = $"{logPrefix}Rewrote {filename} to fix $phrase..."; break; case InstructionHandleResult.NotCompatible: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Broken code in {filename}: {handler.NounPhrase}."); + template = $"{logPrefix}Broken code in {filename}: $phrase."; mod.SetWarning(ModWarning.BrokenCodeLoaded); break; case InstructionHandleResult.DetectedGamePatch: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected game patcher ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected game patcher ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.PatchesGame); break; case InstructionHandleResult.DetectedSaveSerializer: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected possible save serializer change ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected possible save serializer change ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.ChangesSaveSerializer); break; case InstructionHandleResult.DetectedUnvalidatedUpdateTick: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected reference to {handler.NounPhrase} in assembly {filename}."); + template = $"{logPrefix}Detected reference to $phrase in assembly {filename}."; mod.SetWarning(ModWarning.UsesUnvalidatedUpdateTick); break; case InstructionHandleResult.DetectedDynamic: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected 'dynamic' keyword ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected 'dynamic' keyword ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.UsesDynamic); break; case InstructionHandleResult.DetectedConsoleAccess: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected direct console access ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected direct console access ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.AccessesConsole); break; case InstructionHandleResult.DetectedFilesystemAccess: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected filesystem access ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected filesystem access ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.AccessesFilesystem); break; case InstructionHandleResult.DetectedShellAccess: - this.Monitor.LogOnce(loggedMessages, $"{logPrefix}Detected shell or process access ({handler.NounPhrase}) in assembly {filename}."); + template = $"{logPrefix}Detected shell or process access ($phrase) in assembly {filename}."; mod.SetWarning(ModWarning.AccessesShell); break; @@ -397,6 +378,17 @@ namespace StardewModdingAPI.Framework.ModLoading default: throw new NotSupportedException($"Unrecognized instruction handler result '{result}'."); } + if (template == null) + return; + + // format messages + if (handler.Phrases.Any()) + { + foreach (string message in handler.Phrases) + this.Monitor.LogOnce(template.Replace("$phrase", message)); + } + else + this.Monitor.LogOnce(template.Replace("$phrase", handler.DefaultPhrase ?? handler.GetType().Name)); } /// Get the correct reference to use for compatibility with the current platform. diff --git a/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs index 1a7ae636..e1476b73 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/EventFinder.cs @@ -1,3 +1,4 @@ +using System; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.Framework.ModLoading.Framework; @@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The event name for which to find references. /// The result to return for matching instructions. public EventFinder(string fullTypeName, string eventName, InstructionHandleResult result) - : base(nounPhrase: $"{fullTypeName}.{eventName} event") + : base(defaultPhrase: $"{fullTypeName}.{eventName} event") { this.FullTypeName = fullTypeName; this.EventName = eventName; this.Result = result; } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; + if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction)) + this.MarkFlag(this.Result); + + return false; } diff --git a/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs index 9ae07916..c157ed9b 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/FieldFinder.cs @@ -1,3 +1,4 @@ +using System; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.Framework.ModLoading.Framework; @@ -28,39 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The field name for which to find references. /// The result to return for matching instructions. public FieldFinder(string fullTypeName, string fieldName, InstructionHandleResult result) - : base(nounPhrase: $"{fullTypeName}.{fieldName} field") + : base(defaultPhrase: $"{fullTypeName}.{fieldName} field") { this.FullTypeName = fullTypeName; this.FieldName = fieldName; this.Result = result; } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } + if (!this.Flags.Contains(this.Result) && RewriteHelper.IsFieldReferenceTo(instruction, this.FullTypeName, this.FieldName)) + this.MarkFlag(this.Result); - - /********* - ** Protected methods - *********/ - /// Get whether a CIL instruction matches. - /// The IL instruction. - protected bool IsMatch(Instruction instruction) - { - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - return - fieldRef != null - && fieldRef.DeclaringType.FullName == this.FullTypeName - && fieldRef.Name == this.FieldName; + return false; } } } diff --git a/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs index 75584f1f..82c93a7c 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/MethodFinder.cs @@ -1,3 +1,4 @@ +using System; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.Framework.ModLoading.Framework; @@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The method name for which to find references. /// The result to return for matching instructions. public MethodFinder(string fullTypeName, string methodName, InstructionHandleResult result) - : base(nounPhrase: $"{fullTypeName}.{methodName} method") + : base(defaultPhrase: $"{fullTypeName}.{methodName} method") { this.FullTypeName = fullTypeName; this.MethodName = methodName; this.Result = result; } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; + if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction)) + this.MarkFlag(this.Result); + + return false; } diff --git a/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs index 811420c5..c96d61a2 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/PropertyFinder.cs @@ -1,3 +1,4 @@ +using System; using Mono.Cecil; using Mono.Cecil.Cil; using StardewModdingAPI.Framework.ModLoading.Framework; @@ -28,24 +29,25 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// The property name for which to find references. /// The result to return for matching instructions. public PropertyFinder(string fullTypeName, string propertyName, InstructionHandleResult result) - : base(nounPhrase: $"{fullTypeName}.{propertyName} property") + : base(defaultPhrase: $"{fullTypeName}.{propertyName} property") { this.FullTypeName = fullTypeName; this.PropertyName = propertyName; this.Result = result; } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; + if (!this.Flags.Contains(this.Result) && this.IsMatch(instruction)) + this.MarkFlag(this.Result); + + return false; } diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs index 1029d350..a67cfa4f 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMemberWithUnexpectedTypeFinder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Mono.Cecil; @@ -23,18 +24,18 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The assembly names to which to heuristically detect broken references. public ReferenceToMemberWithUnexpectedTypeFinder(string[] validateReferencesToAssemblies) - : base(nounPhrase: "") + : base(defaultPhrase: "") { this.ValidateReferencesToAssemblies = new HashSet(validateReferencesToAssemblies); } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { // field reference FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); @@ -43,13 +44,13 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders // get target field FieldDefinition targetField = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); if (targetField == null) - return InstructionHandleResult.None; + return false; // validate return type if (!RewriteHelper.LooksLikeSameType(fieldRef.FieldType, targetField.FieldType)) { - this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType)}, not {this.GetFriendlyTypeName(fieldRef.FieldType)})"; - return InstructionHandleResult.NotCompatible; + this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (field returns {this.GetFriendlyTypeName(targetField.FieldType)}, not {this.GetFriendlyTypeName(fieldRef.FieldType)})"); + return false; } } @@ -60,21 +61,21 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders // get potential targets MethodDefinition[] candidateMethods = methodReference.DeclaringType.Resolve()?.Methods.Where(found => found.Name == methodReference.Name).ToArray(); if (candidateMethods == null || !candidateMethods.Any()) - return InstructionHandleResult.None; + return false; // compare return types MethodDefinition methodDef = methodReference.Resolve(); if (methodDef == null) - return InstructionHandleResult.None; // validated by ReferenceToMissingMemberFinder + return false; // validated by ReferenceToMissingMemberFinder if (candidateMethods.All(method => !RewriteHelper.LooksLikeSameType(method.ReturnType, methodDef.ReturnType))) { - this.NounPhrase = $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType)})"; - return InstructionHandleResult.NotCompatible; + this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {methodDef.DeclaringType.FullName}.{methodDef.Name} (no such method returns {this.GetFriendlyTypeName(methodDef.ReturnType)})"); + return false; } } - return InstructionHandleResult.None; + return false; } diff --git a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs index fefa88f4..ebb62948 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/ReferenceToMissingMemberFinder.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using Mono.Cecil; @@ -23,18 +24,18 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders /// Construct an instance. /// The assembly names to which to heuristically detect broken references. public ReferenceToMissingMemberFinder(string[] validateReferencesToAssemblies) - : base(nounPhrase: "") + : base(defaultPhrase: "") { this.ValidateReferencesToAssemblies = new HashSet(validateReferencesToAssemblies); } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { // field reference FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); @@ -43,8 +44,8 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders FieldDefinition target = fieldRef.DeclaringType.Resolve()?.Fields.FirstOrDefault(p => p.Name == fieldRef.Name); if (target == null) { - this.NounPhrase = $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (no such field)"; - return InstructionHandleResult.NotCompatible; + this.MarkFlag(InstructionHandleResult.NotCompatible, $"reference to {fieldRef.DeclaringType.FullName}.{fieldRef.Name} (no such field)"); + return false; } } @@ -55,17 +56,20 @@ namespace StardewModdingAPI.Framework.ModLoading.Finders MethodDefinition target = methodRef.Resolve(); if (target == null) { + string phrase = null; if (this.IsProperty(methodRef)) - this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)"; + phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name.Substring(4)} (no such property)"; else if (methodRef.Name == ".ctor") - this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no matching constructor)"; + phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no matching constructor)"; else - this.NounPhrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)"; - return InstructionHandleResult.NotCompatible; + phrase = $"reference to {methodRef.DeclaringType.FullName}.{methodRef.Name} (no such method)"; + + this.MarkFlag(InstructionHandleResult.NotCompatible, phrase); + return false; } } - return InstructionHandleResult.None; + return false; } diff --git a/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs index 5301186b..a1ade536 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/TypeAssemblyFinder.cs @@ -5,21 +5,47 @@ using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Finders { /// Finds incompatible CIL instructions that reference types in a given assembly. - internal class TypeAssemblyFinder : BaseTypeFinder + internal class TypeAssemblyFinder : BaseInstructionHandler { + /********* + ** Fields + *********/ + /// The full assembly name to which to find references. + private readonly string AssemblyName; + + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + + /// Get whether a matched type should be ignored. + private readonly Func ShouldIgnore; + + /********* ** Public methods *********/ /// Construct an instance. /// The full assembly name to which to find references. /// The result to return for matching instructions. - /// A lambda which overrides a matched type. + /// Get whether a matched type should be ignored. public TypeAssemblyFinder(string assemblyName, InstructionHandleResult result, Func shouldIgnore = null) - : base( - isMatch: type => type.Scope.Name == assemblyName && (shouldIgnore == null || !shouldIgnore(type)), - result: result, - nounPhrase: $"{assemblyName} assembly" - ) - { } + : base(defaultPhrase: $"{assemblyName} assembly") + { + this.AssemblyName = assemblyName; + this.Result = result; + this.ShouldIgnore = shouldIgnore; + } + + /// Rewrite a type reference if needed. + /// The assembly module containing the instruction. + /// The type definition to handle. + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + public override bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) + { + if (type.Scope.Name == this.AssemblyName && this.ShouldIgnore?.Invoke(type) != true) + this.MarkFlag(this.Result); + + return false; + } } } diff --git a/src/SMAPI/Framework/ModLoading/Finders/TypeFinder.cs b/src/SMAPI/Framework/ModLoading/Finders/TypeFinder.cs index 3adc31c7..c285414a 100644 --- a/src/SMAPI/Framework/ModLoading/Finders/TypeFinder.cs +++ b/src/SMAPI/Framework/ModLoading/Finders/TypeFinder.cs @@ -5,21 +5,47 @@ using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Finders { /// Finds incompatible CIL instructions that reference a given type. - internal class TypeFinder : BaseTypeFinder + internal class TypeFinder : BaseInstructionHandler { + /********* + ** Fields + *********/ + /// The full type name to match. + private readonly string FullTypeName; + + /// The result to return for matching instructions. + private readonly InstructionHandleResult Result; + + /// Get whether a matched type should be ignored. + private readonly Func ShouldIgnore; + + /********* ** Public methods *********/ /// Construct an instance. /// The full type name to match. /// The result to return for matching instructions. - /// A lambda which overrides a matched type. + /// Get whether a matched type should be ignored. public TypeFinder(string fullTypeName, InstructionHandleResult result, Func shouldIgnore = null) - : base( - isMatch: type => type.FullName == fullTypeName && (shouldIgnore == null || !shouldIgnore(type)), - result: result, - nounPhrase: $"{fullTypeName} type" - ) - { } + : base(defaultPhrase: $"{fullTypeName} type") + { + this.FullTypeName = fullTypeName; + this.Result = result; + this.ShouldIgnore = shouldIgnore; + } + + /// Rewrite a type reference if needed. + /// The assembly module containing the instruction. + /// The type definition to handle. + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + public override bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) + { + if (type.FullName == this.FullTypeName && this.ShouldIgnore?.Invoke(type) != true) + this.MarkFlag(this.Result); + + return false; + } } } diff --git a/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs b/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs index 353de464..79fb45b8 100644 --- a/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/BaseInstructionHandler.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using Mono.Cecil; using Mono.Cecil.Cil; @@ -9,42 +11,38 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework /********* ** Accessors *********/ - /// A brief noun phrase indicating what the handler matches. - public string NounPhrase { get; protected set; } + /// A brief noun phrase indicating what the handler matches, used if is empty. + public string DefaultPhrase { get; } + + /// The rewrite flags raised for the current module. + public ISet Flags { get; } = new HashSet(); + + /// The brief noun phrases indicating what the handler matched for the current module. + public ISet Phrases { get; } = new HashSet(StringComparer.InvariantCultureIgnoreCase); /********* ** Public methods *********/ - /// Perform the predefined logic for a method if applicable. + /// Rewrite a type reference if needed. /// The assembly module containing the instruction. /// The type definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, TypeDefinition type, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + public virtual bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) { - return InstructionHandleResult.None; + return false; } - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public virtual InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public virtual bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - return InstructionHandleResult.None; + return false; } @@ -52,10 +50,28 @@ namespace StardewModdingAPI.Framework.ModLoading.Framework ** Protected methods *********/ /// Construct an instance. - /// A brief noun phrase indicating what the handler matches. - protected BaseInstructionHandler(string nounPhrase) + /// A brief noun phrase indicating what the handler matches. + protected BaseInstructionHandler(string defaultPhrase) { - this.NounPhrase = nounPhrase; + this.DefaultPhrase = defaultPhrase; + } + + /// Raise a result flag. + /// The result flag to set. + /// The result message to add. + /// Returns true for convenience. + protected bool MarkFlag(InstructionHandleResult flag, string resultMessage = null) + { + this.Flags.Add(flag); + if (resultMessage != null) + this.Phrases.Add(resultMessage); + return true; + } + + /// Raise a generic flag indicating that the code was rewritten. + public bool MarkRewritten() + { + return this.MarkFlag(InstructionHandleResult.Rewritten); } } } diff --git a/src/SMAPI/Framework/ModLoading/Framework/BaseTypeFinder.cs b/src/SMAPI/Framework/ModLoading/Framework/BaseTypeFinder.cs deleted file mode 100644 index 8c85b6a5..00000000 --- a/src/SMAPI/Framework/ModLoading/Framework/BaseTypeFinder.cs +++ /dev/null @@ -1,172 +0,0 @@ -using System; -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; - -namespace StardewModdingAPI.Framework.ModLoading.Framework -{ - /// Finds incompatible CIL type reference instructions. - internal abstract class BaseTypeFinder : BaseInstructionHandler - { - /********* - ** Accessors - *********/ - /// Matches the type references to handle. - private readonly Func IsMatchImpl; - - /// The result to return for matching instructions. - private readonly InstructionHandleResult Result; - - - /********* - ** Public methods - *********/ - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(method) - ? this.Result - : InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - return this.IsMatch(instruction) - ? this.Result - : InstructionHandleResult.None; - } - - /// Get whether a CIL instruction matches. - /// The method definition. - public bool IsMatch(MethodDefinition method) - { - // return type - if (this.IsMatch(method.ReturnType)) - return true; - - // parameters - foreach (ParameterDefinition parameter in method.Parameters) - { - if (this.IsMatch(parameter.ParameterType)) - return true; - } - - // generic parameters - foreach (GenericParameter parameter in method.GenericParameters) - { - if (this.IsMatch(parameter)) - return true; - } - - // custom attributes - foreach (CustomAttribute attribute in method.CustomAttributes) - { - if (this.IsMatch(attribute.AttributeType)) - return true; - - foreach (var arg in attribute.ConstructorArguments) - { - if (this.IsMatch(arg.Type)) - return true; - } - } - - // local variables - foreach (VariableDefinition variable in method.Body.Variables) - { - if (this.IsMatch(variable.VariableType)) - return true; - } - - return false; - } - - /// Get whether a CIL instruction matches. - /// The IL instruction. - public bool IsMatch(Instruction instruction) - { - // field reference - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) - { - return - this.IsMatch(fieldRef.DeclaringType) // field on target class - || this.IsMatch(fieldRef.FieldType); // field value is target class - } - - // method reference - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (methodRef != null) - { - // method on target class - if (this.IsMatch(methodRef.DeclaringType)) - return true; - - // method returns target class - if (this.IsMatch(methodRef.ReturnType)) - return true; - - // method parameters of target class - if (methodRef.Parameters.Any(p => this.IsMatch(p.ParameterType))) - return true; - - // generic args of target class - if (methodRef is GenericInstanceMethod genericRef && genericRef.GenericArguments.Any(this.IsMatch)) - return true; - } - - // type reference - if (instruction.Operand is TypeReference typeRef && this.IsMatch(typeRef)) - return true; - - return false; - } - - /// Get whether a type reference matches the expected type. - /// The type to check. - public bool IsMatch(TypeReference type) - { - // root type - if (this.IsMatchImpl(type)) - return true; - - // generic arguments - if (type is GenericInstanceType genericType) - { - if (genericType.GenericArguments.Any(this.IsMatch)) - return true; - } - - // generic parameters (e.g. constraints) - if (type.GenericParameters.Any(this.IsMatch)) - return true; - - return false; - } - - - /********* - ** Protected methods - *********/ - /// Construct an instance. - /// Matches the type references to handle. - /// The result to return for matching instructions. - /// A brief noun phrase indicating what the instruction finder matches. - protected BaseTypeFinder(Func isMatch, InstructionHandleResult result, string nounPhrase) - : base(nounPhrase) - { - this.IsMatchImpl = isMatch; - this.Result = result; - } - } -} diff --git a/src/SMAPI/Framework/ModLoading/Framework/BaseTypeReferenceRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/BaseTypeReferenceRewriter.cs deleted file mode 100644 index 3ccacf22..00000000 --- a/src/SMAPI/Framework/ModLoading/Framework/BaseTypeReferenceRewriter.cs +++ /dev/null @@ -1,209 +0,0 @@ -using System; -using System.Linq; -using Mono.Cecil; -using Mono.Cecil.Cil; -using Mono.Collections.Generic; - -namespace StardewModdingAPI.Framework.ModLoading.Framework -{ - /// Rewrites all references to a type. - internal abstract class BaseTypeReferenceRewriter : BaseInstructionHandler - { - /********* - ** Fields - *********/ - /// The type finder which matches types to rewrite. - private readonly BaseTypeFinder Finder; - - - /********* - ** Public methods - *********/ - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The type definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, TypeDefinition type, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - bool rewritten = this.RewriteCustomAttributesIfNeeded(module, type.CustomAttributes); - - return rewritten - ? InstructionHandleResult.Rewritten - : InstructionHandleResult.None; - } - - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - bool rewritten = false; - - // return type - if (this.Finder.IsMatch(method.ReturnType)) - rewritten |= this.RewriteIfNeeded(module, method.ReturnType, newType => method.ReturnType = newType); - - // parameters - foreach (ParameterDefinition parameter in method.Parameters) - { - if (this.Finder.IsMatch(parameter.ParameterType)) - rewritten |= this.RewriteIfNeeded(module, parameter.ParameterType, newType => parameter.ParameterType = newType); - } - - // generic parameters - for (int i = 0; i < method.GenericParameters.Count; i++) - { - var parameter = method.GenericParameters[i]; - if (this.Finder.IsMatch(parameter)) - rewritten |= this.RewriteIfNeeded(module, parameter, newType => method.GenericParameters[i] = new GenericParameter(parameter.Name, newType)); - } - - // custom attributes - rewritten |= this.RewriteCustomAttributesIfNeeded(module, method.CustomAttributes); - - // local variables - foreach (VariableDefinition variable in method.Body.Variables) - { - if (this.Finder.IsMatch(variable.VariableType)) - rewritten |= this.RewriteIfNeeded(module, variable.VariableType, newType => variable.VariableType = newType); - } - - return rewritten - ? InstructionHandleResult.Rewritten - : InstructionHandleResult.None; - } - - /// Perform the predefined logic for an instruction if applicable. - /// The assembly module containing the instruction. - /// The CIL processor. - /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) - { - if (!this.Finder.IsMatch(instruction)) - return InstructionHandleResult.None; - bool rewritten = false; - - // field reference - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) - { - rewritten |= this.RewriteIfNeeded(module, fieldRef.DeclaringType, newType => fieldRef.DeclaringType = newType); - rewritten |= this.RewriteIfNeeded(module, fieldRef.FieldType, newType => fieldRef.FieldType = newType); - } - - // method reference - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (methodRef != null) - { - rewritten |= this.RewriteIfNeeded(module, methodRef.DeclaringType, newType => methodRef.DeclaringType = newType); - rewritten |= this.RewriteIfNeeded(module, methodRef.ReturnType, newType => methodRef.ReturnType = newType); - foreach (var parameter in methodRef.Parameters) - rewritten |= this.RewriteIfNeeded(module, parameter.ParameterType, newType => parameter.ParameterType = newType); - if (methodRef is GenericInstanceMethod genericRef) - { - for (int i = 0; i < genericRef.GenericArguments.Count; i++) - rewritten |= this.RewriteIfNeeded(module, genericRef.GenericArguments[i], newType => genericRef.GenericArguments[i] = newType); - } - } - - // type reference - if (instruction.Operand is TypeReference typeRef) - rewritten |= this.RewriteIfNeeded(module, typeRef, newType => cil.Replace(instruction, cil.Create(instruction.OpCode, newType))); - - return rewritten - ? InstructionHandleResult.Rewritten - : InstructionHandleResult.None; - } - - - /********* - ** Protected methods - *********/ - /// Construct an instance. - /// The type finder which matches types to rewrite. - /// A brief noun phrase indicating what the instruction finder matches. - protected BaseTypeReferenceRewriter(BaseTypeFinder finder, string nounPhrase) - : base(nounPhrase) - { - this.Finder = finder; - } - - /// Change a type reference if needed. - /// The assembly module containing the instruction. - /// The type to replace if it matches. - /// Assign the new type reference. - protected abstract bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set); - - /// Rewrite custom attributes if needed. - /// The assembly module containing the attributes. - /// The custom attributes to handle. - private bool RewriteCustomAttributesIfNeeded(ModuleDefinition module, Collection attributes) - { - bool rewritten = false; - - for (int attrIndex = 0; attrIndex < attributes.Count; attrIndex++) - { - CustomAttribute attribute = attributes[attrIndex]; - bool curChanged = false; - - // attribute type - TypeReference newAttrType = null; - if (this.Finder.IsMatch(attribute.AttributeType)) - { - rewritten |= this.RewriteIfNeeded(module, attribute.AttributeType, newType => - { - newAttrType = newType; - curChanged = true; - }); - } - - // constructor arguments - TypeReference[] argTypes = new TypeReference[attribute.ConstructorArguments.Count]; - for (int i = 0; i < argTypes.Length; i++) - { - var arg = attribute.ConstructorArguments[i]; - - argTypes[i] = arg.Type; - rewritten |= this.RewriteIfNeeded(module, arg.Type, newType => - { - argTypes[i] = newType; - curChanged = true; - }); - } - - // swap attribute - if (curChanged) - { - // get constructor - MethodDefinition constructor = (newAttrType ?? attribute.AttributeType) - .Resolve() - .Methods - .Where(method => method.IsConstructor) - .FirstOrDefault(ctor => RewriteHelper.HasMatchingSignature(ctor, attribute.Constructor)); - if (constructor == null) - throw new InvalidOperationException($"Can't rewrite attribute type '{attribute.AttributeType.FullName}' to '{newAttrType?.FullName}', no equivalent constructor found."); - - // create new attribute - var newAttr = new CustomAttribute(module.ImportReference(constructor)); - for (int i = 0; i < argTypes.Length; i++) - newAttr.ConstructorArguments.Add(new CustomAttributeArgument(argTypes[i], attribute.ConstructorArguments[i].Value)); - foreach (var prop in attribute.Properties) - newAttr.Properties.Add(new CustomAttributeNamedArgument(prop.Name, prop.Argument)); - foreach (var field in attribute.Fields) - newAttr.Fields.Add(new CustomAttributeNamedArgument(field.Name, field.Argument)); - - // swap attribute - attributes[attrIndex] = newAttr; - rewritten = true; - } - } - - return rewritten; - } - } -} diff --git a/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs new file mode 100644 index 00000000..6aeb00ce --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Framework/RecursiveRewriter.cs @@ -0,0 +1,260 @@ +using System; +using System.Linq; +using Mono.Cecil; +using Mono.Cecil.Cil; +using Mono.Collections.Generic; + +namespace StardewModdingAPI.Framework.ModLoading.Framework +{ + /// Handles recursively rewriting loaded assembly code. + internal class RecursiveRewriter + { + /********* + ** Delegates + *********/ + /// Rewrite a type reference in the assembly code. + /// The current type reference. + /// Replaces the type reference with the given type. + /// Returns whether the type was changed. + public delegate bool RewriteTypeDelegate(TypeReference type, Action replaceWith); + + /// Rewrite a CIL instruction in the assembly code. + /// The current CIL instruction. + /// The CIL instruction processor. + /// Replaces the CIL instruction with the given instruction. + /// Returns whether the instruction was changed. + public delegate bool RewriteInstructionDelegate(Instruction instruction, ILProcessor cil, Action replaceWith); + + + /********* + ** Accessors + *********/ + /// The module to rewrite. + public ModuleDefinition Module { get; } + + /// Handle or rewrite a type reference if needed. + public RewriteTypeDelegate RewriteTypeImpl { get; } + + /// Handle or rewrite a CIL instruction if needed. + public RewriteInstructionDelegate RewriteInstructionImpl { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The module to rewrite. + /// Handle or rewrite a type reference if needed. + /// Handle or rewrite a CIL instruction if needed. + public RecursiveRewriter(ModuleDefinition module, RewriteTypeDelegate rewriteType, RewriteInstructionDelegate rewriteInstruction) + { + this.Module = module; + this.RewriteTypeImpl = rewriteType; + this.RewriteInstructionImpl = rewriteInstruction; + } + + /// Rewrite the loaded module code. + /// Returns whether the module was modified. + public bool RewriteModule() + { + bool anyRewritten = false; + + foreach (TypeDefinition type in this.Module.GetTypes()) + { + anyRewritten |= this.RewriteCustomAttributes(type.CustomAttributes); + anyRewritten |= this.RewriteGenericParameters(type.GenericParameters); + + foreach (MethodDefinition method in type.Methods.Where(p => p.HasBody)) + { + anyRewritten |= this.RewriteTypeReference(method.ReturnType, newType => method.ReturnType = newType); + anyRewritten |= this.RewriteGenericParameters(method.GenericParameters); + anyRewritten |= this.RewriteCustomAttributes(method.CustomAttributes); + + foreach (ParameterDefinition parameter in method.Parameters) + anyRewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType); + + foreach (VariableDefinition variable in method.Body.Variables) + anyRewritten |= this.RewriteTypeReference(variable.VariableType, newType => variable.VariableType = newType); + + // check CIL instructions + ILProcessor cil = method.Body.GetILProcessor(); + Collection instructions = cil.Body.Instructions; + for (int i = 0; i < instructions.Count; i++) + { + var instruction = instructions[i]; + if (instruction.OpCode.Code == Code.Nop) + continue; + + anyRewritten |= this.RewriteInstruction(instruction, cil, newInstruction => + { + anyRewritten = true; + cil.Replace(instruction, newInstruction); + instruction = newInstruction; + }); + } + } + } + + return anyRewritten; + } + + + /********* + ** Private methods + *********/ + /// Rewrite a CIL instruction if needed. + /// The current CIL instruction. + /// The CIL instruction processor. + /// Replaces the CIL instruction with a new one. + private bool RewriteInstruction(Instruction instruction, ILProcessor cil, Action replaceWith) + { + bool rewritten = false; + + // field reference + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) + { + rewritten |= this.RewriteTypeReference(fieldRef.DeclaringType, newType => fieldRef.DeclaringType = newType); + rewritten |= this.RewriteTypeReference(fieldRef.FieldType, newType => fieldRef.FieldType = newType); + } + + // method reference + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (methodRef != null) + { + rewritten |= this.RewriteTypeReference(methodRef.DeclaringType, newType => methodRef.DeclaringType = newType); + rewritten |= this.RewriteTypeReference(methodRef.ReturnType, newType => methodRef.ReturnType = newType); + + foreach (var parameter in methodRef.Parameters) + rewritten |= this.RewriteTypeReference(parameter.ParameterType, newType => parameter.ParameterType = newType); + + if (methodRef is GenericInstanceMethod genericRef) + { + for (int i = 0; i < genericRef.GenericArguments.Count; i++) + rewritten |= this.RewriteTypeReference(genericRef.GenericArguments[i], newType => genericRef.GenericArguments[i] = newType); + } + } + + // type reference + if (instruction.Operand is TypeReference typeRef) + rewritten |= this.RewriteTypeReference(typeRef, newType => replaceWith(cil.Create(instruction.OpCode, newType))); + + // instruction itself + // (should be done after the above type rewrites to ensure valid types) + rewritten |= this.RewriteInstructionImpl(instruction, cil, newInstruction => + { + rewritten = true; + cil.Replace(instruction, newInstruction); + instruction = newInstruction; + }); + + return rewritten; + } + + /// Rewrite a type reference if needed. + /// The current type reference. + /// Replaces the type reference with a new one. + private bool RewriteTypeReference(TypeReference type, Action replaceWith) + { + bool rewritten = false; + + // type + rewritten |= this.RewriteTypeImpl(type, newType => + { + type = newType; + replaceWith(newType); + rewritten = true; + }); + + // generic arguments + if (type is GenericInstanceType genericType) + { + for (int i = 0; i < genericType.GenericArguments.Count; i++) + rewritten |= this.RewriteTypeReference(genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef); + } + + // generic parameters (e.g. constraints) + rewritten |= this.RewriteGenericParameters(type.GenericParameters); + + return rewritten; + } + + /// Rewrite custom attributes if needed. + /// The current custom attributes. + private bool RewriteCustomAttributes(Collection attributes) + { + bool rewritten = false; + + for (int attrIndex = 0; attrIndex < attributes.Count; attrIndex++) + { + CustomAttribute attribute = attributes[attrIndex]; + bool curChanged = false; + + // attribute type + TypeReference newAttrType = null; + rewritten |= this.RewriteTypeReference(attribute.AttributeType, newType => + { + newAttrType = newType; + curChanged = true; + }); + + // constructor arguments + TypeReference[] argTypes = new TypeReference[attribute.ConstructorArguments.Count]; + for (int i = 0; i < argTypes.Length; i++) + { + var arg = attribute.ConstructorArguments[i]; + + argTypes[i] = arg.Type; + rewritten |= this.RewriteTypeReference(arg.Type, newType => + { + argTypes[i] = newType; + curChanged = true; + }); + } + + // swap attribute + if (curChanged) + { + // get constructor + MethodDefinition constructor = (newAttrType ?? attribute.AttributeType) + .Resolve() + .Methods + .Where(method => method.IsConstructor) + .FirstOrDefault(ctor => RewriteHelper.HasMatchingSignature(ctor, attribute.Constructor)); + if (constructor == null) + throw new InvalidOperationException($"Can't rewrite attribute type '{attribute.AttributeType.FullName}' to '{newAttrType?.FullName}', no equivalent constructor found."); + + // create new attribute + var newAttr = new CustomAttribute(this.Module.ImportReference(constructor)); + for (int i = 0; i < argTypes.Length; i++) + newAttr.ConstructorArguments.Add(new CustomAttributeArgument(argTypes[i], attribute.ConstructorArguments[i].Value)); + foreach (var prop in attribute.Properties) + newAttr.Properties.Add(new CustomAttributeNamedArgument(prop.Name, prop.Argument)); + foreach (var field in attribute.Fields) + newAttr.Fields.Add(new CustomAttributeNamedArgument(field.Name, field.Argument)); + + // swap attribute + attributes[attrIndex] = newAttr; + rewritten = true; + } + } + + return rewritten; + } + + /// Rewrites generic type parameters if needed. + /// The current generic type parameters. + private bool RewriteGenericParameters(Collection parameters) + { + bool anyChanged = false; + + for (int i = 0; i < parameters.Count; i++) + { + TypeReference parameter = parameters[i]; + anyChanged |= this.RewriteTypeReference(parameter, newType => parameters[i] = new GenericParameter(parameter.Name, newType)); + } + + return anyChanged; + } + } +} diff --git a/src/SMAPI/Framework/ModLoading/RewriteHelper.cs b/src/SMAPI/Framework/ModLoading/Framework/RewriteHelper.cs similarity index 84% rename from src/SMAPI/Framework/ModLoading/RewriteHelper.cs rename to src/SMAPI/Framework/ModLoading/Framework/RewriteHelper.cs index 553679f9..91c9dec3 100644 --- a/src/SMAPI/Framework/ModLoading/RewriteHelper.cs +++ b/src/SMAPI/Framework/ModLoading/Framework/RewriteHelper.cs @@ -4,7 +4,7 @@ using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; -namespace StardewModdingAPI.Framework.ModLoading +namespace StardewModdingAPI.Framework.ModLoading.Framework { /// Provides helper methods for field rewriters. internal static class RewriteHelper @@ -28,6 +28,28 @@ namespace StardewModdingAPI.Framework.ModLoading : null; } + /// Get whether the field is a reference to the expected type and field. + /// The IL instruction. + /// The full type name containing the expected field. + /// The name of the expected field. + public static bool IsFieldReferenceTo(Instruction instruction, string fullTypeName, string fieldName) + { + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + return RewriteHelper.IsFieldReferenceTo(fieldRef, fullTypeName, fieldName); + } + + /// Get whether the field is a reference to the expected type and field. + /// The field reference to check. + /// The full type name containing the expected field. + /// The name of the expected field. + public static bool IsFieldReferenceTo(FieldReference fieldRef, string fullTypeName, string fieldName) + { + return + fieldRef != null + && fieldRef.DeclaringType.FullName == fullTypeName + && fieldRef.Name == fieldName; + } + /// Get the method reference from an instruction if it matches. /// The IL instruction. public static MethodReference AsMethodReference(Instruction instruction) diff --git a/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs b/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs index f9d320a6..e6de6785 100644 --- a/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs +++ b/src/SMAPI/Framework/ModLoading/IInstructionHandler.cs @@ -1,3 +1,5 @@ +using System; +using System.Collections.Generic; using Mono.Cecil; using Mono.Cecil.Cil; @@ -9,33 +11,32 @@ namespace StardewModdingAPI.Framework.ModLoading /********* ** Accessors *********/ - /// A brief noun phrase indicating what the handler matches. - string NounPhrase { get; } + /// A brief noun phrase indicating what the handler matches, used if is empty. + string DefaultPhrase { get; } + + /// The rewrite flags raised for the current module. + ISet Flags { get; } + + /// The brief noun phrases indicating what the handler matched for the current module. + ISet Phrases { get; } /********* ** Methods *********/ - /// Perform the predefined logic for a method if applicable. + /// Rewrite a type reference if needed. /// The assembly module containing the instruction. /// The type definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - InstructionHandleResult Handle(ModuleDefinition module, TypeDefinition type, PlatformAssemblyMap assemblyMap, bool platformChanged); + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith); - /// Perform the predefined logic for a method if applicable. - /// The assembly module containing the instruction. - /// The method definition to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - InstructionHandleResult Handle(ModuleDefinition module, MethodDefinition method, PlatformAssemblyMap assemblyMap, bool platformChanged); - - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged); + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith); } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs index ff86c6e2..8043b13a 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldReplaceRewriter.cs @@ -2,16 +2,22 @@ using System; using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// Rewrites references to one field with another. - internal class FieldReplaceRewriter : FieldFinder + internal class FieldReplaceRewriter : BaseInstructionHandler { /********* ** Fields *********/ + /// The type containing the field to which references should be rewritten. + private readonly Type Type; + + /// The field name to which references should be rewritten. + private readonly string FromFieldName; + /// The new field to reference. private readonly FieldInfo ToField; @@ -20,31 +26,36 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters ** Public methods *********/ /// Construct an instance. - /// The type whose field to which references should be rewritten. + /// The type whose field to rewrite. /// The field name to rewrite. /// The new field name to reference. public FieldReplaceRewriter(Type type, string fromFieldName, string toFieldName) - : base(type.FullName, fromFieldName, InstructionHandleResult.None) + : base(defaultPhrase: $"{type.FullName}.{fromFieldName} field") { + this.Type = type; + this.FromFieldName = fromFieldName; this.ToField = type.GetField(toFieldName); if (this.ToField == null) throw new InvalidOperationException($"The {type.FullName} class doesn't have a {toFieldName} field."); } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// The CIL instruction to handle. + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - if (!this.IsMatch(instruction)) - return InstructionHandleResult.None; + // get field reference + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (!RewriteHelper.IsFieldReferenceTo(fieldRef, this.Type.FullName, this.FromFieldName)) + return false; + // replace with new field FieldReference newRef = module.ImportReference(this.ToField); - cil.Replace(instruction, cil.Create(instruction.OpCode, newRef)); - return InstructionHandleResult.Rewritten; + replaceWith(cil.Create(instruction.OpCode, newRef)); + return this.MarkRewritten(); } } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs index a43c5e9a..c3b5854e 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/FieldToPropertyRewriter.cs @@ -1,21 +1,24 @@ using System; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// Rewrites field references into property references. - internal class FieldToPropertyRewriter : FieldFinder + internal class FieldToPropertyRewriter : BaseInstructionHandler { /********* ** Fields *********/ - /// The type whose field to which references should be rewritten. + /// The type containing the field to which references should be rewritten. private readonly Type Type; - /// The property name. - private readonly string PropertyName; + /// The field name to which references should be rewritten. + private readonly string FromFieldName; + + /// The new property name. + private readonly string ToPropertyName; /********* @@ -26,10 +29,11 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The field name to rewrite. /// The property name (if different). public FieldToPropertyRewriter(Type type, string fieldName, string propertyName) - : base(type.FullName, fieldName, InstructionHandleResult.None) + : base(defaultPhrase: $"{type.FullName}.{fieldName} field") { this.Type = type; - this.PropertyName = propertyName; + this.FromFieldName = fieldName; + this.ToPropertyName = propertyName; } /// Construct an instance. @@ -38,22 +42,24 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters public FieldToPropertyRewriter(Type type, string fieldName) : this(type, fieldName, fieldName) { } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// The CIL instruction to handle. + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - if (!this.IsMatch(instruction)) - return InstructionHandleResult.None; + // get field ref + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (!RewriteHelper.IsFieldReferenceTo(fieldRef, this.Type.FullName, this.FromFieldName)) + return false; + // replace with property string methodPrefix = instruction.OpCode == OpCodes.Ldsfld || instruction.OpCode == OpCodes.Ldfld ? "get" : "set"; - MethodReference propertyRef = module.ImportReference(this.Type.GetMethod($"{methodPrefix}_{this.PropertyName}")); - cil.Replace(instruction, cil.Create(OpCodes.Call, propertyRef)); - - return InstructionHandleResult.Rewritten; + MethodReference propertyRef = module.ImportReference(this.Type.GetMethod($"{methodPrefix}_{this.ToPropertyName}")); + replaceWith(cil.Create(OpCodes.Call, propertyRef)); + return this.MarkRewritten(); } } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs index 9faca235..a7a0b9c3 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs @@ -1,28 +1,20 @@ using System; +using HarmonyLib; using Mono.Cecil; -using StardewModdingAPI.Framework.ModLoading.Finders; +using Mono.Cecil.Cil; using StardewModdingAPI.Framework.ModLoading.Framework; +using StardewModdingAPI.Framework.ModLoading.RewriteFacades; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// Rewrites Harmony 1.x assembly references to work with Harmony 2.x. - internal class Harmony1AssemblyRewriter : BaseTypeReferenceRewriter + internal class Harmony1AssemblyRewriter : BaseInstructionHandler { /********* ** Fields *********/ - /// The full assembly name to which to find references. - private const string FromAssemblyName = "0Harmony"; - - /// The main Harmony type. - private readonly Type HarmonyType = typeof(HarmonyLib.Harmony); - - - /********* - ** Accessors - *********/ - /// A brief noun phrase indicating what the rewriter matches. - public const string DefaultNounPhrase = "Harmony 1.x"; + /// Whether any Harmony 1.x types were replaced. + private bool ReplacedTypes; /********* @@ -30,41 +22,80 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters *********/ /// Construct an instance. public Harmony1AssemblyRewriter() - : base(new TypeAssemblyFinder(Harmony1AssemblyRewriter.FromAssemblyName, InstructionHandleResult.None), Harmony1AssemblyRewriter.DefaultNounPhrase) - { } + : base(defaultPhrase: "Harmony 1.x") { } + + /// Rewrite a type reference if needed. + /// The assembly module containing the instruction. + /// The type definition to handle. + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + public override bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) + { + // rewrite Harmony 1.x type to Harmony 2.0 type + if (type.Scope is AssemblyNameReference scope && scope.Name == "0Harmony" && scope.Version.Major == 1) + { + Type targetType = this.GetMappedType(type); + replaceWith(module.ImportReference(targetType)); + this.MarkRewritten(); + this.ReplacedTypes = true; + return true; + } + + return false; + } + + /// Rewrite a CIL instruction reference if needed. + /// The assembly module containing the instruction. + /// The CIL processor. + /// The CIL instruction to handle. + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) + { + // rewrite Harmony 1.x methods to Harmony 2.0 + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (this.TryRewriteMethodsToFacade(module, methodRef)) + return true; + + return false; + } /********* ** Private methods *********/ - /// Change a type reference if needed. - /// The assembly module containing the instruction. - /// The type to replace if it matches. - /// Assign the new type reference. - protected override bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set) + /// Rewrite methods to use Harmony facades if needed. + /// The assembly module containing the method reference. + /// The method reference to map. + private bool TryRewriteMethodsToFacade(ModuleDefinition module, MethodReference methodRef) { - bool rewritten = false; + if (!this.ReplacedTypes) + return false; // not Harmony (or already using Harmony 2.0) - // current type - if (type.Scope.Name == Harmony1AssemblyRewriter.FromAssemblyName && type.Scope is AssemblyNameReference assemblyScope && assemblyScope.Version.Major == 1) + // get facade type + Type toType; + switch (methodRef?.DeclaringType.FullName) { - Type targetType = this.GetMappedType(type); - set(module.ImportReference(targetType)); + case "HarmonyLib.Harmony": + toType = typeof(HarmonyInstanceMethods); + break; + + case "HarmonyLib.AccessTools": + toType = typeof(AccessToolsMethods); + break; + + default: + return false; + } + + // map if there's a matching method + if (RewriteHelper.HasMatchingSignature(toType, methodRef)) + { + methodRef.DeclaringType = module.ImportReference(toType); return true; } - // recurse into generic arguments - if (type is GenericInstanceType genericType) - { - for (int i = 0; i < genericType.GenericArguments.Count; i++) - rewritten |= this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef); - } - - // recurse into generic parameters (e.g. constraints) - for (int i = 0; i < type.GenericParameters.Count; i++) - rewritten |= this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef)); - - return rewritten; + return false; } /// Get an equivalent Harmony 2.x type. @@ -73,11 +104,11 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters { // main Harmony object if (type.FullName == "Harmony.HarmonyInstance") - return this.HarmonyType; + return typeof(Harmony); // other objects string fullName = type.FullName.Replace("Harmony.", "HarmonyLib."); - string targetName = this.HarmonyType.AssemblyQualifiedName.Replace(this.HarmonyType.FullName, fullName); + string targetName = typeof(Harmony).AssemblyQualifiedName.Replace(typeof(Harmony).FullName, fullName); return Type.GetType(targetName, throwOnError: true); } } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs index d0fe8b13..b8e53f40 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/MethodParentRewriter.cs @@ -18,9 +18,6 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The type with methods to map to. private readonly Type ToType; - /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. - private readonly bool OnlyIfPlatformChanged; - /********* ** Public methods @@ -28,54 +25,49 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Construct an instance. /// The type whose methods to remap. /// The type with methods to map to. - /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public MethodParentRewriter(string fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null) + public MethodParentRewriter(string fromType, Type toType, string nounPhrase = null) : base(nounPhrase ?? $"{fromType.Split('.').Last()} methods") { this.FromType = fromType; this.ToType = toType; - this.OnlyIfPlatformChanged = onlyIfPlatformChanged; } /// Construct an instance. /// The type whose methods to remap. /// The type with methods to map to. - /// Whether to only rewrite references if loading the assembly on a different platform than it was compiled on. /// A brief noun phrase indicating what the instruction finder matches (or null to generate one). - public MethodParentRewriter(Type fromType, Type toType, bool onlyIfPlatformChanged = false, string nounPhrase = null) - : this(fromType.FullName, toType, onlyIfPlatformChanged, nounPhrase) { } + public MethodParentRewriter(Type fromType, Type toType, string nounPhrase = null) + : this(fromType.FullName, toType, nounPhrase) { } - - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. /// The CIL instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - if (!this.IsMatch(instruction, platformChanged)) - return InstructionHandleResult.None; + // get method ref + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (!this.IsMatch(methodRef)) + return false; - MethodReference methodRef = (MethodReference)instruction.Operand; + // rewrite methodRef.DeclaringType = module.ImportReference(this.ToType); - return InstructionHandleResult.Rewritten; + return this.MarkRewritten(); } /********* - ** Protected methods + ** Private methods *********/ /// Get whether a CIL instruction matches. - /// The IL instruction. - /// Whether the mod was compiled on a different platform. - protected bool IsMatch(Instruction instruction, bool platformChanged) + /// The method reference. + private bool IsMatch(MethodReference methodRef) { - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); return methodRef != null - && (platformChanged || !this.OnlyIfPlatformChanged) && methodRef.DeclaringType.FullName == this.FromType && RewriteHelper.HasMatchingSignature(this.ToType, methodRef); } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs index 7e7c0efa..6ef18b26 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/StaticFieldToConstantRewriter.cs @@ -1,17 +1,23 @@ using System; using Mono.Cecil; using Mono.Cecil.Cil; -using StardewModdingAPI.Framework.ModLoading.Finders; +using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// Rewrites static field references into constant values. /// The constant value type. - internal class StaticFieldToConstantRewriter : FieldFinder + internal class StaticFieldToConstantRewriter : BaseInstructionHandler { /********* ** Fields *********/ + /// The type containing the field to which references should be rewritten. + private readonly Type Type; + + /// The field name to which references should be rewritten. + private readonly string FromFieldName; + /// The constant value to replace with. private readonly TValue Value; @@ -24,24 +30,29 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The field name to rewrite. /// The constant value to replace with. public StaticFieldToConstantRewriter(Type type, string fieldName, TValue value) - : base(type.FullName, fieldName, InstructionHandleResult.None) + : base(defaultPhrase: $"{type.FullName}.{fieldName} field") { + this.Type = type; + this.FromFieldName = fieldName; this.Value = value; } - /// Perform the predefined logic for an instruction if applicable. + /// Rewrite a CIL instruction reference if needed. /// The assembly module containing the instruction. /// The CIL processor. - /// The instruction to handle. - /// Metadata for mapping assemblies to the current platform. - /// Whether the mod was compiled on a different platform. - public override InstructionHandleResult Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, PlatformAssemblyMap assemblyMap, bool platformChanged) + /// The CIL instruction to handle. + /// Replaces the CIL instruction with a new one. + /// Returns whether the instruction was changed. + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction, Action replaceWith) { - if (!this.IsMatch(instruction)) - return InstructionHandleResult.None; + // get field reference + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (!RewriteHelper.IsFieldReferenceTo(fieldRef, this.Type.FullName, this.FromFieldName)) + return false; - cil.Replace(instruction, this.CreateConstantInstruction(cil, this.Value)); - return InstructionHandleResult.Rewritten; + // rewrite to constant + replaceWith(this.CreateConstantInstruction(cil, this.Value)); + return this.MarkRewritten(); } diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs index d95e5ac9..c2120444 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/TypeReferenceRewriter.cs @@ -1,12 +1,11 @@ using System; using Mono.Cecil; -using StardewModdingAPI.Framework.ModLoading.Finders; using StardewModdingAPI.Framework.ModLoading.Framework; namespace StardewModdingAPI.Framework.ModLoading.Rewriters { /// Rewrites all references to a type. - internal class TypeReferenceRewriter : BaseTypeReferenceRewriter + internal class TypeReferenceRewriter : BaseInstructionHandler { /********* ** Fields @@ -17,6 +16,9 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// The new type to reference. private readonly Type ToType; + /// Get whether a matched type should be ignored. + private readonly Func ShouldIgnore; + /********* ** Public methods @@ -24,45 +26,29 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters /// Construct an instance. /// The full type name to which to find references. /// The new type to reference. - /// A lambda which overrides a matched type. + /// Get whether a matched type should be ignored. public TypeReferenceRewriter(string fromTypeFullName, Type toType, Func shouldIgnore = null) - : base(new TypeFinder(fromTypeFullName, InstructionHandleResult.None, shouldIgnore), $"{fromTypeFullName} type") + : base($"{fromTypeFullName} type") { this.FromTypeName = fromTypeFullName; this.ToType = toType; + this.ShouldIgnore = shouldIgnore; } - - /********* - ** Protected methods - *********/ - /// Change a type reference if needed. + /// Rewrite a type reference if needed. /// The assembly module containing the instruction. - /// The type to replace if it matches. - /// Assign the new type reference. - protected override bool RewriteIfNeeded(ModuleDefinition module, TypeReference type, Action set) + /// The type definition to handle. + /// Replaces the type reference with a new one. + /// Returns whether the type was changed. + public override bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) { - bool rewritten = false; + // check type reference + if (type.FullName != this.FromTypeName || this.ShouldIgnore?.Invoke(type) == true) + return false; - // current type - if (type.FullName == this.FromTypeName) - { - set(module.ImportReference(this.ToType)); - return true; - } - - // recurse into generic arguments - if (type is GenericInstanceType genericType) - { - for (int i = 0; i < genericType.GenericArguments.Count; i++) - rewritten |= this.RewriteIfNeeded(module, genericType.GenericArguments[i], typeRef => genericType.GenericArguments[i] = typeRef); - } - - // recurse into generic parameters (e.g. constraints) - for (int i = 0; i < type.GenericParameters.Count; i++) - rewritten |= this.RewriteIfNeeded(module, type.GenericParameters[i], typeRef => type.GenericParameters[i] = new GenericParameter(typeRef)); - - return rewritten; + // rewrite to new type + replaceWith(module.ImportReference(this.ToType)); + return this.MarkRewritten(); } } } diff --git a/src/SMAPI/Metadata/InstructionMetadata.cs b/src/SMAPI/Metadata/InstructionMetadata.cs index d80f64e2..b7aad9da 100644 --- a/src/SMAPI/Metadata/InstructionMetadata.cs +++ b/src/SMAPI/Metadata/InstructionMetadata.cs @@ -25,21 +25,21 @@ namespace StardewModdingAPI.Metadata *********/ /// Get rewriters which detect or fix incompatible CIL instructions in mod assemblies. /// Whether to detect paranoid mode issues. - public IEnumerable GetHandlers(bool paranoidMode) + /// Whether the assembly was rewritten for crossplatform compatibility. + public IEnumerable GetHandlers(bool paranoidMode, bool platformChanged) { /**** ** rewrite CIL to fix incompatible code ****/ // rewrite for crossplatform compatibility - yield return new MethodParentRewriter(typeof(SpriteBatch), typeof(SpriteBatchMethods), onlyIfPlatformChanged: true); + if (platformChanged) + yield return new MethodParentRewriter(typeof(SpriteBatch), typeof(SpriteBatchMethods)); // rewrite for Stardew Valley 1.3 yield return new StaticFieldToConstantRewriter(typeof(Game1), "tileSize", Game1.tileSize); // rewrite for SMAPI 3.6 (Harmony 1.x => 2.0 update) yield return new Harmony1AssemblyRewriter(); - yield return new MethodParentRewriter(typeof(HarmonyLib.Harmony), typeof(HarmonyInstanceMethods), onlyIfPlatformChanged: false, nounPhrase: Harmony1AssemblyRewriter.DefaultNounPhrase); - yield return new MethodParentRewriter(typeof(HarmonyLib.AccessTools), typeof(AccessToolsMethods), onlyIfPlatformChanged: false, nounPhrase: Harmony1AssemblyRewriter.DefaultNounPhrase); /**** ** detect mod issues