diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-26 22:28:32 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-26 22:28:32 -0400 |
commit | bdae52c9ae76303fd8082b10763b9ab7660fbd35 (patch) | |
tree | 7eea76d55249cb49d2df572da150a961040ecdb3 /src | |
parent | 175eaad68373185b010410c5e2de1af9afbba1f5 (diff) | |
download | SMAPI-bdae52c9ae76303fd8082b10763b9ab7660fbd35.tar.gz SMAPI-bdae52c9ae76303fd8082b10763b9ab7660fbd35.tar.bz2 SMAPI-bdae52c9ae76303fd8082b10763b9ab7660fbd35.zip |
fix rewriting for Harmony ExceptionBlock type (#711)
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs index 723983be..3197c151 100644 --- a/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs +++ b/src/SMAPI/Framework/ModLoading/Rewriters/Harmony1AssemblyRewriter.cs @@ -28,7 +28,7 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters public override bool Handle(ModuleDefinition module, TypeReference type, Action<TypeReference> replaceWith) { // rewrite Harmony 1.x type to Harmony 2.0 type - if (type.Scope is AssemblyNameReference scope && scope.Name == "0Harmony" && scope.Version.Major == 1) + if (type.Scope is AssemblyNameReference { Name: "0Harmony" } scope && scope.Version.Major == 1) { Type targetType = this.GetMappedType(type); replaceWith(module.ImportReference(targetType)); @@ -72,24 +72,15 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters return false; // not Harmony (or already using Harmony 2.0) // get facade type - Type toType; - switch (methodRef?.DeclaringType.FullName) + Type toType = methodRef?.DeclaringType.FullName switch { - case "HarmonyLib.Harmony": - toType = typeof(HarmonyInstanceFacade); - break; - - case "HarmonyLib.AccessTools": - toType = typeof(AccessToolsFacade); - break; - - case "HarmonyLib.HarmonyMethod": - toType = typeof(HarmonyMethodFacade); - break; - - default: - return false; - } + "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)) @@ -102,16 +93,23 @@ namespace StardewModdingAPI.Framework.ModLoading.Rewriters } /// <summary>Get an equivalent Harmony 2.x type.</summary> - /// <param name="type">The Harmony 1.x method.</param> + /// <param name="type">The Harmony 1.x type.</param> private Type GetMappedType(TypeReference type) { - // main Harmony object - if (type.FullName == "Harmony.HarmonyInstance") - return typeof(Harmony); + return type.FullName switch + { + "Harmony.HarmonyInstance" => typeof(Harmony), + "Harmony.ILCopying.ExceptionBlock" => typeof(ExceptionBlock), + _ => this.GetMappedTypeByConvention(type) + }; + } - // other objects + /// <summary>Get an equivalent Harmony 2.x type using the convention expected for most types.</summary> + /// <param name="type">The Harmony 1.x type.</param> + private Type GetMappedTypeByConvention(TypeReference type) + { string fullName = type.FullName.Replace("Harmony.", "HarmonyLib."); - string targetName = typeof(Harmony).AssemblyQualifiedName.Replace(typeof(Harmony).FullName, fullName); + string targetName = typeof(Harmony).AssemblyQualifiedName!.Replace(typeof(Harmony).FullName!, fullName); return Type.GetType(targetName, throwOnError: true); } } |