From 52f9aa5aa57fc725b0dd8645bf7ae66bc670a1fe Mon Sep 17 00:00:00 2001 From: Anton Tarasenko Date: Tue, 14 Mar 2023 04:15:05 +0700 Subject: [PATCH] Add command locks --- sources/BaseAPI/API/Commands/CommandAPI.uc | 72 ++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/sources/BaseAPI/API/Commands/CommandAPI.uc b/sources/BaseAPI/API/Commands/CommandAPI.uc index e31d2ec..52c6112 100644 --- a/sources/BaseAPI/API/Commands/CommandAPI.uc +++ b/sources/BaseAPI/API/Commands/CommandAPI.uc @@ -26,6 +26,13 @@ var private array< class > pendingClasses; // Job that is supposed to register pending commands var private CommandRegistrationJob registeringJob; +// Saves `HashTable` with command locks. +// Locks are simply boolean switches that mark for commands whether they can be executed. +// +// Lock is considered "unlocked" if this `HashTable` stores `true` at the key with its name +// and `false` otherwise. +var private HashTable commandLocks; + var private Commands_Feature commandsFeature; // DO NOT CALL MANUALLY @@ -152,5 +159,70 @@ public final function Execute_S(string commandLine, EPlayer callerPlayer) { } } +/// Closes a command lock with a given case-insensitive name. +/// +/// Command locks are basically just boolean values that commands can use to check for whether they +/// are allowed to perform certain actions (e.g. cheats). +public final function bool Lock(BaseText lockName) { + local Text lowerCaseName; + + if (lockName == none) return false; + if (commandsFeature == none) return false; + + if (commandLocks == none) { + commandLocks = _.collections.EmptyHashTable(); + } + lowerCaseName = lockName.LowerCopy(); + commandLocks.SetBool(lowerCaseName, true); + lowerCaseName.FreeSelf(); + return true; +} + +/// Opens a command lock with a given case-insensitive name. +/// +/// Command locks are basically just boolean values that commands can use to check for whether they +/// are allowed to perform certain actions (e.g. cheats). +public final function bool Unlock(BaseText lockName) { + local Text lowerCaseName; + + if (lockName == none) return false; + if (commandsFeature == none) return false; + + if (commandLocks == none) { + commandLocks = _.collections.EmptyHashTable(); + } + lowerCaseName = lockName.LowerCopy(); + commandLocks.SetBool(lowerCaseName, false); + lowerCaseName.FreeSelf(); + return true; +} + +/// Checks if a command lock with a given case-insensitive name is closed. +/// +/// Command locks are basically just boolean values that commands can use to check for whether they +/// are allowed to perform certain actions (e.g. cheats). +public final function bool IsLocked(BaseText lockName) { + local bool result; + local Text lowerCaseName; + + if (lockName == none) return true; + if (commandsFeature == none) return true; + if (commandLocks == none) return true; + + lowerCaseName = lockName.LowerCopy(); + result = commandLocks.GetBool(lowerCaseName); + lowerCaseName.FreeSelf(); + return result; +} + +/// Closes all command locks. +/// +/// Command locks are basically just boolean values that commands can use to check for whether they +/// are allowed to perform certain actions (e.g. cheats). +public final function CloseAllLocks() { + _.memory.Free(commandLocks); + commandLocks = none; +} + defaultproperties { } \ No newline at end of file