forked from Crimso777/Factorio-Access
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce the console command /fac, the same as /c but accessible and…
… in the Factorio Access environment If you are sighted /c is fine. If you are blind /c is wrap things in print and then go check the launcher output, and probably also throw serpent around that. /fac instead handles figuring out the common cases of when to use serpent, overrides lua print to work with the launcher, and lets the specified code run in the FA environment (e.g. it can see storage). Crashes are guarded with pcall on a best effort basis, but there is probably still a way to crash this. Still, it's for devs not players.
- Loading branch information
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
--[[ | ||
Custom console commands, primarily for debugging and internal use. Docs with | ||
the handlers in this file. | ||
Handlers registered by this file are exported and not announced through our | ||
normal handling. This is because the launcher cannot handle queueing. We need | ||
the mod to be silent. | ||
]] | ||
|
||
local mod = {} | ||
|
||
--[[ | ||
/fac <script> | ||
Exactrly the same as /c, but accessible without fiddling around. It: | ||
- Captures return values, then speaks them through serpent: | ||
- Tries to run the code wrapped in a function, with return prepended | ||
- Otherwise, runs the code directly, and uses whatever the chunk returns. | ||
- Overrides Lua print to go to speech, and concatenates everything up so that we | ||
don't "trip" over the announcements. | ||
- Makes printout available, as a mocked version that will just call to print | ||
(IMPORTANT: only works on the current player; pindex is ignored). | ||
- Announces errors, with tracebacks, using pcall. | ||
Also due to launcher limitations, "print" here doesn't do newlines. That'll | ||
cause the launcher to not read right. | ||
]] | ||
---@param cmd CustomCommandData | ||
function cmd_fac(cmd) | ||
local pindex = cmd.player_index | ||
local script = cmd.parameter | ||
|
||
if not cmd.parameter or cmd.parameter == "" then | ||
printout("A script is required", pindex) | ||
return | ||
end | ||
|
||
local printbuffer = "" | ||
|
||
local function print_override(...) | ||
-- Send a copy to launcher stdout for debugging. | ||
print(...) | ||
|
||
local args = table.pack(...) | ||
for i = 1, args.n do | ||
printbuffer = printbuffer .. tostring(args[i]) .. " " | ||
end | ||
end | ||
|
||
local with_return = "return " .. script | ||
|
||
local environment = {} | ||
for k, v in pairs(_ENV) do | ||
environment[k] = v | ||
end | ||
environment.print = print_override | ||
environment.printout = function(arg, pindex) | ||
print_override(arg, "for pindex", pindex) | ||
end | ||
|
||
local chunk, err = load(with_return, "=(load)", "t", environment) | ||
if not chunk then | ||
chunk, err = load(cmd.parameter, "=(load)", "t", environment) | ||
if err then | ||
printout(err, pindex) | ||
print(err) | ||
return | ||
end | ||
end | ||
|
||
local _good, val = pcall(function() | ||
local r = chunk() | ||
return serpent.line(r, { nocode = true }) | ||
end) | ||
|
||
print_override(val) | ||
|
||
print("Printbuffer is", printbuffer) | ||
printout(printbuffer, pindex) | ||
end | ||
|
||
mod.COMMANDS = { | ||
fac = { | ||
help = "See commands.lua", | ||
handler = cmd_fac, | ||
}, | ||
} | ||
|
||
for name, args in pairs(mod.COMMANDS) do | ||
commands.add_command(name, args.help, args.handler) | ||
end | ||
|
||
return mod |