mirror of
https://github.com/minetest-mods/carpets.git
synced 2024-12-20 20:35:42 +01:00
some modutils optimizations
This commit is contained in:
parent
33ee91939b
commit
b7e08f3a2e
2
init.lua
2
init.lua
@ -10,7 +10,7 @@ local depmod = modutils.get_depend_checker(modname)
|
||||
|
||||
local function filter(name, def)
|
||||
-- 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
|
||||
end
|
||||
|
||||
|
40
modutils.lua
40
modutils.lua
@ -1,9 +1,39 @@
|
||||
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
|
||||
if depends[modname] then
|
||||
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
|
||||
return false
|
||||
end
|
||||
@ -14,7 +44,9 @@ function modutils.get_depend_checker(modname)
|
||||
local this = { }
|
||||
this.modname = modname
|
||||
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
|
||||
local modpath = minetest.get_modpath(modname)
|
||||
@ -32,8 +64,10 @@ function modutils.get_depend_checker(modname)
|
||||
for line in dependsfile:lines() do
|
||||
if line:sub(-1) == "?" then
|
||||
line = line:sub(1, -2)
|
||||
this.depends[line] = "optional"
|
||||
else
|
||||
this.depends[line] = "required"
|
||||
end
|
||||
this.depends[line] = true
|
||||
end
|
||||
|
||||
return this
|
||||
|
Loading…
Reference in New Issue
Block a user