Compare commits

2 Commits
+20 -21
View File
@@ -1,3 +1,4 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using HarmonyLib;
@@ -11,7 +12,6 @@ public class CommandHookModSystem : ModSystem
internal static CommandHookModSystem? Instance;
private readonly List<ICommandHookListener> listeners = new();
private readonly List<ICommandHookListener> wildcards = new();
private FrozenDictionary<string, FrozenDictionary<string, CommandRegistration>> registrations =
FrozenDictionary<string, FrozenDictionary<string, CommandRegistration>>.Empty;
@@ -37,7 +37,6 @@ public class CommandHookModSystem : ModSystem
{
harmony?.UnpatchAll(Mod.Info.ModID);
listeners.Clear();
wildcards.Clear();
Instance = null;
}
@@ -60,13 +59,11 @@ public class CommandHookModSystem : ModSystem
return;
listeners[i] = listener;
SyncWildcard(listener, commands);
Rebuild();
return;
}
listeners.Add(listener);
SyncWildcard(listener, commands);
}
public void Unregister(ICommandHookListener listener)
@@ -77,7 +74,6 @@ public class CommandHookModSystem : ModSystem
continue;
listeners.RemoveAt(i);
wildcards.RemoveAll(w => w.ModId == listener.ModId);
Rebuild();
return;
}
@@ -93,10 +89,6 @@ public class CommandHookModSystem : ModSystem
if (commands == null || commands.Count == 0)
continue;
bool isWildcard = commands.Count == 1 && commands[0] == "*";
if (isWildcard)
continue;
foreach (var cmd in commands)
{
if (!builder.TryGetValue(cmd, out var mods))
@@ -108,10 +100,6 @@ public class CommandHookModSystem : ModSystem
}
}
foreach (var wildcard in wildcards)
foreach (var mods in builder.Values)
mods[wildcard.ModId] = wildcard.Registration;
var pruned = new Dictionary<string, FrozenDictionary<string, CommandRegistration>>(
builder.Count
);
@@ -126,9 +114,17 @@ public class CommandHookModSystem : ModSystem
{
if (registrations.TryGetValue(commandName, out var mods))
{
foreach (var (_, reg) in mods)
foreach (var (modId, reg) in mods)
{
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,16 +136,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)
{
foreach (var (modId, reg) in mods)
{
try
{
reg.After?.Invoke(ref data, result);
}
private void SyncWildcard(ICommandHookListener listener, IReadOnlyList<string> commands)
catch (Exception ex)
{
bool isWildcard = commands.Count == 1 && commands[0] == "*";
wildcards.RemoveAll(w => w.ModId == listener.ModId);
if (isWildcard)
wildcards.Add(listener);
Mod.Logger.Error("[{0}] After /{1} threw: {2}", modId, commandName, ex);
}
}
}
}
private static bool CommandListEquals(IReadOnlyList<string> a, IReadOnlyList<string> b)