From 8bfab94213e86c4245961150bd3423ee85213c5d Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 31 Aug 2021 01:13:22 -0400 Subject: reduce unneeded operations when scanning/rewriting mod DLLs --- .../Rewriters/Harmony1AssemblyRewriter.cs | 129 ------------------- .../ModLoading/Rewriters/HarmonyRewriter.cs | 143 +++++++++++++++++++++ .../ModLoading/Rewriters/HeuristicFieldRewriter.cs | 6 +- .../Rewriters/HeuristicMethodRewriter.cs | 6 +- 4 files changed, 149 insertions(+), 135 deletions(-) delete mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs create mode 100644 src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs (limited to 'src/SMAPI/Framework/ModLoading/Rewriters') diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs deleted file mode 100644 index 7a3b428d..00000000 --- a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs +++ /dev/null @@ -1,129 +0,0 @@ -using System; -using HarmonyLib; -using Mono.Cecil; -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 : BaseInstructionHandler - { - /********* - ** Fields - *********/ - /// Whether any Harmony 1.x types were replaced. - private bool ReplacedTypes; - - - /********* - ** Public methods - *********/ - /// Construct an instance. - public Harmony1AssemblyRewriter() - : base(defaultPhrase: "Harmony 1.x") { } - - /// - 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 { Name: "0Harmony" } scope && scope.Version.Major == 1) - { - Type targetType = this.GetMappedType(type); - replaceWith(module.ImportReference(targetType)); - this.OnChanged(); - this.ReplacedTypes = true; - return true; - } - - return false; - } - - /// - public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction) - { - // rewrite Harmony 1.x methods to Harmony 2.0 - MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); - if (this.TryRewriteMethodsToFacade(module, methodRef)) - { - this.OnChanged(); - return true; - } - - // rewrite renamed fields - FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); - if (fieldRef != null) - { - if (fieldRef.DeclaringType.FullName == "HarmonyLib.HarmonyMethod" && fieldRef.Name == "prioritiy") - { - fieldRef.Name = nameof(HarmonyMethod.priority); - this.OnChanged(); - } - } - - return false; - } - - - /********* - ** Private methods - *********/ - /// Update the mod metadata when any Harmony 1.x code is migrated. - private void OnChanged() - { - this.MarkRewritten(); - this.MarkFlag(InstructionHandleResult.DetectedGamePatch); - } - - /// 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) - { - if (!this.ReplacedTypes) - return false; // not Harmony (or already using Harmony 2.0) - - // get facade type - Type toType = methodRef?.DeclaringType.FullName switch - { - "HarmonyLib.Harmony" => typeof(HarmonyInstanceFacade), - "HarmonyLib.AccessTools" => typeof(AccessToolsFacade), - "HarmonyLib.HarmonyMethod" => typeof(HarmonyMethodFacade), - _ => null - }; - if (toType == null) - return false; - - // map if there's a matching method - if (RewriteHelper.HasMatchingSignature(toType, methodRef)) - { - methodRef.DeclaringType = module.ImportReference(toType); - return true; - } - - return false; - } - - /// Get an equivalent Harmony 2.x type. - /// The Harmony 1.x type. - private Type GetMappedType(TypeReference type) - { - return type.FullName switch - { - "Harmony.HarmonyInstance" => typeof(Harmony), - "Harmony.ILCopying.ExceptionBlock" => typeof(ExceptionBlock), - _ => this.GetMappedTypeByConvention(type) - }; - } - - /// Get an equivalent Harmony 2.x type using the convention expected for most types. - /// The Harmony 1.x type. - private Type GetMappedTypeByConvention(TypeReference type) - { - string fullName = type.FullName.Replace("Harmony.", "HarmonyLib."); - string targetName = typeof(Harmony).AssemblyQualifiedName!.Replace(typeof(Harmony).FullName!, fullName); - return Type.GetType(targetName, throwOnError: true); - } - } -} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs new file mode 100644 index 00000000..38ab3d80 --- /dev/null +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HarmonyRewriter.cs @@ -0,0 +1,143 @@ +using System; +using HarmonyLib; +using Mono.Cecil; +using Mono.Cecil.Cil; +using StardewModdingAPI.Framework.ModLoading.Framework; +using StardewModdingAPI.Framework.ModLoading.RewriteFacades; + +namespace StardewModdingAPI.Framework.ModLoading.Rewriters +{ + /// Detects Harmony references, and rewrites Harmony 1.x assembly references to work with Harmony 2.x. + internal class HarmonyRewriter : BaseInstructionHandler + { + /********* + ** Fields + *********/ + /// Whether any Harmony 1.x types were replaced. + private bool ReplacedTypes; + + /// Whether to rewrite Harmony 1.x code. + private readonly bool ShouldRewrite; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public HarmonyRewriter(bool shouldRewrite = true) + : base(defaultPhrase: "Harmony 1.x") + { + this.ShouldRewrite = shouldRewrite; + } + + /// + public override bool Handle(ModuleDefinition module, TypeReference type, Action replaceWith) + { + // detect Harmony + if (type.Scope is not AssemblyNameReference { Name: "0Harmony" } scope) + return false; + + // rewrite Harmony 1.x type to Harmony 2.0 type + if (this.ShouldRewrite && scope.Version.Major == 1) + { + Type targetType = this.GetMappedType(type); + replaceWith(module.ImportReference(targetType)); + this.OnChanged(); + this.ReplacedTypes = true; + return true; + } + + this.MarkFlag(InstructionHandleResult.DetectedGamePatch); + return false; + } + + /// + public override bool Handle(ModuleDefinition module, ILProcessor cil, Instruction instruction) + { + if (this.ShouldRewrite) + { + // rewrite Harmony 1.x methods to Harmony 2.0 + MethodReference methodRef = RewriteHelper.AsMethodReference(instruction); + if (this.TryRewriteMethodsToFacade(module, methodRef)) + { + this.OnChanged(); + return true; + } + + // rewrite renamed fields + FieldReference fieldRef = RewriteHelper.AsFieldReference(instruction); + if (fieldRef != null) + { + if (fieldRef.DeclaringType.FullName == "HarmonyLib.HarmonyMethod" && fieldRef.Name == "prioritiy") + { + fieldRef.Name = nameof(HarmonyMethod.priority); + this.OnChanged(); + } + } + } + + return false; + } + + + /********* + ** Private methods + *********/ + /// Update the mod metadata when any Harmony 1.x code is migrated. + private void OnChanged() + { + this.MarkRewritten(); + this.MarkFlag(InstructionHandleResult.DetectedGamePatch); + } + + /// 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) + { + if (!this.ReplacedTypes) + return false; // not Harmony (or already using Harmony 2.0) + + // get facade type + Type toType = methodRef?.DeclaringType.FullName switch + { + "HarmonyLib.Harmony" => typeof(HarmonyInstanceFacade), + "HarmonyLib.AccessTools" => typeof(AccessToolsFacade), + "HarmonyLib.HarmonyMethod" => typeof(HarmonyMethodFacade), + _ => null + }; + if (toType == null) + return false; + + // map if there's a matching method + if (RewriteHelper.HasMatchingSignature(toType, methodRef)) + { + methodRef.DeclaringType = module.ImportReference(toType); + return true; + } + + return false; + } + + /// Get an equivalent Harmony 2.x type. + /// The Harmony 1.x type. + private Type GetMappedType(TypeReference type) + { + return type.FullName switch + { + "Harmony.HarmonyInstance" => typeof(Harmony), + "Harmony.ILCopying.ExceptionBlock" => typeof(ExceptionBlock), + _ => this.GetMappedTypeByConvention(type) + }; + } + + /// Get an equivalent Harmony 2.x type using the convention expected for most types. + /// The Harmony 1.x type. + private Type GetMappedTypeByConvention(TypeReference type) + { + string fullName = type.FullName.Replace("Harmony.", "HarmonyLib."); + string targetName = typeof(Harmony).AssemblyQualifiedName!.Replace(typeof(Harmony).FullName!, fullName); + return Type.GetType(targetName, throwOnError: true); + } + } +} diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs index f59a6ab1..57f1dd17 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicFieldRewriter.cs @@ -13,7 +13,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters ** Fields *********/ /// The assembly names to which to rewrite broken references. - private readonly HashSet RewriteReferencesToAssemblies; + private readonly ISet RewriteReferencesToAssemblies; /********* @@ -21,10 +21,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters *********/ /// Construct an instance. /// The assembly names to which to rewrite broken references. - public HeuristicFieldRewriter(string[] rewriteReferencesToAssemblies) + public HeuristicFieldRewriter(ISet rewriteReferencesToAssemblies) : base(defaultPhrase: "field changed to property") // ignored since we specify phrases { - this.RewriteReferencesToAssemblies = new HashSet(rewriteReferencesToAssemblies); + this.RewriteReferencesToAssemblies = rewriteReferencesToAssemblies; } /// diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs index e133b6fa..89de437e 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/HeuristicMethodRewriter.cs @@ -13,7 +13,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters ** Fields *********/ /// The assembly names to which to rewrite broken references. - private readonly HashSet RewriteReferencesToAssemblies; + private readonly ISet RewriteReferencesToAssemblies; /********* @@ -21,10 +21,10 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters *********/ /// Construct an instance. /// The assembly names to which to rewrite broken references. - public HeuristicMethodRewriter(string[] rewriteReferencesToAssemblies) + public HeuristicMethodRewriter(ISet rewriteReferencesToAssemblies) : base(defaultPhrase: "methods with missing parameters") // ignored since we specify phrases { - this.RewriteReferencesToAssemblies = new HashSet(rewriteReferencesToAssemblies); + this.RewriteReferencesToAssemblies = rewriteReferencesToAssemblies; } /// -- cgit