2014-02-17 17:41:48 -05:00
-- InfoReg.lua
-- Implements registration functions that process g_PluginInfo
--- Lists all the subcommands that the player has permissions for
local function ListSubcommands ( a_Player , a_Subcommands , a_CmdString )
2014-03-24 03:15:27 -04:00
if ( a_Player == nil ) then
LOGINFO ( " The " .. a_CmdString .. " command requires another verb: " )
else
a_Player : SendMessage ( " The " .. a_CmdString .. " command requires another verb: " )
end
2016-09-16 10:30:52 -04:00
2014-03-24 03:15:27 -04:00
-- Enum all the subcommands:
2014-04-07 09:47:42 -04:00
local Verbs = { }
2014-02-17 17:41:48 -05:00
for cmd , info in pairs ( a_Subcommands ) do
2014-04-07 09:47:42 -04:00
if ( ( a_Player == nil ) or ( a_Player : HasPermission ( info.Permission or " " ) ) ) then
table.insert ( Verbs , a_CmdString .. " " .. cmd )
2014-02-17 17:41:48 -05:00
end
end
2014-04-07 09:47:42 -04:00
table.sort ( Verbs )
2016-09-16 10:30:52 -04:00
2014-03-24 03:15:27 -04:00
-- Send the list:
if ( a_Player == nil ) then
for idx , verb in ipairs ( Verbs ) do
2014-04-07 09:47:42 -04:00
LOGINFO ( " " .. verb )
2014-03-24 03:15:27 -04:00
end
else
for idx , verb in ipairs ( Verbs ) do
2014-04-07 09:47:42 -04:00
a_Player : SendMessage ( cCompositeChat ( " " , mtInfo ) : AddSuggestCommandPart ( verb , verb ) )
2014-03-24 03:15:27 -04:00
end
2014-02-17 17:41:48 -05:00
end
end
--- This is a generic command callback used for handling multicommands' parent commands
-- For example, if there are "/gal save" and "/gal load" commands, this callback handles the "/gal" command
2014-03-24 03:15:27 -04:00
-- It is used for both console and in-game commands; the console version has a_Player set to nil
2015-05-10 15:33:49 -04:00
local function MultiCommandHandler ( a_Split , a_Player , a_CmdString , a_CmdInfo , a_Level , a_EntireCommand )
local Verb = a_Split [ a_Level + 1 ]
2014-02-17 17:41:48 -05:00
if ( Verb == nil ) then
-- No verb was specified. If there is a handler for the upper level command, call it:
if ( a_CmdInfo.Handler ~= nil ) then
2015-05-10 15:33:49 -04:00
return a_CmdInfo.Handler ( a_Split , a_Player , a_EntireCommand )
2014-02-17 17:41:48 -05:00
end
-- Let the player know they need to give a subcommand:
2014-11-22 07:31:58 -05:00
assert ( type ( a_CmdInfo.Subcommands ) == " table " , " Info.lua error: There is no handler for command \" " .. a_CmdString .. " \" and there are no subcommands defined at level " .. a_Level )
2015-05-10 15:33:49 -04:00
ListSubcommands ( a_Player , a_CmdInfo.Subcommands , a_CmdString )
return true
2014-02-17 17:41:48 -05:00
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- A verb was specified, look it up in the subcommands table:
2015-05-10 15:33:49 -04:00
local Subcommand = a_CmdInfo.Subcommands [ Verb ]
2014-02-17 17:41:48 -05:00
if ( Subcommand == nil ) then
if ( a_Level > 1 ) then
-- This is a true subcommand, display the message and make MCS think the command was handled
-- Otherwise we get weird behavior: for "/cmd verb" we get "unknown command /cmd" although "/cmd" is valid
2014-03-24 03:15:27 -04:00
if ( a_Player == nil ) then
LOGWARNING ( " The " .. a_CmdString .. " command doesn't support verb " .. Verb )
else
a_Player : SendMessage ( " The " .. a_CmdString .. " command doesn't support verb " .. Verb )
end
2015-05-10 15:33:49 -04:00
return true
2014-02-17 17:41:48 -05:00
end
-- This is a top-level command, let MCS handle the unknown message
return false ;
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- Check the permission:
2014-03-24 03:15:27 -04:00
if ( a_Player ~= nil ) then
if not ( a_Player : HasPermission ( Subcommand.Permission or " " ) ) then
2015-05-10 15:33:49 -04:00
a_Player : SendMessage ( " You don't have permission to execute this command " )
return true
2014-03-24 03:15:27 -04:00
end
2014-02-17 17:41:48 -05:00
end
2016-09-16 10:30:52 -04:00
2014-03-16 16:30:44 -04:00
-- If the handler is not valid, check the next sublevel:
2014-02-17 17:41:48 -05:00
if ( Subcommand.Handler == nil ) then
if ( Subcommand.Subcommands == nil ) then
2015-05-10 15:33:49 -04:00
LOG ( " Cannot find handler for command " .. a_CmdString .. " " .. Verb )
return false
2014-02-17 17:41:48 -05:00
end
2015-05-10 15:33:49 -04:00
return MultiCommandHandler ( a_Split , a_Player , a_CmdString .. " " .. Verb , Subcommand , a_Level + 1 , a_EntireCommand )
2014-02-17 17:41:48 -05:00
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- Execute:
2015-05-10 15:33:49 -04:00
return Subcommand.Handler ( a_Split , a_Player , a_EntireCommand )
2014-02-17 17:41:48 -05:00
end
--- Registers all commands specified in the g_PluginInfo.Commands
function RegisterPluginInfoCommands ( )
-- A sub-function that registers all subcommands of a single command, using the command's Subcommands table
-- The a_Prefix param already contains the space after the previous command
-- a_Level is the depth of the subcommands being registered, with 1 being the top level command
local function RegisterSubcommands ( a_Prefix , a_Subcommands , a_Level )
2015-05-10 15:33:49 -04:00
assert ( a_Subcommands ~= nil )
2016-09-16 10:30:52 -04:00
2014-02-25 12:15:09 -05:00
-- A table that will hold aliases to subcommands temporarily, during subcommand iteration
local AliasTable = { }
2016-09-16 10:30:52 -04:00
2014-02-25 12:15:09 -05:00
-- Iterate through the subcommands, register them, and accumulate aliases:
2014-02-17 17:41:48 -05:00
for cmd , info in pairs ( a_Subcommands ) do
2015-05-10 15:33:49 -04:00
local CmdName = a_Prefix .. cmd
local Handler = info.Handler
2014-02-17 17:41:48 -05:00
-- Provide a special handler for multicommands:
if ( info.Subcommands ~= nil ) then
2015-05-10 15:33:49 -04:00
Handler = function ( a_Split , a_Player , a_EntireCommand )
return MultiCommandHandler ( a_Split , a_Player , CmdName , info , a_Level , a_EntireCommand )
2014-02-17 17:41:48 -05:00
end
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
if ( Handler == nil ) then
2015-05-10 15:33:49 -04:00
LOGWARNING ( g_PluginInfo.Name .. " : Invalid handler for command " .. CmdName .. " , command will not be registered. " )
2014-02-17 17:41:48 -05:00
else
2015-05-10 15:33:49 -04:00
local HelpString
2014-02-17 17:41:48 -05:00
if ( info.HelpString ~= nil ) then
2015-05-10 15:33:49 -04:00
HelpString = " - " .. info.HelpString
2014-02-17 17:41:48 -05:00
else
2015-05-10 15:33:49 -04:00
HelpString = " "
2014-02-17 17:41:48 -05:00
end
2016-09-16 10:30:52 -04:00
cPluginManager : BindCommand ( CmdName , info.Permission or " " , Handler , HelpString )
2014-02-17 17:41:48 -05:00
-- Register all aliases for the command:
if ( info.Alias ~= nil ) then
if ( type ( info.Alias ) == " string " ) then
2015-05-10 15:33:49 -04:00
info.Alias = { info.Alias }
2014-02-17 17:41:48 -05:00
end
for idx , alias in ipairs ( info.Alias ) do
2016-09-16 10:30:52 -04:00
cPluginManager : BindCommand ( a_Prefix .. alias , info.Permission or " " , Handler , HelpString )
2014-02-25 12:15:09 -05:00
-- Also copy the alias's info table as a separate subcommand,
-- so that MultiCommandHandler() handles it properly. Need to off-load into a separate table
-- than the one we're currently iterating and join after the iterating.
AliasTable [ alias ] = info
2014-02-17 17:41:48 -05:00
end
end
2014-02-25 12:15:09 -05:00
end -- else (if Handler == nil)
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- Recursively register any subcommands:
if ( info.Subcommands ~= nil ) then
2015-05-10 15:33:49 -04:00
RegisterSubcommands ( a_Prefix .. cmd .. " " , info.Subcommands , a_Level + 1 )
2014-02-17 17:41:48 -05:00
end
2014-02-25 12:15:09 -05:00
end -- for cmd, info - a_Subcommands[]
2016-09-16 10:30:52 -04:00
2014-02-25 12:15:09 -05:00
-- Add the subcommand aliases that were off-loaded during registration:
for alias , info in pairs ( AliasTable ) do
a_Subcommands [ alias ] = info
2014-02-17 17:41:48 -05:00
end
2014-02-25 12:15:09 -05:00
AliasTable = { }
2014-02-17 17:41:48 -05:00
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- Loop through all commands in the plugin info, register each:
2016-09-22 06:54:13 -04:00
if ( g_PluginInfo.Commands ) then
RegisterSubcommands ( " " , g_PluginInfo.Commands , 1 )
end
2014-02-17 17:41:48 -05:00
end
--- Registers all console commands specified in the g_PluginInfo.ConsoleCommands
function RegisterPluginInfoConsoleCommands ( )
-- A sub-function that registers all subcommands of a single command, using the command's Subcommands table
-- The a_Prefix param already contains the space after the previous command
2014-03-24 03:15:27 -04:00
local function RegisterSubcommands ( a_Prefix , a_Subcommands , a_Level )
2015-05-10 15:33:49 -04:00
assert ( a_Subcommands ~= nil )
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
for cmd , info in pairs ( a_Subcommands ) do
2015-05-10 15:33:49 -04:00
local CmdName = a_Prefix .. cmd
2014-03-24 03:15:27 -04:00
local Handler = info.Handler
if ( Handler == nil ) then
2015-05-10 15:33:49 -04:00
Handler = function ( a_Split , a_EntireCommand )
return MultiCommandHandler ( a_Split , nil , CmdName , info , a_Level , a_EntireCommand )
2014-03-24 03:15:27 -04:00
end
end
2016-09-16 10:30:52 -04:00
cPluginManager : BindConsoleCommand ( CmdName , Handler , info.HelpString or " " )
2014-02-17 17:41:48 -05:00
-- Recursively register any subcommands:
if ( info.Subcommands ~= nil ) then
2015-05-10 15:33:49 -04:00
RegisterSubcommands ( a_Prefix .. cmd .. " " , info.Subcommands , a_Level + 1 )
2014-02-17 17:41:48 -05:00
end
end
end
2016-09-16 10:30:52 -04:00
2014-02-17 17:41:48 -05:00
-- Loop through all commands in the plugin info, register each:
2016-09-22 06:54:13 -04:00
if ( g_PluginInfo.ConsoleCommands ) then
RegisterSubcommands ( " " , g_PluginInfo.ConsoleCommands , 1 )
end
2014-02-17 17:41:48 -05:00
end