-
Notifications
You must be signed in to change notification settings - Fork 198
/
lua.lua
75 lines (59 loc) · 1.94 KB
/
lua.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
-- Execute lua commands interactively or from files.
--[====[
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.
]====]
local utils = require 'utils'
local args = {...}
local cmd = args[1]
env = env or utils.df_shortcut_env()
if cmd=="--file" or cmd=="-f" then
local f,err=loadfile (args[2])
if f==nil then
qerror(err)
end
dfhack.safecall(f,table.unpack(args,3))
elseif cmd=="--save" or cmd=="-s" then
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
dfhack.safecall(f,table.unpack(args,3))
elseif cmd~=nil then
-- Support some of the prefixes allowed by dfhack.interpreter
local prefix
if string.match(cmd, "^[~@!]") then
prefix = string.sub(cmd, 1, 1)
cmd = 'return '..string.sub(cmd, 2)
end
local f, err = load(cmd, '=(lua command)', 't', env)
if f == nil then
qerror(err)
end
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])
elseif prefix == '@' then
printall_ipairs(rv[2])
end
end
else
dfhack.interpreter("lua","lua.history",env)
end