view DependencyLoader/DependencyLoader.lua @ 11:47d15fc9208e

updated the .pkgmeta to reflect the directory changes added LibScriptLink external changed the main module's Dependencies to OptDeps moved embeds.xml and Ace3.xml
author mckenziemc
date Sun, 05 Dec 2010 03:39:26 -0800
parents e0a4a8b5b389
children b230b94d4487
line wrap: on
line source
--	DependencyLoader
--	

local addonName, addonTable = ...


local DependencyLoader = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceHook-3.0")
_G[addonName] = DependencyLoader

addonTable.interface = DependencyLoader


local libPrint = LibStub("LibPrint-1.0")
local LibScriptLink = LibStub("LibScriptLink-1.0")

DependencyLoader.printStream = libPrint:NewStream("DependencyLoader", "DpLdr", print)
DependencyLoader.debugStream = libPrint:NewStream("DependencyLoader", "DpLdr", "mcm")


function DependencyLoader:Print(...)
	self.printStream:Print(...)
end


function DependencyLoader:Debug(...)
	self.debugStream:Print(...)
end


function DependencyLoader:OnInitialize()
	self:Debug("Initializing", addonName)
	self:Enable()
end


function DependencyLoader:OnEnable()
	self:Print("Enabled", addonName)
	
	self:Hook("EnableAddOn", true)
	
	self:FixCurrentAddons()
end


function DependencyLoader:OnDisable()
	self:Print("Disabled", addonName)
end


--	Does not consider user settings or addon errata.
function DependencyLoader:IsForceLoadAvailable()
	return IsLoggedIn() and true or false
end


function DependencyLoader:IsForceLoadAllowed()
	--	TODO: check user settings
	return true
end


function DependencyLoader:CanForceLoad()
	return self:IsForceLoadAvailable() and self:IsForceLoadAllowed()
end


--	Enables any dependencies needed by the addons 
--	that have already been enabled
function DependencyLoader:FixCurrentAddons()
	local requestReload = false
	
	for i=1, GetNumAddOns() do
		local addon = addonTable.classes.Addon:Get(i)
		
		--	TODO: what if an addon was loaded but its deps were then disabled?
		if addon:IsEnabled() and not addon:IsLoaded() then
			local tree = addonTable.classes.Tree:Get(addon)
			
			if self:CanForceLoad() and tree:CanForceLoad() then
				tree:ForceLoad()
			elseif tree:CanLoD() then
				tree:PrepareForLoD()
			elseif tree:CanLoad() then
				tree:PrepareForReload()
				requestReload = true
			end
		end
	end
	
	if requestReload then
		local message = LibScriptLink:NewLink(ReloadUI) .. " to reload your UI."
		self:Print(message)
	end
end


function DependencyLoader:EnableAddOn(...)
	print("DependencyLoader:EnableAddOn", ...)
end