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