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-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
|
|
|
|
|
|
|
|
-- 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-01 02:50:56 -06:00
|
|
|
setmetatable(pkg, { __index = (env or _G) })
|
|
|
|
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)
|
|
|
|
local x = pos.x
|
|
|
|
if x and x ~= -30000 then
|
|
|
|
return x, pos.y, pos.z
|
|
|
|
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-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-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
|