diff --git a/control.lua b/control.lua index 2724d18b..5c7db20b 100644 --- a/control.lua +++ b/control.lua @@ -15,7 +15,6 @@ Debug = require 'utils.debug' require 'resources.version' local files = { - 'modules.test', 'modules.commands.me', 'modules.commands.kill', 'modules.commands.admin-chat', diff --git a/expcore/commands.lua b/expcore/commands.lua index 4679b437..432e2680 100644 --- a/expcore/commands.lua +++ b/expcore/commands.lua @@ -521,6 +521,18 @@ function Commands.success(value) return Commands.defines.success end +-- logs command usage to file +local function command_log(player,command,comment,params,raw,details) + game.write_file('log/commands.log',game.table_to_json{ + player_name=player.name, + command_name=command.name, + comment=comment, + details=details, + params=params, + raw=raw + }..'\n',true,0) +end + --- Main event function that is ran for all commands, used internally please avoid direct use -- @tparam command_event table passed directly from command event from the add_command function function Commands.run_command(command_event) @@ -530,12 +542,14 @@ function Commands.run_command(command_event) -- checks if player is allowed to use the command local authorized, auth_fail = Commands.authorize(player,command_data.name) if not authorized then + command_log(player,command_data,'Failed Auth',{},command_event.parameter) Commands.error(auth_fail,'utility/cannot_build') return end -- null param check if command_data.min_param_count > 0 and not command_event.parameter then + command_log(player,command_data,'No Params Given',{},command_event.parameter) Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description}) return end @@ -564,6 +578,7 @@ function Commands.run_command(command_event) -- there are too many params given to the command if not command_data.auto_concat then -- error as they should not be more + command_log(player,command_data,'Invalid Input: Too Many Params',raw_params,input_string) Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description}) return else @@ -592,6 +607,7 @@ function Commands.run_command(command_event) -- checks param count local param_count = #raw_params if param_count < command_data.min_param_count then + command_log(player,command_data,'Invalid Input: Not Enough Params',raw_params,input_string) Commands.error({'expcore-commands.invalid-inputs',command_data.name,command_data.description}) return end @@ -608,27 +624,34 @@ function Commands.run_command(command_event) if not type(parse_callback) == 'function' then -- if its not a function throw and error Commands.internal_error(false,command_data.name,'Invalid param parse '..tostring(param_data.parse)) + command_log(player,command_data,'Internal Error: Invalid Param Parse',params,command_event.parameter,tostring(param_data.parse)) return end -- used below as the reject function local parse_fail = function(error_message) error_message = error_message or '' + command_log(player,command_data,'Invalid Param Given',raw_params,input_string) return Commands.error{'expcore-commands.invalid-param',param_name,error_message} end -- input: string, player: LuaPlayer, reject: function, ... extra args local success,param_parsed = pcall(parse_callback,raw_params[index],player,parse_fail,unpack(param_data.parse_args)) - if Commands.internal_error(success,command_data.name,param_parsed) then return end + if Commands.internal_error(success,command_data.name,param_parsed) then + return command_log(player,command_data,'Internal Error: Param Parse Fail',params,command_event.parameter,param_parsed) + end if param_data.optional == true and param_parsed == nil then -- if it is optional and param is nil then it is set to default param_parsed = param_data.default if type(param_parsed) == 'function' then -- player: LuaPlayer success,param_parsed = pcall(param_parsed,player) - if Commands.internal_error(success,command_data.name,param_parsed) then return end + if Commands.internal_error(success,command_data.name,param_parsed) then + return command_log(player,command_data,'Internal Error: Default Value Fail',params,command_event.parameter,param_parsed) + end end elseif param_parsed == nil or param_parsed == Commands.defines.error or param_parsed == parse_fail then -- no value was returned or error was returned, if nil then give generic error if not param_parsed == Commands.defines.error then + command_log(player,command_data,'Invalid Param Given',raw_params,input_string,param_name) Commands.error{'expcore-commands.command-error-param-format',param_name,'please make sure it is the correct type'} end return @@ -642,8 +665,13 @@ function Commands.run_command(command_event) -- player: LuaPlayer, ... command params, raw: string table.insert(params,input_string) local success, err = pcall(command_data.callback,player,unpack(params)) - if Commands.internal_error(success,command_data.name,err) then return end - if err ~= Commands.defines.error and err ~= Commands.defines.success and err ~= Commands.error and err ~= Commands.success then Commands.success(err) end + if Commands.internal_error(success,command_data.name,err) then + return command_log(player,command_data,'Internal Error: Command Callback Fail',params,command_event.parameter,err) + end + if err ~= Commands.defines.error and err ~= Commands.defines.success and err ~= Commands.error and err ~= Commands.success then + Commands.success(err) + end + command_log(player,command_data,'Success',raw_params,input_string) end return Commands \ No newline at end of file diff --git a/modules/commands/admin-chat.lua b/modules/commands/admin-chat.lua index ff0d82b1..a178349c 100644 --- a/modules/commands/admin-chat.lua +++ b/modules/commands/admin-chat.lua @@ -1,5 +1,6 @@ local Commands = require 'expcore.commands' require 'expcore.common_parse' +require 'modules.commands.admin-only-auth' Commands.new_command('admin-chat','Sends a message in chat that only admins can see.') :add_param('message',false) -- the message to send in the admin chat diff --git a/modules/commands/admin-only-auth.lua b/modules/commands/admin-only-auth.lua new file mode 100644 index 00000000..e5b5baf1 --- /dev/null +++ b/modules/commands/admin-only-auth.lua @@ -0,0 +1,13 @@ +local Commands = require 'expcore.commands' + +Commands.add_authenticator(function(player,command,tags,reject) + if tags.admin_only then + if player.admin then + return true + else + return reject('This command is for admins only!') + end + else + return true + end +end) \ No newline at end of file diff --git a/modules/commands/cheat-mode.lua b/modules/commands/cheat-mode.lua index 565060e9..c2a1b206 100644 --- a/modules/commands/cheat-mode.lua +++ b/modules/commands/cheat-mode.lua @@ -1,5 +1,6 @@ local Commands = require 'expcore.commands' require 'expcore.common_parse' +require 'modules.commands.admin-only-auth' Commands.new_command('toggle-cheat-mode','Toggles cheat mode for your player, or another player.') :add_param('player',true,'player') -- player to toggle chest mode of, can be nil for self diff --git a/modules/commands/interface.lua b/modules/commands/interface.lua index 91bc7b60..43306a41 100644 --- a/modules/commands/interface.lua +++ b/modules/commands/interface.lua @@ -1,6 +1,7 @@ local Commands = require 'expcore.commands' local Global = require 'utils.global' local Common = require 'expcore.common' +require 'modules.commands.admin-only-auth' -- modules that are loaded into the interface env to be accessed local interface_modules = { diff --git a/modules/commands/kill.lua b/modules/commands/kill.lua index dd1c99b5..a5c990c3 100644 --- a/modules/commands/kill.lua +++ b/modules/commands/kill.lua @@ -1,5 +1,6 @@ local Commands = require 'expcore.commands' require 'expcore.common_parse' +require 'modules.commands.admin-only-auth' Commands.new_command('kill','Kills yourself or another player.') :add_param('player',true,'player-alive') -- the player to kill, must be alive to be valid diff --git a/modules/commands/teleport.lua b/modules/commands/teleport.lua index cde35570..bcb64ee8 100644 --- a/modules/commands/teleport.lua +++ b/modules/commands/teleport.lua @@ -1,5 +1,6 @@ local Commands = require 'expcore.commands' require 'expcore.common_parse' +require 'modules.commands.admin-only-auth' local function teleport(from_player,to_player) local surface = to_player.surface diff --git a/modules/test.lua b/modules/test.lua deleted file mode 100644 index 70ae3a36..00000000 --- a/modules/test.lua +++ /dev/null @@ -1,56 +0,0 @@ -local Event = require 'utils.event' - -function thisIsATestFunction(...) - game.print(serpent.line({...})) -end - -Event.add(defines.events.on_console_chat,function(event) - if event.player_index then game.print('Message: '..event.message) end -end) - -local Commands = require 'expcore.commands' -- require the Commands module - -Commands.add_authenticator(function(player,command,tags,reject) - if tags.admin_only then - if player.admin then - return true - else - return reject('This command is for admins only!') - end - else - return true - end -end) - -Commands.add_parse('number_range_int',function(input,player,reject,range_min,range_max) - local rtn = tonumber(input) and math.floor(tonumber(input)) or nil - if not rtn or rtn < range_min or rtn > range_max then - return reject('Number entered is not in range: '..range_min..', '..range_max) - else - return rtn - end -end) - -Commands.new_command('repeat-name','Will repeat you name a number of times in chat.') - :add_param('repeat-count',false,'number_range_int',1,5) - :add_param('smiley',true,function(input,player,reject) - if not input then return end - if input:lower() == 'true' or input:lower() == 'yes' then - return true - else - return false - end -end) -:add_defaults{smiley=false} -:add_tag('admin_only',true) -:add_alias('name','rname') -:register(function(player,repeat_count,smiley,raw) - game.print(player.name..' used a command with input: '..raw) - local msg = ') '..player.name - if smiley then - msg = ':'..msg - end - for i = 1,repeat_count do - Commands.print(i..msg) - end -end) \ No newline at end of file