some modutils optimizations

This commit is contained in:
Alexander Weber 2016-11-05 17:23:52 +01:00
parent 33ee91939b
commit b7e08f3a2e
2 changed files with 39 additions and 5 deletions

@ -10,7 +10,7 @@ local depmod = modutils.get_depend_checker(modname)
local function filter(name, def) local function filter(name, def)
-- disable carpets from loaded modules but not defined in dependency -- disable carpets from loaded modules but not defined in dependency
if not depmod:check_depend(def.mod_origin) then if not depmod:check_depend_by_itemname(name) then
return false return false
end end

@ -1,9 +1,39 @@
local modutils = {} local modutils = {}
local function check_depend(this, modname) -- class methods
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
-- object methods
local function check_depend(this, modname, deptype)
local depends = this.depends local depends = this.depends
if depends[modname] then if depends[modname] then
return true if not deptype then --"required" or "optional" only
return true
else
return (depends[modname] == deptype)
end
else
return false
end
end
local function check_depend_by_itemname(this, itemname, deptype)
local modname = modutils.get_modname_by_itemname(itemname)
if modname then
return this:check_depend(modname, deptype)
else else
return false return false
end end
@ -14,7 +44,9 @@ function modutils.get_depend_checker(modname)
local this = { } local this = { }
this.modname = modname this.modname = modname
this.depends = {} -- depends.txt parsed this.depends = {} -- depends.txt parsed
this.check_depend = check_depend -- method this.check_depend = check_depend
this.check_depend_by_itemname = check_depend_by_itemname
-- get module path -- get module path
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
@ -32,8 +64,10 @@ function modutils.get_depend_checker(modname)
for line in dependsfile:lines() do for line in dependsfile:lines() do
if line:sub(-1) == "?" then if line:sub(-1) == "?" then
line = line:sub(1, -2) line = line:sub(1, -2)
this.depends[line] = "optional"
else
this.depends[line] = "required"
end end
this.depends[line] = true
end end
return this return this