summaryrefslogtreecommitdiff
path: root/src/SMAPI/Patches
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI/Patches')
-rw-r--r--src/SMAPI/Patches/DialogueErrorPatch.cs67
-rw-r--r--src/SMAPI/Patches/EventErrorPatch.cs33
-rw-r--r--src/SMAPI/Patches/LoadContextPatch.cs8
-rw-r--r--src/SMAPI/Patches/LoadErrorPatch.cs8
-rw-r--r--src/SMAPI/Patches/ObjectErrorPatch.cs37
-rw-r--r--src/SMAPI/Patches/ScheduleErrorPatch.cs37
6 files changed, 181 insertions, 9 deletions
diff --git a/src/SMAPI/Patches/DialogueErrorPatch.cs b/src/SMAPI/Patches/DialogueErrorPatch.cs
index 1e49826d..8043eda3 100644
--- a/src/SMAPI/Patches/DialogueErrorPatch.cs
+++ b/src/SMAPI/Patches/DialogueErrorPatch.cs
@@ -1,11 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
-using System.Reflection;
-using Harmony;
using StardewModdingAPI.Framework.Patching;
using StardewModdingAPI.Framework.Reflection;
using StardewValley;
+#if HARMONY_2
+using HarmonyLib;
+using StardewModdingAPI.Framework;
+#else
+using System.Reflection;
+using Harmony;
+#endif
namespace StardewModdingAPI.Patches
{
@@ -47,6 +52,19 @@ namespace StardewModdingAPI.Patches
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+ {
+ harmony.Patch(
+ original: AccessTools.Constructor(typeof(Dialogue), new[] { typeof(string), typeof(NPC) }),
+ finalizer: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Finalize_Dialogue_Constructor))
+ );
+ harmony.Patch(
+ original: AccessTools.Property(typeof(NPC), nameof(NPC.CurrentDialogue)).GetMethod,
+ finalizer: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Finalize_NPC_CurrentDialogue))
+ );
+ }
+#else
public void Apply(HarmonyInstance harmony)
{
harmony.Patch(
@@ -58,11 +76,53 @@ namespace StardewModdingAPI.Patches
prefix: new HarmonyMethod(this.GetType(), nameof(DialogueErrorPatch.Before_NPC_CurrentDialogue))
);
}
-
+#endif
/*********
** Private methods
*********/
+#if HARMONY_2
+ /// <summary>The method to call after the Dialogue constructor.</summary>
+ /// <param name="__instance">The instance being patched.</param>
+ /// <param name="masterDialogue">The dialogue being parsed.</param>
+ /// <param name="speaker">The NPC for which the dialogue is being parsed.</param>
+ /// <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_Dialogue_Constructor(Dialogue __instance, string masterDialogue, NPC speaker, Exception __exception)
+ {
+ if (__exception != null)
+ {
+ // log message
+ string name = !string.IsNullOrWhiteSpace(speaker?.Name) ? speaker.Name : null;
+ DialogueErrorPatch.MonitorForGame.Log($"Failed parsing dialogue string{(name != null ? $" for {name}" : "")}:\n{masterDialogue}\n{__exception.GetLogSummary()}", LogLevel.Error);
+
+ // set default dialogue
+ IReflectedMethod parseDialogueString = DialogueErrorPatch.Reflection.GetMethod(__instance, "parseDialogueString");
+ IReflectedMethod checkForSpecialDialogueAttributes = DialogueErrorPatch.Reflection.GetMethod(__instance, "checkForSpecialDialogueAttributes");
+ parseDialogueString.Invoke("...");
+ checkForSpecialDialogueAttributes.Invoke();
+ }
+
+ return null;
+ }
+
+ /// <summary>The method to call after <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="__exception">The exception thrown by the wrapped method, if any.</param>
+ /// <returns>Returns the exception to throw, if any.</returns>
+ private static Exception Finalize_NPC_CurrentDialogue(NPC __instance, ref Stack<Dialogue> __result, Exception __exception)
+ {
+ if (__exception == null)
+ return null;
+
+ DialogueErrorPatch.MonitorForGame.Log($"Failed loading current dialogue for NPC {__instance.Name}:\n{__exception.GetLogSummary()}", LogLevel.Error);
+ __result = new Stack<Dialogue>();
+
+ return null;
+ }
+#else
+
/// <summary>The method to call instead of the Dialogue constructor.</summary>
/// <param name="__instance">The instance being patched.</param>
/// <param name="masterDialogue">The dialogue being parsed.</param>
@@ -129,5 +189,6 @@ namespace StardewModdingAPI.Patches
PatchHelper.StopIntercept(key);
}
}
+#endif
}
}
diff --git a/src/SMAPI/Patches/EventErrorPatch.cs b/src/SMAPI/Patches/EventErrorPatch.cs
index 504d1d2e..4dbb25f3 100644
--- a/src/SMAPI/Patches/EventErrorPatch.cs
+++ b/src/SMAPI/Patches/EventErrorPatch.cs
@@ -1,6 +1,11 @@
using System.Diagnostics.CodeAnalysis;
+#if HARMONY_2
+using System;
+using HarmonyLib;
+#else
using System.Reflection;
using Harmony;
+#endif
using StardewModdingAPI.Framework.Patching;
using StardewValley;
@@ -38,6 +43,15 @@ namespace StardewModdingAPI.Patches
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+ {
+ harmony.Patch(
+ original: AccessTools.Method(typeof(GameLocation), "checkEventPrecondition"),
+ finalizer: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Finalize_GameLocation_CheckEventPrecondition))
+ );
+ }
+#else
public void Apply(HarmonyInstance harmony)
{
harmony.Patch(
@@ -45,11 +59,29 @@ namespace StardewModdingAPI.Patches
prefix: new HarmonyMethod(this.GetType(), nameof(EventErrorPatch.Before_GameLocation_CheckEventPrecondition))
);
}
+#endif
/*********
** Private methods
*********/
+#if HARMONY_2
+ /// <summary>The method to call instead of the GameLocation.CheckEventPrecondition.</summary>
+ /// <param name="__result">The return value of the original method.</param>
+ /// <param name="precondition">The precondition to be parsed.</param>
+ /// <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_GameLocation_CheckEventPrecondition(ref int __result, string precondition, Exception __exception)
+ {
+ if (__exception != null)
+ {
+ __result = -1;
+ EventErrorPatch.MonitorForGame.Log($"Failed parsing event precondition ({precondition}):\n{__exception.InnerException}", LogLevel.Error);
+ }
+
+ return null;
+ }
+#else
/// <summary>The method to call instead of the GameLocation.CheckEventPrecondition.</summary>
/// <param name="__instance">The instance being patched.</param>
/// <param name="__result">The return value of the original method.</param>
@@ -78,5 +110,6 @@ namespace StardewModdingAPI.Patches
PatchHelper.StopIntercept(key);
}
}
+#endif
}
}
diff --git a/src/SMAPI/Patches/LoadContextPatch.cs b/src/SMAPI/Patches/LoadContextPatch.cs
index 0cc8c8eb..768ddd6b 100644
--- a/src/SMAPI/Patches/LoadContextPatch.cs
+++ b/src/SMAPI/Patches/LoadContextPatch.cs
@@ -1,6 +1,10 @@
using System;
using System.Diagnostics.CodeAnalysis;
+#if HARMONY_2
+using HarmonyLib;
+#else
using Harmony;
+#endif
using StardewModdingAPI.Enums;
using StardewModdingAPI.Framework.Patching;
using StardewModdingAPI.Framework.Reflection;
@@ -47,7 +51,11 @@ namespace StardewModdingAPI.Patches
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+#else
public void Apply(HarmonyInstance harmony)
+#endif
{
// detect CreatedBasicInfo
harmony.Patch(
diff --git a/src/SMAPI/Patches/LoadErrorPatch.cs b/src/SMAPI/Patches/LoadErrorPatch.cs
index 77415ff2..5e67b169 100644
--- a/src/SMAPI/Patches/LoadErrorPatch.cs
+++ b/src/SMAPI/Patches/LoadErrorPatch.cs
@@ -2,7 +2,11 @@ using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
+#if HARMONY_2
+using HarmonyLib;
+#else
using Harmony;
+#endif
using StardewModdingAPI.Framework.Exceptions;
using StardewModdingAPI.Framework.Patching;
using StardewValley;
@@ -49,7 +53,11 @@ namespace StardewModdingAPI.Patches
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+#else
public void Apply(HarmonyInstance harmony)
+#endif
{
harmony.Patch(
original: AccessTools.Method(typeof(SaveGame), nameof(SaveGame.loadDataToLocations)),
diff --git a/src/SMAPI/Patches/ObjectErrorPatch.cs b/src/SMAPI/Patches/ObjectErrorPatch.cs
index d3b8800a..4edcc64e 100644
--- a/src/SMAPI/Patches/ObjectErrorPatch.cs
+++ b/src/SMAPI/Patches/ObjectErrorPatch.cs
@@ -1,11 +1,16 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
-using System.Reflection;
-using Harmony;
using StardewModdingAPI.Framework.Patching;
using StardewValley;
using StardewValley.Menus;
using SObject = StardewValley.Object;
+#if HARMONY_2
+using System;
+using HarmonyLib;
+#else
+using System.Reflection;
+using Harmony;
+#endif
namespace StardewModdingAPI.Patches
{
@@ -27,7 +32,11 @@ namespace StardewModdingAPI.Patches
*********/
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+#else
public void Apply(HarmonyInstance harmony)
+#endif
{
// object.getDescription
harmony.Patch(
@@ -38,7 +47,11 @@ namespace StardewModdingAPI.Patches
// object.getDisplayName
harmony.Patch(
original: AccessTools.Method(typeof(SObject), "loadDisplayName"),
+#if HARMONY_2
+ finalizer: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Finalize_Object_loadDisplayName))
+#else
prefix: new HarmonyMethod(this.GetType(), nameof(ObjectErrorPatch.Before_Object_loadDisplayName))
+#endif
);
// IClickableMenu.drawToolTip
@@ -68,6 +81,22 @@ namespace StardewModdingAPI.Patches
return true;
}
+#if HARMONY_2
+ /// <summary>The method to call after <see cref="StardewValley.Object.loadDisplayName"/>.</summary>
+ /// <param name="__result">The patched method's return value.</param>
+ /// <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)
+ {
+ if (__exception is KeyNotFoundException)
+ {
+ __result = "???";
+ return null;
+ }
+
+ return __exception;
+ }
+#else
/// <summary>The method to call instead of <see cref="StardewValley.Object.loadDisplayName"/>.</summary>
/// <param name="__instance">The instance being patched.</param>
/// <param name="__result">The patched method's return value.</param>
@@ -98,12 +127,12 @@ namespace StardewModdingAPI.Patches
PatchHelper.StopIntercept(key);
}
}
+#endif
/// <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))
diff --git a/src/SMAPI/Patches/ScheduleErrorPatch.cs b/src/SMAPI/Patches/ScheduleErrorPatch.cs
index 799fcb40..cc2238b0 100644
--- a/src/SMAPI/Patches/ScheduleErrorPatch.cs
+++ b/src/SMAPI/Patches/ScheduleErrorPatch.cs
@@ -1,9 +1,15 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
-using System.Reflection;
-using Harmony;
using StardewModdingAPI.Framework.Patching;
using StardewValley;
+#if HARMONY_2
+using System;
+using HarmonyLib;
+using StardewModdingAPI.Framework;
+#else
+using System.Reflection;
+using Harmony;
+#endif
namespace StardewModdingAPI.Patches
{
@@ -39,11 +45,19 @@ namespace StardewModdingAPI.Patches
/// <summary>Apply the Harmony patch.</summary>
/// <param name="harmony">The Harmony instance.</param>
+#if HARMONY_2
+ public void Apply(Harmony harmony)
+#else
public void Apply(HarmonyInstance harmony)
+#endif
{
harmony.Patch(
original: AccessTools.Method(typeof(NPC), "parseMasterSchedule"),
+#if HARMONY_2
+ finalizer: new HarmonyMethod(this.GetType(), nameof(ScheduleErrorPatch.Finalize_NPC_parseMasterSchedule))
+#else
prefix: new HarmonyMethod(this.GetType(), nameof(ScheduleErrorPatch.Before_NPC_parseMasterSchedule))
+#endif
);
}
@@ -51,6 +65,24 @@ namespace StardewModdingAPI.Patches
/*********
** Private methods
*********/
+#if HARMONY_2
+ /// <summary>The method to call instead of <see cref="NPC.parseMasterSchedule"/>.</summary>
+ /// <param name="rawData">The raw schedule data to parse.</param>
+ /// <param name="__instance">The instance being patched.</param>
+ /// <param name="__result">The patched method's return value.</param>
+ /// <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_NPC_parseMasterSchedule(string rawData, NPC __instance, ref Dictionary<int, SchedulePathDescription> __result, Exception __exception)
+ {
+ if (__exception != null)
+ {
+ ScheduleErrorPatch.MonitorForGame.Log($"Failed parsing schedule for NPC {__instance.Name}:\n{rawData}\n{__exception.GetLogSummary()}", LogLevel.Error);
+ __result = new Dictionary<int, SchedulePathDescription>();
+ }
+
+ return null;
+ }
+#else
/// <summary>The method to call instead of <see cref="NPC.parseMasterSchedule"/>.</summary>
/// <param name="rawData">The raw schedule data to parse.</param>
/// <param name="__instance">The instance being patched.</param>
@@ -79,5 +111,6 @@ namespace StardewModdingAPI.Patches
PatchHelper.StopIntercept(key);
}
}
+#endif
}
}