feat: add Register with frozen dictionary rebuild

This commit is contained in:
2026-06-04 22:13:38 +02:00
parent 2c6be63c33
commit 937abba03d
+26
View File
@@ -1,4 +1,5 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using HarmonyLib;
using Vintagestory.API.Common;
using Vintagestory.API.Server;
@@ -30,6 +31,31 @@ public class CommandHookModSystem : ModSystem
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)
{
if (registrations.TryGetValue(commandName, out var mods))