Make modlib independent of Minetest

This commit is contained in:
Lars Mueller 2021-03-04 12:18:26 +01:00
parent d728157204
commit 44cca2569c
4 changed files with 35 additions and 16 deletions

@ -13,13 +13,15 @@ function build_tree(dict)
end
return tree
end
function build_setting_tree()
modlib.conf.settings = build_tree(minetest.settings:to_table())
end
-- deprecated, use modlib.mod.configuration instead
minetest.mkdir(minetest.get_worldpath().."/config")
function get_path(confname)
return minetest.get_worldpath().."/config/"..confname
if minetest then
function build_setting_tree()
modlib.conf.settings = build_tree(minetest.settings:to_table())
end
-- deprecated, use modlib.mod.configuration instead
minetest.mkdir(minetest.get_worldpath().."/config")
function get_path(confname)
return minetest.get_worldpath().."/config/"..confname
end
end
function read_conf(text)
local lines = modlib.text.split_lines(text, nil, true)

@ -47,6 +47,8 @@ end
function create_if_not_exists_from_file(filename, src_filename) return create_if_not_exists(filename, read(src_filename)) end
if not minetest then return end
-- Process Bridge Helpers
process_bridges = {}
@ -73,7 +75,7 @@ end
function process_bridge_listen(name, line_consumer, step)
local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
modlib.minetest.register_globalstep(step or 0.1, function()
local content = io.open(bridge.input, "r")
local line = content:read()
while line do
@ -86,7 +88,7 @@ end
function process_bridge_serve(name, step)
local bridge = process_bridges[name]
modlib.minetest.register_globalstep(step or 0.1, function(dtime)
modlib.minetest.register_globalstep(step or 0.1, function()
bridge.output_file:close()
process_bridges[name].output_file = io.open(bridge.output, "a")
end)
@ -95,7 +97,6 @@ end
function process_bridge_write(name, message)
local bridge = process_bridges[name]
bridge.output_file:write(message .. "\n")
-- append(bridge.input, message)
end
function process_bridge_start(name, command, os_execute)

@ -62,6 +62,15 @@ local function loadfile_exports(filename)
return env
end
local minetest_only = {
mod = true,
minetest = true,
data = true,
log = true,
player = true,
-- not actually minetest-only, but a deprecated component
conf = true
}
for _, component in ipairs{
"mod",
"conf",
@ -83,20 +92,27 @@ for _, component in ipairs{
"ranked_set",
"b3d"
} do
modlib[component] = loadfile_exports(get_resource(component .. ".lua"))
if minetest or not minetest_only[component] then
local path = minetest and get_resource(component .. ".lua") or component .. ".lua"
modlib[component] = loadfile_exports(path)
end
end
-- Aliases
modlib.string = modlib.text
modlib.number = modlib.math
modlib.conf.build_setting_tree()
if minetest then
modlib.conf.build_setting_tree()
modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
modlib.mod.get_resource = get_resource
modlib.mod.loadfile_exports = loadfile_exports
end
_ml = modlib
--[[
--modlib.mod.include("test.lua")
]]
]]
return modlib

@ -1,5 +1,5 @@
-- Make random random
math.randomseed(minetest.get_us_time())
math.randomseed(minetest and minetest.get_us_time() or os.time() + os.clock())
for _ = 1, 100 do math.random() end
function round(number, steps)