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").."/modutils.lua")
depmod = modutils.get_depmod("carpets")
depmod = carpets.modutils.get_depmod("carpets")
function carpet.enabledfilter(name, def)
-- 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
end

@ -1,22 +1,29 @@
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)
-- Get dependency object (depmod)
local currentmod = minetest.get_current_modname()
local envroot = nil
-- Definition of returning object attributes
local depmod = {
modname, -- module name from get_depmod
deplist = {}, -- depends.txt parsed
}
if not currentmod or currentmod == "" then --not minetest or something hacky
envroot = _G --fallback for hacky calls, populate global
else
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
function depmod.check_depmod(checknode)
local _check_depmod = function(this, checknode)
-- check if the node (checknode) is from dependent module
local delimpos = string.find(checknode, ":")
if delimpos then
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
return true
end
@ -25,19 +32,29 @@ function modutils.get_depmod(modname)
end
end
-- Full returning object attributes
depmod.modname = modname
function envroot.modutils.get_depmod(modname)
-- local variable definition
local depentry = {} -- Entry in deplist
local depmodname -- temp value
-- Definition of returning object attributes
local this = { }
this.modname = modname
this.deplist = {} -- depends.txt parsed
this.check_depmod = _check_depmod -- 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 dependsfile then
if not dependsfile then
return nil
end
-- 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)
@ -45,9 +62,8 @@ function modutils.get_depmod(modname)
depentry.required = true
depmodname = dependsline
end
depmod.deplist[depmodname] = depentry
end
this.deplist[depmodname] = depentry
end
return depmod
return this
end