qa_block-cd2025/modutils.lua

72 lines
1.5 KiB
Lua
Raw Permalink Normal View History

2016-11-05 12:07:17 +01:00
local modutils = {}
function modutils.get_modname_by_itemname(itemname)
local def = minetest.registered_items[itemname]
if def then
return def.mod_origin
else --not loaded item
local delimpos = string.find(itemname, ":")
if delimpos then
return string.sub(itemname, 1, delimpos - 1)
else
return nil
end
end
end
2017-03-19 03:09:40 +01:00
function modutils.get_depend_checker(modname)
local self = {
modname = modname,
depends = {} -- depends.txt parsed
}
-- Check if dependency exists to mod modname
function self:check_depend(modname, deptype)
if self.depends[modname] then
if not deptype then --"required" or "optional" only
return true
else
return (self.depends[modname] == deptype)
end
else
2017-03-19 03:09:40 +01:00
return false
end
end
2017-03-19 03:09:40 +01:00
--Check if dependency exists to item origin mod
function self:check_depend_by_itemname(itemname, deptype)
local modname = modutils.get_modname_by_itemname(itemname)
if modname then
return self:check_depend(modname, deptype)
else
return false
end
end
2016-11-05 12:07:17 +01:00
-- get module path
local modpath = minetest.get_modpath(modname)
if not modpath then
return nil -- module not found
end
-- read the depends file
2016-11-05 12:07:17 +01:00
local dependsfile = io.open(modpath .. "/depends.txt")
if not dependsfile then
return nil
end
-- parse the depends file
2016-11-05 12:07:17 +01:00
for line in dependsfile:lines() do
if line:sub(-1) == "?" then
line = line:sub(1, -2)
2017-03-19 03:09:40 +01:00
self.depends[line] = "optional"
else
2017-03-19 03:09:40 +01:00
self.depends[line] = "required"
end
end
2017-03-19 03:09:40 +01:00
dependsfile:close()
return self
end
2016-11-05 12:07:17 +01:00
return modutils