diff --git a/init.lua b/init.lua index 658f2a2..16aa90a 100644 --- a/init.lua +++ b/init.lua @@ -5,28 +5,12 @@ local modpath = minetest.get_modpath(modname) dofile(modpath .. "/api.lua") -local depends = (function() - local file = io.open(modpath .. "/depends.txt") - if not file then - return {} - end - - local depends = {} - for line in file:lines() do - if line:sub(-1) == "?" then - line = line:sub(1, -2) - end - depends[line] = true - end - - file:close() - - return depends -end)() +local modutils = dofile(modpath .. "/modutils.lua") +local depmod = modutils.get_depend_checker(modname) local function filter(name, def) -- disable carpets from loaded modules but not defined in dependency - if not depends[def.mod_origin] then + if not depmod:check_depend(def.mod_origin) then return false end diff --git a/modutils.lua b/modutils.lua new file mode 100644 index 0000000..b7634c5 --- /dev/null +++ b/modutils.lua @@ -0,0 +1,42 @@ +local modutils = {} + +local function check_depend(this, modname) + local depends = this.depends + if depends[modname] then + return true + else + return false + end +end + +function modutils.get_depend_checker(modname) + -- Definition of returning object attributes + local this = { } + this.modname = modname + this.depends = {} -- depends.txt parsed + this.check_depend = check_depend -- method + + -- get module path + local modpath = minetest.get_modpath(modname) + if not modpath then + return nil -- module not found + end + + -- read the depends file + local dependsfile = io.open(modpath .. "/depends.txt") + if not dependsfile then + return nil + end + + -- parse the depends file + for line in dependsfile:lines() do + if line:sub(-1) == "?" then + line = line:sub(1, -2) + end + this.depends[line] = true + end + + return this +end + +return modutils