2012-03-31 05:40:54 -06:00
|
|
|
-- Common startup file for all dfhack plugins with lua support
|
2012-04-01 02:50:56 -06:00
|
|
|
-- The global dfhack table is already created by C++ init code.
|
2012-03-31 05:40:54 -06:00
|
|
|
|
2012-04-21 10:15:57 -06:00
|
|
|
-- Setup the global environment.
|
|
|
|
-- BASE_G is the original lua global environment,
|
|
|
|
-- preserved as a common denominator for all modules.
|
|
|
|
-- This file uses it instead of the new default one.
|
|
|
|
|
|
|
|
local dfhack = dfhack
|
|
|
|
local base_env = dfhack.BASE_G
|
|
|
|
local _ENV = base_env
|
|
|
|
|
2012-04-05 09:55:59 -06:00
|
|
|
-- Console color constants
|
|
|
|
|
|
|
|
COLOR_RESET = -1
|
|
|
|
COLOR_BLACK = 0
|
|
|
|
COLOR_BLUE = 1
|
|
|
|
COLOR_GREEN = 2
|
|
|
|
COLOR_CYAN = 3
|
|
|
|
COLOR_RED = 4
|
|
|
|
COLOR_MAGENTA = 5
|
|
|
|
COLOR_BROWN = 6
|
|
|
|
COLOR_GREY = 7
|
|
|
|
COLOR_DARKGREY = 8
|
|
|
|
COLOR_LIGHTBLUE = 9
|
|
|
|
COLOR_LIGHTGREEN = 10
|
|
|
|
COLOR_LIGHTCYAN = 11
|
|
|
|
COLOR_LIGHTRED = 12
|
|
|
|
COLOR_LIGHTMAGENTA = 13
|
|
|
|
COLOR_YELLOW = 14
|
|
|
|
COLOR_WHITE = 15
|
|
|
|
|
2012-04-17 01:45:09 -06:00
|
|
|
-- Events
|
|
|
|
|
|
|
|
if dfhack.is_core_context then
|
|
|
|
SC_WORLD_LOADED = 0
|
|
|
|
SC_WORLD_UNLOADED = 1
|
|
|
|
SC_MAP_LOADED = 2
|
|
|
|
SC_MAP_UNLOADED = 3
|
|
|
|
SC_VIEWSCREEN_CHANGED = 4
|
|
|
|
SC_CORE_INITIALIZED = 5
|
|
|
|
end
|
|
|
|
|
2012-04-05 09:55:59 -06:00
|
|
|
-- Error handling
|
|
|
|
|
2012-04-03 10:02:01 -06:00
|
|
|
safecall = dfhack.safecall
|
|
|
|
|
2012-04-04 03:34:07 -06:00
|
|
|
function dfhack.pcall(f, ...)
|
|
|
|
return xpcall(f, dfhack.onerror, ...)
|
|
|
|
end
|
|
|
|
|
2012-04-07 04:21:38 -06:00
|
|
|
function dfhack.with_finalize(...)
|
|
|
|
return dfhack.call_with_finalizer(0,true,...)
|
|
|
|
end
|
|
|
|
function dfhack.with_onerror(...)
|
|
|
|
return dfhack.call_with_finalizer(0,false,...)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function call_delete(obj)
|
|
|
|
if obj then obj:delete() end
|
|
|
|
end
|
|
|
|
|
|
|
|
function dfhack.with_temp_object(obj,fn,...)
|
|
|
|
return dfhack.call_with_finalizer(1,true,call_delete,obj,fn,obj,...)
|
|
|
|
end
|
|
|
|
|
2012-04-05 09:55:59 -06:00
|
|
|
-- Module loading
|
|
|
|
|
2012-04-01 02:50:56 -06:00
|
|
|
function mkmodule(module,env)
|
|
|
|
local pkg = package.loaded[module]
|
|
|
|
if pkg == nil then
|
|
|
|
pkg = {}
|
|
|
|
else
|
|
|
|
if type(pkg) ~= 'table' then
|
|
|
|
error("Not a table in package.loaded["..module.."]")
|
|
|
|
end
|
|
|
|
end
|
Allow plugins to export functions to lua with safe reload support.
- To ensure reload safety functions have to be wrapped. Every call
checks the loaded state and locks a mutex in Plugin. If the plugin
is unloaded, calling its functions throws a lua error. Therefore,
plugins may not create closures or export yieldable functions.
- The set of function argument and return types supported by
LuaWrapper is severely limited when compared to being compiled
inside the main library.
Currently supported types: numbers, bool, std::string, df::foo,
df::foo*, std::vector<bool>, std::vector<df::foo*>.
- To facilitate postponing initialization until after all plugins
have been loaded, the core sends a SC_CORE_INITIALIZED event.
- As an example, the burrows plugin now exports its functions.
2012-04-14 09:44:07 -06:00
|
|
|
local plugname = string.match(module,'^plugins%.(%w+)$')
|
|
|
|
if plugname then
|
|
|
|
dfhack.open_plugin(pkg,plugname)
|
|
|
|
end
|
2012-04-21 10:15:57 -06:00
|
|
|
setmetatable(pkg, { __index = (env or base_env) })
|
2012-04-01 02:50:56 -06:00
|
|
|
return pkg
|
|
|
|
end
|
|
|
|
|
|
|
|
function reload(module)
|
|
|
|
if type(package.loaded[module]) ~= 'table' then
|
|
|
|
error("Module not loaded: "..module)
|
|
|
|
end
|
|
|
|
local path,err = package.searchpath(module,package.path)
|
|
|
|
if not path then
|
|
|
|
error(err)
|
|
|
|
end
|
|
|
|
dofile(path)
|
|
|
|
end
|
|
|
|
|
2012-04-05 09:55:59 -06:00
|
|
|
-- Misc functions
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
function printall(table)
|
2012-04-06 01:21:28 -06:00
|
|
|
if type(table) == 'table' or df.isvalid(table) == 'ref' then
|
|
|
|
for k,v in pairs(table) do
|
|
|
|
print(string.format("%-23s\t = %s",tostring(k),tostring(v)))
|
|
|
|
end
|
2012-04-01 06:43:40 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-11 09:42:05 -06:00
|
|
|
function copyall(table)
|
|
|
|
local rv = {}
|
|
|
|
for k,v in pairs(table) do rv[k] = v end
|
|
|
|
return rv
|
|
|
|
end
|
|
|
|
|
2012-04-12 08:37:27 -06:00
|
|
|
function pos2xyz(pos)
|
2012-05-13 03:58:41 -06:00
|
|
|
if pos then
|
|
|
|
local x = pos.x
|
|
|
|
if x and x ~= -30000 then
|
|
|
|
return x, pos.y, pos.z
|
|
|
|
end
|
2012-04-12 08:37:27 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function xyz2pos(x,y,z)
|
|
|
|
if x then
|
|
|
|
return {x=x,y=y,z=z}
|
|
|
|
else
|
|
|
|
return {x=-30000,y=-30000,z=-30000}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-26 02:56:28 -06:00
|
|
|
function rawset_default(target,source)
|
|
|
|
for k,v in pairs(source) do
|
|
|
|
if rawget(target,k) == nil then
|
|
|
|
rawset(target,k,v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-23 11:30:53 -06:00
|
|
|
function safe_index(obj,idx,...)
|
|
|
|
if obj == nil or idx == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if type(idx) == 'number' and (idx < 0 or idx >= #obj) then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
obj = obj[idx]
|
|
|
|
if select('#',...) > 0 then
|
|
|
|
return safe_index(obj,...)
|
|
|
|
else
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- String conversions
|
|
|
|
|
2012-04-15 09:09:25 -06:00
|
|
|
function dfhack.event:__tostring()
|
|
|
|
return "<event>"
|
|
|
|
end
|
|
|
|
|
2012-04-01 06:43:40 -06:00
|
|
|
function dfhack.persistent:__tostring()
|
|
|
|
return "<persistent "..self.entry_id..":"..self.key.."=\""
|
|
|
|
..self.value.."\":"..table.concat(self.ints,",")..">"
|
|
|
|
end
|
|
|
|
|
2012-04-06 09:56:19 -06:00
|
|
|
function dfhack.matinfo:__tostring()
|
|
|
|
return "<material "..self.type..":"..self.index.." "..self:getToken()..">"
|
|
|
|
end
|
|
|
|
|
2012-04-11 06:20:16 -06:00
|
|
|
function dfhack.maps.getSize()
|
|
|
|
local map = df.global.world.map
|
|
|
|
return map.x_count_block, map.y_count_block, map.z_count_block
|
|
|
|
end
|
|
|
|
|
|
|
|
function dfhack.maps.getTileSize()
|
|
|
|
local map = df.global.world.map
|
|
|
|
return map.x_count, map.y_count, map.z_count
|
|
|
|
end
|
|
|
|
|
2012-05-01 08:55:30 -06:00
|
|
|
function dfhack.buildings.getSize(bld)
|
|
|
|
local x, y = bld.x1, bld.y1
|
|
|
|
return bld.x2+1-x, bld.y2+1-y, bld.centerx-x, bld.centery-y
|
|
|
|
end
|
|
|
|
|
2012-04-15 11:50:22 -06:00
|
|
|
-- Interactive
|
|
|
|
|
|
|
|
local print_banner = true
|
|
|
|
|
|
|
|
function dfhack.interpreter(prompt,hfile,env)
|
|
|
|
if not dfhack.is_interactive() then
|
|
|
|
return nil, 'not interactive'
|
|
|
|
end
|
|
|
|
|
|
|
|
print("Type quit to exit interactive lua interpreter.")
|
|
|
|
|
|
|
|
if print_banner then
|
|
|
|
print("Shortcuts:\n"..
|
|
|
|
" '= foo' => '_1,_2,... = foo'\n"..
|
|
|
|
" '! foo' => 'print(foo)'\n"..
|
|
|
|
"Both save the first result as '_'.")
|
|
|
|
print_banner = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local prompt_str = "["..(prompt or 'lua').."]# "
|
2012-04-23 11:30:53 -06:00
|
|
|
local prompt_cont = string.rep(' ',#prompt_str-4)..">>> "
|
2012-04-15 11:50:22 -06:00
|
|
|
local prompt_env = {}
|
2012-04-23 11:30:53 -06:00
|
|
|
local cmdlinelist = {}
|
|
|
|
local t_prompt = nil
|
2012-04-15 11:50:22 -06:00
|
|
|
local vcnt = 1
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
local pfix_handlers = {
|
|
|
|
['!'] = function(data)
|
|
|
|
print(table.unpack(data,2,data.n))
|
|
|
|
end,
|
|
|
|
['~'] = function(data)
|
|
|
|
print(table.unpack(data,2,data.n))
|
|
|
|
printall(data[2])
|
|
|
|
end,
|
|
|
|
['='] = function(data)
|
|
|
|
for i=2,data.n do
|
|
|
|
local varname = '_'..vcnt
|
|
|
|
prompt_env[varname] = data[i]
|
|
|
|
dfhack.print(varname..' = ')
|
|
|
|
safecall(print, data[i])
|
|
|
|
vcnt = vcnt + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2012-04-15 11:50:22 -06:00
|
|
|
setmetatable(prompt_env, { __index = env or _G })
|
2012-04-23 11:30:53 -06:00
|
|
|
|
2012-04-15 11:50:22 -06:00
|
|
|
while true do
|
2012-04-16 09:46:20 -06:00
|
|
|
local cmdline = dfhack.lineedit(t_prompt or prompt_str, hfile)
|
2012-04-15 11:50:22 -06:00
|
|
|
|
|
|
|
if cmdline == nil or cmdline == 'quit' then
|
|
|
|
break
|
|
|
|
elseif cmdline ~= '' then
|
|
|
|
local pfix = string.sub(cmdline,1,1)
|
|
|
|
|
2012-04-29 11:07:39 -06:00
|
|
|
if not t_prompt and pfix_handlers[pfix] then
|
2012-04-15 11:50:22 -06:00
|
|
|
cmdline = 'return '..string.sub(cmdline,2)
|
2012-04-23 11:30:53 -06:00
|
|
|
else
|
|
|
|
pfix = nil
|
2012-04-15 11:50:22 -06:00
|
|
|
end
|
2012-04-23 11:30:53 -06:00
|
|
|
|
|
|
|
table.insert(cmdlinelist,cmdline)
|
|
|
|
cmdline = table.concat(cmdlinelist,'\n')
|
|
|
|
|
|
|
|
local code,err = load(cmdline, '=(interactive)', 't', prompt_env)
|
2012-04-15 11:50:22 -06:00
|
|
|
|
|
|
|
if code == nil then
|
2012-04-23 11:30:53 -06:00
|
|
|
if not pfix and err:sub(-5)=="<eof>" then
|
|
|
|
t_prompt=prompt_cont
|
|
|
|
else
|
|
|
|
dfhack.printerr(err)
|
|
|
|
cmdlinelist={}
|
|
|
|
t_prompt=nil
|
|
|
|
end
|
2012-04-15 11:50:22 -06:00
|
|
|
else
|
2012-04-23 11:30:53 -06:00
|
|
|
cmdlinelist={}
|
|
|
|
t_prompt=nil
|
|
|
|
|
2012-04-15 11:50:22 -06:00
|
|
|
local data = table.pack(safecall(code))
|
|
|
|
|
|
|
|
if data[1] and data.n > 1 then
|
|
|
|
prompt_env._ = data[2]
|
2012-04-29 11:07:39 -06:00
|
|
|
safecall(pfix_handlers[pfix], data)
|
2012-04-15 11:50:22 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2012-05-04 09:47:18 -06:00
|
|
|
-- Command scripts
|
|
|
|
|
|
|
|
dfhack.scripts = dfhack.scripts or {}
|
|
|
|
|
|
|
|
function dfhack.run_script(file,...)
|
|
|
|
local env = dfhack.scripts[file]
|
|
|
|
if env == nil then
|
|
|
|
env = {}
|
|
|
|
setmetatable(env, { __index = base_env })
|
|
|
|
dfhack.scripts[file] = env
|
|
|
|
end
|
|
|
|
local f,perr = loadfile(file, 't', env)
|
|
|
|
if f == nil then
|
|
|
|
error(perr)
|
|
|
|
end
|
|
|
|
return f(...)
|
|
|
|
end
|
|
|
|
|
2012-04-01 02:50:56 -06:00
|
|
|
-- Feed the table back to the require() mechanism.
|
2012-03-31 05:40:54 -06:00
|
|
|
return dfhack
|