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