diff --git a/CommandHook/CommandHookModSystem.cs b/CommandHook/CommandHookModSystem.cs index 1b0ba29..130be33 100644 --- a/CommandHook/CommandHookModSystem.cs +++ b/CommandHook/CommandHookModSystem.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Frozen; using System.Collections.Generic; using HarmonyLib; @@ -109,8 +110,8 @@ public class CommandHookModSystem : ModSystem } foreach (var wildcard in wildcards) - foreach (var mods in builder.Values) - mods[wildcard.ModId] = wildcard.Registration; + foreach (var mods in builder.Values) + mods[wildcard.ModId] = wildcard.Registration; var pruned = new Dictionary>( builder.Count @@ -126,9 +127,17 @@ public class CommandHookModSystem : ModSystem { if (registrations.TryGetValue(commandName, out var mods)) { - foreach (var (_, reg) in mods) + foreach (var (modId, reg) in mods) { - reg.Before?.Invoke(ref data); + try + { + reg.Before?.Invoke(ref data); + } + catch (Exception ex) + { + Mod.Logger.Error("[{0}] Before /{1} threw: {2}", modId, commandName, ex); + } + if (data.Cancel) break; } @@ -140,8 +149,19 @@ public class CommandHookModSystem : ModSystem internal void FireAfter(string commandName, ref CommandData data, TextCommandResult result) { if (registrations.TryGetValue(commandName, out var mods)) - foreach (var (_, reg) in mods) - reg.After?.Invoke(ref data, result); + { + foreach (var (modId, reg) in mods) + { + try + { + reg.After?.Invoke(ref data, result); + } + catch (Exception ex) + { + Mod.Logger.Error("[{0}] After /{1} threw: {2}", modId, commandName, ex); + } + } + } } private void SyncWildcard(ICommandHookListener listener, IReadOnlyList commands)