From 8263a4406be34b438d295220b99c48502a6636bd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 7 Oct 2021 18:46:41 -0400 Subject: add key to duplicate-dictionary-key exceptions --- .../Patches/DictionaryPatcher.cs | 33 ++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/SMAPI.Mods.ErrorHandler') diff --git a/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatcher.cs b/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatcher.cs index 6ad64e16..8ceafcc5 100644 --- a/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatcher.cs +++ b/src/SMAPI.Mods.ErrorHandler/Patches/DictionaryPatcher.cs @@ -48,6 +48,11 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches original: AccessTools.Method(dictionaryType, "get_Item") ?? throw new InvalidOperationException($"Can't find method {PatchHelper.GetMethodString(dictionaryType, "get_Item")} to patch."), finalizer: this.GetHarmonyMethod(nameof(DictionaryPatcher.Finalize_GetItem)) ); + + harmony.Patch( + original: AccessTools.Method(dictionaryType, "Add") ?? throw new InvalidOperationException($"Can't find method {PatchHelper.GetMethodString(dictionaryType, "Add")} to patch."), + finalizer: this.GetHarmonyMethod(nameof(DictionaryPatcher.Finalize_Add)) + ); } } } @@ -63,13 +68,31 @@ namespace StardewModdingAPI.Mods.ErrorHandler.Patches private static Exception Finalize_GetItem(object key, Exception __exception) { if (__exception is KeyNotFoundException) - { - DictionaryPatcher.Reflection - .GetField(__exception, "_message") - .SetValue($"{__exception.Message}\nkey: '{key}'"); - } + DictionaryPatcher.AddKey(__exception, key); return __exception; } + + /// The method to call after a dictionary insert throws an exception. + /// The dictionary key being inserted. + /// The exception thrown by the wrapped method, if any. + /// Returns the exception to throw, if any. + private static Exception Finalize_Add(object key, Exception __exception) + { + if (__exception is ArgumentException) + DictionaryPatcher.AddKey(__exception, key); + + return __exception; + } + + /// Add the dictionary key to an exception message. + /// The exception whose message to edit. + /// The dictionary key. + private static void AddKey(Exception exception, object key) + { + DictionaryPatcher.Reflection + .GetField(exception, "_message") + .SetValue($"{exception.Message}\nkey: '{key}'"); + } } } -- cgit