diff options
Diffstat (limited to 'mesecons_luacontroller')
-rw-r--r-- | mesecons_luacontroller/init.lua | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/mesecons_luacontroller/init.lua b/mesecons_luacontroller/init.lua index 1c411dd..3abe6ed 100644 --- a/mesecons_luacontroller/init.lua +++ b/mesecons_luacontroller/init.lua @@ -37,6 +37,24 @@ local rules = { d = {x = 0, y = 0, z = -1, name="D"}, } +-- Mods can place their own "libraries" in here to be loaded via require() from in a Luacontroller. +-- These can take two different forms: +-- Function (recommended for libraries adding new functionality): A function that, when called, returns something that will be passed to the LuaC code. +-- Function signature is getlibrary(env, pos) where 'env' is the environment that the Luacontroller code is running in, and 'pos' is the position of the controller. +-- Table (recommended for libraries containing mostly lookup tables): A table that will be copied, and the copy returned to the LuaC code. +-- When using the table format, any functions in the table will have their environment changed to that of the Luacontroller. +mesecon.luacontroller_libraries = {} + +--This prepares the actual require() function that will be available in the LuaC environment. +local function get_require(pos, env) + return function(name) + if type(mesecon.luacontroller_libraries[name]) == "function" then + return mesecon.luacontroller_libraries[name](env, pos) + elseif type(mesecon.luacontroller_libraries[name]) == "table" then + return mesecon.tablecopy_change_env(mesecon.luacontroller_libraries[name], env) + end + end +end ------------------ -- Action stuff -- @@ -546,6 +564,8 @@ local function create_environment(pos, mem, event, itbl, send_warning) for _, name in pairs(safe_globals) do env[name] = _G[name] end + + env.require = get_require(pos, env) return env end |