refactor: replace dictionaries with flat ModEntry/HookEntry structs
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Vintagestory.API.Common;
|
||||
using Vintagestory.API.Datastructures;
|
||||
using Vintagestory.API.Server;
|
||||
@@ -7,8 +7,9 @@ namespace CommandHook;
|
||||
|
||||
public class CommandHookModSystem : ModSystem
|
||||
{
|
||||
private readonly Dictionary<string, CommandRegistration> wildcards = new();
|
||||
private readonly Dictionary<string, ModRegistration> registrations = new();
|
||||
private ModEntry[] modEntries = Array.Empty<ModEntry>();
|
||||
private HookEntry[] hashTable = Array.Empty<HookEntry>();
|
||||
private uint hashTableSize = 0;
|
||||
|
||||
public override bool ShouldLoad(EnumAppSide forSide) => forSide == EnumAppSide.Server;
|
||||
|
||||
@@ -18,22 +19,6 @@ public class CommandHookModSystem : ModSystem
|
||||
api.Event.PlayerChat += OnPlayerChat;
|
||||
}
|
||||
|
||||
public void RegisterWildcard(string modId, CommandRegistration registration)
|
||||
{
|
||||
wildcards[modId] = registration;
|
||||
}
|
||||
|
||||
public void Register(string modId, string command, CommandRegistration registration)
|
||||
{
|
||||
if (!registrations.TryGetValue(modId, out var modReg))
|
||||
{
|
||||
modReg = new ModRegistration(new Dictionary<string, CommandRegistration>());
|
||||
registrations[modId] = modReg;
|
||||
}
|
||||
|
||||
modReg.Commands[command] = registration;
|
||||
}
|
||||
|
||||
private void OnPlayerChat(
|
||||
IServerPlayer player,
|
||||
int channelId,
|
||||
@@ -46,22 +31,6 @@ public class CommandHookModSystem : ModSystem
|
||||
return;
|
||||
|
||||
var cmdData = new CommandData(player, message);
|
||||
|
||||
foreach (var (_, reg) in wildcards)
|
||||
reg.Before?.Invoke(ref cmdData);
|
||||
|
||||
foreach (var (_, modReg) in registrations)
|
||||
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
|
||||
reg.Before?.Invoke(ref cmdData);
|
||||
|
||||
if (cmdData.Cancel)
|
||||
consumed.value = true;
|
||||
|
||||
foreach (var (_, reg) in wildcards)
|
||||
reg.After?.Invoke(ref cmdData);
|
||||
|
||||
foreach (var (_, modReg) in registrations)
|
||||
if (modReg.Commands.TryGetValue(cmdData.CommandName, out var reg))
|
||||
reg.After?.Invoke(ref cmdData);
|
||||
// TODO dispatch
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CommandHook;
|
||||
|
||||
public struct HookEntry
|
||||
{
|
||||
public readonly string? CommandName; // null = wildcard
|
||||
public readonly CommandRegistration Registration;
|
||||
|
||||
public HookEntry(string? commandName, CommandRegistration registration)
|
||||
{
|
||||
CommandName = commandName;
|
||||
Registration = registration;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CommandHook;
|
||||
|
||||
public struct ModEntry
|
||||
{
|
||||
public readonly string ModId;
|
||||
public readonly HookEntry[] Hooks;
|
||||
|
||||
public ModEntry(string modId, HookEntry[] hooks)
|
||||
{
|
||||
ModId = modId;
|
||||
Hooks = hooks;
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CommandHook;
|
||||
|
||||
public struct ModRegistration
|
||||
{
|
||||
public readonly Dictionary<string, CommandRegistration> Commands;
|
||||
|
||||
public ModRegistration(Dictionary<string, CommandRegistration> commands)
|
||||
{
|
||||
Commands = commands;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user