modlib/init.lua

142 lines
2.9 KiB
Lua
Raw Permalink Normal View History

2019-05-01 16:53:01 +02:00
-- Lua version check
if _VERSION then
2021-03-27 20:10:49 +01:00
if _VERSION < "Lua 5" then
error("Outdated Lua version! modlib requires Lua 5 or greater.")
end
if _VERSION > "Lua 5.1" then
-- not throwing error("Too new Lua version! modlib requires Lua 5.1 or smaller.") anymore
unpack = unpack or table.unpack -- unpack was moved to table.unpack in Lua 5.2
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
end
end
return fn
end
function getfenv(fn)
local i = 1
local name, val
repeat
name, val = debug.getupvalue(fn, i)
if name == "_ENV" then
return val
end
i = i + 1
until not name
end
end
2020-02-09 01:39:54 +01:00
end
2019-07-13 19:55:49 +02:00
2021-02-01 16:22:37 +01:00
modlib = {
2021-03-27 20:44:28 +01:00
-- TODO bump on release
version = 54,
2021-03-27 20:10:49 +01:00
dir_delim = rawget(_G, "DIR_DELIM") or "/",
_RG = setmetatable({}, {
__index = function(_, index)
return rawget(_G, index)
end,
__newindex = function(_, index, value)
return rawset(_G, index, value)
end
}),
assertdump = function(v, value)
if not v then
error(dump(value), 2)
end
end
2021-02-01 16:22:37 +01:00
}
local function get_resource(modname, resource, ...)
2021-03-27 20:10:49 +01:00
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
2020-02-29 12:55:02 +01:00
end
2020-02-28 22:12:05 +01:00
local function loadfile_exports(filename)
2021-03-27 20:10:49 +01:00
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
2020-02-09 01:39:54 +01:00
end
2021-03-04 12:18:26 +01:00
local minetest_only = {
2021-03-27 20:10:49 +01:00
mod = true,
minetest = true,
data = true,
log = true,
player = true,
-- not actually minetest-only, but a deprecated component
conf = true
2021-03-04 12:18:26 +01:00
}
for _, component in ipairs{
2021-03-27 20:10:49 +01:00
"mod",
"conf",
"schema",
"data",
"file",
"func",
"log",
"math",
"player",
"table",
"text",
"vector",
"quaternion",
{
name = "minetest",
"misc",
"collisionboxes",
"liquid",
"raycast",
"wielditem_change",
"colorspec"
},
"trie",
"kdtree",
"heap",
"ranked_set",
"binary",
"b3d",
"bluon"
} do
2021-03-27 20:10:49 +01:00
if component.name then
if minetest then
modlib[component.name] = loadfile_exports(get_resource(minetest.get_current_modname(), component.name, component[1] .. ".lua"))
for index = 2, #component do
modlib.mod.include_env(get_resource(minetest.get_current_modname(), component.name, component[index] .. ".lua"), modlib[component.name])
end
end
elseif minetest or not minetest_only[component] then
local path = minetest and get_resource(component .. ".lua") or component .. ".lua"
modlib[component] = loadfile_exports(path)
end
2020-02-09 01:39:54 +01:00
end
2019-05-01 16:53:01 +02:00
-- Aliases
modlib.string = modlib.text
modlib.number = modlib.math
2021-03-04 12:18:26 +01:00
if minetest then
2021-03-27 20:10:49 +01:00
modlib.conf.build_setting_tree()
2021-03-27 20:10:49 +01:00
modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
2021-03-04 12:18:26 +01:00
end
2020-02-28 22:12:05 +01:00
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"
2021-03-04 12:18:26 +01:00
]]
return modlib