diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-20 19:38:08 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-05-20 19:38:08 -0400 |
commit | 310eb1fe9afdb820d5584a46200bf073fc00ccb7 (patch) | |
tree | 72c16c6d4f8447f45e8e6612cb25cd255d2b308c /src/SMAPI/Patches/ObjectErrorPatch.cs | |
parent | aa5cc2c9be8bdc79c6fa7b1b9c2581a05b88117d (diff) | |
parent | c5c30189e43f93c3f3c66207945187a974656c9e (diff) | |
download | SMAPI-310eb1fe9afdb820d5584a46200bf073fc00ccb7.tar.gz SMAPI-310eb1fe9afdb820d5584a46200bf073fc00ccb7.tar.bz2 SMAPI-310eb1fe9afdb820d5584a46200bf073fc00ccb7.zip |
Merge branch 'mod/harmony-2.0' into develop
# Conflicts:
# docs/release-notes.md
# src/SMAPI/Framework/ModLoading/AssemblyLoader.cs
Diffstat (limited to 'src/SMAPI/Patches/ObjectErrorPatch.cs')
-rw-r--r-- | src/SMAPI/Patches/ObjectErrorPatch.cs | 43 |
1 files changed, 13 insertions, 30 deletions
diff --git a/src/SMAPI/Patches/ObjectErrorPatch.cs b/src/SMAPI/Patches/ObjectErrorPatch.cs index d3b8800a..189a14a0 100644 --- a/src/SMAPI/Patches/ObjectErrorPatch.cs +++ b/src/SMAPI/Patches/ObjectErrorPatch.cs @@ -1,7 +1,7 @@ +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Reflection; -using Harmony; +using HarmonyLib; using StardewModdingAPI.Framework.Patching; using StardewValley; using StardewValley.Menus; @@ -27,7 +27,7 @@ namespace StardewModdingAPI.Patches *********/ /// <summary>Apply the Harmony patch.</summary> /// <param name="harmony">The Harmony instance.</param> - public void Apply(HarmonyInstance harmony) + public void Apply(Harmony harmony) { // object.getDescription harmony.Patch( @@ -38,7 +38,7 @@ namespace StardewModdingAPI.Patches // object.getDisplayName harmony.Patch( original: AccessTools.Method(typeof(SObject), "loadDisplayName"), - prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Before_Object_loadDisplayName)) + finalizer: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Finalize_Object_loadDisplayName)) ); // IClickableMenu.drawToolTip @@ -68,42 +68,25 @@ namespace StardewModdingAPI.Patches return true; } - /// <summary>The method to call instead of <see cref="StardewValley.Object.loadDisplayName"/>.</summary> - /// <param name="__instance">The instance being patched.</param> + /// <summary>The method to call after <see cref="StardewValley.Object.loadDisplayName"/>.</summary> /// <param name="__result">The patched method's return value.</param> - /// <param name="__originalMethod">The method being wrapped.</param> - /// <returns>Returns whether to execute the original method.</returns> - private static bool Before_Object_loadDisplayName(SObject __instance, ref string __result, MethodInfo __originalMethod) + /// <param name="__exception">The exception thrown by the wrapped method, if any.</param> + /// <returns>Returns the exception to throw, if any.</returns> + private static Exception Finalize_Object_loadDisplayName(ref string __result, Exception __exception) { - const string key = nameof(Before_Object_loadDisplayName); - if (!PatchHelper.StartIntercept(key)) - return true; - - try - { - __result = (string)__originalMethod.Invoke(__instance, new object[0]); - return false; - } - catch (TargetInvocationException ex) when (ex.InnerException is KeyNotFoundException) + if (__exception is KeyNotFoundException) { __result = "???"; - return false; - } - catch - { - return true; - } - finally - { - PatchHelper.StopIntercept(key); + return null; } + + return __exception; } /// <summary>The method to call instead of <see cref="IClickableMenu.drawToolTip"/>.</summary> - /// <param name="__instance">The instance being patched.</param> /// <param name="hoveredItem">The item for which to draw a tooltip.</param> /// <returns>Returns whether to execute the original method.</returns> - private static bool Before_IClickableMenu_DrawTooltip(IClickableMenu __instance, Item hoveredItem) + private static bool Before_IClickableMenu_DrawTooltip(Item hoveredItem) { // invalid edible item cause crash when drawing tooltips if (hoveredItem is SObject obj && obj.Edibility != -300 && !Game1.objectInformation.ContainsKey(obj.ParentSheetIndex)) |