Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add library support to Luacontrollers #557

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions mesecons/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,24 @@ function mesecon.tablecopy(obj) -- deep copy
return obj
end

-- Performs a deep copy of a table, changing the environment of any functions.
-- Adapted from the builtin table.copy() function.
function mesecon.tablecopy_change_env(t, env, seen)
local n = {}
seen = seen or {}
seen[t] = n
for k, v in pairs(t) do
if type(v) == "function" then
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
end
end
return n
end

-- Returns whether two values are equal.
-- In tables, keys are compared for identity but values are compared recursively.
-- There is no protection from infinite recursion.
Expand Down
20 changes: 20 additions & 0 deletions mesecons_luacontroller/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,24 @@ 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 = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have this mentioned somewhere, eg. in the code comment at the top of the file.


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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repetitive mesecon.luacontroller_libraries[name]

end
end
end

local safe_globals = {
-- Don't add pcall/xpcall unless willing to deal with the consequences (unless very careful, incredibly likely to allow killing server indirectly)
"assert", "error", "ipairs", "next", "pairs", "select",
Expand Down Expand Up @@ -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
Expand Down