diff DependencyLoader/Tree.lua @ 15:a46bf694050c

cleaned up Tree's methods a bit and improved documentation Addon:Exists will now return false for Blizzard addons (needs to be handled better) Addon.lua will now use the raw hooks from the interface module fixed the inverted returns from IsForceLoadAvailable EnableAddOn and LoadAddOn hooks will now skip the extra processing if the addon does not exist or is a Blizzard addon moved the EnableAddOn queing to the interface
author mckenziemc
date Sat, 11 Dec 2010 01:48:39 -0800
parents b230b94d4487
children e7995d599184
line wrap: on
line diff
--- a/DependencyLoader/Tree.lua	Fri Dec 10 05:29:22 2010 -0800
+++ b/DependencyLoader/Tree.lua	Sat Dec 11 01:48:39 2010 -0800
@@ -12,15 +12,18 @@
 local Tree, tree = addonTable:NewClass("Tree")
 
 
+local classes = addonTable.classes
+
+
 Tree.trees = {}
 
 
---	internal
---	Creates a new tree object
+---	(private) Creates a new tree object.
+--	Assumptions: root addon exists and is not a Blizzard addon.
 --	@param	root	Name, index, or Addon object of the root addon.
 function Tree:New(root)
 	if type(root) ~= "table" then
-		root = addonTable.classes.Addon:Get(root)
+		root = classes.Addon:Get(root)
 	end
 	
 	local instance = {}
@@ -33,10 +36,11 @@
 
 
 ---	Retrieves the tree rooted at the specified addon
+--	Assumptions: root addon exists and is not a Blizzard addon
 --	@param	root	Name, index, or Addon object of the root.
 function Tree:Get(root)
 	if type(root) ~= "table" then
-		root = addonTable.classes.Addon:Get(root)
+		root = classes.Addon:Get(root)
 	end
 	
 	local tree = self.trees[root]
@@ -51,28 +55,40 @@
 end
 
 
---	Checks if the tree rooted at the specified addon 
+--	NOTE: All tree:Can functions should check the root too.
+--	That way, the hooks don't have to do "if addon:Can_ and tree:Can_", 
+--	plus the tree can cache info that includes the capabilities of the 
+--	root addon.
+
+
+---	Checks if the tree rooted at the specified addon 
 --	can be loaded if all dependencies are enabled 
 --	and the UI is reloaded.
 --	Basically makes sure all dependencies are installed.
 function tree:CanLoad()
-	if not self.root:CanLoad() then
+	local root = self.root
+	
+	if root:IsLoaded() then
+		return true
+	end
+	
+	if not root:CanLoad() then
 		return false
 	end
 
 	--	check all the dependencies
-	local dependencies = {self.root:GetDependencies()}
+	local dependencies = {root:GetDependencies()}
+	
 	for i, depName in pairs(dependencies) do
-		local dep = addonTable.classes.Addon:Get(depName)
-		
-		if not dep:CanLoad() then
+		if not classes.Addon:Exists(depName) then
 			return false
 		end
 		
-		local depTree = Tree:Get(depName)
+		local dep = classes.Addon:Get(depName)
+		local depTree = Tree:Get(dep)
 		
 		if not depTree:CanLoad() then
-			return false		--	missing dependency
+			return false
 		end
 	end
 	
@@ -81,25 +97,29 @@
 
 
 ---	Checks if the tree can be loaded on demand.
---	Does not consider force-loading; dependencies must 
---	be enabled and LoD, or loaded
+--	Does not allow for force-loading; dependencies must 
+--	already be loaded, or enabled and LoD.
 function tree:CanLoD()
-	--	since this will be used recursively, return true if 
-	--	this is already loaded.
-	if self.root:IsLoaded() then
+	local root = self.root
+	
+	if root:IsLoaded() then
 		return true
 	end
 	
-	--	true if all dependencies are loaded or LoD
-	
-	if not self.root:CanLoD() then
+	if not root:CanLoD() then
 		return false
 	end
 	
 	--	now check dependencies recursively
-	local dependencies = {self.root:GetDependencies()}
+	local dependencies = {root:GetDependencies()}
+	
 	for i, depName in pairs(dependencies) do
-		local depTree = Tree:Get(depName)
+		if not classes.Addon:Exists(depName) then
+			return false
+		end
+		
+		local dep = classes.Addon:Get(depName)
+		local depTree = Tree:Get(dep)
 		
 		if not depTree:CanLoD() then
 			return false
@@ -110,31 +130,35 @@
 end
 
 
