From 60270c9695ef57823da36df7952eb0cfd4a8364d Mon Sep 17 00:00:00 2001
From: Alexander Weber <web.alexander@web.de>
Date: Wed, 19 Oct 2016 23:28:55 +0200
Subject: [PATCH] Some modutils rewrite Add framework stuff to allow usage of
 modutils in other mods at the same time

---
 init.lua     |  5 +--
 modutils.lua | 86 +++++++++++++++++++++++++++++++---------------------
 2 files changed, 54 insertions(+), 37 deletions(-)

diff --git a/init.lua b/init.lua
index 43524c5..80ea92c 100644
--- a/init.lua
+++ b/init.lua
@@ -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
 
diff --git a/modutils.lua b/modutils.lua
index aa3e6ff..d8e277f 100644
--- a/modutils.lua
+++ b/modutils.lua
@@ -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)
--- 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)
-		-- 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
-				if name == checkmodname then
-					return true
-				end
+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(this.deplist) do
+			if name == checkmodname then
+				return true
 			end
-			return false
 		end
+		return false
 	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
-		for dependsline in dependsfile:lines() do
-			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
+	if not dependsfile then
+		return nil
 	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