From 7b4bba89c0a34bbec967225918886714ebf0874f Mon Sep 17 00:00:00 2001 From: bi Date: Fri, 8 Jun 2018 14:37:20 +0300 Subject: [PATCH] Commands: refactoring --- .../mods/vmf/modules/core/commands.lua | 92 ++++++++++--------- 1 file changed, 51 insertions(+), 41 deletions(-) diff --git a/vmf/scripts/mods/vmf/modules/core/commands.lua b/vmf/scripts/mods/vmf/modules/core/commands.lua index 8fad3fa..459e4fa 100644 --- a/vmf/scripts/mods/vmf/modules/core/commands.lua +++ b/vmf/scripts/mods/vmf/modules/core/commands.lua @@ -1,24 +1,22 @@ local vmf = get_mod("VMF") ---[[ - * letters' case doesn't matter - * ctrl+c & ctrl+v - * very flexible chat history without any glitches that occured in the past - * commands are not shown if the mod is disabled - not sure about UI scaling -]] local _commands = {} --- #################################################################################################################### --- ##### VMFMod ####################################################################################################### --- #################################################################################################################### - -VMFMod.command = function (self, command_name, command_description, command_function) +-- ##################################################################################################################### +-- ##### VMFMod ######################################################################################################## +-- ##################################################################################################################### +--[[ + Registers chat command. + * command_name [string] : command's name; should contain only [a-z, A-Z, 0-9, _] characters + * command_description [string] : (optional) command's decription; can be multiline + * command_function [function]: function, that will be executed, when chat command is activated; supports arguments +--]] +function VMFMod:command(command_name, command_description, command_function) if vmf.check_wrong_argument_type(self, "command", "command_name", command_name, "string") or vmf.check_wrong_argument_type(self, "command", "command_description", command_description, "string", "nil") or - vmf.check_wrong_argument_type(self, "command", "command_function", command_function, "function") then - + vmf.check_wrong_argument_type(self, "command", "command_function", command_function, "function") + then return end @@ -31,8 +29,8 @@ VMFMod.command = function (self, command_name, command_description, command_func local command_data = _commands[command_name] if command_data and command_data.mod ~= self then - self:error("(command): command name '%s' is already used by another mod '%s'", - command_name, command_data.mod:get_name()) + self:error("(command): command name '%s' is already used by another mod '%s'", command_name, + command_data.mod:get_name()) return end @@ -45,8 +43,11 @@ VMFMod.command = function (self, command_name, command_description, command_func end -VMFMod.command_remove = function (self, command_name) - +--[[ + Removes registered chat command. + * command_name [string]: command's name +--]] +function VMFMod:command_remove(command_name) if vmf.check_wrong_argument_type(self, "command_remove", "command_name", command_name, "string") then return end @@ -55,8 +56,11 @@ VMFMod.command_remove = function (self, command_name) end -VMFMod.command_disable = function (self, command_name) - +--[[ + Disables registered chat command so it can be enabled later. + * command_name [string]: command's name +--]] +function VMFMod:command_disable(command_name) if vmf.check_wrong_argument_type(self, "command_disable", "command_name", command_name, "string") then return end @@ -67,8 +71,11 @@ VMFMod.command_disable = function (self, command_name) end -VMFMod.command_enable = function (self, command_name) - +--[[ + Enables disabled chat command. + * command_name [string]: command's name +--]] +function VMFMod:command_enable(command_name) if vmf.check_wrong_argument_type(self, "command_enable", "command_name", command_name, "string") then return end @@ -79,8 +86,10 @@ VMFMod.command_enable = function (self, command_name) end -VMFMod.remove_all_commands = function (self) - +--[[ + Removes all registered chat commands for the mod. +--]] +function VMFMod:remove_all_commands() for command_name, command_data in pairs(_commands) do if command_data.mod == self then _commands[command_name] = nil @@ -89,7 +98,10 @@ VMFMod.remove_all_commands = function (self) end -VMFMod.disable_all_commands = function (self) +--[[ + Disables all registered chat commands for the mod. +--]] +function VMFMod:disable_all_commands() for _, command_data in pairs(_commands) do if command_data.mod == self then command_data.is_enabled = false @@ -98,7 +110,10 @@ VMFMod.disable_all_commands = function (self) end -VMFMod.enable_all_commands = function (self) +--[[ + Enables all disabled chat commands for the mod. +--]] +function VMFMod:enable_all_commands() for _, command_data in pairs(_commands) do if command_data.mod == self then command_data.is_enabled = true @@ -106,46 +121,41 @@ VMFMod.enable_all_commands = function (self) end end --- #################################################################################################################### --- ##### VMF internal functions and variables ######################################################################### --- #################################################################################################################### - -vmf.get_commands_list = function(name_contains, exact_match) +-- ##################################################################################################################### +-- ##### VMF internal functions and variables ########################################################################## +-- ##################################################################################################################### +-- Returns a table with command data entries whose name contains 'name_contains' string. If `exact_match` is set +-- to 'true', it will return a table with only one command, whose name fully matches 'name_contains' string. Returns +-- empty table if nothing is found. +function vmf.get_commands_list(name_contains, exact_match) name_contains = name_contains:lower() - local commands_list = {} - for command_name, command_data in pairs(_commands) do if exact_match then - if command_name == name_contains and command_data.is_enabled then table.insert(commands_list, {name = command_name, description = command_data.description}) break end - else local command_match = ( string.sub(command_name, 1, string.len(name_contains)) == name_contains ) if command_match and command_data.is_enabled and command_data.mod:is_enabled() then table.insert(commands_list, {name = command_name, description = command_data.description}) end end - end - table.sort(commands_list, function(a, b) return a.name < b.name end) - return commands_list end -vmf.run_command = function(command_name, ...) - +-- Safely executes function bound to a command with a set name +function vmf.run_command(command_name, ...) local command_data = _commands[command_name] if command_data then local error_prefix = "(commands) " .. tostring(command_name) vmf.xpcall_no_return_values(command_data.mod, error_prefix, command_data.exec_function, ...) else - vmf:error("(commands): command '%s' wasn't found.", command_name) -- should never see this + vmf:error("(commands): command '%s' wasn't found.", command_name) -- Should never see this end end \ No newline at end of file