----	Checks if this tree can be loaded on demand as long as
---	force-loading is allowed for preparing dependencies.
-function tree:CanLoDWithForce()
-	if not self.root:CanLoD() then
+---	Checks if this tree can be force-loaded.
+--	Does not check user settings nor if force-loading is actually available.
+--	@return	canForceLoad	True if this tree can be force loaded, false otherwise
+function tree:CanForceLoad()
+	local root = self.root
+	--	TODO: remove redundencies (for now they're here for design flexibility)
+	
+	--	this addon must be loaded, able to load-on-demand, or able to force-load
+	if root:IsLoaded() then
+		return true
+	end
+	
+	if not root:CanForceLoad() then
 		return false
 	end
 	
 	--	now check dependencies recursively
-	local dependencies = {self.root:GetDependencies()}
+	local dependencies = {root:GetDependencies()}
+	
 	for i, depName in pairs(dependencies) do
-		local dep = addonTable.classes.Addon:Get(depName)
-		local depTree = Tree:Get(depName)
-		
-		if not dep:CanLoad() then
+		if not classes.Addon:Exists(depName) then
 			return false
 		end
 		
-		if dep:CanLoD() then
-			if not depTree:CanLoDWithForce() then
-				return false
-			end
-		elseif dep:CanForceLoad() then
-			if not depTree:CanForceLoad() then
-				return
-			end
+		local dep = classes.Addon:Get(depName)
+		local depTree = Tree:Get(dep)
+		
+		if not depTree:CanForceLoad() then
+			return false
 		end
 	end
 	
@@ -142,94 +166,31 @@
 end
 
 
+---	Prepares this tree to be loaded, whether through 
+--	a ui reload or by loading on demand.
+--	Assumptions: CanLoad is true for this tree
+function tree:PrepareForLoad()
+	local root = self.root
+	
+	--	The Addon class will take care of delaying enables 
+	--	till after PLAYER_LOGIN if necessary.
+	
+	root:Enable()
+	
+	--	enable dependencies
+	local dependencies = {root:GetDependencies()}
+	
+	for i, depName in pairs(dependencies) do
+		Tree:Get(depName):PrepareForLoad()
+	end
 
----	Checks if this tree can be force-loaded.
---	Does not check user settings nor if force-loading is actually available.
---	@return	canForceLoad	True if this tree can be force loaded, false otherwise
-function tree:CanForceLoad()
-	--	TODO: remove redundencies (for now they're here for design flexibility)
+	--	prepare embeds, if they are available separately
+	local embeds = {root:GetEmbeds(addon)}
 	
-	--	this addon must be loaded, able to load-on-demand, or able to force-load
-	if self.root:IsLoaded() then
-		return true
-	end
-	
-	if not self.root:CanLoad() then
-		return false
-	end
-	
-	if not self.root:CanForceLoad() then
-		return false
-	end
-	
-	--	now check dependencies recursively
-	local dependencies = {self.root:GetDependencies()}
-	for i, depName in pairs(dependencies) do
-		local dep = addonTable.classes.Addon:Get(depName)
-		local depTree = Tree:Get(depName)
-		
-		if not dep:CanLoad() then
-			return false
+	for i, embedName in pairs(embeds) do
+		if classes.Addon:Exists(embedName) then
+			Tree:Get(embedName):PrepareForLoad()
 		end
-		
-		if not dep:CanLoD() and not dep:CanForceLoad() then
-			return false
-		end
-		
-		if not depTree:CanForceLoad() then
-			return false
-		end
-	end
-	
-	return
-end
-
-
-
---[[
---	Loads the tree rooted at the specified addon.
---	FIXME: load the root too or not?
---	Supports both LoD addons and those that require force-loading.
-function tree:Load(addon)
-	--	don't check if the tree can actually be loaded.
-	--	external code should do that itself to check if it 
-	--	should even call this at all.
-
-	if self:ForceLoadAvailable() then
-		--	LoD trees can also be force-loaded
-		self:ForceLoadTree(addon)
-	else
-		self:LoadLoDTree(addon)
-	end
-end
-]]
-
-
-function tree:PrepareForReload()
-		--	if force-loading is enabled then we have to queue this if the addon isn't lod
-	local function callback()
-		self.root:Enable()
-		
-		--	check dependencies
-		local dependencies = {self.root:GetDependencies()}
-		for i, depName in pairs(dependencies) do
-			Tree:Get(depName):PrepareForReload()
-		end
-
-		--	prepare embeds, if they are available separately
-		local embeds = {self.root:GetEmbeds(addon)}
-		for i, embedName in pairs(embeds) do
-			Tree:Get(embedName):PrepareForReload()
-		end
-	end
-	
-	if IsLoggedIn() then
-		callback()
-	else
-		--	TODO: replace with cleaner alternative?
-		local frame = CreateFrame("Frame")
-		frame:SetScript("OnEvent", callback)
-		frame:RegisterEvent("PLAYER_LOGIN")
 	end
 end
 
@@ -237,89 +198,79 @@
 --	I think the problem this solves is a major issue with 
 --	migrating to separate libs. think about it more and document 
 --	here and in project description
+
+---	Force-loads this addon tree. If a subtree can be loaded on 
+--	demand, this function will enable but not load the subtree.
+--	Assumptions: the root addon should be force-loaded, not just enabled.
+--function tree:ForceLoad()
+
+--	Assumptions: the root is loadable-on demand, and force-load is available.
+--	and tree can be force-loaded
 function tree:PrepareForLoD()
-	--	assume root is LoD
+	local root = self.root
 	
-	--	check dependencies
-	local dependencies = {self.root:GetDependencies(addon)}
+	--	prepare dependencies first (for consistency)
+	local dependencies = {root:GetDependencies()}
+	
 	for i, depName in pairs(dependencies) do
-		local depTree = Tree:Get(depName)
-		depTree:PrepareForLoD()
+		local dep = classes.Addon:Get(depName)
 		
-		--[[
-		if dep:CanLoD() then
-			--	don't load it now but make sure its dependencies are prepared
-			self:PrepareLoDTree(depName)
-		else
-			--	FIXME: if it's already loaded
-			--	force-load it now so we can load the parent on demand
-			self:ForceLoadTree(depName)
+		if not dep:IsLoaded() then
+			if dep:CanLoD() then
+				--	don't load it now but make sure its dependencies are prepared
+				Tree:Get(dep):PrepareForLoD()
+			else
+				--	Based on our assumption about the tree, this addon 
+				--	should be able to force-load.
+				--	force-load it now so we can load the parent on demand
+				Tree:Get(dep):ForceLoad()
+			end
 		end
-		]]
 	end
 
 	--	prepare embeds, if they are available separately
-	local embeds = {self.root:GetEmbeds(addon)}	--	FIXME: addon?
+	local embeds = {root:GetEmbeds()}
+	
 	for i, embedName in pairs(embeds) do
-		local embedTree = Tree:Get(embedName)
-		embedTree:PrepareForLoD()
-		
-		--[[
-		if embed then
-			if embed:CanLoD() then
-				--	don't load it now but make sure its dependencies are prepared
-				self:PrepareLoDTree(embedName)
-			else
-				--	FIXME: if it's already loaded
-				--	force-load it now so we can load the parent on demand
-				self:ForceLoadTree(depName)
+		if classes.Addon:Exists(embedName) then
+			local embed = classes.Addon:Get(embedName)
+			
+			if not embed:IsLoaded() then
+				if embed:CanLoD() then
+					Tree:Get(embed):PrepareForLoD()
+				else
+					Tree:Get(embed):ForceLoad()
+				end
 			end
 		end
-		]]
 	end
+
+	root:Enable()
 end
 
 
---	load the root too, since it may actually be a leaf
+---	Force-loads this tree.
+--	This will also load any LoD addons in the tree
 function tree:ForceLoad()
+	local root = self.root
+	
+	--	FIXME: if already loaded
+
 	--	load dependencies
-	local dependencies = {self.root:GetDependencies()}
+	local dependencies = {root:GetDependencies()}
+	
 	for i, depName in pairs(dependencies) do
-		local depTree = Tree:Get(depName)
-		depTree:ForceLoad()
+		Tree:Get(depName):ForceLoad()
 	end
 
 	--	load embeds, if they are available separately
-	local embeds = {self.root:GetEmbeds()}
+	local embeds = {root:GetEmbeds()}
+	
 	for i, embedName in pairs(embeds) do
-		local embedTree = Tree:Get(embedName)
-		embedTree:ForceLoad()
+		if classes.Addon:Exists(embedName) then
+			Tree:Get(embedName):ForceLoad()
+		end
 	end
 	
 	root:ForceLoad()
 end
-
-
---[[	don't think i need this
-function core:LoadLoDTree(root)
-	root = self:GetAddon(root)
-	assert(root)
-	
-	--	load dependencies
-	local dependencies = {root:GetDependencies(addon)}
-	for i, depName in pairs(dependencies) do
-		self:LoadLoDTree(depName)
-	end
-
-	--	load embeds, if they are available separately
-	local embeds = {root:GetEmbeds(addon)}
-	for i, embedName in pairs(embeds) do
-		if Addon:Exists(embedName) then
-			self:LoadLoDTree(embedName)
-		end
-	end
-	
-	root:LoD()
-end
-]]
-