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