prevent mods from crashing the game with invalid dialogue in more cases

This commit is contained in:
Jesse Plamondon-Willard 2019-07-12 20:01:38 -04:00
parent b02c5459e1
commit 6285c79548
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
5 changed files with 50 additions and 12 deletions

View File

@ -5,6 +5,7 @@ These changes have not been released yet.
* For players:
* SMAPI now prevents invalid items from breaking menus on hover.
* SMAPI now prevents invalid event preconditions from crashing the game (thanks to berkayylmao!).
* SMAPI now prevents more invalid dialogues from crashing the game.
* Fixed errors during early startup not shown before exit.
* For the web UI:

View File

@ -21,6 +21,9 @@ namespace StardewModdingAPI.Patches
/// <summary>Simplifies access to private code.</summary>
private static Reflector Reflection;
/// <summary>Whether the <see cref="NPC.CurrentDialogue"/> getter is currently being intercepted.</summary>
private static bool IsInterceptingCurrentDialogue;
/*********
** Accessors
@ -48,7 +51,11 @@ namespace StardewModdingAPI.Patches
{
harmony.Patch(
original: AccessTools.Constructor(typeof(Dialogue), new[] { typeof(string), typeof(NPC) }),
prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Prefix))
prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Before_Dialogue_Constructor))
);
harmony.Patch(
original: AccessTools.Property(typeof(NPC), nameof(NPC.CurrentDialogue)).GetMethod,
prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Before_NPC_CurrentDialogue))
);
}
@ -63,7 +70,7 @@ namespace StardewModdingAPI.Patches
/// <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 Prefix(Dialogue __instance, string masterDialogue, NPC speaker)
private static bool Before_Dialogue_Constructor(Dialogue __instance, string masterDialogue, NPC speaker)
{
// get private members
bool nameArraysTranslated = DialogueErrorPatch.Reflection.GetField<bool>(typeof(Dialogue), "nameArraysTranslated").GetValue();
@ -96,5 +103,35 @@ namespace StardewModdingAPI.Patches
return false;
}
/// <summary>The method to call instead of <see cref="NPC.CurrentDialogue"/>.</summary>
/// <param name="__instance">The instance being patched.</param>
/// <param name="__result">The return value of the original method.</param>
/// <param name="__originalMethod">The method being wrapped.</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 Before_NPC_CurrentDialogue(NPC __instance, ref Stack<Dialogue> __result, MethodInfo __originalMethod)
{
if (DialogueErrorPatch.IsInterceptingCurrentDialogue)
return true;
try
{
DialogueErrorPatch.IsInterceptingCurrentDialogue = true;
__result = (Stack<Dialogue>)__originalMethod.Invoke(__instance, new object[0]);
return false;
}
catch (TargetInvocationException ex)
{
DialogueErrorPatch.MonitorForGame.Log($"Failed loading current dialogue for NPC {__instance.Name}:\n{ex.InnerException ?? ex}", LogLevel.Error);
__result = new Stack<Dialogue>();
return false;
}
finally
{
DialogueErrorPatch.IsInterceptingCurrentDialogue = false;
}
}
}
}

View File

@ -42,7 +42,7 @@ namespace StardewModdingAPI.Patches
{
harmony.Patch(
original: AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"),
prefix: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Prefix))
prefix: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Before_GameLocation_CheckEventPrecondition))
);
}
@ -58,7 +58,7 @@ namespace StardewModdingAPI.Patches
/// <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 Prefix(GameLocation __instance, ref int __result, string precondition, MethodInfo __originalMethod)
private static bool Before_GameLocation_CheckEventPrecondition(GameLocation __instance, ref int __result, string precondition, MethodInfo __originalMethod)
{
if (EventErrorPatch.IsIntercepted)
return true;

View File

@ -55,8 +55,8 @@ namespace StardewModdingAPI.Patches
{
harmony.Patch(
original: AccessTools.Method(typeof(Game1), nameof(Game1.loadForNewGame)),
prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Prefix)),
postfix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Postfix))
prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Before_Game1_LoadForNewGame)),
postfix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.After_Game1_LoadForNewGame))
);
}
@ -67,7 +67,7 @@ namespace StardewModdingAPI.Patches
/// <summary>The method to call instead of <see cref="Game1.loadForNewGame"/>.</summary>
/// <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>
private static bool Prefix()
private static bool Before_Game1_LoadForNewGame()
{
LoadContextPatch.IsCreating = Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField<bool>(menu, "transitioningCharacterCreationMenu").GetValue();
LoadContextPatch.TimesLocationsCleared = 0;
@ -83,7 +83,7 @@ namespace StardewModdingAPI.Patches
/// <summary>The method to call instead after <see cref="Game1.loadForNewGame"/>.</summary>
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
private static void Postfix()
private static void After_Game1_LoadForNewGame()
{
if (LoadContextPatch.IsCreating)
{

View File

@ -27,13 +27,13 @@ namespace StardewModdingAPI.Patches
// object.getDescription
harmony.Patch(
original: AccessTools.Method(typeof(SObject), nameof(SObject.getDescription)),
prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Object_GetDescription_Prefix))
prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Before_Object_GetDescription))
);
// IClickableMenu.drawToolTip
harmony.Patch(
original: AccessTools.Method(typeof(IClickableMenu), nameof(IClickableMenu.drawToolTip)),
prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.IClickableMenu_DrawTooltip_Prefix))
prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Before_IClickableMenu_DrawTooltip))
);
}
@ -47,7 +47,7 @@ namespace StardewModdingAPI.Patches
/// <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 Object_GetDescription_Prefix(SObject __instance, ref string __result)
private static bool Before_Object_GetDescription(SObject __instance, ref string __result)
{
// invalid bigcraftables crash instead of showing '???' like invalid non-bigcraftables
if (!__instance.IsRecipe && __instance.bigCraftable.Value && !Game1.bigCraftablesInformation.ContainsKey(__instance.ParentSheetIndex))
@ -65,7 +65,7 @@ namespace StardewModdingAPI.Patches
/// <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)
private static bool Before_IClickableMenu_DrawTooltip(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))