a-planet-alive/mods/lib_api/modlib/init.lua

169 lines
3.5 KiB
Lua
Raw Normal View History

2021-04-25 08:59:28 -07:00
local rawget, rawset = rawget, rawset
2020-11-01 05:11:26 -08:00
-- Lua version check
if _VERSION then
2021-04-03 06:32:35 -07: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
local 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-11-01 05:11:26 -08:00
end
2021-04-03 06:32:35 -07:00
local modules = {}
for _, file in pairs{
"schema",
"file",
"func",
"math",
"table",
"text",
"vector",
"quaternion",
"trie",
"kdtree",
"heap",
"ranked_set",
"binary",
"b3d",
2021-04-25 08:59:28 -07:00
"bluon",
2021-06-13 02:38:12 -07:00
"persistence",
"debug"
2021-04-03 06:32:35 -07:00
} do
modules[file] = file
end
if minetest then
modules.minetest = {
"misc",
"collisionboxes",
"liquid",
"raycast",
"wielditem_change",
2021-06-13 02:38:12 -07:00
"colorspec",
"schematic"
2021-04-03 06:32:35 -07:00
}
for _, file in pairs{
"log",
"player",
-- deprecated
"conf"
} do
modules[file] = file
end
end
2021-02-13 07:30:02 -08:00
2021-04-03 06:32:35 -07:00
local load_module, get_resource, loadfile_exports
modlib = setmetatable({
-- TODO bump on release
2021-06-13 02:38:12 -07:00
version = 69,
2021-04-03 06:32:35 -07:00
modname = minetest and minetest.get_current_modname(),
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 = minetest and function(v, value)
if not v then
error(dump(value), 2)
end
end
}, {
__index = function(self, module_name)
local files = modules[module_name]
2021-06-13 02:38:12 -07:00
local environment
2021-04-03 06:32:35 -07:00
if type(files) == "string" then
2021-06-13 02:38:12 -07:00
environment = load_module(files)
2021-04-03 06:32:35 -07:00
elseif files then
2021-06-13 02:38:12 -07:00
environment = loadfile_exports(get_resource(self.modname, module_name, files[1] .. ".lua"))
2021-04-03 06:32:35 -07:00
for index = 2, #files do
2021-06-13 02:38:12 -07:00
self.mod.include_env(get_resource(self.modname, module_name, files[index] .. ".lua"), environment)
2021-04-03 06:32:35 -07:00
end
2021-06-13 02:38:12 -07:00
else
return
end
local module = {}
for key, value in pairs(environment) do
-- Shallow copy. Doesn't use `modlib.table.shallowcopy` as that is part of a module, too.
module[key] = value
2021-04-03 06:32:35 -07:00
end
self[module_name] = module
return module
end
})
function get_resource(modname, resource, ...)
if not resource then
resource = modname
modname = minetest.get_current_modname()
end
return table.concat({minetest.get_modpath(modname), resource, ...}, modlib.dir_delim)
2020-11-01 05:11:26 -08:00
end
2021-04-03 06:32:35 -07:00
function loadfile_exports(filename)
local env = setmetatable({}, {__index = _G})
local file = assert(loadfile(filename))
setfenv(file, env)
file()
return env
2020-11-01 05:11:26 -08:00
end
2021-04-25 08:59:28 -07:00
local parent_dir
if not minetest then
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
2021-04-03 06:32:35 -07:00
function load_module(module_name)
local file = module_name .. ".lua"
return loadfile_exports(minetest and get_resource(modlib.modname, file) or (parent_dir .. file))
2020-11-01 05:11:26 -08:00
end
2021-04-03 06:32:35 -07:00
modlib.mod = minetest and loadfile_exports(get_resource(modlib.modname, "mod.lua"))
2021-02-02 10:53:21 -08:00
-- Aliases
modlib.string = modlib.text
modlib.number = modlib.math
2021-03-04 14:39:47 -08:00
if minetest then
2021-04-03 06:32:35 -07:00
modlib.conf.build_setting_tree()
2020-11-01 05:11:26 -08:00
2021-04-03 06:32:35 -07:00
modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
2021-03-04 14:39:47 -08:00
end
2020-11-01 05:11:26 -08:00
_ml = modlib
--[[
2021-04-03 06:32:35 -07:00
--modlib.mod.include"test.lua"
2021-03-04 14:39:47 -08:00
]]
return modlib