Tercio@23: --- **AceAddon-3.0** provides a template for creating addon objects. Tercio@23: -- It'll provide you with a set of callback functions that allow you to simplify the loading Tercio@23: -- process of your addon.\\ Tercio@23: -- Callbacks provided are:\\ Tercio@23: -- * **OnInitialize**, which is called directly after the addon is fully loaded. Tercio@23: -- * **OnEnable** which gets called during the PLAYER_LOGIN event, when most of the data provided by the game is already present. Tercio@23: -- * **OnDisable**, which is only called when your addon is manually being disabled. Tercio@23: -- @usage Tercio@23: -- -- A small (but complete) addon, that doesn't do anything, Tercio@23: -- -- but shows usage of the callbacks. Tercio@23: -- local MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") Tercio@23: -- Tercio@23: -- function MyAddon:OnInitialize() Tercio@23: -- -- do init tasks here, like loading the Saved Variables, Tercio@23: -- -- or setting up slash commands. Tercio@23: -- end Tercio@23: -- Tercio@23: -- function MyAddon:OnEnable() Tercio@23: -- -- Do more initialization here, that really enables the use of your addon. Tercio@23: -- -- Register Events, Hook functions, Create Frames, Get information from Tercio@23: -- -- the game that wasn't available in OnInitialize Tercio@23: -- end Tercio@23: -- Tercio@23: -- function MyAddon:OnDisable() Tercio@23: -- -- Unhook, Unregister Events, Hide frames that you created. Tercio@23: -- -- You would probably only use an OnDisable if you want to Tercio@23: -- -- build a "standby" mode, or be able to toggle modules on/off. Tercio@23: -- end Tercio@23: -- @class file Tercio@23: -- @name AceAddon-3.0.lua Tercio@23: -- @release $Id: AceAddon-3.0.lua 1084 2013-04-27 20:14:11Z nevcairiel $ Tercio@23: Tercio@23: local MAJOR, MINOR = "AceAddon-3.0", 12 Tercio@23: local AceAddon, oldminor = LibStub:NewLibrary(MAJOR, MINOR) Tercio@23: Tercio@23: if not AceAddon then return end -- No Upgrade needed. Tercio@23: Tercio@23: AceAddon.frame = AceAddon.frame or CreateFrame("Frame", "AceAddon30Frame") -- Our very own frame Tercio@23: AceAddon.addons = AceAddon.addons or {} -- addons in general Tercio@23: AceAddon.statuses = AceAddon.statuses or {} -- statuses of addon. Tercio@23: AceAddon.initializequeue = AceAddon.initializequeue or {} -- addons that are new and not initialized Tercio@23: AceAddon.enablequeue = AceAddon.enablequeue or {} -- addons that are initialized and waiting to be enabled Tercio@23: AceAddon.embeds = AceAddon.embeds or setmetatable({}, {__index = function(tbl, key) tbl[key] = {} return tbl[key] end }) -- contains a list of libraries embedded in an addon Tercio@23: Tercio@23: -- Lua APIs Tercio@23: local tinsert, tconcat, tremove = table.insert, table.concat, table.remove Tercio@23: local fmt, tostring = string.format, tostring Tercio@23: local select, pairs, next, type, unpack = select, pairs, next, type, unpack Tercio@23: local loadstring, assert, error = loadstring, assert, error Tercio@23: local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget Tercio@23: Tercio@23: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Tercio@23: -- List them here for Mikk's FindGlobals script Tercio@23: -- GLOBALS: LibStub, IsLoggedIn, geterrorhandler Tercio@23: Tercio@23: --[[ Tercio@23: xpcall safecall implementation Tercio@23: ]] Tercio@23: local xpcall = xpcall Tercio@23: Tercio@23: local function errorhandler(err) Tercio@23: return geterrorhandler()(err) Tercio@23: end Tercio@23: Tercio@23: local function CreateDispatcher(argCount) Tercio@23: local code = [[ Tercio@23: local xpcall, eh = ... Tercio@23: local method, ARGS Tercio@23: local function call() return method(ARGS) end Tercio@23: Tercio@23: local function dispatch(func, ...) Tercio@23: method = func Tercio@23: if not method then return end Tercio@23: ARGS = ... Tercio@23: return xpcall(call, eh) Tercio@23: end Tercio@23: Tercio@23: return dispatch Tercio@23: ]] Tercio@23: Tercio@23: local ARGS = {} Tercio@23: for i = 1, argCount do ARGS[i] = "arg"..i end Tercio@23: code = code:gsub("ARGS", tconcat(ARGS, ", ")) Tercio@23: return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler) Tercio@23: end Tercio@23: Tercio@23: local Dispatchers = setmetatable({}, {__index=function(self, argCount) Tercio@23: local dispatcher = CreateDispatcher(argCount) Tercio@23: rawset(self, argCount, dispatcher) Tercio@23: return dispatcher Tercio@23: end}) Tercio@23: Dispatchers[0] = function(func) Tercio@23: return xpcall(func, errorhandler) Tercio@23: end Tercio@23: Tercio@23: local function safecall(func, ...) Tercio@23: -- we check to see if the func is passed is actually a function here and don't error when it isn't Tercio@23: -- this safecall is used for optional functions like OnInitialize OnEnable etc. When they are not Tercio@23: -- present execution should continue without hinderance Tercio@23: if type(func) == "function" then Tercio@23: return Dispatchers[select('#', ...)](func, ...) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: -- local functions that will be implemented further down Tercio@23: local Enable, Disable, EnableModule, DisableModule, Embed, NewModule, GetModule, GetName, SetDefaultModuleState, SetDefaultModuleLibraries, SetEnabledState, SetDefaultModulePrototype Tercio@23: Tercio@23: -- used in the addon metatable Tercio@23: local function addontostring( self ) return self.name end Tercio@23: Tercio@23: -- Check if the addon is queued for initialization Tercio@23: local function queuedForInitialization(addon) Tercio@23: for i = 1, #AceAddon.initializequeue do Tercio@23: if AceAddon.initializequeue[i] == addon then Tercio@23: return true Tercio@23: end Tercio@23: end Tercio@23: return false Tercio@23: end Tercio@23: Tercio@23: --- Create a new AceAddon-3.0 addon. Tercio@23: -- Any libraries you specified will be embeded, and the addon will be scheduled for Tercio@23: -- its OnInitialize and OnEnable callbacks. Tercio@23: -- The final addon object, with all libraries embeded, will be returned. Tercio@23: -- @paramsig [object ,]name[, lib, ...] Tercio@23: -- @param object Table to use as a base for the addon (optional) Tercio@23: -- @param name Name of the addon object to create Tercio@23: -- @param lib List of libraries to embed into the addon Tercio@23: -- @usage Tercio@23: -- -- Create a simple addon object Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceEvent-3.0") Tercio@23: -- Tercio@23: -- -- Create a Addon object based on the table of a frame Tercio@23: -- local MyFrame = CreateFrame("Frame") Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon(MyFrame, "MyAddon", "AceEvent-3.0") Tercio@23: function AceAddon:NewAddon(objectorname, ...) Tercio@23: local object,name Tercio@23: local i=1 Tercio@23: if type(objectorname)=="table" then Tercio@23: object=objectorname Tercio@23: name=... Tercio@23: i=2 Tercio@23: else Tercio@23: name=objectorname Tercio@23: end Tercio@23: if type(name)~="string" then Tercio@23: error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) Tercio@23: end Tercio@23: if self.addons[name] then Tercio@23: error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - Addon '%s' already exists."):format(name), 2) Tercio@23: end Tercio@23: Tercio@23: object = object or {} Tercio@23: object.name = name Tercio@23: Tercio@23: local addonmeta = {} Tercio@23: local oldmeta = getmetatable(object) Tercio@23: if oldmeta then Tercio@23: for k, v in pairs(oldmeta) do addonmeta[k] = v end Tercio@23: end Tercio@23: addonmeta.__tostring = addontostring Tercio@23: Tercio@23: setmetatable( object, addonmeta ) Tercio@23: self.addons[name] = object Tercio@23: object.modules = {} Tercio@23: object.orderedModules = {} Tercio@23: object.defaultModuleLibraries = {} Tercio@23: Embed( object ) -- embed NewModule, GetModule methods Tercio@23: self:EmbedLibraries(object, select(i,...)) Tercio@23: Tercio@23: -- add to queue of addons to be initialized upon ADDON_LOADED Tercio@23: tinsert(self.initializequeue, object) Tercio@23: return object Tercio@23: end Tercio@23: Tercio@23: Tercio@23: --- Get the addon object by its name from the internal AceAddon registry. Tercio@23: -- Throws an error if the addon object cannot be found (except if silent is set). Tercio@23: -- @param name unique name of the addon object Tercio@23: -- @param silent if true, the addon is optional, silently return nil if its not found Tercio@23: -- @usage Tercio@23: -- -- Get the Addon Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: function AceAddon:GetAddon(name, silent) Tercio@23: if not silent and not self.addons[name] then Tercio@23: error(("Usage: GetAddon(name): 'name' - Cannot find an AceAddon '%s'."):format(tostring(name)), 2) Tercio@23: end Tercio@23: return self.addons[name] Tercio@23: end Tercio@23: Tercio@23: -- - Embed a list of libraries into the specified addon. Tercio@23: -- This function will try to embed all of the listed libraries into the addon Tercio@23: -- and error if a single one fails. Tercio@23: -- Tercio@23: -- **Note:** This function is for internal use by :NewAddon/:NewModule Tercio@23: -- @paramsig addon, [lib, ...] Tercio@23: -- @param addon addon object to embed the libs in Tercio@23: -- @param lib List of libraries to embed into the addon Tercio@23: function AceAddon:EmbedLibraries(addon, ...) Tercio@23: for i=1,select("#", ... ) do Tercio@23: local libname = select(i, ...) Tercio@23: self:EmbedLibrary(addon, libname, false, 4) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: -- - Embed a library into the addon object. Tercio@23: -- This function will check if the specified library is registered with LibStub Tercio@23: -- and if it has a :Embed function to call. It'll error if any of those conditions Tercio@23: -- fails. Tercio@23: -- Tercio@23: -- **Note:** This function is for internal use by :EmbedLibraries Tercio@23: -- @paramsig addon, libname[, silent[, offset]] Tercio@23: -- @param addon addon object to embed the library in Tercio@23: -- @param libname name of the library to embed Tercio@23: -- @param silent marks an embed to fail silently if the library doesn't exist (optional) Tercio@23: -- @param offset will push the error messages back to said offset, defaults to 2 (optional) Tercio@23: function AceAddon:EmbedLibrary(addon, libname, silent, offset) Tercio@23: local lib = LibStub:GetLibrary(libname, true) Tercio@23: if not lib and not silent then Tercio@23: error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Cannot find a library instance of %q."):format(tostring(libname)), offset or 2) Tercio@23: elseif lib and type(lib.Embed) == "function" then Tercio@23: lib:Embed(addon) Tercio@23: tinsert(self.embeds[addon], libname) Tercio@23: return true Tercio@23: elseif lib then Tercio@23: error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Library '%s' is not Embed capable"):format(libname), offset or 2) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: --- Return the specified module from an addon object. Tercio@23: -- Throws an error if the addon object cannot be found (except if silent is set) Tercio@23: -- @name //addon//:GetModule Tercio@23: -- @paramsig name[, silent] Tercio@23: -- @param name unique name of the module Tercio@23: -- @param silent if true, the module is optional, silently return nil if its not found (optional) Tercio@23: -- @usage Tercio@23: -- -- Get the Addon Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- -- Get the Module Tercio@23: -- MyModule = MyAddon:GetModule("MyModule") Tercio@23: function GetModule(self, name, silent) Tercio@23: if not self.modules[name] and not silent then Tercio@23: error(("Usage: GetModule(name, silent): 'name' - Cannot find module '%s'."):format(tostring(name)), 2) Tercio@23: end Tercio@23: return self.modules[name] Tercio@23: end Tercio@23: Tercio@23: local function IsModuleTrue(self) return true end Tercio@23: Tercio@23: --- Create a new module for the addon. Tercio@23: -- The new module can have its own embeded libraries and/or use a module prototype to be mixed into the module.\\ Tercio@23: -- A module has the same functionality as a real addon, it can have modules of its own, and has the same API as Tercio@23: -- an addon object. Tercio@23: -- @name //addon//:NewModule Tercio@23: -- @paramsig name[, prototype|lib[, lib, ...]] Tercio@23: -- @param name unique name of the module Tercio@23: -- @param prototype object to derive this module from, methods and values from this table will be mixed into the module (optional) Tercio@23: -- @param lib List of libraries to embed into the addon Tercio@23: -- @usage Tercio@23: -- -- Create a module with some embeded libraries Tercio@23: -- MyModule = MyAddon:NewModule("MyModule", "AceEvent-3.0", "AceHook-3.0") Tercio@23: -- Tercio@23: -- -- Create a module with a prototype Tercio@23: -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } Tercio@23: -- MyModule = MyAddon:NewModule("MyModule", prototype, "AceEvent-3.0", "AceHook-3.0") Tercio@23: function NewModule(self, name, prototype, ...) Tercio@23: if type(name) ~= "string" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end Tercio@23: if type(prototype) ~= "string" and type(prototype) ~= "table" and type(prototype) ~= "nil" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'prototype' - table (prototype), string (lib) or nil expected got '%s'."):format(type(prototype)), 2) end Tercio@23: Tercio@23: if self.modules[name] then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - Module '%s' already exists."):format(name), 2) end Tercio@23: Tercio@23: -- modules are basically addons. We treat them as such. They will be added to the initializequeue properly as well. Tercio@23: -- NewModule can only be called after the parent addon is present thus the modules will be initialized after their parent is. Tercio@23: local module = AceAddon:NewAddon(fmt("%s_%s", self.name or tostring(self), name)) Tercio@23: Tercio@23: module.IsModule = IsModuleTrue Tercio@23: module:SetEnabledState(self.defaultModuleState) Tercio@23: module.moduleName = name Tercio@23: Tercio@23: if type(prototype) == "string" then Tercio@23: AceAddon:EmbedLibraries(module, prototype, ...) Tercio@23: else Tercio@23: AceAddon:EmbedLibraries(module, ...) Tercio@23: end Tercio@23: AceAddon:EmbedLibraries(module, unpack(self.defaultModuleLibraries)) Tercio@23: Tercio@23: if not prototype or type(prototype) == "string" then Tercio@23: prototype = self.defaultModulePrototype or nil Tercio@23: end Tercio@23: Tercio@23: if type(prototype) == "table" then Tercio@23: local mt = getmetatable(module) Tercio@23: mt.__index = prototype Tercio@23: setmetatable(module, mt) -- More of a Base class type feel. Tercio@23: end Tercio@23: Tercio@23: safecall(self.OnModuleCreated, self, module) -- Was in Ace2 and I think it could be a cool thing to have handy. Tercio@23: self.modules[name] = module Tercio@23: tinsert(self.orderedModules, module) Tercio@23: Tercio@23: return module Tercio@23: end Tercio@23: Tercio@23: --- Returns the real name of the addon or module, without any prefix. Tercio@23: -- @name //addon//:GetName Tercio@23: -- @paramsig Tercio@23: -- @usage Tercio@23: -- print(MyAddon:GetName()) Tercio@23: -- -- prints "MyAddon" Tercio@23: function GetName(self) Tercio@23: return self.moduleName or self.name Tercio@23: end Tercio@23: Tercio@23: --- Enables the Addon, if possible, return true or false depending on success. Tercio@23: -- This internally calls AceAddon:EnableAddon(), thus dispatching a OnEnable callback Tercio@23: -- and enabling all modules of the addon (unless explicitly disabled).\\ Tercio@23: -- :Enable() also sets the internal `enableState` variable to true Tercio@23: -- @name //addon//:Enable Tercio@23: -- @paramsig Tercio@23: -- @usage Tercio@23: -- -- Enable MyModule Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyModule = MyAddon:GetModule("MyModule") Tercio@23: -- MyModule:Enable() Tercio@23: function Enable(self) Tercio@23: self:SetEnabledState(true) Tercio@23: Tercio@23: -- nevcairiel 2013-04-27: don't enable an addon/module if its queued for init still Tercio@23: -- it'll be enabled after the init process Tercio@23: if not queuedForInitialization(self) then Tercio@23: return AceAddon:EnableAddon(self) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: --- Disables the Addon, if possible, return true or false depending on success. Tercio@23: -- This internally calls AceAddon:DisableAddon(), thus dispatching a OnDisable callback Tercio@23: -- and disabling all modules of the addon.\\ Tercio@23: -- :Disable() also sets the internal `enableState` variable to false Tercio@23: -- @name //addon//:Disable Tercio@23: -- @paramsig Tercio@23: -- @usage Tercio@23: -- -- Disable MyAddon Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyAddon:Disable() Tercio@23: function Disable(self) Tercio@23: self:SetEnabledState(false) Tercio@23: return AceAddon:DisableAddon(self) Tercio@23: end Tercio@23: Tercio@23: --- Enables the Module, if possible, return true or false depending on success. Tercio@23: -- Short-hand function that retrieves the module via `:GetModule` and calls `:Enable` on the module object. Tercio@23: -- @name //addon//:EnableModule Tercio@23: -- @paramsig name Tercio@23: -- @usage Tercio@23: -- -- Enable MyModule using :GetModule Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyModule = MyAddon:GetModule("MyModule") Tercio@23: -- MyModule:Enable() Tercio@23: -- Tercio@23: -- -- Enable MyModule using the short-hand Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyAddon:EnableModule("MyModule") Tercio@23: function EnableModule(self, name) Tercio@23: local module = self:GetModule( name ) Tercio@23: return module:Enable() Tercio@23: end Tercio@23: Tercio@23: --- Disables the Module, if possible, return true or false depending on success. Tercio@23: -- Short-hand function that retrieves the module via `:GetModule` and calls `:Disable` on the module object. Tercio@23: -- @name //addon//:DisableModule Tercio@23: -- @paramsig name Tercio@23: -- @usage Tercio@23: -- -- Disable MyModule using :GetModule Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyModule = MyAddon:GetModule("MyModule") Tercio@23: -- MyModule:Disable() Tercio@23: -- Tercio@23: -- -- Disable MyModule using the short-hand Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") Tercio@23: -- MyAddon:DisableModule("MyModule") Tercio@23: function DisableModule(self, name) Tercio@23: local module = self:GetModule( name ) Tercio@23: return module:Disable() Tercio@23: end Tercio@23: Tercio@23: --- Set the default libraries to be mixed into all modules created by this object. Tercio@23: -- Note that you can only change the default module libraries before any module is created. Tercio@23: -- @name //addon//:SetDefaultModuleLibraries Tercio@23: -- @paramsig lib[, lib, ...] Tercio@23: -- @param lib List of libraries to embed into the addon Tercio@23: -- @usage Tercio@23: -- -- Create the addon object Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") Tercio@23: -- -- Configure default libraries for modules (all modules need AceEvent-3.0) Tercio@23: -- MyAddon:SetDefaultModuleLibraries("AceEvent-3.0") Tercio@23: -- -- Create a module Tercio@23: -- MyModule = MyAddon:NewModule("MyModule") Tercio@23: function SetDefaultModuleLibraries(self, ...) Tercio@23: if next(self.modules) then Tercio@23: error("Usage: SetDefaultModuleLibraries(...): cannot change the module defaults after a module has been registered.", 2) Tercio@23: end Tercio@23: self.defaultModuleLibraries = {...} Tercio@23: end Tercio@23: Tercio@23: --- Set the default state in which new modules are being created. Tercio@23: -- Note that you can only change the default state before any module is created. Tercio@23: -- @name //addon//:SetDefaultModuleState Tercio@23: -- @paramsig state Tercio@23: -- @param state Default state for new modules, true for enabled, false for disabled Tercio@23: -- @usage Tercio@23: -- -- Create the addon object Tercio@23: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") Tercio@23: -- -- Set the default state to "disabled" Tercio@23: -- MyAddon:SetDefaultModuleState(false) Tercio@23: -- -- Create a module and explicilty enable it Tercio@23: -- MyModule = MyAddon:NewModule("MyModule") Tercio@23: -- MyModule:Enable() Tercio@23: function SetDefaultModuleState(self, state) Tercio@23: if next(self.modules) then Tercio@23: error("Usage: SetDefaultModuleState(state): cannot change the module defaults after a module has been registered.", 2) Tercio@23: end Tercio@23: self.defaultModuleState = state Tercio@23: end Tercio@23: Tercio@23: --- Set the default prototype to use for new modules on creation. Tercio@23: -- Note that you can only change the default prototype before any module is created. Tercio@23: -- @name //addon//:SetDefaultModulePrototype Tercio@23: -- @paramsig prototype Tercio@23: -- @param prototype Default prototype for the new modules (table) Tercio@23: -- @usage Tercio@23: -- -- Define a prototype Tercio@23: -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } Tercio@23: -- -- Set the default prototype Tercio@23: -- MyAddon:SetDefaultModulePrototype(prototype) Tercio@23: -- -- Create a module and explicitly Enable it Tercio@23: -- MyModule = MyAddon:NewModule("MyModule") Tercio@23: -- MyModule:Enable() Tercio@23: -- -- should print "OnEnable called!" now Tercio@23: -- @see NewModule Tercio@23: function SetDefaultModulePrototype(self, prototype) Tercio@23: if next(self.modules) then Tercio@23: error("Usage: SetDefaultModulePrototype(prototype): cannot change the module defaults after a module has been registered.", 2) Tercio@23: end Tercio@23: if type(prototype) ~= "table" then Tercio@23: error(("Usage: SetDefaultModulePrototype(prototype): 'prototype' - table expected got '%s'."):format(type(prototype)), 2) Tercio@23: end Tercio@23: self.defaultModulePrototype = prototype Tercio@23: end Tercio@23: Tercio@23: --- Set the state of an addon or module Tercio@23: -- This should only be called before any enabling actually happend, e.g. in/before OnInitialize. Tercio@23: -- @name //addon//:SetEnabledState Tercio@23: -- @paramsig state Tercio@23: -- @param state the state of an addon or module (enabled=true, disabled=false) Tercio@23: function SetEnabledState(self, state) Tercio@23: self.enabledState = state Tercio@23: end Tercio@23: Tercio@23: Tercio@23: --- Return an iterator of all modules associated to the addon. Tercio@23: -- @name //addon//:IterateModules Tercio@23: -- @paramsig Tercio@23: -- @usage Tercio@23: -- -- Enable all modules Tercio@23: -- for name, module in MyAddon:IterateModules() do Tercio@23: -- module:Enable() Tercio@23: -- end Tercio@23: local function IterateModules(self) return pairs(self.modules) end Tercio@23: Tercio@23: -- Returns an iterator of all embeds in the addon Tercio@23: -- @name //addon//:IterateEmbeds Tercio@23: -- @paramsig Tercio@23: local function IterateEmbeds(self) return pairs(AceAddon.embeds[self]) end Tercio@23: Tercio@23: --- Query the enabledState of an addon. Tercio@23: -- @name //addon//:IsEnabled Tercio@23: -- @paramsig Tercio@23: -- @usage Tercio@23: -- if MyAddon:IsEnabled() then Tercio@23: -- MyAddon:Disable() Tercio@23: -- end Tercio@23: local function IsEnabled(self) return self.enabledState end Tercio@23: local mixins = { Tercio@23: NewModule = NewModule, Tercio@23: GetModule = GetModule, Tercio@23: Enable = Enable, Tercio@23: Disable = Disable, Tercio@23: EnableModule = EnableModule, Tercio@23: DisableModule = DisableModule, Tercio@23: IsEnabled = IsEnabled, Tercio@23: SetDefaultModuleLibraries = SetDefaultModuleLibraries, Tercio@23: SetDefaultModuleState = SetDefaultModuleState, Tercio@23: SetDefaultModulePrototype = SetDefaultModulePrototype, Tercio@23: SetEnabledState = SetEnabledState, Tercio@23: IterateModules = IterateModules, Tercio@23: IterateEmbeds = IterateEmbeds, Tercio@23: GetName = GetName, Tercio@23: } Tercio@23: local function IsModule(self) return false end Tercio@23: local pmixins = { Tercio@23: defaultModuleState = true, Tercio@23: enabledState = true, Tercio@23: IsModule = IsModule, Tercio@23: } Tercio@23: -- Embed( target ) Tercio@23: -- target (object) - target object to embed aceaddon in Tercio@23: -- Tercio@23: -- this is a local function specifically since it's meant to be only called internally Tercio@23: function Embed(target, skipPMixins) Tercio@23: for k, v in pairs(mixins) do Tercio@23: target[k] = v Tercio@23: end Tercio@23: if not skipPMixins then Tercio@23: for k, v in pairs(pmixins) do Tercio@23: target[k] = target[k] or v Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: Tercio@23: -- - Initialize the addon after creation. Tercio@23: -- This function is only used internally during the ADDON_LOADED event Tercio@23: -- It will call the **OnInitialize** function on the addon object (if present), Tercio@23: -- and the **OnEmbedInitialize** function on all embeded libraries. Tercio@23: -- Tercio@23: -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. Tercio@23: -- @param addon addon object to intialize Tercio@23: function AceAddon:InitializeAddon(addon) Tercio@23: safecall(addon.OnInitialize, addon) Tercio@23: Tercio@23: local embeds = self.embeds[addon] Tercio@23: for i = 1, #embeds do Tercio@23: local lib = LibStub:GetLibrary(embeds[i], true) Tercio@23: if lib then safecall(lib.OnEmbedInitialize, lib, addon) end Tercio@23: end Tercio@23: Tercio@23: -- we don't call InitializeAddon on modules specifically, this is handled Tercio@23: -- from the event handler and only done _once_ Tercio@23: end Tercio@23: Tercio@23: -- - Enable the addon after creation. Tercio@23: -- Note: This function is only used internally during the PLAYER_LOGIN event, or during ADDON_LOADED, Tercio@23: -- if IsLoggedIn() already returns true at that point, e.g. for LoD Addons. Tercio@23: -- It will call the **OnEnable** function on the addon object (if present), Tercio@23: -- and the **OnEmbedEnable** function on all embeded libraries.\\ Tercio@23: -- This function does not toggle the enable state of the addon itself, and will return early if the addon is disabled. Tercio@23: -- Tercio@23: -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. Tercio@23: -- Use :Enable on the addon itself instead. Tercio@23: -- @param addon addon object to enable Tercio@23: function AceAddon:EnableAddon(addon) Tercio@23: if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end Tercio@23: if self.statuses[addon.name] or not addon.enabledState then return false end Tercio@23: Tercio@23: -- set the statuses first, before calling the OnEnable. this allows for Disabling of the addon in OnEnable. Tercio@23: self.statuses[addon.name] = true Tercio@23: Tercio@23: safecall(addon.OnEnable, addon) Tercio@23: Tercio@23: -- make sure we're still enabled before continueing Tercio@23: if self.statuses[addon.name] then Tercio@23: local embeds = self.embeds[addon] Tercio@23: for i = 1, #embeds do Tercio@23: local lib = LibStub:GetLibrary(embeds[i], true) Tercio@23: if lib then safecall(lib.OnEmbedEnable, lib, addon) end Tercio@23: end Tercio@23: Tercio@23: -- enable possible modules. Tercio@23: local modules = addon.orderedModules Tercio@23: for i = 1, #modules do Tercio@23: self:EnableAddon(modules[i]) Tercio@23: end Tercio@23: end Tercio@23: return self.statuses[addon.name] -- return true if we're disabled Tercio@23: end Tercio@23: Tercio@23: -- - Disable the addon Tercio@23: -- Note: This function is only used internally. Tercio@23: -- It will call the **OnDisable** function on the addon object (if present), Tercio@23: -- and the **OnEmbedDisable** function on all embeded libraries.\\ Tercio@23: -- This function does not toggle the enable state of the addon itself, and will return early if the addon is still enabled. Tercio@23: -- Tercio@23: -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. Tercio@23: -- Use :Disable on the addon itself instead. Tercio@23: -- @param addon addon object to enable Tercio@23: function AceAddon:DisableAddon(addon) Tercio@23: if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end Tercio@23: if not self.statuses[addon.name] then return false end Tercio@23: Tercio@23: -- set statuses first before calling OnDisable, this allows for aborting the disable in OnDisable. Tercio@23: self.statuses[addon.name] = false Tercio@23: Tercio@23: safecall( addon.OnDisable, addon ) Tercio@23: Tercio@23: -- make sure we're still disabling... Tercio@23: if not self.statuses[addon.name] then Tercio@23: local embeds = self.embeds[addon] Tercio@23: for i = 1, #embeds do Tercio@23: local lib = LibStub:GetLibrary(embeds[i], true) Tercio@23: if lib then safecall(lib.OnEmbedDisable, lib, addon) end Tercio@23: end Tercio@23: -- disable possible modules. Tercio@23: local modules = addon.orderedModules Tercio@23: for i = 1, #modules do Tercio@23: self:DisableAddon(modules[i]) Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: return not self.statuses[addon.name] -- return true if we're disabled Tercio@23: end Tercio@23: Tercio@23: --- Get an iterator over all registered addons. Tercio@23: -- @usage Tercio@23: -- -- Print a list of all installed AceAddon's Tercio@23: -- for name, addon in AceAddon:IterateAddons() do Tercio@23: -- print("Addon: " .. name) Tercio@23: -- end Tercio@23: function AceAddon:IterateAddons() return pairs(self.addons) end Tercio@23: Tercio@23: --- Get an iterator over the internal status registry. Tercio@23: -- @usage Tercio@23: -- -- Print a list of all enabled addons Tercio@23: -- for name, status in AceAddon:IterateAddonStatus() do Tercio@23: -- if status then Tercio@23: -- print("EnabledAddon: " .. name) Tercio@23: -- end Tercio@23: -- end Tercio@23: function AceAddon:IterateAddonStatus() return pairs(self.statuses) end Tercio@23: Tercio@23: -- Following Iterators are deprecated, and their addon specific versions should be used Tercio@23: -- e.g. addon:IterateEmbeds() instead of :IterateEmbedsOnAddon(addon) Tercio@23: function AceAddon:IterateEmbedsOnAddon(addon) return pairs(self.embeds[addon]) end Tercio@23: function AceAddon:IterateModulesOfAddon(addon) return pairs(addon.modules) end Tercio@23: Tercio@23: -- Event Handling Tercio@23: local function onEvent(this, event, arg1) Tercio@23: -- 2011-08-17 nevcairiel - ignore the load event of Blizzard_DebugTools, so a potential startup error isn't swallowed up Tercio@23: if (event == "ADDON_LOADED" and arg1 ~= "Blizzard_DebugTools") or event == "PLAYER_LOGIN" then Tercio@23: -- if a addon loads another addon, recursion could happen here, so we need to validate the table on every iteration Tercio@23: while(#AceAddon.initializequeue > 0) do Tercio@23: local addon = tremove(AceAddon.initializequeue, 1) Tercio@23: -- this might be an issue with recursion - TODO: validate Tercio@23: if event == "ADDON_LOADED" then addon.baseName = arg1 end Tercio@23: AceAddon:InitializeAddon(addon) Tercio@23: tinsert(AceAddon.enablequeue, addon) Tercio@23: end Tercio@23: Tercio@23: if IsLoggedIn() then Tercio@23: while(#AceAddon.enablequeue > 0) do Tercio@23: local addon = tremove(AceAddon.enablequeue, 1) Tercio@23: AceAddon:EnableAddon(addon) Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: end Tercio@23: Tercio@23: AceAddon.frame:RegisterEvent("ADDON_LOADED") Tercio@23: AceAddon.frame:RegisterEvent("PLAYER_LOGIN") Tercio@23: AceAddon.frame:SetScript("OnEvent", onEvent) Tercio@23: Tercio@23: -- upgrade embeded Tercio@23: for name, addon in pairs(AceAddon.addons) do Tercio@23: Embed(addon, true) Tercio@23: end Tercio@23: Tercio@23: -- 2010-10-27 nevcairiel - add new "orderedModules" table Tercio@23: if oldminor and oldminor < 10 then Tercio@23: for name, addon in pairs(AceAddon.addons) do Tercio@23: addon.orderedModules = {} Tercio@23: for module_name, module in pairs(addon.modules) do Tercio@23: tinsert(addon.orderedModules, module) Tercio@23: end Tercio@23: end Tercio@23: end