Mercurial > wow > askmrrobot
comparison Libs/AceAddon-3.0/AceAddon-3.0.lua @ 57:01b63b8ed811 v21
total rewrite to version 21
author | yellowfive |
---|---|
date | Fri, 05 Jun 2015 11:05:15 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
56:75431c084aa0 | 57:01b63b8ed811 |
---|---|
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 1084 2013-04-27 20:14:11Z nevcairiel $ | |
32 | |
33 local MAJOR, MINOR = "AceAddon-3.0", 12 | |
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 -- Check if the addon is queued for initialization | |
112 local function queuedForInitialization(addon) | |
113 for i = 1, #AceAddon.initializequeue do | |
114 if AceAddon.initializequeue[i] == addon then | |
115 return true | |
116 end | |
117 end | |
118 return false | |
119 end | |
120 | |
121 --- Create a new AceAddon-3.0 addon. | |
122 -- Any libraries you specified will be embeded, and the addon will be scheduled for | |
123 -- its OnInitialize and OnEnable callbacks. | |
124 -- The final addon object, with all libraries embeded, will be returned. | |
125 -- @paramsig [object ,]name[, lib, ...] | |
126 -- @param object Table to use as a base for the addon (optional) | |
127 -- @param name Name of the addon object to create | |
128 -- @param lib List of libraries to embed into the addon | |
129 -- @usage | |
130 -- -- Create a simple addon object | |
131 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon", "AceEvent-3.0") | |
132 -- | |
133 -- -- Create a Addon object based on the table of a frame | |
134 -- local MyFrame = CreateFrame("Frame") | |
135 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon(MyFrame, "MyAddon", "AceEvent-3.0") | |
136 function AceAddon:NewAddon(objectorname, ...) | |
137 local object,name | |
138 local i=1 | |
139 if type(objectorname)=="table" then | |
140 object=objectorname | |
141 name=... | |
142 i=2 | |
143 else | |
144 name=objectorname | |
145 end | |
146 if type(name)~="string" then | |
147 error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) | |
148 end | |
149 if self.addons[name] then | |
150 error(("Usage: NewAddon([object,] name, [lib, lib, lib, ...]): 'name' - Addon '%s' already exists."):format(name), 2) | |
151 end | |
152 | |
153 object = object or {} | |
154 object.name = name | |
155 | |
156 local addonmeta = {} | |
157 local oldmeta = getmetatable(object) | |
158 if oldmeta then | |
159 for k, v in pairs(oldmeta) do addonmeta[k] = v end | |
160 end | |
161 addonmeta.__tostring = addontostring | |
162 | |
163 setmetatable( object, addonmeta ) | |
164 self.addons[name] = object | |
165 object.modules = {} | |
166 object.orderedModules = {} | |
167 object.defaultModuleLibraries = {} | |
168 Embed( object ) -- embed NewModule, GetModule methods | |
169 self:EmbedLibraries(object, select(i,...)) | |
170 | |
171 -- add to queue of addons to be initialized upon ADDON_LOADED | |
172 tinsert(self.initializequeue, object) | |
173 return object | |
174 end | |
175 | |
176 | |
177 --- Get the addon object by its name from the internal AceAddon registry. | |
178 -- Throws an error if the addon object cannot be found (except if silent is set). | |
179 -- @param name unique name of the addon object | |
180 -- @param silent if true, the addon is optional, silently return nil if its not found | |
181 -- @usage | |
182 -- -- Get the Addon | |
183 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
184 function AceAddon:GetAddon(name, silent) | |
185 if not silent and not self.addons[name] then | |
186 error(("Usage: GetAddon(name): 'name' - Cannot find an AceAddon '%s'."):format(tostring(name)), 2) | |
187 end | |
188 return self.addons[name] | |
189 end | |
190 | |
191 -- - Embed a list of libraries into the specified addon. | |
192 -- This function will try to embed all of the listed libraries into the addon | |
193 -- and error if a single one fails. | |
194 -- | |
195 -- **Note:** This function is for internal use by :NewAddon/:NewModule | |
196 -- @paramsig addon, [lib, ...] | |
197 -- @param addon addon object to embed the libs in | |
198 -- @param lib List of libraries to embed into the addon | |
199 function AceAddon:EmbedLibraries(addon, ...) | |
200 for i=1,select("#", ... ) do | |
201 local libname = select(i, ...) | |
202 self:EmbedLibrary(addon, libname, false, 4) | |
203 end | |
204 end | |
205 | |
206 -- - Embed a library into the addon object. | |
207 -- This function will check if the specified library is registered with LibStub | |
208 -- and if it has a :Embed function to call. It'll error if any of those conditions | |
209 -- fails. | |
210 -- | |
211 -- **Note:** This function is for internal use by :EmbedLibraries | |
212 -- @paramsig addon, libname[, silent[, offset]] | |
213 -- @param addon addon object to embed the library in | |
214 -- @param libname name of the library to embed | |
215 -- @param silent marks an embed to fail silently if the library doesn't exist (optional) | |
216 -- @param offset will push the error messages back to said offset, defaults to 2 (optional) | |
217 function AceAddon:EmbedLibrary(addon, libname, silent, offset) | |
218 local lib = LibStub:GetLibrary(libname, true) | |
219 if not lib and not silent then | |
220 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Cannot find a library instance of %q."):format(tostring(libname)), offset or 2) | |
221 elseif lib and type(lib.Embed) == "function" then | |
222 lib:Embed(addon) | |
223 tinsert(self.embeds[addon], libname) | |
224 return true | |
225 elseif lib then | |
226 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Library '%s' is not Embed capable"):format(libname), offset or 2) | |
227 end | |
228 end | |
229 | |
230 --- Return the specified module from an addon object. | |
231 -- Throws an error if the addon object cannot be found (except if silent is set) | |
232 -- @name //addon//:GetModule | |
233 -- @paramsig name[, silent] | |
234 -- @param name unique name of the module | |
235 -- @param silent if true, the module is optional, silently return nil if its not found (optional) | |
236 -- @usage | |
237 -- -- Get the Addon | |
238 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
239 -- -- Get the Module | |
240 -- MyModule = MyAddon:GetModule("MyModule") | |
241 function GetModule(self, name, silent) | |
242 if not self.modules[name] and not silent then | |
243 error(("Usage: GetModule(name, silent): 'name' - Cannot find module '%s'."):format(tostring(name)), 2) | |
244 end | |
245 return self.modules[name] | |
246 end | |
247 | |
248 local function IsModuleTrue(self) return true end | |
249 | |
250 --- Create a new module for the addon. | |
251 -- The new module can have its own embeded libraries and/or use a module prototype to be mixed into the module.\\ | |
252 -- A module has the same functionality as a real addon, it can have modules of its own, and has the same API as | |
253 -- an addon object. | |
254 -- @name //addon//:NewModule | |
255 -- @paramsig name[, prototype|lib[, lib, ...]] | |
256 -- @param name unique name of the module | |
257 -- @param prototype object to derive this module from, methods and values from this table will be mixed into the module (optional) | |
258 -- @param lib List of libraries to embed into the addon | |
259 -- @usage | |
260 -- -- Create a module with some embeded libraries | |
261 -- MyModule = MyAddon:NewModule("MyModule", "AceEvent-3.0", "AceHook-3.0") | |
262 -- | |
263 -- -- Create a module with a prototype | |
264 -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } | |
265 -- MyModule = MyAddon:NewModule("MyModule", prototype, "AceEvent-3.0", "AceHook-3.0") | |
266 function NewModule(self, name, prototype, ...) | |
267 if type(name) ~= "string" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end | |
268 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 | |
269 | |
270 if self.modules[name] then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - Module '%s' already exists."):format(name), 2) end | |
271 | |
272 -- modules are basically addons. We treat them as such. They will be added to the initializequeue properly as well. | |
273 -- NewModule can only be called after the parent addon is present thus the modules will be initialized after their parent is. | |
274 local module = AceAddon:NewAddon(fmt("%s_%s", self.name or tostring(self), name)) | |
275 | |
276 module.IsModule = IsModuleTrue | |
277 module:SetEnabledState(self.defaultModuleState) | |
278 module.moduleName = name | |
279 | |
280 if type(prototype) == "string" then | |
281 AceAddon:EmbedLibraries(module, prototype, ...) | |
282 else | |
283 AceAddon:EmbedLibraries(module, ...) | |
284 end | |
285 AceAddon:EmbedLibraries(module, unpack(self.defaultModuleLibraries)) | |
286 | |
287 if not prototype or type(prototype) == "string" then | |
288 prototype = self.defaultModulePrototype or nil | |
289 end | |
290 | |
291 if type(prototype) == "table" then | |
292 local mt = getmetatable(module) | |
293 mt.__index = prototype | |
294 setmetatable(module, mt) -- More of a Base class type feel. | |
295 end | |
296 | |
297 safecall(self.OnModuleCreated, self, module) -- Was in Ace2 and I think it could be a cool thing to have handy. | |
298 self.modules[name] = module | |
299 tinsert(self.orderedModules, module) | |
300 | |
301 return module | |
302 end | |
303 | |
304 --- Returns the real name of the addon or module, without any prefix. | |
305 -- @name //addon//:GetName | |
306 -- @paramsig | |
307 -- @usage | |
308 -- print(MyAddon:GetName()) | |
309 -- -- prints "MyAddon" | |
310 function GetName(self) | |
311 return self.moduleName or self.name | |
312 end | |
313 | |
314 --- Enables the Addon, if possible, return true or false depending on success. | |
315 -- This internally calls AceAddon:EnableAddon(), thus dispatching a OnEnable callback | |
316 -- and enabling all modules of the addon (unless explicitly disabled).\\ | |
317 -- :Enable() also sets the internal `enableState` variable to true | |
318 -- @name //addon//:Enable | |
319 -- @paramsig | |
320 -- @usage | |
321 -- -- Enable MyModule | |
322 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
323 -- MyModule = MyAddon:GetModule("MyModule") | |
324 -- MyModule:Enable() | |
325 function Enable(self) | |
326 self:SetEnabledState(true) | |
327 | |
328 -- nevcairiel 2013-04-27: don't enable an addon/module if its queued for init still | |
329 -- it'll be enabled after the init process | |
330 if not queuedForInitialization(self) then | |
331 return AceAddon:EnableAddon(self) | |
332 end | |
333 end | |
334 | |
335 --- Disables the Addon, if possible, return true or false depending on success. | |
336 -- This internally calls AceAddon:DisableAddon(), thus dispatching a OnDisable callback | |
337 -- and disabling all modules of the addon.\\ | |
338 -- :Disable() also sets the internal `enableState` variable to false | |
339 -- @name //addon//:Disable | |
340 -- @paramsig | |
341 -- @usage | |
342 -- -- Disable MyAddon | |
343 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
344 -- MyAddon:Disable() | |
345 function Disable(self) | |
346 self:SetEnabledState(false) | |
347 return AceAddon:DisableAddon(self) | |
348 end | |
349 | |
350 --- Enables the Module, if possible, return true or false depending on success. | |
351 -- Short-hand function that retrieves the module via `:GetModule` and calls `:Enable` on the module object. | |
352 -- @name //addon//:EnableModule | |
353 -- @paramsig name | |
354 -- @usage | |
355 -- -- Enable MyModule using :GetModule | |
356 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
357 -- MyModule = MyAddon:GetModule("MyModule") | |
358 -- MyModule:Enable() | |
359 -- | |
360 -- -- Enable MyModule using the short-hand | |
361 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
362 -- MyAddon:EnableModule("MyModule") | |
363 function EnableModule(self, name) | |
364 local module = self:GetModule( name ) | |
365 return module:Enable() | |
366 end | |
367 | |
368 --- Disables the Module, if possible, return true or false depending on success. | |
369 -- Short-hand function that retrieves the module via `:GetModule` and calls `:Disable` on the module object. | |
370 -- @name //addon//:DisableModule | |
371 -- @paramsig name | |
372 -- @usage | |
373 -- -- Disable MyModule using :GetModule | |
374 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
375 -- MyModule = MyAddon:GetModule("MyModule") | |
376 -- MyModule:Disable() | |
377 -- | |
378 -- -- Disable MyModule using the short-hand | |
379 -- MyAddon = LibStub("AceAddon-3.0"):GetAddon("MyAddon") | |
380 -- MyAddon:DisableModule("MyModule") | |
381 function DisableModule(self, name) | |
382 local module = self:GetModule( name ) | |
383 return module:Disable() | |
384 end | |
385 | |
386 --- Set the default libraries to be mixed into all modules created by this object. | |
387 -- Note that you can only change the default module libraries before any module is created. | |
388 -- @name //addon//:SetDefaultModuleLibraries | |
389 -- @paramsig lib[, lib, ...] | |
390 -- @param lib List of libraries to embed into the addon | |
391 -- @usage | |
392 -- -- Create the addon object | |
393 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") | |
394 -- -- Configure default libraries for modules (all modules need AceEvent-3.0) | |
395 -- MyAddon:SetDefaultModuleLibraries("AceEvent-3.0") | |
396 -- -- Create a module | |
397 -- MyModule = MyAddon:NewModule("MyModule") | |
398 function SetDefaultModuleLibraries(self, ...) | |
399 if next(self.modules) then | |
400 error("Usage: SetDefaultModuleLibraries(...): cannot change the module defaults after a module has been registered.", 2) | |
401 end | |
402 self.defaultModuleLibraries = {...} | |
403 end | |
404 | |
405 --- Set the default state in which new modules are being created. | |
406 -- Note that you can only change the default state before any module is created. | |
407 -- @name //addon//:SetDefaultModuleState | |
408 -- @paramsig state | |
409 -- @param state Default state for new modules, true for enabled, false for disabled | |
410 -- @usage | |
411 -- -- Create the addon object | |
412 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("MyAddon") | |
413 -- -- Set the default state to "disabled" | |
414 -- MyAddon:SetDefaultModuleState(false) | |
415 -- -- Create a module and explicilty enable it | |
416 -- MyModule = MyAddon:NewModule("MyModule") | |
417 -- MyModule:Enable() | |
418 function SetDefaultModuleState(self, state) | |
419 if next(self.modules) then | |
420 error("Usage: SetDefaultModuleState(state): cannot change the module defaults after a module has been registered.", 2) | |
421 end | |
422 self.defaultModuleState = state | |
423 end | |
424 | |
425 --- Set the default prototype to use for new modules on creation. | |
426 -- Note that you can only change the default prototype before any module is created. | |
427 -- @name //addon//:SetDefaultModulePrototype | |
428 -- @paramsig prototype | |
429 -- @param prototype Default prototype for the new modules (table) | |
430 -- @usage | |
431 -- -- Define a prototype | |
432 -- local prototype = { OnEnable = function(self) print("OnEnable called!") end } | |
433 -- -- Set the default prototype | |
434 -- MyAddon:SetDefaultModulePrototype(prototype) | |
435 -- -- Create a module and explicitly Enable it | |
436 -- MyModule = MyAddon:NewModule("MyModule") | |
437 -- MyModule:Enable() | |
438 -- -- should print "OnEnable called!" now | |
439 -- @see NewModule | |
440 function SetDefaultModulePrototype(self, prototype) | |
441 if next(self.modules) then | |
442 error("Usage: SetDefaultModulePrototype(prototype): cannot change the module defaults after a module has been registered.", 2) | |
443 end | |
444 if type(prototype) ~= "table" then | |
445 error(("Usage: SetDefaultModulePrototype(prototype): 'prototype' - table expected got '%s'."):format(type(prototype)), 2) | |
446 end | |
447 self.defaultModulePrototype = prototype | |
448 end | |
449 | |
450 --- Set the state of an addon or module | |
451 -- This should only be called before any enabling actually happend, e.g. in/before OnInitialize. | |
452 -- @name //addon//:SetEnabledState | |
453 -- @paramsig state | |
454 -- @param state the state of an addon or module (enabled=true, disabled=false) | |
455 function SetEnabledState(self, state) | |
456 self.enabledState = state | |
457 end | |
458 | |
459 | |
460 --- Return an iterator of all modules associated to the addon. | |
461 -- @name //addon//:IterateModules | |
462 -- @paramsig | |
463 -- @usage | |
464 -- -- Enable all modules | |
465 -- for name, module in MyAddon:IterateModules() do | |
466 -- module:Enable() | |
467 -- end | |
468 local function IterateModules(self) return pairs(self.modules) end | |
469 | |
470 -- Returns an iterator of all embeds in the addon | |
471 -- @name //addon//:IterateEmbeds | |
472 -- @paramsig | |
473 local function IterateEmbeds(self) return pairs(AceAddon.embeds[self]) end | |
474 | |
475 --- Query the enabledState of an addon. | |
476 -- @name //addon//:IsEnabled | |
477 -- @paramsig | |
478 -- @usage | |
479 -- if MyAddon:IsEnabled() then | |
480 -- MyAddon:Disable() | |
481 -- end | |
482 local function IsEnabled(self) return self.enabledState end | |
483 local mixins = { | |
484 NewModule = NewModule, | |
485 GetModule = GetModule, | |
486 Enable = Enable, | |
487 Disable = Disable, | |
488 EnableModule = EnableModule, | |
489 DisableModule = DisableModule, | |
490 IsEnabled = IsEnabled, | |
491 SetDefaultModuleLibraries = SetDefaultModuleLibraries, | |
492 SetDefaultModuleState = SetDefaultModuleState, | |
493 SetDefaultModulePrototype = SetDefaultModulePrototype, | |
494 SetEnabledState = SetEnabledState, | |
495 IterateModules = IterateModules, | |
496 IterateEmbeds = IterateEmbeds, | |
497 GetName = GetName, | |
498 } | |
499 local function IsModule(self) return false end | |
500 local pmixins = { | |
501 defaultModuleState = true, | |
502 enabledState = true, | |
503 IsModule = IsModule, | |
504 } | |
505 -- Embed( target ) | |
506 -- target (object) - target object to embed aceaddon in | |
507 -- | |
508 -- this is a local function specifically since it's meant to be only called internally | |
509 function Embed(target, skipPMixins) | |
510 for k, v in pairs(mixins) do | |
511 target[k] = v | |
512 end | |
513 if not skipPMixins then | |
514 for k, v in pairs(pmixins) do | |
515 target[k] = target[k] or v | |
516 end | |
517 end | |
518 end | |
519 | |
520 | |
521 -- - Initialize the addon after creation. | |
522 -- This function is only used internally during the ADDON_LOADED event | |
523 -- It will call the **OnInitialize** function on the addon object (if present), | |
524 -- and the **OnEmbedInitialize** function on all embeded libraries. | |
525 -- | |
526 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
527 -- @param addon addon object to intialize | |
528 function AceAddon:InitializeAddon(addon) | |
529 safecall(addon.OnInitialize, addon) | |
530 | |
531 local embeds = self.embeds[addon] | |
532 for i = 1, #embeds do | |
533 local lib = LibStub:GetLibrary(embeds[i], true) | |
534 if lib then safecall(lib.OnEmbedInitialize, lib, addon) end | |
535 end | |
536 | |
537 -- we don't call InitializeAddon on modules specifically, this is handled | |
538 -- from the event handler and only done _once_ | |
539 end | |
540 | |
541 -- - Enable the addon after creation. | |
542 -- Note: This function is only used internally during the PLAYER_LOGIN event, or during ADDON_LOADED, | |
543 -- if IsLoggedIn() already returns true at that point, e.g. for LoD Addons. | |
544 -- It will call the **OnEnable** function on the addon object (if present), | |
545 -- and the **OnEmbedEnable** function on all embeded libraries.\\ | |
546 -- This function does not toggle the enable state of the addon itself, and will return early if the addon is disabled. | |
547 -- | |
548 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
549 -- Use :Enable on the addon itself instead. | |
550 -- @param addon addon object to enable | |
551 function AceAddon:EnableAddon(addon) | |
552 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
553 if self.statuses[addon.name] or not addon.enabledState then return false end | |
554 | |
555 -- set the statuses first, before calling the OnEnable. this allows for Disabling of the addon in OnEnable. | |
556 self.statuses[addon.name] = true | |
557 | |
558 safecall(addon.OnEnable, addon) | |
559 | |
560 -- make sure we're still enabled before continueing | |
561 if self.statuses[addon.name] then | |
562 local embeds = self.embeds[addon] | |
563 for i = 1, #embeds do | |
564 local lib = LibStub:GetLibrary(embeds[i], true) | |
565 if lib then safecall(lib.OnEmbedEnable, lib, addon) end | |
566 end | |
567 | |
568 -- enable possible modules. | |
569 local modules = addon.orderedModules | |
570 for i = 1, #modules do | |
571 self:EnableAddon(modules[i]) | |
572 end | |
573 end | |
574 return self.statuses[addon.name] -- return true if we're disabled | |
575 end | |
576 | |
577 -- - Disable the addon | |
578 -- Note: This function is only used internally. | |
579 -- It will call the **OnDisable** function on the addon object (if present), | |
580 -- and the **OnEmbedDisable** function on all embeded libraries.\\ | |
581 -- This function does not toggle the enable state of the addon itself, and will return early if the addon is still enabled. | |
582 -- | |
583 -- **Note:** Do not call this function manually, unless you're absolutely sure that you know what you are doing. | |
584 -- Use :Disable on the addon itself instead. | |
585 -- @param addon addon object to enable | |
586 function AceAddon:DisableAddon(addon) | |
587 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
588 if not self.statuses[addon.name] then return false end | |
589 | |
590 -- set statuses first before calling OnDisable, this allows for aborting the disable in OnDisable. | |
591 self.statuses[addon.name] = false | |
592 | |
593 safecall( addon.OnDisable, addon ) | |
594 | |
595 -- make sure we're still disabling... | |
596 if not self.statuses[addon.name] then | |
597 local embeds = self.embeds[addon] | |
598 for i = 1, #embeds do | |
599 local lib = LibStub:GetLibrary(embeds[i], true) | |
600 if lib then safecall(lib.OnEmbedDisable, lib, addon) end | |
601 end | |
602 -- disable possible modules. | |
603 local modules = addon.orderedModules | |
604 for i = 1, #modules do | |
605 self:DisableAddon(modules[i]) | |
606 end | |
607 end | |
608 | |
609 return not self.statuses[addon.name] -- return true if we're disabled | |
610 end | |
611 | |
612 --- Get an iterator over all registered addons. | |
613 -- @usage | |
614 -- -- Print a list of all installed AceAddon's | |
615 -- for name, addon in AceAddon:IterateAddons() do | |
616 -- print("Addon: " .. name) | |
617 -- end | |
618 function AceAddon:IterateAddons() return pairs(self.addons) end | |
619 | |
620 --- Get an iterator over the internal status registry. | |
621 -- @usage | |
622 -- -- Print a list of all enabled addons | |
623 -- for name, status in AceAddon:IterateAddonStatus() do | |
624 -- if status then | |
625 -- print("EnabledAddon: " .. name) | |
626 -- end | |
627 -- end | |
628 function AceAddon:IterateAddonStatus() return pairs(self.statuses) end | |
629 | |
630 -- Following Iterators are deprecated, and their addon specific versions should be used | |
631 -- e.g. addon:IterateEmbeds() instead of :IterateEmbedsOnAddon(addon) | |
632 function AceAddon:IterateEmbedsOnAddon(addon) return pairs(self.embeds[addon]) end | |
633 function AceAddon:IterateModulesOfAddon(addon) return pairs(addon.modules) end | |
634 | |
635 -- Event Handling | |
636 local function onEvent(this, event, arg1) | |
637 -- 2011-08-17 nevcairiel - ignore the load event of Blizzard_DebugTools, so a potential startup error isn't swallowed up | |
638 if (event == "ADDON_LOADED" and arg1 ~= "Blizzard_DebugTools") or event == "PLAYER_LOGIN" then | |
639 -- if a addon loads another addon, recursion could happen here, so we need to validate the table on every iteration | |
640 while(#AceAddon.initializequeue > 0) do | |
641 local addon = tremove(AceAddon.initializequeue, 1) | |
642 -- this might be an issue with recursion - TODO: validate | |
643 if event == "ADDON_LOADED" then addon.baseName = arg1 end | |
644 AceAddon:InitializeAddon(addon) | |
645 tinsert(AceAddon.enablequeue, addon) | |
646 end | |
647 | |
648 if IsLoggedIn() then | |
649 while(#AceAddon.enablequeue > 0) do | |
650 local addon = tremove(AceAddon.enablequeue, 1) | |
651 AceAddon:EnableAddon(addon) | |
652 end | |
653 end | |
654 end | |
655 end | |
656 | |
657 AceAddon.frame:RegisterEvent("ADDON_LOADED") | |
658 AceAddon.frame:RegisterEvent("PLAYER_LOGIN") | |
659 AceAddon.frame:SetScript("OnEvent", onEvent) | |
660 | |
661 -- upgrade embeded | |
662 for name, addon in pairs(AceAddon.addons) do | |
663 Embed(addon, true) | |
664 end | |
665 | |
666 -- 2010-10-27 nevcairiel - add new "orderedModules" table | |
667 if oldminor and oldminor < 10 then | |
668 for name, addon in pairs(AceAddon.addons) do | |
669 addon.orderedModules = {} | |
670 for module_name, module in pairs(addon.modules) do | |
671 tinsert(addon.orderedModules, module) | |
672 end | |
673 end | |
674 end |