feat: add Register with frozen dictionary rebuild
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System.Collections.Frozen;
|
using System.Collections.Frozen;
|
||||||
|
using System.Collections.Generic;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Vintagestory.API.Common;
|
using Vintagestory.API.Common;
|
||||||
using Vintagestory.API.Server;
|
using Vintagestory.API.Server;
|
||||||
@@ -30,6 +31,31 @@ public class CommandHookModSystem : ModSystem
|
|||||||
Instance = null;
|
Instance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void Register(string modId, string commandName, CommandRegistration registration)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public bool FireBefore(string commandName, ref CommandData data)
|
public bool FireBefore(string commandName, ref CommandData data)
|
||||||
{
|
{
|
||||||
if (registrations.TryGetValue(commandName, out var mods))
|
if (registrations.TryGetValue(commandName, out var mods))
|
||||||
|
|||||||
Reference in New Issue
Block a user