summaryrefslogtreecommitdiff
path: root/src/SMAPI.Mods.ErrorHandler
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-07-28 00:49:54 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-07-28 00:49:54 -0400
commit6a6c484b9867524d2eaa617da4dde36fec8c3110 (patch)
treef8949615d01350874d5c09a095db9596ae5a6bca /src/SMAPI.Mods.ErrorHandler
parentb2a1a51dea572163993d496d7c1da569739dfa0e (diff)
downloadSMAPI-6a6c484b9867524d2eaa617da4dde36fec8c3110.tar.gz
SMAPI-6a6c484b9867524d2eaa617da4dde36fec8c3110.tar.bz2
SMAPI-6a6c484b9867524d2eaa617da4dde36fec8c3110.zip
add accessed key to dictionary KeyNotFoundException message
Diffstat (limited to 'src/SMAPI.Mods.ErrorHandler')
-rw-r--r--src/SMAPI.Mods.ErrorHandler/ModEntry.cs1
-rw-r--r--src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatches.cs81
-rw-r--r--src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj1
3 files changed, 83 insertions, 0 deletions
diff --git a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
index d9426d75..719eddab 100644
--- a/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
+++ b/src/SMAPI.Mods.ErrorHandler/ModEntry.cs
@@ -31,6 +31,7 @@ namespace StardewModdingAPI.Mods.ErrorHandler
// apply patches
new GamePatcher(this.Monitor).Apply(
new DialogueErrorPatch(monitorForGame, this.Helper.Reflection),
+ new DictionaryPatches(this.Helper.Reflection),
new EventPatches(monitorForGame),
new GameLocationPatches(monitorForGame),
new ObjectErrorPatch(),
diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatches.cs b/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatches.cs
new file mode 100644
index 00000000..ce88999d
--- /dev/null
+++ b/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatches.cs
@@ -0,0 +1,81 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using HarmonyLib;
+using StardewModdingAPI.Framework.Patching;
+using StardewValley.GameData;
+using StardewValley.GameData.HomeRenovations;
+using StardewValley.GameData.Movies;
+
+namespace StardewModdingAPI.Mods.ErrorHandler.Patches
+{
+ /// <summary>A Harmony patch for <see cref="Dictionary{TKey,TValue}"/> which adds the accessed key to <see cref="KeyNotFoundException"/> exceptions.</summary>
+ /// <remarks>Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.</remarks>
+ [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
+ internal class DictionaryPatches : IHarmonyPatch
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>Simplifies access to private code.</summary>
+ private static IReflectionHelper Reflection;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="reflector">Simplifies access to private code.</param>
+ public DictionaryPatches(IReflectionHelper reflector)
+ {
+ DictionaryPatches.Reflection = reflector;
+ }
+
+ /// <inheritdoc />
+ public void Apply(Harmony harmony)
+ {
+ Type[] keyTypes = { typeof(int), typeof(string) };
+ Type[] valueTypes = { typeof(int), typeof(string), typeof(HomeRenovation), typeof(MovieData), typeof(SpecialOrderData) };
+
+ foreach (Type keyType in keyTypes)
+ {
+ foreach (Type valueType in valueTypes)
+ {
+ Type dictionaryType = typeof(Dictionary<,>).MakeGenericType(keyType, valueType);
+
+ harmony.Patch(
+ original: AccessTools.Method(dictionaryType, "get_Item"),
+ finalizer: new HarmonyMethod(this.GetType(), nameof(DictionaryPatches.Finalize_GetItem))
+ );
+ }
+ }
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>The method to call after the dictionary indexer throws an exception.</summary>
+ /// <param name="key">The dictionary key being fetched.</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_GetItem(object key, Exception __exception)
+ {
+ if (__exception is KeyNotFoundException)
+ AddKeyTo(__exception, key?.ToString());
+
+ return __exception;
+ }
+
+ /// <summary>Add the accessed key to an exception message.</summary>
+ /// <param name="exception">The exception to modify.</param>
+ /// <param name="key">The dictionary key.</param>
+ private static void AddKeyTo(Exception exception, string key)
+ {
+ DictionaryPatches.Reflection
+ .GetField<string>(exception, "_message")
+ .SetValue($"{exception.Message}\nkey: '{key}'");
+ }
+ }
+}
diff --git a/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
index 006a09ca..531d3699 100644
--- a/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
+++ b/src/SMAPI.Mods.ErrorHandler/SMAPI.Mods.ErrorHandler.csproj
@@ -15,6 +15,7 @@
<ItemGroup>
<Reference Include="$(GameExecutableName)" HintPath="$(GamePath)\$(GameExecutableName).exe" Private="False" />
+ <Reference Include="StardewValley.GameData" HintPath="$(GamePath)\StardewValley.GameData.dll" Private="False" />
<Reference Include="xTile" HintPath="$(GamePath)\xTile.dll" Private="False" />
</ItemGroup>