2012-11-12 01:48:17 -07:00
|
|
|
-- Execute lua commands interactively or from files.
|
2015-10-23 22:10:15 -06:00
|
|
|
--[[=begin
|
|
|
|
|
|
|
|
lua
|
|
|
|
===
|
|
|
|
There are the following ways to invoke this command:
|
|
|
|
|
|
|
|
1. ``lua`` (without any parameters)
|
|
|
|
|
|
|
|
This starts an interactive lua interpreter.
|
|
|
|
|
|
|
|
2. ``lua -f "filename"`` or ``lua --file "filename"``
|
|
|
|
|
|
|
|
This loads and runs the file indicated by filename.
|
|
|
|
|
|
|
|
3. ``lua -s ["filename"]`` or ``lua --save ["filename"]``
|
|
|
|
|
|
|
|
This loads and runs the file indicated by filename from the save
|
|
|
|
directory. If the filename is not supplied, it loads "dfhack.lua".
|
|
|
|
|
|
|
|
4. ``:lua`` *lua statement...*
|
|
|
|
|
|
|
|
Parses and executes the lua statement like the interactive interpreter would.
|
|
|
|
|
|
|
|
=end]]
|
2012-11-12 01:48:17 -07:00
|
|
|
|
2012-09-24 15:24:37 -06:00
|
|
|
local args={...}
|
2012-11-12 01:48:17 -07:00
|
|
|
local cmd = args[1]
|
|
|
|
|
2016-03-04 18:18:08 -07:00
|
|
|
env = env or {}
|
|
|
|
setmetatable(env, {__index = function(self, k)
|
|
|
|
if k == 'scr' or k == 'screen' then
|
|
|
|
return dfhack.gui.getCurViewscreen()
|
|
|
|
elseif k == 'bld' or k == 'building' then
|
|
|
|
return dfhack.gui.getSelectedBuilding()
|
|
|
|
elseif k == 'item' then
|
|
|
|
return dfhack.gui.getSelectedItem()
|
|
|
|
elseif k == 'job' then
|
|
|
|
return dfhack.gui.getSelectedJob()
|
|
|
|
elseif k == 'wsjob' or k == 'workshop_job' then
|
|
|
|
return dfhack.gui.getSelectedWorkshopJob()
|
|
|
|
elseif k == 'unit' then
|
|
|
|
return dfhack.gui.getSelectedUnit()
|
|
|
|
else
|
|
|
|
return _G[k]
|
|
|
|
end
|
|
|
|
end})
|
|
|
|
|
2012-11-12 01:48:17 -07:00
|
|
|
if cmd=="--file" or cmd=="-f" then
|
2012-11-11 03:39:49 -07:00
|
|
|
local f,err=loadfile (args[2])
|
|
|
|
if f==nil then
|
|
|
|
qerror(err)
|
|
|
|
end
|
2013-10-01 08:58:56 -06:00
|
|
|
dfhack.safecall(f,table.unpack(args,3))
|
2012-11-12 01:48:17 -07:00
|
|
|
elseif cmd=="--save" or cmd=="-s" then
|
2012-11-11 03:39:49 -07:00
|
|
|
if df.global.world.cur_savegame.save_dir=="" then
|
|
|
|
qerror("Savefile not loaded")
|
|
|
|
end
|
|
|
|
local fname=args[2] or "dfhack.lua"
|
|
|
|
fname=string.format("data/save/%s/%s",df.global.world.cur_savegame.save_dir,fname)
|
|
|
|
local f,err=loadfile (fname)
|
|
|
|
if f==nil then
|
|
|
|
qerror(err)
|
|
|
|
end
|
2013-10-01 08:58:56 -06:00
|
|
|
dfhack.safecall(f,table.unpack(args,3))
|
2012-11-12 01:48:17 -07:00
|
|
|
elseif cmd~=nil then
|
|
|
|
-- Support some of the prefixes allowed by dfhack.interpreter
|
|
|
|
local prefix
|
2014-09-08 18:30:18 -06:00
|
|
|
if string.match(cmd, "^[~@!]") then
|
2012-11-12 01:48:17 -07:00
|
|
|
prefix = string.sub(cmd, 1, 1)
|
|
|
|
cmd = 'return '..string.sub(cmd, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
local f,err=load(cmd,'=(lua command)', 't')
|
2012-11-11 03:39:49 -07:00
|
|
|
if f==nil then
|
|
|
|
qerror(err)
|
|
|
|
end
|
2012-11-12 01:48:17 -07:00
|
|
|
|
|
|
|
local rv = table.pack(dfhack.safecall(f,table.unpack(args,2)))
|
|
|
|
|
|
|
|
if rv[1] and prefix then
|
|
|
|
print(table.unpack(rv,2,rv.n))
|
|
|
|
if prefix == '~' then
|
|
|
|
printall(rv[2])
|
2014-09-08 18:30:18 -06:00
|
|
|
elseif prefix == '@' then
|
|
|
|
printall_ipairs(rv[2])
|
2012-11-12 01:48:17 -07:00
|
|
|
end
|
|
|
|
end
|
2012-09-24 15:24:37 -06:00
|
|
|
else
|
2016-03-04 18:18:08 -07:00
|
|
|
dfhack.interpreter("lua","lua.history",env)
|
2012-11-12 01:48:17 -07:00
|
|
|
end
|