Mercurial > wow > itemauditor
comparison Libs/AceAddon-3.0/AceAddon-3.0.lua @ 0:169f5211fc7f
First public revision.
At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
| author | Asa Ayers <Asa.Ayers@Gmail.com> | 
|---|---|
| date | Thu, 20 May 2010 19:22:19 -0700 | 
| parents | |
| children | 
   comparison
  equal
  deleted
  inserted
  replaced
| -1:000000000000 | 0:169f5211fc7f | 
|---|---|
| 1 --- **AceAddon-3.0** provides a template for creating addon objects. | |
| 2 -- It'll provide you with a set of callback functions that allow you to simplify the loading | |
| 3 -- process of your addon.\\ | |
| 4 -- Callbacks provided are:\\ | |
| 5 -- * **OnInitialize**, which is called directly after the addon is fully loaded. | |
| 6 -- * **OnEnable** which gets called during the PLAYER_LOGIN event, when most of the data provided by the game is already present. | |
| 7 -- * **OnDisable**, which is only called when your addon is manually being disabled. | |
| 8 -- @usage | |
| 9 -- -- A small (but complete) addon, that doesn't do anything, | |
| 10 -- -- but shows usage of the callbacks. | |
| 11 -- local MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") | |
| 12 -- | |
| 13 -- function MyAddon:OnInitialize() | |
| 14 -- -- do init tasks here, like loading the Saved Variables, | |
| 15 -- -- or setting up slash commands. | |
| 16 -- end | |
| 17 -- | |
| 18 -- function MyAddon:OnEnable() | |
| 19 -- -- Do more initialization here, that really enables the use of your addon. | |
| 20 -- -- Register Events, Hook functions, Create Frames, Get information from | |
| 21 -- -- the game that wasn't available in OnInitialize | |
| 22 -- end | |
| 23 -- | |
| 24 -- function MyAddon:OnDisable() | |
| 25 -- -- Unhook, Unregister Events, Hide frames that you created. | |
| 26 -- -- You would probably only use an OnDisable if you want to | |
| 27 -- -- build a "standby" mode, or be able to toggle modules on/off. | |
| 28 -- end | |
| 29 -- @class file | |
| 30 -- @name AceAddon-3.0.lua | |
| 31 -- @release $Id: AceAddon-3.0.lua 895 2009-12-06 16:28:55Z nevcairiel $ | |
| 32 | |
| 33 local MAJOR, MINOR = "AceAddon-3.0", 5 | |
| 34 local AceAddon, oldminor = LibStub:NewLibrary(MAJOR, MINOR) | |
| 35 | |
| 36 if not AceAddon then return end -- No Upgrade needed. | |
| 37 | |
| 38 AceAddon.frame = AceAddon.frame or CreateFrame("Frame", "AceAddon30Frame") -- Our very own frame | |
| 39 AceAddon.addons = AceAddon.addons or {} -- addons in general | |
| 40 AceAddon.statuses = AceAddon.statuses or {} -- statuses of addon. | |
| 41 AceAddon.initializequeue = AceAddon.initializequeue or {} -- addons that are new and not initialized | |
| 42 AceAddon.enablequeue = AceAddon.enablequeue or {} -- addons that are initialized and waiting to be enabled | |
| 43 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 | |
| 44 | |
| 45 -- Lua APIs | |
| 46 local tinsert, tconcat, tremove = table.insert, table.concat, table.remove | |
| 47 local fmt, tostring = string.format, tostring | |
| 48 local select, pairs, next, type, unpack = select, pairs, next, type, unpack | |
| 49 local loadstring, assert, error = loadstring, assert, error | |
| 50 local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget | |
| 51 | |
| 52 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded | |
| 53 -- List them here for Mikk's FindGlobals script | |
| 54 -- GLOBALS: LibStub, IsLoggedIn, geterrorhandler | |
| 55 | |
| 56 --[[ | |
| 57 xpcall safecall implementation | |
| 58 ]] | |
| 59 local xpcall = xpcall | |
| 60 | |
| 61 local function errorhandler(err) | |
| 62 return geterrorhandler()(err) | |
| 63 end | |
| 64 | |
| 65 local function CreateDispatcher(argCount) | |
| 66 local code = [[ | |
| 67 local xpcall, eh = ... | |
| 68 local method, ARGS | |
| 69 local function call() return method(ARGS) end | |
| 70 | |
| 71 local function dispatch(func, ...) | |
| 72 method = func | |
| 73 if not method then return end | |
| 74 ARGS = ... | |
| 75 return xpcall(call, eh) | |
| 76 end | |
| 77 | |
| 78 return dispatch | |
| 79 ]] | |
| 80 | |
| 81 local ARGS = {} | |
| 82 for i = 1, argCount do ARGS[i] = "arg"..i end | |
| 83 code = code:gsub("ARGS", tconcat(ARGS, ", ")) | |
| 84 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler) | |
| 85 end | |
| 86 | |
| 87 local Dispatchers = setmetatable({}, {__index=function(self, argCount) | |
| 88 local dispatcher = CreateDispatcher(argCount) | |
| 89 rawset(self, argCount, dispatcher) | |
| 90 return dispatcher | |
| 91 end}) | |
| 92 Dispatchers[0] = function(func) | |
| 93 return xpcall(func, errorhandler) | |
| 94 end | |
| 95 | |
| 96 local function safecall(func, ...) | |
| 97 -- we check to see if the func is passed is actually a function here and don't error when it isn't | |
| 98 -- this safecall is used for optional functions like OnInitialize OnEnable etc. When they are not | |
| 99 -- present execution should continue without hinderance | |
| 100 if type(func) == "function" then | |
| 101 return Dispatchers[select('#', ...)](func, ...) | |
| 102 end | |
| 103 end | |
| 104 | |
| 105 -- local functions that will be implemented further down | |
| 106 local Enable, Disable, EnableModule, DisableModule, Embed, NewModule, GetModule, GetName, SetDefaultModuleState, SetDefaultModuleLibraries, SetEnabledState, SetDefaultModulePrototype | |
| 107 | |
| 108 -- used in the addon metatable | |
| 109 local function addontostring( self ) return self.name end | |
| 110 | |
| 111 --- Create a new AceAddon-3.0 addon. | |
| 112 -- Any libraries you specified will be embeded, and the addon will be scheduled for | |
| 113 -- its OnInitialize and OnEnable callbacks. | |
| 114 -- The final addon object, with all libraries embeded, will be returned. | |
| 115 -- @paramsig [object ,]name[, lib, ...] | |
| 116 -- @param object Table to use as a base for the addon (optional) | |
| 117 -- @param name Name of the addon object to create | |
| 118 -- @param lib List of libraries to embed into the addon | |
| 119 -- @usage | |
| 120 -- -- Create a simple addon object | |
| 121 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceEvent-3.0") | |
| 122 -- | |
| 123 -- -- Create a Addon object based on the table of a frame | |
| 124 -- local MyFrame = CreateFrame("Frame") | |
| 125 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon(MyFrame, "MyAddon", "AceEvent-3.0") | |
| 126 function AceAddon:NewAddon(objectorname, ...) | |
| 127 local object,name | |
| 128 local i=1 | |
| 129 if type(objectorname)=="table" then | |
| 130 object=objectorname | |
| 131 name=... | |
| 132 i=2 | |
| 133 else | |
| 134 name=objectorname | |
| 135 end | |
| 136 if type(name)~="string" then | |
| 137 error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) | |
| 138 end | |
| 139 if self.addons[name] then | |
| 140 error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - Addon '%s' already exists."):format(name), 2) | |
| 141 end | |
| 142 | |
| 143 object = object or {} | |
| 144 object.name = name | |
| 145 | |
| 146 local addonmeta = {} | |
| 147 local oldmeta = getmetatable(object) | |
| 148 if oldmeta then | |
| 149 for k, v in pairs(oldmeta) do addonmeta[k] = v end | |
| 150 end | |
| 151 addonmeta.__tostring = addontostring | |
| 152 | |
| 153 setmetatable( object, addonmeta ) | |
| 154 self.addons[name] = object | |
| 155 object.modules = {} | |
| 156 object.defaultModuleLibraries = {} | |
| 157 Embed( object ) -- embed NewModule, GetModule methods | |
| 158 self:EmbedLibraries(object, select(i,...)) | |
| 159 | |
| 160 -- add to queue of addons to be initialized upon ADDON_LOADED | |
| 161 tinsert(self.initializequeue, object) | |
| 162 return object | |
| 163 end | |
| 164 | |
| 165 | |
| 166 --- Get the addon object by its name from the internal AceAddon registry. | |
| 167 -- Throws an error if the addon object cannot be found (except if silent is set). | |
| 168 -- @param name unique name of the addon object | |
| 169 -- @param silent if true, the addon is optional, silently return nil if its not found | |
| 170 -- @usage | |
| 171 -- -- Get the Addon | |
| 172 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 173 function AceAddon:GetAddon(name, silent) | |
| 174 if not silent and not self.addons[name] then | |
| 175 error(("Usage: GetAddon(name): 'name' - Cannot find an AceAddon '%s'."):format(tostring(name)), 2) | |
| 176 end | |
| 177 return self.addons[name] | |
| 178 end | |
| 179 | |
| 180 -- - Embed a list of libraries into the specified addon. | |
| 181 -- This function will try to embed all of the listed libraries into the addon | |
| 182 -- and error if a single one fails. | |
| 183 -- | |
| 184 -- **Note:** This function is for internal use by :NewAddon/:NewModule | |
| 185 -- @paramsig addon, [lib, ...] | |
| 186 -- @param addon addon object to embed the libs in | |
| 187 -- @param lib List of libraries to embed into the addon | |
| 188 function AceAddon:EmbedLibraries(addon, ...) | |
| 189 for i=1,select("#", ... ) do | |
| 190 local libname = select(i, ...) | |
| 191 self:EmbedLibrary(addon, libname, false, 4) | |
| 192 end | |
| 193 end | |
| 194 | |
| 195 -- - Embed a library into the addon object. | |
| 196 -- This function will check if the specified library is registered with LibStub | |
| 197 -- and if it has a :Embed function to call. It'll error if any of those conditions | |
| 198 -- fails. | |
| 199 -- | |
| 200 -- **Note:** This function is for internal use by :EmbedLibraries | |
| 201 -- @paramsig addon, libname[, silent[, offset]] | |
| 202 -- @param addon addon object to embed the library in | |
| 203 -- @param libname name of the library to embed | |
| 204 -- @param silent marks an embed to fail silently if the library doesn't exist (optional) | |
| 205 -- @param offset will push the error messages back to said offset, defaults to 2 (optional) | |
| 206 function AceAddon:EmbedLibrary(addon, libname, silent, offset) | |
| 207 local lib = LibStub:GetLibrary(libname, true) | |
| 208 if not lib and not silent then | |
| 209 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Cannot find a library instance of %q."):format(tostring(libname)), offset or 2) | |
| 210 elseif lib and type(lib.Embed) == "function" then | |
| 211 lib:Embed(addon) | |
| 212 tinsert(self.embeds[addon], libname) | |
| 213 return true | |
| 214 elseif lib then | |
| 215 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Library '%s' is not Embed capable"):format(libname), offset or 2) | |
| 216 end | |
| 217 end | |
| 218 | |
| 219 --- Return the specified module from an addon object. | |
| 220 -- Throws an error if the addon object cannot be found (except if silent is set) | |
| 221 -- @name //addon//:GetModule | |
| 222 -- @paramsig name[, silent] | |
| 223 -- @param name unique name of the module | |
| 224 -- @param silent if true, the module is optional, silently return nil if its not found (optional) | |
| 225 -- @usage | |
| 226 -- -- Get the Addon | |
| 227 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 228 -- -- Get the Module | |
| 229 -- MyModule = MyAddon:GetModule("MyModule") | |
| 230 function GetModule(self, name, silent) | |
| 231 if not self.modules[name] and not silent then | |
| 232 error(("Usage: GetModule(name, silent): 'name' - Cannot find module '%s'."):format(tostring(name)), 2) | |
| 233 end | |
| 234 return self.modules[name] | |
| 235 end | |
| 236 | |
| 237 local function IsModuleTrue(self) return true end | |
| 238 | |
| 239 --- Create a new module for the addon. | |
| 240 -- The new module can have its own embeded libraries and/or use a module prototype to be mixed into the module.\\ | |
| 241 -- A module has the same functionality as a real addon, it can have modules of its own, and has the same API as | |
| 242 -- an addon object. | |
| 243 -- @name //addon//:NewModule | |
| 244 -- @paramsig name[, prototype|lib[, lib, ...]] | |
| 245 -- @param name unique name of the module | |
| 246 -- @param prototype object to derive this module from, methods and values from this table will be mixed into the module (optional) | |
| 247 -- @param lib List of libraries to embed into the addon | |
| 248 -- @usage | |
| 249 -- -- Create a module with some embeded libraries | |
| 250 -- MyModule = MyAddon:NewModule("MyModule", "AceEvent-3.0", "AceHook-3.0") | |
| 251 -- | |
| 252 -- -- Create a module with a prototype | |
| 253 -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } | |
| 254 -- MyModule = MyAddon:NewModule("MyModule", prototype, "AceEvent-3.0", "AceHook-3.0") | |
| 255 function NewModule(self, name, prototype, ...) | |
| 256 if type(name) ~= "string" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end | |
| 257 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 | |
| 258 | |
| 259 if self.modules[name] then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - Module '%s' already exists."):format(name), 2) end | |
| 260 | |
| 261 -- modules are basically addons. We treat them as such. They will be added to the initializequeue properly as well. | |
| 262 -- NewModule can only be called after the parent addon is present thus the modules will be initialized after their parent is. | |
| 263 local module = AceAddon:NewAddon(fmt("%s_%s", self.name or tostring(self), name)) | |
| 264 | |
| 265 module.IsModule = IsModuleTrue | |
| 266 module:SetEnabledState(self.defaultModuleState) | |
| 267 module.moduleName = name | |
| 268 | |
| 269 if type(prototype) == "string" then | |
| 270 AceAddon:EmbedLibraries(module, prototype, ...) | |
| 271 else | |
| 272 AceAddon:EmbedLibraries(module, ...) | |
| 273 end | |
| 274 AceAddon:EmbedLibraries(module, unpack(self.defaultModuleLibraries)) | |
| 275 | |
| 276 if not prototype or type(prototype) == "string" then | |
| 277 prototype = self.defaultModulePrototype or nil | |
| 278 end | |
| 279 | |
| 280 if type(prototype) == "table" then | |
| 281 local mt = getmetatable(module) | |
| 282 mt.__index = prototype | |
| 283 setmetatable(module, mt) -- More of a Base class type feel. | |
| 284 end | |
| 285 | |
| 286 safecall(self.OnModuleCreated, self, module) -- Was in Ace2 and I think it could be a cool thing to have handy. | |
| 287 self.modules[name] = module | |
| 288 | |
| 289 return module | |
| 290 end | |
| 291 | |
| 292 --- Returns the real name of the addon or module, without any prefix. | |
| 293 -- @name //addon//:GetName | |
| 294 -- @paramsig | |
| 295 -- @usage | |
| 296 -- print(MyAddon:GetName()) | |
| 297 -- -- prints "MyAddon" | |
| 298 function GetName(self) | |
| 299 return self.moduleName or self.name | |
| 300 end | |
| 301 | |
| 302 --- Enables the Addon, if possible, return true or false depending on success. | |
| 303 -- This internally calls AceAddon:EnableAddon(), thus dispatching a OnEnable callback | |
| 304 -- and enabling all modules of the addon (unless explicitly disabled).\\ | |
| 305 -- :Enable() also sets the internal `enableState` variable to true | |
| 306 -- @name //addon//:Enable | |
| 307 -- @paramsig | |
| 308 -- @usage | |
| 309 -- -- Enable MyModule | |
| 310 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 311 -- MyModule = MyAddon:GetModule("MyModule") | |
| 312 -- MyModule:Enable() | |
| 313 function Enable(self) | |
| 314 self:SetEnabledState(true) | |
| 315 return AceAddon:EnableAddon(self) | |
| 316 end | |
| 317 | |
| 318 --- Disables the Addon, if possible, return true or false depending on success. | |
| 319 -- This internally calls AceAddon:DisableAddon(), thus dispatching a OnDisable callback | |
| 320 -- and disabling all modules of the addon.\\ | |
| 321 -- :Disable() also sets the internal `enableState` variable to false | |
| 322 -- @name //addon//:Disable | |
| 323 -- @paramsig | |
| 324 -- @usage | |
| 325 -- -- Disable MyAddon | |
| 326 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 327 -- MyAddon:Disable() | |
| 328 function Disable(self) | |
| 329 self:SetEnabledState(false) | |
| 330 return AceAddon:DisableAddon(self) | |
| 331 end | |
| 332 | |
| 333 --- Enables the Module, if possible, return true or false depending on success. | |
| 334 -- Short-hand function that retrieves the module via `:GetModule` and calls `:Enable` on the module object. | |
| 335 -- @name //addon//:EnableModule | |
| 336 -- @paramsig name | |
| 337 -- @usage | |
| 338 -- -- Enable MyModule using :GetModule | |
| 339 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 340 -- MyModule = MyAddon:GetModule("MyModule") | |
| 341 -- MyModule:Enable() | |
| 342 -- | |
| 343 -- -- Enable MyModule using the short-hand | |
| 344 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 345 -- MyAddon:EnableModule("MyModule") | |
| 346 function EnableModule(self, name) | |
| 347 local module = self:GetModule( name ) | |
| 348 return module:Enable() | |
| 349 end | |
| 350 | |
| 351 --- Disables the Module, if possible, return true or false depending on success. | |
| 352 -- Short-hand function that retrieves the module via `:GetModule` and calls `:Disable` on the module object. | |
| 353 -- @name //addon//:DisableModule | |
| 354 -- @paramsig name | |
| 355 -- @usage | |
| 356 -- -- Disable MyModule using :GetModule | |
| 357 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 358 -- MyModule = MyAddon:GetModule("MyModule") | |
| 359 -- MyModule:Disable() | |
| 360 -- | |
| 361 -- -- Disable MyModule using the short-hand | |
| 362 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
| 363 -- MyAddon:DisableModule("MyModule") | |
| 364 function DisableModule(self, name) | |
| 365 local module = self:GetModule( name ) | |
| 366 return module:Disable() | |
| 367 end | |
| 368 | |
| 369 --- Set the default libraries to be mixed into all modules created by this object. | |
| 370 -- Note that you can only change the default module libraries before any module is created. | |
| 371 -- @name //addon//:SetDefaultModuleLibraries | |
| 372 -- @paramsig lib[, lib, ...] | |
| 373 -- @param lib List of libraries to embed into the addon | |
| 374 -- @usage | |
| 375 -- -- Create the addon object | |
| 376 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") | |
| 377 -- -- Configure default libraries for modules (all modules need AceEvent-3.0) | |
| 378 -- MyAddon:SetDefaultModuleLibraries("AceEvent-3.0") | |
| 379 -- -- Create a module | |
| 380 -- MyModule = MyAddon:NewModule("MyModule") | |
| 381 function SetDefaultModuleLibraries(self, ...) | |
| 382 if next(self.modules) then | |
| 383 error("Usage: SetDefaultModuleLibraries(...): cannot change the module defaults after a module has been registered.", 2) | |
| 384 end | |
| 385 self.defaultModuleLibraries = {...} | |
| 386 end | |
| 387 | |
| 388 --- Set the default state in which new modules are being created. | |
| 389 -- Note that you can only change the default state before any module is created. | |
| 390 -- @name //addon//:SetDefaultModuleState | |
| 391 -- @paramsig state | |
| 392 -- @param state Default state for new modules, true for enabled, false for disabled | |
| 393 -- @usage | |
| 394 -- -- Create the addon object | |
| 395 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") | |
| 396 -- -- Set the default state to "disabled" | |
| 397 -- MyAddon:SetDefaultModuleState(false) | |
| 398 -- -- Create a module and explicilty enable it | |
| 399 -- MyModule = MyAddon:NewModule("MyModule") | |
| 400 -- MyModule:Enable() | |
| 401 function SetDefaultModuleState(self, state) | |
| 402 if next(self.modules) then | |
| 403 error("Usage: SetDefaultModuleState(state): cannot change the module defaults after a module has been registered.", 2) | |
| 404 end | |
| 405 self.defaultModuleState = state | |
| 406 end | |
| 407 | |
| 408 --- Set the default prototype to use for new modules on creation. | |
| 409 -- Note that you can only change the default prototype before any module is created. | |
| 410 -- @name //addon//:SetDefaultModulePrototype | |
| 411 -- @paramsig prototype | |
| 412 -- @param prototype Default prototype for the new modules (table) | |
| 413 -- @usage | |
| 414 -- -- Define a prototype | |
| 415 -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } | |
| 416 -- -- Set the default prototype | |
| 417 -- MyAddon:SetDefaultModulePrototype(prototype) | |
| 418 -- -- Create a module and explicitly Enable it | |
| 419 -- MyModule = MyAddon:NewModule("MyModule") | |
| 420 -- MyModule:Enable() | |
| 421 -- -- should print "OnEnable called!" now | |
| 422 -- @see NewModule | |
| 423 function SetDefaultModulePrototype(self, prototype) | |
| 424 if next(self.modules) then | |
| 425 error("Usage: SetDefaultModulePrototype(prototype): cannot change the module defaults after a module has been registered.", 2) | |
| 426 end | |
| 427 if type(prototype) ~= "table" then | |
| 428 error(("Usage: SetDefaultModulePrototype(prototype): 'prototype' - table expected got '%s'."):format(type(prototype)), 2) | |
| 429 end | |
| 430 self.defaultModulePrototype = prototype | |
| 431 end | |
| 432 | |
| 433 --- Set the state of an addon or module | |
| 434 -- This should only be called before any enabling actually happend, e.g. in/before OnInitialize. | |
| 435 -- @name //addon//:SetEnabledState | |
| 436 -- @paramsig state | |
| 437 -- @param state the state of an addon or module (enabled=true, disabled=false) | |
| 438 function SetEnabledState(self, state) | |
| 439 self.enabledState = state | |
| 440 end | |
| 441 | |
| 442 | |
| 443 --- Return an iterator of all modules associated to the addon. | |
| 444 -- @name //addon//:IterateModules | |
| 445 -- @paramsig | |
| 446 -- @usage | |
| 447 -- -- Enable all modules | |
| 448 -- for name, module in MyAddon:IterateModules() do | |
| 449 -- module:Enable() | |
| 450 -- end | |
| 451 local function IterateModules(self) return pairs(self.modules) end | |
| 452 | |
| 453 -- Returns an iterator of all embeds in the addon | |
| 454 -- @name //addon//:IterateEmbeds | |
| 455 -- @paramsig | |
| 456 local function IterateEmbeds(self) return pairs(AceAddon.embeds[self]) end | |
| 457 | |
| 458 --- Query the enabledState of an addon. | |
| 459 -- @name //addon//:IsEnabled | |
| 460 -- @paramsig | |
| 461 -- @usage | |
| 462 -- if MyAddon:IsEnabled() then | |
| 463 -- MyAddon:Disable() | |
| 464 -- end | |
| 465 local function IsEnabled(self) return self.enabledState end | |
| 466 local mixins = { | |
| 467 NewModule = NewModule, | |
| 468 GetModule = GetModule, | |
| 469 Enable = Enable, | |
| 470 Disable = Disable, | |
| 471 EnableModule = EnableModule, | |
| 472 DisableModule = DisableModule, | |
| 473 IsEnabled = IsEnabled, | |
| 474 SetDefaultModuleLibraries = SetDefaultModuleLibraries, | |
| 475 SetDefaultModuleState = SetDefaultModuleState, | |
| 476 SetDefaultModulePrototype = SetDefaultModulePrototype, | |
| 477 SetEnabledState = SetEnabledState, | |
| 478 IterateModules = IterateModules, | |
| 479 IterateEmbeds = IterateEmbeds, | |
| 480 GetName = GetName, | |
| 481 } | |
| 482 local function IsModule(self) return false end | |
| 483 local pmixins = { | |
| 484 defaultModuleState = true, | |
| 485 enabledState = true, | |
| 486 IsModule = IsModule, | |
| 487 } | |
| 488 -- Embed( target ) | |
| 489 -- target (object) - target object to embed aceaddon in | |
| 490 -- | |
| 491 -- this is a local function specifically since it's meant to be only called internally | |
| 492 function Embed(target) | |
| 493 for k, v in pairs(mixins) do | |
| 494 target[k] = v | |
| 495 end | |
| 496 for k, v in pairs(pmixins) do | |
| 497 target[k] = target[k] or v | |
| 498 end | |
| 499 end | |
| 500 | |
| 501 | |
| 502 -- - Initialize the addon after creation. | |
| 503 -- This function is only used internally during the ADDON_LOADED event | |
| 504 -- It will call the **OnInitialize** function on the addon object (if present), | |
| 505 -- and the **OnEmbedInitialize** function on all embeded libraries. | |
| 506 -- | |
| 507 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
| 508 -- @param addon addon object to intialize | |
| 509 function AceAddon:InitializeAddon(addon) | |
| 510 safecall(addon.OnInitialize, addon) | |
| 511 | |
| 512 local embeds = self.embeds[addon] | |
| 513 for i = 1, #embeds do | |
| 514 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 515 if lib then safecall(lib.OnEmbedInitialize, lib, addon) end | |
| 516 end | |
| 517 | |
| 518 -- we don't call InitializeAddon on modules specifically, this is handled | |
| 519 -- from the event handler and only done _once_ | |
| 520 end | |
| 521 | |
| 522 -- - Enable the addon after creation. | |
| 523 -- Note: This function is only used internally during the PLAYER_LOGIN event, or during ADDON_LOADED, | |
| 524 -- if IsLoggedIn() already returns true at that point, e.g. for LoD Addons. | |
| 525 -- It will call the **OnEnable** function on the addon object (if present), | |
| 526 -- and the **OnEmbedEnable** function on all embeded libraries.\\ | |
| 527 -- This function does not toggle the enable state of the addon itself, and will return early if the addon is disabled. | |
| 528 -- | |
| 529 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
| 530 -- Use :Enable on the addon itself instead. | |
| 531 -- @param addon addon object to enable | |
| 532 function AceAddon:EnableAddon(addon) | |
| 533 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
| 534 if self.statuses[addon.name] or not addon.enabledState then return false end | |
| 535 | |
| 536 -- set the statuses first, before calling the OnEnable. this allows for Disabling of the addon in OnEnable. | |
| 537 self.statuses[addon.name] = true | |
| 538 | |
| 539 safecall(addon.OnEnable, addon) | |
| 540 | |
| 541 -- make sure we're still enabled before continueing | |
| 542 if self.statuses[addon.name] then | |
| 543 local embeds = self.embeds[addon] | |
| 544 for i = 1, #embeds do | |
| 545 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 546 if lib then safecall(lib.OnEmbedEnable, lib, addon) end | |
| 547 end | |
| 548 | |
| 549 -- enable possible modules. | |
| 550 for name, module in pairs(addon.modules) do | |
| 551 self:EnableAddon(module) | |
| 552 end | |
| 553 end | |
| 554 return self.statuses[addon.name] -- return true if we're disabled | |
| 555 end | |
| 556 | |
| 557 -- - Disable the addon | |
| 558 -- Note: This function is only used internally. | |
| 559 -- It will call the **OnDisable** function on the addon object (if present), | |
| 560 -- and the **OnEmbedDisable** function on all embeded libraries.\\ | |
| 561 -- This function does not toggle the enable state of the addon itself, and will return early if the addon is still enabled. | |
| 562 -- | |
| 563 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
| 564 -- Use :Disable on the addon itself instead. | |
| 565 -- @param addon addon object to enable | |
| 566 function AceAddon:DisableAddon(addon) | |
| 567 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
| 568 if not self.statuses[addon.name] then return false end | |
| 569 | |
| 570 -- set statuses first before calling OnDisable, this allows for aborting the disable in OnDisable. | |
| 571 self.statuses[addon.name] = false | |
| 572 | |
| 573 safecall( addon.OnDisable, addon ) | |
| 574 | |
| 575 -- make sure we're still disabling... | |
| 576 if not self.statuses[addon.name] then | |
| 577 local embeds = self.embeds[addon] | |
| 578 for i = 1, #embeds do | |
| 579 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 580 if lib then safecall(lib.OnEmbedDisable, lib, addon) end | |
| 581 end | |
| 582 -- disable possible modules. | |
| 583 for name, module in pairs(addon.modules) do | |
| 584 self:DisableAddon(module) | |
| 585 end | |
| 586 end | |
| 587 | |
| 588 return not self.statuses[addon.name] -- return true if we're disabled | |
| 589 end | |
| 590 | |
| 591 --- Get an iterator over all registered addons. | |
| 592 -- @usage | |
| 593 -- -- Print a list of all installed AceAddon's | |
| 594 -- for name, addon in AceAddon:IterateAddons() do | |
| 595 -- print("Addon: " .. name) | |
| 596 -- end | |
| 597 function AceAddon:IterateAddons() return pairs(self.addons) end | |
| 598 | |
| 599 --- Get an iterator over the internal status registry. | |
| 600 -- @usage | |
| 601 -- -- Print a list of all enabled addons | |
| 602 -- for name, status in AceAddon:IterateAddonStatus() do | |
| 603 -- if status then | |
| 604 -- print("EnabledAddon: " .. name) | |
| 605 -- end | |
| 606 -- end | |
| 607 function AceAddon:IterateAddonStatus() return pairs(self.statuses) end | |
| 608 | |
| 609 -- Following Iterators are deprecated, and their addon specific versions should be used | |
| 610 -- e.g. addon:IterateEmbeds() instead of :IterateEmbedsOnAddon(addon) | |
| 611 function AceAddon:IterateEmbedsOnAddon(addon) return pairs(self.embeds[addon]) end | |
| 612 function AceAddon:IterateModulesOfAddon(addon) return pairs(addon.modules) end | |
| 613 | |
| 614 -- Event Handling | |
| 615 local function onEvent(this, event, arg1) | |
| 616 if event == "ADDON_LOADED" or event == "PLAYER_LOGIN" then | |
| 617 -- if a addon loads another addon, recursion could happen here, so we need to validate the table on every iteration | |
| 618 while(#AceAddon.initializequeue > 0) do | |
| 619 local addon = tremove(AceAddon.initializequeue, 1) | |
| 620 -- this might be an issue with recursion - TODO: validate | |
| 621 if event == "ADDON_LOADED" then addon.baseName = arg1 end | |
| 622 AceAddon:InitializeAddon(addon) | |
| 623 tinsert(AceAddon.enablequeue, addon) | |
| 624 end | |
| 625 | |
| 626 if IsLoggedIn() then | |
| 627 while(#AceAddon.enablequeue > 0) do | |
| 628 local addon = tremove(AceAddon.enablequeue, 1) | |
| 629 AceAddon:EnableAddon(addon) | |
| 630 end | |
| 631 end | |
| 632 end | |
| 633 end | |
| 634 | |
| 635 AceAddon.frame:RegisterEvent("ADDON_LOADED") | |
| 636 AceAddon.frame:RegisterEvent("PLAYER_LOGIN") | |
| 637 AceAddon.frame:SetScript("OnEvent", onEvent) | |
| 638 | |
| 639 -- upgrade embeded | |
| 640 for name, addon in pairs(AceAddon.addons) do | |
| 641 Embed(addon) | |
| 642 end | 
