Skip to content

Commit

Permalink
Luacontroller library enhancements
Browse files Browse the repository at this point in the history
Libraries can now be registered as a function which will be called when the library is requested. This allows functionality such as libraries that behave differently depending on where the Luacontroller is (for example, a sensor of some sort that only works if the LuaC is next to it) as well as various initialization that the library may need to perform. Supplying a table is still supported and works as before.
  • Loading branch information
cheapie committed Mar 26, 2021
1 parent 9904be9 commit 91e3b13
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
5 changes: 2 additions & 3 deletions mesecons/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ function mesecon.tablecopy_change_env(t, env, seen)
seen[t] = n
for k, v in pairs(t) do
if type(v) == "function" then
local newfunc = v
setfenv(newfunc, env)
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = newfunc
setfenv(v, env)
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = v
else
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] =
(type(v) == "table" and (seen[v] or mesecon.tablecopy_change_env(v, env, seen))) or v
Expand Down
16 changes: 12 additions & 4 deletions mesecons_luacontroller/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,20 @@ local function get_digiline_send(pos, itbl, send_warning)
end
end

-- 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 = {}

local function get_require(env)
local function get_require(pos, env)
return function(name)
if mesecon.luacontroller_libraries[name] then
return mesecon.tablecopy_change_env(mesecon.luacontroller_libraries[name],env)
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
Expand Down Expand Up @@ -557,7 +565,7 @@ local function create_environment(pos, mem, event, itbl, send_warning)
env[name] = _G[name]
end

env.require = get_require(env)
env.require = get_require(pos, env)

return env
end
Expand Down

0 comments on commit 91e3b13

Please sign in to comment.