Mercurial > wow > reaction
comparison lib/AceAddon-3.0/AceAddon-3.0.lua @ 28:21bcaf8215ff
- converted to Ace3
- rearranged file layout
- configGUI menus not working right now
| author | Flick <flickerstreak@gmail.com> |
|---|---|
| date | Mon, 17 Mar 2008 18:24:53 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 27:f1e838841ce1 | 28:21bcaf8215ff |
|---|---|
| 1 --[[ $Id: AceAddon-3.0.lua 63220 2008-02-29 11:29:58Z nevcairiel $ ]] | |
| 2 local MAJOR, MINOR = "AceAddon-3.0", 3 | |
| 3 local AceAddon, oldminor = LibStub:NewLibrary(MAJOR, MINOR) | |
| 4 | |
| 5 if not AceAddon then return end -- No Upgrade needed. | |
| 6 | |
| 7 AceAddon.frame = AceAddon.frame or CreateFrame("Frame", "AceAddon30Frame") -- Our very own frame | |
| 8 AceAddon.addons = AceAddon.addons or {} -- addons in general | |
| 9 AceAddon.statuses = AceAddon.statuses or {} -- statuses of addon. | |
| 10 AceAddon.initializequeue = AceAddon.initializequeue or {} -- addons that are new and not initialized | |
| 11 AceAddon.enablequeue = AceAddon.enablequeue or {} -- addons that are initialized and waiting to be enabled | |
| 12 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 | |
| 13 | |
| 14 local tinsert, tconcat = table.insert, table.concat | |
| 15 local fmt = string.format | |
| 16 local pairs, next, type = pairs, next, type | |
| 17 | |
| 18 --[[ | |
| 19 xpcall safecall implementation | |
| 20 ]] | |
| 21 local xpcall = xpcall | |
| 22 | |
| 23 local function errorhandler(err) | |
| 24 return geterrorhandler()(err) | |
| 25 end | |
| 26 | |
| 27 local function CreateDispatcher(argCount) | |
| 28 local code = [[ | |
| 29 local xpcall, eh = ... | |
| 30 local method, ARGS | |
| 31 local function call() return method(ARGS) end | |
| 32 | |
| 33 local function dispatch(func, ...) | |
| 34 method = func | |
| 35 if not method then return end | |
| 36 ARGS = ... | |
| 37 return xpcall(call, eh) | |
| 38 end | |
| 39 | |
| 40 return dispatch | |
| 41 ]] | |
| 42 | |
| 43 local ARGS = {} | |
| 44 for i = 1, argCount do ARGS[i] = "arg"..i end | |
| 45 code = code:gsub("ARGS", tconcat(ARGS, ", ")) | |
| 46 return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler) | |
| 47 end | |
| 48 | |
| 49 local Dispatchers = setmetatable({}, {__index=function(self, argCount) | |
| 50 local dispatcher = CreateDispatcher(argCount) | |
| 51 rawset(self, argCount, dispatcher) | |
| 52 return dispatcher | |
| 53 end}) | |
| 54 Dispatchers[0] = function(func) | |
| 55 return xpcall(func, errorhandler) | |
| 56 end | |
| 57 | |
| 58 local function safecall(func, ...) | |
| 59 -- we check to see if the func is passed is actually a function here and don't error when it isn't | |
| 60 -- this safecall is used for optional functions like OnInitialize OnEnable etc. When they are not | |
| 61 -- present execution should continue without hinderance | |
| 62 if type(func) == "function" then | |
| 63 return Dispatchers[select('#', ...)](func, ...) | |
| 64 end | |
| 65 end | |
| 66 | |
| 67 -- local functions that will be implemented further down | |
| 68 local Enable, Disable, EnableModule, DisableModule, Embed, NewModule, GetModule, GetName, SetDefaultModuleState, SetDefaultModuleLibraries, SetEnabledState, SetDefaultModulePrototype | |
| 69 | |
| 70 -- used in the addon metatable | |
| 71 local function addontostring( self ) return self.name end | |
| 72 | |
| 73 -- AceAddon:NewAddon( name, [lib, lib, lib, ...] ) | |
| 74 -- name (string) - unique addon object name | |
| 75 -- [lib] (string) - optional libs to embed in the addon object | |
| 76 -- | |
| 77 -- returns the addon object when succesful | |
| 78 function AceAddon:NewAddon(name, ...) | |
| 79 if type(name) ~= "string" then error(("Usage: NewAddon(name, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end | |
| 80 | |
| 81 if self.addons[name] then error(("Usage: NewAddon(name, [lib, lib, lib, ...]): 'name' - Addon '%s' already exists."):format(name), 2) end | |
| 82 | |
| 83 local addon = setmetatable( {name = name}, { __tostring = addontostring } ) | |
| 84 self.addons[name] = addon | |
| 85 addon.modules = {} | |
| 86 addon.defaultModuleLibraries = {} | |
| 87 Embed( addon ) -- embed NewModule, GetModule methods | |
| 88 self:EmbedLibraries(addon, ...) | |
| 89 | |
| 90 -- add to queue of addons to be initialized upon ADDON_LOADED | |
| 91 tinsert(self.initializequeue, addon) | |
| 92 return addon | |
| 93 end | |
| 94 | |
| 95 -- AceAddon:GetAddon( name, [silent]) | |
| 96 -- name (string) - unique addon object name | |
| 97 -- silent (boolean) - if true, addon is optional, silently return nil if its not found | |
| 98 -- | |
| 99 -- throws an error if the addon object can not be found (except silent is set) | |
| 100 -- returns the addon object if found | |
| 101 function AceAddon:GetAddon(name, silent) | |
| 102 if not silent and not self.addons[name] then | |
| 103 error(("Usage: GetAddon(name): 'name' - Cannot find an AceAddon '%s'."):format(tostring(name)), 2) | |
| 104 end | |
| 105 return self.addons[name] | |
| 106 end | |
| 107 | |
| 108 -- AceAddon:EmbedLibraries( addon, [lib, lib, lib, ...] ) | |
| 109 -- addon (object) - addon to embed the libs in | |
| 110 -- [lib] (string) - optional libs to embed | |
| 111 function AceAddon:EmbedLibraries(addon, ...) | |
| 112 for i=1,select("#", ... ) do | |
| 113 local libname = select(i, ...) | |
| 114 self:EmbedLibrary(addon, libname, false, 4) | |
| 115 end | |
| 116 end | |
| 117 | |
| 118 -- AceAddon:EmbedLibrary( addon, libname, silent, offset ) | |
| 119 -- addon (object) - addon to embed the libs in | |
| 120 -- libname (string) - lib to embed | |
| 121 -- [silent] (boolean) - optional, marks an embed to fail silently if the library doesn't exist. | |
| 122 -- [offset] (number) - will push the error messages back to said offset defaults to 2 | |
| 123 function AceAddon:EmbedLibrary(addon, libname, silent, offset) | |
| 124 local lib = LibStub:GetLibrary(libname, true) | |
| 125 if not lib and not silent then | |
| 126 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Cannot find a library instance of %q."):format(tostring(libname)), offset or 2) | |
| 127 elseif lib and type(lib.Embed) == "function" then | |
| 128 lib:Embed(addon) | |
| 129 tinsert(self.embeds[addon], libname) | |
| 130 return true | |
| 131 elseif lib then | |
| 132 error(("Usage: EmbedLibrary(addon, libname, silent, offset): 'libname' - Library '%s' is not Embed capable"):format(libname), offset or 2) | |
| 133 end | |
| 134 end | |
| 135 | |
| 136 -- addon:GetModule( name, [silent]) | |
| 137 -- name (string) - unique module object name | |
| 138 -- silent (boolean) - if true, module is optional, silently return nil if its not found | |
| 139 -- | |
| 140 -- throws an error if the addon object can not be found (except silent is set) | |
| 141 -- returns the module object if found | |
| 142 function GetModule(self, name, silent) | |
| 143 if not self.modules[name] and not silent then | |
| 144 error(("Usage: GetModule(name, silent): 'name' - Cannot find module '%s'."):format(tostring(name)), 2) | |
| 145 end | |
| 146 return self.modules[name] | |
| 147 end | |
| 148 | |
| 149 local function IsModuleTrue(self) return true end | |
| 150 | |
| 151 -- addon:NewModule( name, [prototype, [lib, lib, lib, ...] ) | |
| 152 -- name (string) - unique module object name for this addon | |
| 153 -- prototype (object) - object to derive this module from, methods and values from this table will be mixed into the module, if a string is passed a lib is assumed | |
| 154 -- [lib] (string) - optional libs to embed in the addon object | |
| 155 -- | |
| 156 -- returns the addon object when succesful | |
| 157 function NewModule(self, name, prototype, ...) | |
| 158 if type(name) ~= "string" then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - string expected got '%s'."):format(type(name)), 2) end | |
| 159 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 | |
| 160 | |
| 161 if self.modules[name] then error(("Usage: NewModule(name, [prototype, [lib, lib, lib, ...]): 'name' - Module '%s' already exists."):format(name), 2) end | |
| 162 | |
| 163 -- modules are basically addons. We treat them as such. They will be added to the initializequeue properly as well. | |
| 164 -- NewModule can only be called after the parent addon is present thus the modules will be initialized after their parent is. | |
| 165 local module = AceAddon:NewAddon(fmt("%s_%s", self.name or tostring(self), name)) | |
| 166 | |
| 167 module.IsModule = IsModuleTrue | |
| 168 module:SetEnabledState(self.defaultModuleState) | |
| 169 module.moduleName = name | |
| 170 | |
| 171 if type(prototype) == "string" then | |
| 172 AceAddon:EmbedLibraries(module, prototype, ...) | |
| 173 else | |
| 174 AceAddon:EmbedLibraries(module, ...) | |
| 175 end | |
| 176 AceAddon:EmbedLibraries(module, unpack(self.defaultModuleLibraries)) | |
| 177 | |
| 178 if not prototype or type(prototype) == "string" then | |
| 179 prototype = self.defaultModulePrototype or nil | |
| 180 end | |
| 181 | |
| 182 if type(prototype) == "table" then | |
| 183 local mt = getmetatable(module) | |
| 184 mt.__index = prototype | |
| 185 setmetatable(module, mt) -- More of a Base class type feel. | |
| 186 end | |
| 187 | |
| 188 safecall(self.OnModuleCreated, self, module) -- Was in Ace2 and I think it could be a cool thing to have handy. | |
| 189 self.modules[name] = module | |
| 190 | |
| 191 return module | |
| 192 end | |
| 193 | |
| 194 --addon:GetName() | |
| 195 -- Returns the real name of the addon or module, without any prefix | |
| 196 function GetName(self) | |
| 197 return self.moduleName or self.name | |
| 198 end | |
| 199 | |
| 200 --addon:Enable() | |
| 201 -- Enables the Addon if possible, return true or false depending on success | |
| 202 function Enable(self) | |
| 203 self:SetEnabledState(true) | |
| 204 return AceAddon:EnableAddon(self) | |
| 205 end | |
| 206 | |
| 207 --addon:Disable() | |
| 208 -- Disables the Addon if possible, return true or false depending on success | |
| 209 function Disable(self) | |
| 210 self:SetEnabledState(false) | |
| 211 return AceAddon:DisableAddon(self) | |
| 212 end | |
| 213 | |
| 214 -- addon:EnableModule( name ) | |
| 215 -- name (string) - unique module object name | |
| 216 -- | |
| 217 -- Enables the Module if possible, return true or false depending on success | |
| 218 function EnableModule(self, name) | |
| 219 local module = self:GetModule( name ) | |
| 220 return module:Enable() | |
| 221 end | |
| 222 | |
| 223 -- addon:DisableModule( name ) | |
| 224 -- name (string) - unique module object name | |
| 225 -- | |
| 226 -- Disables the Module if possible, return true or false depending on success | |
| 227 function DisableModule(self, name) | |
| 228 local module = self:GetModule( name ) | |
| 229 return module:Disable() | |
| 230 end | |
| 231 | |
| 232 -- addon:SetDefaultModuleLibraries( [lib, lib, lib, ...] ) | |
| 233 -- [lib] (string) - libs to embed in every module | |
| 234 function SetDefaultModuleLibraries(self, ...) | |
| 235 if next(self.modules) then | |
| 236 error("Usage: SetDefaultModuleLibraries(...): cannot change the module defaults after a module has been registered.", 2) | |
| 237 end | |
| 238 self.defaultModuleLibraries = {...} | |
| 239 end | |
| 240 | |
| 241 -- addon:SetDefaultModuleState( state ) | |
| 242 -- state (boolean) - default state for new modules (enabled=true, disabled=false) | |
| 243 function SetDefaultModuleState(self, state) | |
| 244 if next(self.modules) then | |
| 245 error("Usage: SetDefaultModuleState(state): cannot change the module defaults after a module has been registered.", 2) | |
| 246 end | |
| 247 self.defaultModuleState = state | |
| 248 end | |
| 249 | |
| 250 -- addon:SetDefaultModulePrototype( prototype ) | |
| 251 -- prototype (string or table) - the default prototype to use if none is specified on module creation | |
| 252 function SetDefaultModulePrototype(self, prototype) | |
| 253 if next(self.modules) then | |
| 254 error("Usage: SetDefaultModulePrototype(prototype): cannot change the module defaults after a module has been registered.", 2) | |
| 255 end | |
| 256 if type(prototype) ~= "table" then | |
| 257 error(("Usage: SetDefaultModulePrototype(prototype): 'prototype' - table expected got '%s'."):format(type(prototype)), 2) | |
| 258 end | |
| 259 self.defaultModulePrototype = prototype | |
| 260 end | |
| 261 | |
| 262 -- addon:SetEnabledState ( state ) | |
| 263 -- state ( boolean ) - set the state of an addon or module (enabled=true, disabled=false) | |
| 264 -- | |
| 265 -- should only be called before any Enabling actually happend, aka in OnInitialize | |
| 266 function SetEnabledState(self, state) | |
| 267 self.enabledState = state | |
| 268 end | |
| 269 | |
| 270 | |
| 271 local function IterateModules(self) return pairs(self.modules) end | |
| 272 local function IterateEmbeds(self) return pairs(AceAddon.embeds[self]) end | |
| 273 local function IsEnabled(self) return self.enabledState end | |
| 274 local mixins = { | |
| 275 NewModule = NewModule, | |
| 276 GetModule = GetModule, | |
| 277 Enable = Enable, | |
| 278 Disable = Disable, | |
| 279 EnableModule = EnableModule, | |
| 280 DisableModule = DisableModule, | |
| 281 IsEnabled = IsEnabled, | |
| 282 SetDefaultModuleLibraries = SetDefaultModuleLibraries, | |
| 283 SetDefaultModuleState = SetDefaultModuleState, | |
| 284 SetDefaultModulePrototype = SetDefaultModulePrototype, | |
| 285 SetEnabledState = SetEnabledState, | |
| 286 IterateModules = IterateModules, | |
| 287 IterateEmbeds = IterateEmbeds, | |
| 288 GetName = GetName, | |
| 289 } | |
| 290 local function IsModule(self) return false end | |
| 291 local pmixins = { | |
| 292 defaultModuleState = true, | |
| 293 enabledState = true, | |
| 294 IsModule = IsModule, | |
| 295 } | |
| 296 -- Embed( target ) | |
| 297 -- target (object) - target object to embed aceaddon in | |
| 298 -- | |
| 299 -- this is a local function specifically since it's meant to be only called internally | |
| 300 function Embed(target) | |
| 301 for k, v in pairs(mixins) do | |
| 302 target[k] = v | |
| 303 end | |
| 304 for k, v in pairs(pmixins) do | |
| 305 target[k] = target[k] or v | |
| 306 end | |
| 307 end | |
| 308 | |
| 309 | |
| 310 -- AceAddon:IntializeAddon( addon ) | |
| 311 -- addon (object) - addon to intialize | |
| 312 -- | |
| 313 -- calls OnInitialize on the addon object if available | |
| 314 -- calls OnEmbedInitialize on embedded libs in the addon object if available | |
| 315 function AceAddon:InitializeAddon(addon) | |
| 316 safecall(addon.OnInitialize, addon) | |
| 317 | |
| 318 local embeds = self.embeds[addon] | |
| 319 for i = 1, #embeds do | |
| 320 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 321 if lib then safecall(lib.OnEmbedInitialize, lib, addon) end | |
| 322 end | |
| 323 | |
| 324 -- we don't call InitializeAddon on modules specifically, this is handled | |
| 325 -- from the event handler and only done _once_ | |
| 326 end | |
| 327 | |
| 328 -- AceAddon:EnableAddon( addon ) | |
| 329 -- addon (object) - addon to enable | |
| 330 -- | |
| 331 -- calls OnEnable on the addon object if available | |
| 332 -- calls OnEmbedEnable on embedded libs in the addon object if available | |
| 333 function AceAddon:EnableAddon(addon) | |
| 334 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
| 335 if self.statuses[addon.name] or not addon.enabledState then return false end | |
| 336 -- TODO: handle 'first'? Or let addons do it on their own? | |
| 337 safecall(addon.OnEnable, addon) | |
| 338 local embeds = self.embeds[addon] | |
| 339 for i = 1, #embeds do | |
| 340 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 341 if lib then safecall(lib.OnEmbedEnable, lib, addon) end | |
| 342 end | |
| 343 self.statuses[addon.name] = addon.enabledState | |
| 344 | |
| 345 -- enable possible modules. | |
| 346 for name, module in pairs(addon.modules) do | |
| 347 self:EnableAddon(module) | |
| 348 end | |
| 349 | |
| 350 return true | |
| 351 end | |
| 352 | |
| 353 -- AceAddon:DisableAddon( addon ) | |
| 354 -- addon (object|string) - addon to disable | |
| 355 -- | |
| 356 -- calls OnDisable on the addon object if available | |
| 357 -- calls OnEmbedDisable on embedded libs in the addon object if available | |
| 358 function AceAddon:DisableAddon(addon) | |
| 359 if type(addon) == "string" then addon = AceAddon:GetAddon(addon) end | |
| 360 if not self.statuses[addon.name] then return false end | |
| 361 safecall( addon.OnDisable, addon ) | |
| 362 local embeds = self.embeds[addon] | |
| 363 for i = 1, #embeds do | |
| 364 local lib = LibStub:GetLibrary(embeds[i], true) | |
| 365 if lib then safecall(lib.OnEmbedDisable, lib, addon) end | |
| 366 end | |
| 367 self.statuses[addon.name] = addon.enabledState | |
| 368 | |
| 369 -- disable possible modules. | |
| 370 for name, module in pairs(addon.modules) do | |
| 371 self:DisableAddon(module) | |
| 372 end | |
| 373 | |
| 374 return true | |
| 375 end | |
| 376 | |
| 377 --The next few funcs are just because no one should be reaching into the internal registries | |
| 378 --Thoughts? | |
| 379 function AceAddon:IterateAddons() return pairs(self.addons) end | |
| 380 function AceAddon:IterateEmbedsOnAddon(addon) return pairs(self.embeds[addon]) end | |
| 381 function AceAddon:IterateAddonStatus() return pairs(self.statuses) end | |
| 382 function AceAddon:IterateModulesOfAddon(addon) return pairs(addon.modules) end | |
| 383 | |
| 384 -- Event Handling | |
| 385 local function onEvent(this, event, arg1) | |
| 386 if event == "ADDON_LOADED" or event == "PLAYER_LOGIN" then | |
| 387 -- if a addon loads another addon, recursion could happen here, so we need to validate the table on every iteration | |
| 388 while(#AceAddon.initializequeue > 0) do | |
| 389 local addon = tremove(AceAddon.initializequeue, 1) | |
| 390 -- this might be an issue with recursion - TODO: validate | |
| 391 if event == "ADDON_LOADED" then addon.baseName = arg1 end | |
| 392 AceAddon:InitializeAddon(addon) | |
| 393 tinsert(AceAddon.enablequeue, addon) | |
| 394 end | |
| 395 | |
| 396 if IsLoggedIn() then | |
| 397 while(#AceAddon.enablequeue > 0) do | |
| 398 local addon = tremove(AceAddon.enablequeue, 1) | |
| 399 AceAddon:EnableAddon(addon) | |
| 400 end | |
| 401 end | |
| 402 end | |
| 403 end | |
| 404 | |
| 405 AceAddon.frame:RegisterEvent("ADDON_LOADED") | |
| 406 AceAddon.frame:RegisterEvent("PLAYER_LOGIN") | |
| 407 AceAddon.frame:SetScript("OnEvent", onEvent) | |
| 408 | |
| 409 -- upgrade embeded | |
| 410 for name, addon in pairs(AceAddon.addons) do | |
| 411 Embed(addon) | |
| 412 end |
