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