2016-11-05 12:07:17 +01:00
|
|
|
local modutils = {}
|
2016-10-19 09:24:04 +02:00
|
|
|
|
2016-11-05 17:25:01 +01:00
|
|
|
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
|
2016-11-05 17:25:01 +01:00
|
|
|
else
|
2017-03-19 03:09:40 +01:00
|
|
|
return false
|
2016-11-05 17:25:01 +01:00
|
|
|
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
|
2016-10-19 09:24:04 +02:00
|
|
|
end
|
2016-11-05 12:07:17 +01:00
|
|
|
|
2016-10-19 23:31:52 +02:00
|
|
|
-- get module path
|
2016-10-19 09:24:04 +02:00
|
|
|
local modpath = minetest.get_modpath(modname)
|
|
|
|
if not modpath then
|
|
|
|
return nil -- module not found
|
|
|
|
end
|
2016-10-19 23:31:52 +02:00
|
|
|
|
|
|
|
-- read the depends file
|
2016-11-05 12:07:17 +01:00
|
|
|
local dependsfile = io.open(modpath .. "/depends.txt")
|
2016-10-19 23:31:52 +02:00
|
|
|
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"
|
2016-11-05 17:25:01 +01:00
|
|
|
else
|
2017-03-19 03:09:40 +01:00
|
|
|
self.depends[line] = "required"
|
2016-10-19 09:24:04 +02:00
|
|
|
end
|
|
|
|
end
|
2017-03-19 03:09:40 +01:00
|
|
|
dependsfile:close()
|
|
|
|
return self
|
2016-10-19 09:24:04 +02:00
|
|
|
end
|
2016-11-05 12:07:17 +01:00
|
|
|
|
|
|
|
return modutils
|