Commands: refactoring
This commit is contained in:
parent
fcb0b570d8
commit
7b4bba89c0
1 changed files with 51 additions and 41 deletions
|
@ -1,24 +1,22 @@
|
||||||
local vmf = get_mod("VMF")
|
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 = {}
|
local _commands = {}
|
||||||
|
|
||||||
-- ####################################################################################################################
|
-- #####################################################################################################################
|
||||||
-- ##### VMFMod #######################################################################################################
|
-- ##### VMFMod ########################################################################################################
|
||||||
-- ####################################################################################################################
|
-- #####################################################################################################################
|
||||||
|
|
||||||
VMFMod.command = function (self, command_name, command_description, command_function)
|
|
||||||
|
|
||||||
|
--[[
|
||||||
|
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
|
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_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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -31,8 +29,8 @@ VMFMod.command = function (self, command_name, command_description, command_func
|
||||||
|
|
||||||
local command_data = _commands[command_name]
|
local command_data = _commands[command_name]
|
||||||
if command_data and command_data.mod ~= self then
|
if command_data and command_data.mod ~= self then
|
||||||
self:error("(command): command name '%s' is already used by another mod '%s'",
|
self:error("(command): command name '%s' is already used by another mod '%s'", command_name,
|
||||||
command_name, command_data.mod:get_name())
|
command_data.mod:get_name())
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -45,8 +43,11 @@ VMFMod.command = function (self, command_name, command_description, command_func
|
||||||
end
|
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
|
if vmf.check_wrong_argument_type(self, "command_remove", "command_name", command_name, "string") then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -55,8 +56,11 @@ VMFMod.command_remove = function (self, command_name)
|
||||||
end
|
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
|
if vmf.check_wrong_argument_type(self, "command_disable", "command_name", command_name, "string") then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -67,8 +71,11 @@ VMFMod.command_disable = function (self, command_name)
|
||||||
end
|
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
|
if vmf.check_wrong_argument_type(self, "command_enable", "command_name", command_name, "string") then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
@ -79,8 +86,10 @@ VMFMod.command_enable = function (self, command_name)
|
||||||
end
|
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
|
for command_name, command_data in pairs(_commands) do
|
||||||
if command_data.mod == self then
|
if command_data.mod == self then
|
||||||
_commands[command_name] = nil
|
_commands[command_name] = nil
|
||||||
|
@ -89,7 +98,10 @@ VMFMod.remove_all_commands = function (self)
|
||||||
end
|
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
|
for _, command_data in pairs(_commands) do
|
||||||
if command_data.mod == self then
|
if command_data.mod == self then
|
||||||
command_data.is_enabled = false
|
command_data.is_enabled = false
|
||||||
|
@ -98,7 +110,10 @@ VMFMod.disable_all_commands = function (self)
|
||||||
end
|
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
|
for _, command_data in pairs(_commands) do
|
||||||
if command_data.mod == self then
|
if command_data.mod == self then
|
||||||
command_data.is_enabled = true
|
command_data.is_enabled = true
|
||||||
|
@ -106,46 +121,41 @@ VMFMod.enable_all_commands = function (self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ####################################################################################################################
|
-- #####################################################################################################################
|
||||||
-- ##### VMF internal functions and variables #########################################################################
|
-- ##### VMF internal functions and variables ##########################################################################
|
||||||
-- ####################################################################################################################
|
-- #####################################################################################################################
|
||||||
|
|
||||||
vmf.get_commands_list = function(name_contains, exact_match)
|
|
||||||
|
|
||||||
|
-- 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()
|
name_contains = name_contains:lower()
|
||||||
|
|
||||||
local commands_list = {}
|
local commands_list = {}
|
||||||
|
|
||||||
for command_name, command_data in pairs(_commands) do
|
for command_name, command_data in pairs(_commands) do
|
||||||
if exact_match then
|
if exact_match then
|
||||||
|
|
||||||
if command_name == name_contains and command_data.is_enabled then
|
if command_name == name_contains and command_data.is_enabled then
|
||||||
table.insert(commands_list, {name = command_name, description = command_data.description})
|
table.insert(commands_list, {name = command_name, description = command_data.description})
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
local command_match = ( string.sub(command_name, 1, string.len(name_contains)) == name_contains )
|
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
|
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})
|
table.insert(commands_list, {name = command_name, description = command_data.description})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
table.sort(commands_list, function(a, b) return a.name < b.name end)
|
table.sort(commands_list, function(a, b) return a.name < b.name end)
|
||||||
|
|
||||||
return commands_list
|
return commands_list
|
||||||
end
|
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]
|
local command_data = _commands[command_name]
|
||||||
if command_data then
|
if command_data then
|
||||||
local error_prefix = "(commands) " .. tostring(command_name)
|
local error_prefix = "(commands) " .. tostring(command_name)
|
||||||
vmf.xpcall_no_return_values(command_data.mod, error_prefix, command_data.exec_function, ...)
|
vmf.xpcall_no_return_values(command_data.mod, error_prefix, command_data.exec_function, ...)
|
||||||
else
|
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
|
||||||
end
|
end
|
Loading…
Add table
Reference in a new issue