feat: variadic register/unregister with shared rebuild helpers
This commit is contained in:
@@ -31,29 +31,44 @@ public class CommandHookModSystem : ModSystem
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
public void Register(string modId, string commandName, CommandRegistration registration)
|
||||
public void Register(
|
||||
string modId,
|
||||
CommandRegistration registration,
|
||||
params string[] commandNames
|
||||
)
|
||||
{
|
||||
var builder = new Dictionary<string, Dictionary<string, CommandRegistration>>();
|
||||
var builder = Thaw();
|
||||
|
||||
foreach (var (cmd, mods) in registrations)
|
||||
foreach (var commandName in commandNames)
|
||||
{
|
||||
var inner = new Dictionary<string, CommandRegistration>();
|
||||
foreach (var (id, reg) in mods)
|
||||
inner[id] = reg;
|
||||
builder[cmd] = inner;
|
||||
}
|
||||
|
||||
if (!builder.TryGetValue(commandName, out var target))
|
||||
{
|
||||
target = new Dictionary<string, CommandRegistration>();
|
||||
builder[commandName] = target;
|
||||
}
|
||||
target[modId] = registration;
|
||||
}
|
||||
|
||||
registrations = builder.ToFrozenDictionary(
|
||||
pair => pair.Key,
|
||||
pair => pair.Value.ToFrozenDictionary()
|
||||
);
|
||||
registrations = Freeze(builder);
|
||||
}
|
||||
|
||||
public void Unregister(string modId, params string[] commandNames)
|
||||
{
|
||||
var builder = Thaw();
|
||||
|
||||
if (commandNames.Length == 0)
|
||||
{
|
||||
foreach (var (_, mods) in builder)
|
||||
mods.Remove(modId);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var commandName in commandNames)
|
||||
if (builder.TryGetValue(commandName, out var mods))
|
||||
mods.Remove(modId);
|
||||
}
|
||||
|
||||
registrations = Freeze(builder);
|
||||
}
|
||||
|
||||
public bool FireBefore(string commandName, ref CommandData data)
|
||||
@@ -71,4 +86,32 @@ public class CommandHookModSystem : ModSystem
|
||||
foreach (var (_, reg) in mods)
|
||||
reg.After?.Invoke(ref data, result);
|
||||
}
|
||||
|
||||
private Dictionary<string, Dictionary<string, CommandRegistration>> Thaw()
|
||||
{
|
||||
var builder = new Dictionary<string, Dictionary<string, CommandRegistration>>();
|
||||
|
||||
foreach (var (cmd, mods) in registrations)
|
||||
{
|
||||
var inner = new Dictionary<string, CommandRegistration>();
|
||||
foreach (var (id, reg) in mods)
|
||||
inner[id] = reg;
|
||||
builder[cmd] = inner;
|
||||
}
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
private static FrozenDictionary<string, FrozenDictionary<string, CommandRegistration>> Freeze(
|
||||
Dictionary<string, Dictionary<string, CommandRegistration>> builder
|
||||
)
|
||||
{
|
||||
var pruned = new Dictionary<string, FrozenDictionary<string, CommandRegistration>>();
|
||||
|
||||
foreach (var (cmd, mods) in builder)
|
||||
if (mods.Count > 0)
|
||||
pruned[cmd] = mods.ToFrozenDictionary();
|
||||
|
||||
return pruned.ToFrozenDictionary();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user