Browse Source

Change `Commands` feature to use new collections

pull/8/head
Anton Tarasenko 2 years ago
parent
commit
f1032babf9
  1. 54
      sources/Commands/Commands_Feature.uc

54
sources/Commands/Commands_Feature.uc

@ -23,10 +23,10 @@
class Commands_Feature extends Feature; class Commands_Feature extends Feature;
// Delimiters that always separate command name from it's parameters // Delimiters that always separate command name from it's parameters
var private array<Text> commandDelimiters; var private array<Text> commandDelimiters;
// Registered commands, recorded as (<command_name>, <command_instance>) pairs. // Registered commands, recorded as (<command_name>, <command_instance>) pairs.
// Keys should be deallocated when their entry is removed. // Keys should be deallocated when their entry is removed.
var private AssociativeArray registeredCommands; var private HashTable registeredCommands;
// When this flag is set to true, mutate input becomes available // When this flag is set to true, mutate input becomes available
// despite `useMutateInput` flag to allow to unlock server in case of an error // despite `useMutateInput` flag to allow to unlock server in case of an error
@ -48,7 +48,7 @@ var LoggerAPI.Definition errCommandDuplicate;
protected function OnEnabled() protected function OnEnabled()
{ {
registeredCommands = _.collections.EmptyAssociativeArray(); registeredCommands = _.collections.EmptyHashTable();
RegisterCommand(class'ACommandHelp'); RegisterCommand(class'ACommandHelp');
// Macro selector // Macro selector
commandDelimiters[0] = P("@"); commandDelimiters[0] = P("@");
@ -70,12 +70,8 @@ protected function OnDisabled()
} }
useChatInput = false; useChatInput = false;
useMutateInput = false; useMutateInput = false;
if (registeredCommands != none) _.memory.Free(registeredCommands);
{ registeredCommands = none;
registeredCommands.Empty(true);
registeredCommands.FreeSelf();
registeredCommands = none;
}
commandDelimiters.length = 0; commandDelimiters.length = 0;
_.memory.Free(chatCommandPrefix); _.memory.Free(chatCommandPrefix);
chatCommandPrefix = none; chatCommandPrefix = none;
@ -228,11 +224,13 @@ public final function RegisterCommand(class<Command> commandClass)
.Arg(commandName) .Arg(commandName)
.ArgClass(commandClass); .ArgClass(commandClass);
_.memory.Free(newCommandInstance); _.memory.Free(newCommandInstance);
_.memory.Free(existingCommandInstance);
return; return;
} }
// Otherwise record new command // Otherwise record new command
// `commandName` used as a key, do not deallocate it // `commandName` used as a key, do not deallocate it
registeredCommands.SetItem(commandName, newCommandInstance, true); registeredCommands.SetItem(commandName, newCommandInstance);
_.memory.Free(newCommandInstance);
} }
/** /**
@ -260,14 +258,21 @@ public final function RemoveCommand(class<Command> commandClass)
{ {
nextCommand = Command(iter.Get()); nextCommand = Command(iter.Get());
nextCommandName = Text(iter.GetKey()); nextCommandName = Text(iter.GetKey());
if (nextCommand == none) continue; if ( nextCommand == none || nextCommandName == none
if (nextCommandName == none) continue; || nextCommand.class != commandClass)
if (nextCommand.class != commandClass) continue; {
_.memory.Free(nextCommand);
_.memory.Free(nextCommandName);
continue;
}
keysToRemove[keysToRemove.length] = nextCommandName; keysToRemove[keysToRemove.length] = nextCommandName;
_.memory.Free(nextCommand);
} }
iter.FreeSelf(); iter.FreeSelf();
for (i = 0; i < keysToRemove.length; i += 1) { for (i = 0; i < keysToRemove.length; i += 1)
registeredCommands.RemoveItem(keysToRemove[i], true); {
registeredCommands.RemoveItem(keysToRemove[i]);
_.memory.Free(keysToRemove[i]);
} }
} }
@ -300,23 +305,12 @@ public final function Command GetCommand(BaseText commandName)
*/ */
public final function array<Text> GetCommandNames() public final function array<Text> GetCommandNames()
{ {
local int i; local array<Text> emptyResult;
local array<AcediaObject> keys;
local Text nextKeyAsText;
local array<Text> keysAsText;
if (registeredCommands == none) { if (registeredCommands != none) {
return keysAsText; return registeredCommands.GetTextKeys();
}
keys = registeredCommands.GetKeys();
for (i = 0; i < keys.length; i += 1)
{
nextKeyAsText = Text(keys[i]);
if (nextKeyAsText != none) {
keysAsText[keysAsText.length] = nextKeyAsText.Copy();
}
} }
return keysAsText; return emptyResult;
} }
/** /**

Loading…
Cancel
Save