prevent invalid items from breaking menus on hover

This commit is contained in:
Jesse Plamondon-Willard 2019-03-08 21:49:35 -05:00
parent 448cf30e1b
commit 99e64eeeb2
No known key found for this signature in database
GPG Key ID: 7D7C8097B62033CE
2 changed files with 28 additions and 5 deletions

View File

@ -3,6 +3,7 @@
These changes have not been released yet. These changes have not been released yet.
* For players: * For players:
* SMAPI now prevents invalid items from breaking menus on hover.
* Fixed Save Backup not pruning old backups if they're uncompressed. * Fixed Save Backup not pruning old backups if they're uncompressed.
* Fixed issues when a farmhand reconnects before the game notices they're disconnected. * Fixed issues when a farmhand reconnects before the game notices they're disconnected.
* Fixed 'received message' logs shown in non-developer mode. * Fixed 'received message' logs shown in non-developer mode.

View File

@ -1,8 +1,8 @@
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Harmony; using Harmony;
using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Patching;
using StardewValley; using StardewValley;
using StardewValley.Menus;
using SObject = StardewValley.Object; using SObject = StardewValley.Object;
namespace StardewModdingAPI.Patches namespace StardewModdingAPI.Patches
@ -24,10 +24,17 @@ namespace StardewModdingAPI.Patches
/// <param name="harmony">The Harmony instance.</param> /// <param name="harmony">The Harmony instance.</param>
public void Apply(HarmonyInstance harmony) public void Apply(HarmonyInstance harmony)
{ {
MethodInfo method = AccessTools.Method(typeof(SObject), nameof(SObject.getDescription)); // object.getDescription
MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(ObjectErrorPatch.Prefix)); harmony.Patch(
original: AccessTools.Method(typeof(SObject), nameof(SObject.getDescription)),
prefix: new HarmonyMethod(AccessTools.Method(this.GetType(), nameof(ObjectErrorPatch.Object_GetDescription_Prefix)))
);
harmony.Patch(method, new HarmonyMethod(prefix), null); // IClickableMenu.drawToolTip
harmony.Patch(
original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.drawToolTip)),
prefix: new HarmonyMethod(AccessTools.Method(this.GetType(), nameof(ObjectErrorPatch.IClickableMenu_DrawTooltip_Prefix)))
);
} }
@ -40,7 +47,7 @@ namespace StardewModdingAPI.Patches
/// <returns>Returns whether to execute the original method.</returns> /// <returns>Returns whether to execute the original method.</returns>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks> /// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")] [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
private static bool Prefix(SObject __instance, ref string __result) private static bool Object_GetDescription_Prefix(SObject __instance, ref string __result)
{ {
// invalid bigcraftables crash instead of showing '???' like invalid non-bigcraftables // invalid bigcraftables crash instead of showing '???' like invalid non-bigcraftables
if (!__instance.IsRecipe && __instance.bigCraftable.Value && !Game1.bigCraftablesInformation.ContainsKey(__instance.ParentSheetIndex)) if (!__instance.IsRecipe && __instance.bigCraftable.Value && !Game1.bigCraftablesInformation.ContainsKey(__instance.ParentSheetIndex))
@ -51,5 +58,20 @@ namespace StardewModdingAPI.Patches
return true; return true;
} }
/// <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>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
[SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony.")]
private static bool IClickableMenu_DrawTooltip_Prefix(IClickableMenu __instance, Item hoveredItem)
{
// invalid edible item cause crash when drawing tooltips
if (hoveredItem is SObject obj && obj.Edibility != -300 && !Game1.objectInformation.ContainsKey(obj.ParentSheetIndex))
return false;
return true;
}
} }
} }