modlib/init.lua

147 lines
3.6 KiB
Lua
Raw Normal View History

2021-03-29 14:37:57 +02:00
local modules = {}
for _, file in pairs{
"schema",
"file",
"func",
2022-10-01 18:11:44 +02:00
"less_than",
"iterator",
2021-03-29 14:37:57 +02:00
"math",
"table",
"vararg",
2021-03-29 14:37:57 +02:00
"text",
2022-09-09 14:17:18 +02:00
"utf8",
2021-03-29 14:37:57 +02:00
"vector",
"matrix4",
2021-03-29 14:37:57 +02:00
"quaternion",
"trie",
"kdtree",
2021-07-12 21:32:15 +02:00
"hashlist",
2022-01-19 16:45:47 +01:00
"hashheap",
2021-03-29 14:37:57 +02:00
"heap",
"binary",
"b3d",
2021-09-07 21:08:46 +02:00
"json",
2021-07-06 21:56:20 +02:00
"luon",
2021-04-21 21:11:00 +02:00
"bluon",
2023-02-28 13:39:20 +01:00
"base64",
2021-05-04 16:18:46 +02:00
"persistence",
2022-01-09 17:32:11 +01:00
"debug",
"web"
2021-03-29 14:37:57 +02:00
} do
modules[file] = file
2021-03-29 14:37:57 +02:00
end
if minetest then
modules.minetest = "minetest"
2021-03-29 14:37:57 +02:00
end
2022-10-03 18:27:34 +02:00
-- modlib.mod is an alias for modlib.minetest.mod
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
2022-10-03 18:22:59 +02:00
-- TOFIX
2021-06-17 19:45:08 +02:00
local init_path = arg and arg[0]
parent_dir = init_path and init_path:match"^.[/\\]" or ""
end
2022-10-03 18:22:59 +02:00
local dir_delim = rawget(_G, "DIR_DELIM") -- Minetest
or (rawget(_G, "package") and package.config and assert(package.config:match("^(.-)[\r\n]"))) or "/"
local function concat_path(path)
return table.concat(path, dir_delim)
end
2021-06-17 19:45:08 +02:00
-- 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 concat_path{minetest.get_modpath(modname), resource, ...}
2021-06-17 19:45:08 +02:00
end
2021-11-06 18:45:12 +01:00
local function load_module(self, module_name_or_alias)
local module_name = modules[module_name_or_alias]
if not module_name then
-- no such module
return
end
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
return environment
end
local rawget, rawset = rawget, rawset
2022-10-03 18:22:59 +02:00
modlib = setmetatable({}, { __index = load_module })
-- TODO bump on release
2023-04-02 15:10:53 +02:00
modlib.version = 102
2022-10-03 18:22:59 +02:00
if minetest then
modlib.modname = minetest.get_current_modname()
end
-- Raw globals
modlib._RG = setmetatable({}, {
__index = function(_, index)
return rawget(_G, index)
end,
__newindex = function(_, index, value)
return rawset(_G, index, value)
end
})
-- Globals merged with modlib
modlib.G = setmetatable({}, {__index = function(self, module_name)
local module = load_module(self, module_name)
if module == nil then
return _G[module_name]
end
if _G[module_name] then
setmetatable(module, {__index = _G[module_name]})
end
return module
end})
-- "Imports" modlib by changing the environment of the calling function
--! This alters environments at the expense of performance. Use with caution.
--! Prefer localizing modlib library functions or API tables if possible.
function modlib.set_environment()
setfenv(2, setmetatable({}, {__index = modlib.G}))
end
2021-02-01 16:22:37 +01:00
2021-11-06 18:38:08 +01:00
-- Force load file module to pass dir_delim & to set concat_path
modlib.file = assert(loadfile(get_resource"file.lua"))(dir_delim)
modlib.file.concat_path = concat_path
2021-03-29 14:37:57 +02:00
2021-08-15 22:07:31 +02:00
if minetest then
2022-10-03 18:27:34 +02:00
-- Force-loading of the minetest & mod modules
-- Also sets modlib.mod -> modlib.minetest.mod alias.
local ml_mt = modlib.minetest
ml_mt.mod.get_resource = get_resource
modlib.mod = ml_mt.mod
2021-08-16 17:21:28 +02:00
-- HACK force load minetest/gametime.lua to ensure that the globalstep is registered earlier than globalsteps of mods depending on modlib
2021-08-15 22:07:31 +02:00
dofile(get_resource(modlib.modname, "minetest", "gametime.lua"))
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-04 12:18:26 +01:00
end
2020-02-28 22:12:05 +01:00
2022-01-04 13:42:46 +01:00
-- Run build scripts
-- dofile(modlib.mod.get_resource("modlib", "build", "html_entities.lua"))
2020-03-23 20:20:43 +01:00
2021-06-17 19:45:08 +02:00
-- TODO verify localizations suffice
2023-03-04 15:06:56 +01:00
return modlib