2020-11-15 11:28:05 +01:00
|
|
|
-- Silence builtin/strict.lua
|
|
|
|
setmetatable(_G, {})
|
2019-05-01 16:53:01 +02:00
|
|
|
-- Lua version check
|
|
|
|
if _VERSION then
|
|
|
|
if _VERSION < "Lua 5" then
|
2020-02-09 01:39:54 +01:00
|
|
|
error("Outdated Lua version! modlib requires Lua 5 or greater.")
|
2019-05-01 16:53:01 +02:00
|
|
|
end
|
2020-03-23 20:20:43 +01:00
|
|
|
if _VERSION > "Lua 5.1" then
|
2020-02-09 01:39:54 +01:00
|
|
|
-- not throwing error("Too new Lua version! modlib requires Lua 5.1 or smaller.") anymore
|
2020-03-13 18:13:45 +01:00
|
|
|
unpack = unpack or table.unpack -- unpack was moved to table.unpack in Lua 5.2
|
2020-02-09 01:39:54 +01:00
|
|
|
loadstring = load
|
|
|
|
function setfenv(fn, env)
|
|
|
|
local i = 1
|
|
|
|
while true do
|
|
|
|
name = debug.getupvalue(fn, i)
|
|
|
|
if name == "_ENV" then
|
|
|
|
debug.setupvalue(fn, i, env)
|
|
|
|
break
|
|
|
|
elseif not name then
|
|
|
|
break
|
2019-07-13 19:55:49 +02:00
|
|
|
end
|
|
|
|
end
|
2020-02-09 01:39:54 +01:00
|
|
|
return fn
|
2019-07-13 19:55:49 +02:00
|
|
|
end
|
2020-02-09 01:39:54 +01:00
|
|
|
function getfenv(fn)
|
|
|
|
local i = 1
|
|
|
|
local name, val
|
|
|
|
repeat
|
|
|
|
name, val = debug.getupvalue(fn, i)
|
|
|
|
if name == "_ENV" then
|
|
|
|
return val
|
2019-05-01 16:53:01 +02:00
|
|
|
end
|
2020-02-09 01:39:54 +01:00
|
|
|
i = i + 1
|
|
|
|
until not name
|
2019-05-05 15:09:26 +02:00
|
|
|
end
|
2019-05-03 19:04:47 +02:00
|
|
|
end
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2019-07-13 19:55:49 +02:00
|
|
|
|
2020-09-22 19:18:47 +02:00
|
|
|
-- TODO get rid of code duplication
|
2020-02-29 12:55:02 +01:00
|
|
|
local function get_resource(modname, resource)
|
2020-09-22 19:18:47 +02:00
|
|
|
if not resource then
|
|
|
|
resource = modname
|
|
|
|
modname = minetest.get_current_modname()
|
|
|
|
end
|
2020-02-29 12:55:02 +01:00
|
|
|
return minetest.get_modpath(modname) .. "/" .. resource
|
|
|
|
end
|
|
|
|
|
2020-02-28 22:12:05 +01:00
|
|
|
local function loadfile_exports(filename)
|
2020-09-22 19:18:47 +02:00
|
|
|
local env = setmetatable({}, {__index = _G})
|
2020-02-09 01:39:54 +01:00
|
|
|
local file = assert(loadfile(filename))
|
|
|
|
setfenv(file, env)
|
|
|
|
file()
|
|
|
|
return env
|
|
|
|
end
|
|
|
|
|
|
|
|
local components = {
|
2020-02-28 22:12:05 +01:00
|
|
|
mod = {},
|
2020-02-29 12:55:02 +01:00
|
|
|
class = {},
|
|
|
|
conf = {},
|
|
|
|
data = {},
|
|
|
|
file = {},
|
2020-03-23 20:20:43 +01:00
|
|
|
func = {},
|
2020-02-29 12:55:02 +01:00
|
|
|
log = {},
|
|
|
|
minetest = {},
|
|
|
|
number = {},
|
|
|
|
player = {},
|
|
|
|
table = {},
|
|
|
|
text = {string = "local"},
|
2020-09-22 19:18:47 +02:00
|
|
|
threading = {},
|
|
|
|
vector = {}
|
2019-05-01 16:53:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-09 01:39:54 +01:00
|
|
|
modlib = {}
|
2019-05-01 16:53:01 +02:00
|
|
|
|
2020-02-09 01:39:54 +01:00
|
|
|
for component, additional in pairs(components) do
|
2020-09-22 19:18:47 +02:00
|
|
|
local comp = loadfile_exports(get_resource(component .. ".lua"))
|
2020-02-09 01:39:54 +01:00
|
|
|
modlib[component] = comp
|
|
|
|
for alias, scope in pairs(additional) do
|
|
|
|
if scope == "global" then
|
|
|
|
_G[alias] = comp
|
|
|
|
else
|
|
|
|
modlib[alias] = comp
|
2019-05-01 16:53:01 +02:00
|
|
|
end
|
|
|
|
end
|
2020-02-09 01:39:54 +01:00
|
|
|
end
|
2019-05-01 16:53:01 +02:00
|
|
|
|
2020-06-02 23:07:33 +02:00
|
|
|
modlib.conf.build_setting_tree()
|
|
|
|
|
2020-02-28 22:12:05 +01:00
|
|
|
modlib.mod.loadfile_exports = loadfile_exports
|
|
|
|
|
2020-09-22 19:18:47 +02:00
|
|
|
_ml = modlib
|
2020-03-23 20:20:43 +01:00
|
|
|
|
2020-09-22 19:18:47 +02:00
|
|
|
--[[
|
|
|
|
--modlib.mod.include("test.lua")
|
|
|
|
]]
|