view DependencyLoader_Core/Addon.lua @ 0:9852fcd5e59e

initial import
author mckenziemc
date Tue, 30 Nov 2010 16:13:04 -0800
parents
children 930871e163bc
line wrap: on
line source
--	Addon
--	Represents individual addon modules


local addonName, addonTable = ...

print("running Addon.lua")

--	NOTE: I assume that the API addon functions are 
--	slightly quicker with an index than with a number.

--	TODO: modify the dependency stuff to use the Errata module if available

local Addon, addon = addonTable:NewClass("Addon")

function Addon:New(id)
	assert(type(id) == "number" or type(id) == "string")

	local instance = {}
	
	setmetatable(instance, self.instanceMetatable)
	
	if type(id) == "number" then
		--	TODO: make sure it's in range
		instance.index = id
		instance.name = GetAddOnInfo(id)
	else
		--	FIXME: allow blizzard addons?
		local index
		
		for i=1,GetNumAddOns() do
			if GetAddOnInfo(i) == id then
				index = i
				break
			end
		end
		
		if index then
			instance.name = GetAddOnInfo(id)
			instance.index = index
		else
			error("Addon not found")
		end
	end
	
	return instance
end


--	Checks if an addon exists with the specified name.
--	@param	addon	Name of the addon.
--	@return			True if the addon is present, false otherwise.
function Addon:Exists(addon)
	if type(addon) == "number" then
		if addon >= 1 and addon <= GetNumAddOns() then
			return true
		else
			return false
		end
	elseif type(addon) == "string" then
		local status = select(6, GetAddOnInfo(addon))
		
		if status == "MISSING" then
			return false
		else
			return true
		end
	else
		error()
	end
end


function addon:GetName()
	return self.name
end


function addon:GetIndex()
	return self.index
end


function addon:IsEnabled()
	--	FIXME: written while tired; review later
	local status = select(6, GetAddOnInfo(self.index))
	
	if status == "DISABLED"	then
		return false
	else
		return true
	end
end

--	FIXME: an addon may be present but unloadable if loading out of date addons is disabled.
--	NOTE: CanForceLoad and CanLoD don't check the status of dependencies

function addon:CanForceLoad()
	return true		--	TODO: check if there's any reason addons can't be forceloaded
end

function addon:CanLoD()
	--	FIXME: what will the client say about addons using LoadManagers if the LM was force-loaded?
	if IsAddOnLoadOnDemand(self.name) then
		return true
	else
		return false
	end
end

--	NOTE: only call for LoD, not force-loading
function addon:Load()
	assert(self:CanLoD())
	
	EnableAddOn(self.name)
	LoadAddOn(self.name)
end

function addon:ForceLoad()
	assert(self:CanForceLoad())
	--	TODO: make sure force-loading is available at this time
	
	EnableAddOn(self.name)	--	This should cause the game to also load this addon
end


function addon:GetDependencies()
	return GetAddOnDependencies(self.index)
end


function addon:GetEmbeds()
	local embeds = {}
	
	local embedString = GetAddOnMetadata(self.name, "X-Embeds")
	
	if embedString then
		for match in string.gmatch(embedString, "[^,%s]+") do
			table.insert(embeds, match)
		end
	end
	
	return unpack(embeds)
end

function addon:IsLoaded()
	if IsAddOnLoaded(self.index) then
		return true
	else
		return false
	end
end