1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2025-09-16 09:16:22 +02:00

Server/Debug/Player only flags and Debug extraction

This commit is contained in:
Lynn
2018-11-24 20:44:02 +01:00
parent 749c9ad15b
commit 9d35776849
5 changed files with 127 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
-- dependencies
local BaseDebug = require 'utils.debug'
local min = math.min
local max = math.max
local floor = math.floor
@@ -10,76 +11,19 @@ local Debug = {}
local default_base_color = {r = 1, g = 1, b = 1}
local default_delta_color = {r = 0, g = 0, b = 0}
-- private state
local debug = false
local cheats = false
function Debug.enable_debug()
debug = true
end
function Debug.disable_debug()
debug = false
end
function Debug.enable_cheats()
cheats = true
end
function Debug.disable_cheats()
cheats = true
end
global.message_count = 0
--[[--
Shows the given message if debug is enabled.
@param message string
]]
---@deprecated use 'utils.debug'.print instead
function Debug.print(message)
if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end
global.message_count = global.message_count + 1
if (debug) then
game.print('[' .. global.message_count .. '] ' .. tostring(message))
log('[' .. global.message_count .. '] ' .. tostring(message))
end
BaseDebug.print(message)
end
--[[--
Shows the given message with serpent enabled, if debug is enabled.
@param message string
]]
function Debug.print_serpent(message)
Debug.print(serpent.line(message))
end
--[[--
Shows the given message if debug is on.
@param x number
@param y number
@param message string
]]
---@deprecated use 'utils.debug'.print_position instead
function Debug.print_position(position, message)
message = message or ''
if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then message = type(message) end
global.message_count = global.message_count + 1
if (debug) then
game.print('[' .. global.message_count .. '] {x=' .. position.x .. ', y=' .. position.y .. '} ' .. tostring(message))
end
BaseDebug.print_position(position, message)
end
--[[--
Executes the given callback if cheating is enabled.
@param callback function
]]
---@deprecated use 'utils.debug'.cheat instead
function Debug.cheat(callback)
if (cheats) then
callback()
end
BaseDebug.cheat(callback)
end
--[[--

View File

@@ -1,6 +1,5 @@
-- dependencies
local Config = require 'map_gen.Diggy.Config'
local Debug = require 'map_gen.Diggy.Debug'
local ScenarioInfo = require 'features.gui.info'
local Event = require 'utils.event'
@@ -36,10 +35,8 @@ local function each_enabled_feature(if_enabled)
end
end
--[[--
Register the events required to initialize the scenario.
]]
function Scenario.register(debug, cheats)
---Register the events required to initialize the scenario.
function Scenario.register()
if global.diggy_scenario_registered then
error('Cannot register the Diggy scenario multiple times.')
return
@@ -50,14 +47,6 @@ function Scenario.register(debug, cheats)
global.config.fish_market.enable = nil
end
if debug then
Debug.enable_debug()
end
if cheats then
Debug.enable_cheats()
end
local extra_map_info = ''
each_enabled_feature(

View File

@@ -1,2 +1,2 @@
-- authors Linaori, valansch
require 'map_gen.Diggy.Scenario'.register(_DEBUG, _CHEATS)
require 'map_gen.Diggy.Scenario'.register()

View File

@@ -1,13 +1,18 @@
local insert = table.insert
local format = string.format
local Command = {}
---Adds a command to be executed.
---
---Options table accepts the following structure: {
--- description = 'Teleports you to the player',
--- arguments = {'foo', 'bar'},
--- description = 'A description of the command',
--- arguments = {'foo', 'bar'}, -- maps arguments to these names in the given sequence
--- default_values = {'bar' = nil}, -- gives a default value to 'bar' when omitted
--- admin_only = true, -- defaults to false
--- debug_only = false, -- only registers it if _DEBUG is set to true when false
--- allowed_by_server = false -- lets the server execute this, defaults to false
--- allowed_by_player = true -- lets players execute this, defaults to true
--- log_command = true, -- defaults to false unless admin only, then always true
---}
---
@@ -22,27 +27,69 @@ local Command = {}
function Command.add(command_name, options, callback)
local description = options.description or '[Undocumented command]'
local arguments = options.arguments or {}
local default_values = options.default_values or {}
local admin_only = options.admin_only or false
local debug_only = options.debug_only or false
local allowed_by_server = options.allowed_by_server or false
local allowed_by_player = options.allowed_by_player
local log_command = options.log_command or options.admin_only or false
local argument_list = ''
for _, argument in ipairs(arguments) do
argument_list = string.format('%s<%s> ', argument_list, argument)
if nil == options.allowed_by_player then
allowed_by_player = true
end
commands.add_command(command_name, argument_list .. description .. (admin_only and ' (Admin Only)' or ''), function (command)
if not _DEBUG and debug_only then
return
end
if not allowed_by_player and not allowed_by_server then
error(format("The command '%s' is not allowed by the server nor player, please enable at least one of them.", command_name))
end
for _, argument_name in ipairs(arguments) do
local argument_display = argument_name
for default_value_name, _ in pairs(default_values) do
if default_value_name == argument_name then
argument_display = argument_display .. ':optional'
break
end
end
argument_list = format('%s<%s> ', argument_list, argument_display)
end
local extra = ''
if allowed_by_server and not allowed_by_player then
extra = ' (Server Only)'
elseif allowed_by_player and admin_only then
extra = ' (Admin Only)'
end
commands.add_command(command_name, argument_list .. description .. extra, function (command)
local print -- custom print reference in case no player is present
local player_index = command.player_index
local player = game.player
if not player or not player.valid then
print = function (message)
log(string.format('Trying to print message to player #%d, but not such player found: %s', player_index, message))
log(format('Trying to print message to player #%d, but not such player found: %s', player_index, message))
end
if not allowed_by_server then
log(format("The command '%s' is not allowed to be executed by the server.", command_name))
return
end
else
print = player.print
if not allowed_by_player then
print(format("The command '%s' is not allowed to be executed by players.", command_name))
return
end
if admin_only and not player.admin then
print(string.format('The %s command is only available to admins.', command_name))
print(format("The command '%s' requires an admin to be be executed", command_name))
return
end
end
@@ -57,8 +104,18 @@ function Command.add(command_name, options, callback)
for index, argument in ipairs(arguments) do
local parameter = from_command[index]
if not parameter then
insert(errors, string.format('Argument %s from command %s is missing.', argument, command_name))
for default_value_name, default_value in pairs(default_values) do
if default_value_name == argument then
parameter = default_value
break
end
end
end
if not parameter then
insert(errors, format('Argument %s from command %s is missing.', argument, command_name))
else
named_arguments[argument] = parameter
end
@@ -76,7 +133,7 @@ function Command.add(command_name, options, callback)
end
if log_command then
log(string.format(
log(format(
'[%s Command] %s, used: %s %s',
admin_only and 'Admin' or 'Player',
player and player.valid and player.name or '<server>',
@@ -97,8 +154,8 @@ function Command.add(command_name, options, callback)
end)
if not success then
log(string.format('Error while running %s: %s', command_name, error))
print(string.format('There was an error running %s, it has been logged.', command_name))
log(format('Error while running %s: %s', command_name, error))
print(format('There was an error running %s, it has been logged.', command_name))
end
end)
end

49
utils/debug.lua Normal file
View File

@@ -0,0 +1,49 @@
-- dependencies
local format = string.format
local serialize = serpent.line
-- this
local Debug = {}
global.debug_message_count = 0
---@return number next index
local function increment()
local next = global.debug_message_count + 1
global.debug_message_count = next
return next
end
---Shows the given message if debug is enabled. Uses serpent to print non scalars.
---@param message table
function Debug.print(message)
if not _DEBUG then
return
end
if type(message) ~= 'string' and type(message) ~= 'number' and type(message) ~= 'boolean' then
message = serialize(message)
end
message = format('[%d] %s', increment(), tostring(message))
game.print(message)
log(message)
end
---Shows the given message if debug is on.
---@param position Position
---@param message string
function Debug.print_position(position, message)
Debug.print(format('%s %s', serialize(position), message))
end
---Executes the given callback if cheating is enabled.
---@param callback function
function Debug.cheat(callback)
if _CHEATS then
callback()
end
end
return Debug