Some modutils rewrite

Add framework stuff to allow usage of modutils in other mods at the same time
This commit is contained in:
Alexander Weber 2016-10-19 23:28:55 +02:00
parent c058a4589e
commit 60270c9695
2 changed files with 54 additions and 37 deletions

@ -1,12 +1,13 @@
dofile(minetest.get_modpath("carpets").."/carpet_api.lua") dofile(minetest.get_modpath("carpets").."/carpet_api.lua")
dofile(minetest.get_modpath("carpets").."/modutils.lua") dofile(minetest.get_modpath("carpets").."/modutils.lua")
depmod = modutils.get_depmod("carpets") depmod = carpets.modutils.get_depmod("carpets")
function carpet.enabledfilter(name, def) function carpet.enabledfilter(name, def)
-- disable carpets from loaded modules but not defined in dependency -- disable carpets from loaded modules but not defined in dependency
if depmod.check_depmod(name) == false then if depmod:check_depmod(name) == false then
return false return false
end end

@ -1,53 +1,69 @@
modutils = {} -- not modutils related, will be moved to a framework, if the utils framework will be developed :/
-- The envroot is accessable by global variable with module name as name (like if used in "carpets" mod, the modutils are at carpets.modutils available)
-- Different per module environment root allow the use of diffent versions of the same tool in different mods
function modutils.get_depmod(modname) local currentmod = minetest.get_current_modname()
-- Get dependency object (depmod) local envroot = nil
-- Definition of returning object attributes if not currentmod or currentmod == "" then --not minetest or something hacky
local depmod = { envroot = _G --fallback for hacky calls, populate global
modname, -- module name from get_depmod else
deplist = {}, -- depends.txt parsed if not _G[currentmod] then
} _G[currentmod] = {}
end
envroot = _G[currentmod]
end
-- framework stuff done. -- Now the tool
envroot.modutils = {}
-- Definition of returning object methods -- Definition of returning object methods
function depmod.check_depmod(checknode) local _check_depmod = function(this, checknode)
-- check if the node (checknode) is from dependent module -- check if the node (checknode) is from dependent module
local delimpos = string.find(checknode, ":") local delimpos = string.find(checknode, ":")
if delimpos then if delimpos then
local checkmodname = string.sub(checknode, 1, delimpos - 1) local checkmodname = string.sub(checknode, 1, delimpos - 1)
for name, ref in pairs(depmod.deplist) do for name, ref in pairs(this.deplist) do
if name == checkmodname then if name == checkmodname then
return true return true
end
end end
return false
end end
return false
end end
end
-- Full returning object attributes function envroot.modutils.get_depmod(modname)
depmod.modname = modname
-- local variable definition -- Definition of returning object attributes
local depentry = {} -- Entry in deplist local this = { }
local depmodname -- temp value this.modname = modname
this.deplist = {} -- depends.txt parsed
this.check_depmod = _check_depmod -- method
-- get module path
local modpath = minetest.get_modpath(modname) local modpath = minetest.get_modpath(modname)
if not modpath then if not modpath then
return nil -- module not found return nil -- module not found
end end
-- read the depends file
local dependsfile = io.open(modpath.."/depends.txt") local dependsfile = io.open(modpath.."/depends.txt")
if dependsfile then if not dependsfile then
for dependsline in dependsfile:lines() do return nil
if string.sub(dependsline, -1) == "?" then
depentry.required = false
depmodname = string.sub(dependsline, 1, -2)
else
depentry.required = true
depmodname = dependsline
end
depmod.deplist[depmodname] = depentry
end
end end
return depmod -- parse the depends file
for dependsline in dependsfile:lines() do
local depentry = {} -- Entry in deplist
local depmodname
if string.sub(dependsline, -1) == "?" then
depentry.required = false
depmodname = string.sub(dependsline, 1, -2)
else
depentry.required = true
depmodname = dependsline
end
this.deplist[depmodname] = depentry
end
return this
end end