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