Skip to content

Commit 91e3b13

Browse files
author
cheapie
committed
Luacontroller library enhancements
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.
1 parent 9904be9 commit 91e3b13

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

mesecons/util.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,8 @@ function mesecon.tablecopy_change_env(t, env, seen)
201201
seen[t] = n
202202
for k, v in pairs(t) do
203203
if type(v) == "function" then
204-
local newfunc = v
205-
setfenv(newfunc, env)
206-
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = newfunc
204+
setfenv(v, env)
205+
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] = v
207206
else
208207
n[(type(k) == "table" and (seen[k] or mesecon.tablecopy_change_env(k, env, seen))) or k] =
209208
(type(v) == "table" and (seen[v] or mesecon.tablecopy_change_env(v, env, seen))) or v

mesecons_luacontroller/init.lua

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,12 +459,20 @@ local function get_digiline_send(pos, itbl, send_warning)
459459
end
460460
end
461461

462+
-- Mods can place their own "libraries" in here to be loaded via require() from in a Luacontroller.
463+
-- These can take two different forms:
464+
-- Function (recommended for libraries adding new functionality): A function that, when called, returns something that will be passed to the LuaC code.
465+
-- 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.
466+
-- Table (recommended for libraries containing mostly lookup tables): A table that will be copied, and the copy returned to the LuaC code.
467+
-- When using the table format, any functions in the table will have their environment changed to that of the Luacontroller.
462468
mesecon.luacontroller_libraries = {}
463469

464-
local function get_require(env)
470+
local function get_require(pos, env)
465471
return function(name)
466-
if mesecon.luacontroller_libraries[name] then
467-
return mesecon.tablecopy_change_env(mesecon.luacontroller_libraries[name],env)
472+
if type(mesecon.luacontroller_libraries[name]) == "function" then
473+
return mesecon.luacontroller_libraries[name](env, pos)
474+
elseif type(mesecon.luacontroller_libraries[name]) == "table" then
475+
return mesecon.tablecopy_change_env(mesecon.luacontroller_libraries[name], env)
468476
end
469477
end
470478
end
@@ -557,7 +565,7 @@ local function create_environment(pos, mem, event, itbl, send_warning)
557565
env[name] = _G[name]
558566
end
559567

560-
env.require = get_require(env)
568+
env.require = get_require(pos, env)
561569

562570
return env
563571
end

0 commit comments

Comments
 (0)