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
|
2021-03-30 18:04:45 +02:00
|
|
|
local name = debug.getupvalue(fn, i)
|
2021-03-27 20:10:49 +01:00
|
|
|
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-03-29 14:37:57 +02:00
|
|
|
local modules = {}
|
|
|
|
for _, file in pairs{
|
|
|
|
"schema",
|
|
|
|
"file",
|
|
|
|
"func",
|
|
|
|
"math",
|
|
|
|
"table",
|
|
|
|
"text",
|
|
|
|
"vector",
|
|
|
|
"quaternion",
|
|
|
|
"trie",
|
|
|
|
"kdtree",
|
2021-07-12 21:32:15 +02:00
|
|
|
"hashlist",
|
2021-03-29 14:37:57 +02:00
|
|
|
"heap",
|
|
|
|
"ranked_set",
|
|
|
|
"binary",
|
|
|
|
"b3d",
|
2021-07-06 21:56:20 +02:00
|
|
|
"luon",
|
2021-04-21 21:11:00 +02:00
|
|
|
"bluon",
|
2021-05-04 16:18:46 +02:00
|
|
|
"persistence",
|
|
|
|
"debug"
|
2021-03-29 14:37:57 +02:00
|
|
|
} do
|
2021-06-18 20:55:08 +02:00
|
|
|
modules[file] = file
|
2021-03-29 14:37:57 +02:00
|
|
|
end
|
|
|
|
if minetest then
|
|
|
|
for _, file in pairs{
|
2021-06-17 19:45:08 +02:00
|
|
|
"minetest",
|
2021-03-29 14:37:57 +02:00
|
|
|
"log",
|
|
|
|
"player",
|
|
|
|
-- deprecated
|
|
|
|
"conf"
|
|
|
|
} do
|
2021-06-18 20:55:08 +02:00
|
|
|
modules[file] = file
|
2021-03-29 14:37:57 +02:00
|
|
|
end
|
|
|
|
end
|
2021-06-18 20:55:08 +02:00
|
|
|
-- aliases
|
|
|
|
modules.string = "text"
|
|
|
|
modules.number = "math"
|
2021-03-29 14:37:57 +02:00
|
|
|
|
2021-06-17 19:45:08 +02: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
|
|
|
|
|
|
|
|
-- only used if Minetest is available
|
|
|
|
local 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)
|
|
|
|
end
|
|
|
|
|
2021-06-18 20:44:07 +02:00
|
|
|
local rawget, rawset = rawget, rawset
|
2021-03-29 14:37:57 +02:00
|
|
|
modlib = setmetatable({
|
2021-03-27 21:01:29 +01:00
|
|
|
-- TODO bump on release
|
2021-08-09 15:24:01 +02:00
|
|
|
version = 72,
|
2021-03-29 14:37:57 +02:00
|
|
|
modname = minetest and minetest.get_current_modname(),
|
2021-06-17 19:45:08 +02:00
|
|
|
-- TODO
|
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
|
|
|
|
}),
|
2021-03-31 11:23:49 +02:00
|
|
|
assertdump = minetest and function(v, value)
|
2021-03-27 20:10:49 +01:00
|
|
|
if not v then
|
|
|
|
error(dump(value), 2)
|
|
|
|
end
|
|
|
|
end
|
2021-03-29 14:37:57 +02:00
|
|
|
}, {
|
2021-06-18 20:55:08 +02:00
|
|
|
__index = function(self, module_name_or_alias)
|
|
|
|
local module_name = modules[module_name_or_alias]
|
|
|
|
if not module_name then
|
2021-06-17 19:45:08 +02:00
|
|
|
-- module not available, return nil
|
2021-05-12 18:30:16 +02:00
|
|
|
return
|
|
|
|
end
|
2021-06-18 20:55:08 +02:00
|
|
|
local environment
|
|
|
|
if module_name ~= module_name_or_alias then
|
|
|
|
-- alias handling
|
|
|
|
environment = self[module_name]
|
|
|
|
else
|
|
|
|
environment = dofile(minetest
|
|
|
|
and get_resource(self.modname, module_name .. ".lua")
|
|
|
|
or (parent_dir .. module_name .. ".lua"))
|
|
|
|
end
|
|
|
|
self[module_name_or_alias] = environment
|
2021-06-17 19:45:08 +02:00
|
|
|
return environment
|
2021-03-29 14:37:57 +02:00
|
|
|
end
|
|
|
|
})
|
2021-02-01 16:22:37 +01:00
|
|
|
|
2021-06-17 19:45:08 +02:00
|
|
|
modlib.mod = minetest and dofile(get_resource(modlib.modname, "mod.lua"))
|
2021-03-29 14:37:57 +02:00
|
|
|
|
2021-03-04 12:18:26 +01:00
|
|
|
if minetest then
|
2021-08-09 14:30:55 +02:00
|
|
|
local ie = minetest.request_insecure_environment()
|
|
|
|
if ie then
|
|
|
|
-- Force load persistence namespace to pass insecure require
|
|
|
|
-- TODO currently no need to set _G.require, lsqlite3 loads no dependencies that way
|
|
|
|
modlib.persistence = assert(loadfile(get_resource"persistence.lua"))(ie.require)
|
|
|
|
end
|
2021-03-27 20:10:49 +01:00
|
|
|
modlib.conf.build_setting_tree()
|
|
|
|
modlib.mod.get_resource = get_resource
|
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
|
|
|
--[[
|
2021-03-25 17:46:04 +01:00
|
|
|
--modlib.mod.include"test.lua"
|
2021-03-04 12:18:26 +01:00
|
|
|
]]
|
|
|
|
|
2021-06-17 19:45:08 +02:00
|
|
|
-- TODO verify localizations suffice
|
2021-03-04 12:18:26 +01:00
|
|
|
return modlib
|