Files
factorio-scenario-ExpCluster/exp_commands/module/commands/rcon.lua
2024-11-19 22:35:44 +00:00

91 lines
3.5 KiB
Lua

--[[-- Command Module - Rcon
System command which runs arbitrary code within a custom (not sandboxed) environment
@commands _system-rcon
--- Get the names of all online players, using rcon
/_system-rcon local names = {}; for index, player in pairs(game.connected_player) do names[index] = player.name end; return names;
--- Get the names of all online players, using clustorio ipcs
/_system-rcon local names = {}; for index, player in pairs(game.connected_player) do names[index] = player.name end; ipc("online-players", names);
]]
local ExpUtil = require("modules/exp_util")
local Async = require("modules/exp_util/async")
local Storage = require("modules/exp_util/storage")
local Commands = require("modules/exp_commands")
local Clustorio = require("modules/clusterio/api")
local rcon_env = {}
local rcon_statics = {}
local rcon_callbacks = {}
setmetatable(rcon_statics, { __index = _G })
setmetatable(rcon_env, { __index = rcon_statics })
--- Some common static values which can be added now
--- @diagnostic disable: name-style-check
rcon_statics.Async = Async
rcon_statics.ExpUtil = ExpUtil
rcon_statics.Commands = Commands
rcon_statics.Clustorio = Clustorio
rcon_statics.print = Commands.print
rcon_statics.ipc = Clustorio.send_json
--- @diagnostic enable: name-style-check
--- Some common callback values which are useful when a player uses the command
function rcon_callbacks.player(player) return player end
function rcon_callbacks.surface(player) return player and player.surface end
function rcon_callbacks.force(player) return player and player.force end
function rcon_callbacks.position(player) return player and player.position end
function rcon_callbacks.entity(player) return player and player.selected end
function rcon_callbacks.tile(player) return player and player.surface.get_tile(player.position) end
--- The rcon env is saved between command runs to prevent desyncs
Storage.register(rcon_env, function(tbl)
rcon_env = setmetatable(tbl, { __index = rcon_statics })
end)
--- Static values can be added to the rcon env which are not stored in global such as modules
function Commands.add_rcon_static(name, value)
ExpUtil.assert_not_runtime()
rcon_statics[name] = value
end
--- Callback values can be added to the rcon env, these are called on each invocation and should return one value
function Commands.add_rcon_callback(name, callback)
ExpUtil.assert_not_runtime()
rcon_callbacks[name] = callback
end
Commands.new("_rcon", { "exp-commands_rcon.description" })
:argument("invocation", { "exp-commands_rcon.arg-invocation" }, Commands.types.string)
:enable_auto_concatenation()
:add_flags{ "system_only" }
:register(function(player, invocation_string)
--- @cast invocation_string string
-- Construct the environment the command will run within
local env = setmetatable({}, { __index = rcon_env, __newindex = rcon_env })
for name, callback in pairs(rcon_callbacks) do
local _, rtn = pcall(callback, player.index > 0 and player or nil)
rawset(env, name, rtn)
end
-- Compile and run the invocation string
local invocation, compile_error = load(invocation_string, "rcon-invocation", "t", env)
if compile_error then
return Commands.status.invalid_input(compile_error)
else
local success, rtn = xpcall(invocation, debug.traceback)
if success == false then
return Commands.status.error(rtn)
else
return Commands.status.success(rtn)
end
end
end)