vanilla-wow-addons – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | --[[ |
2 | Name: AceModuleCore-2.0 |
||
3 | Revision: $Rev: 4588 $ |
||
4 | Author(s): Kaelten (kaelten@gmail.com) |
||
5 | ckknight (ckknight@gmail.com) |
||
6 | Website: http://www.wowace.com/ |
||
7 | Documentation: http://wiki.wowace.com/index.php/AceModuleCore-2.0 |
||
8 | SVN: http://svn.wowace.com/root/trunk/Ace2/AceModuleCore-2.0 |
||
9 | Description: Mixin to provide a module system so that modules or plugins can |
||
10 | use an addon as its core. |
||
11 | Dependencies: AceLibrary, AceOO-2.0, AceAddon-2.0, Compost-2.0 (optional) |
||
12 | ]] |
||
13 | |||
14 | local MAJOR_VERSION = "AceModuleCore-2.0" |
||
15 | local MINOR_VERSION = "$Revision: 4588 $" |
||
16 | |||
17 | if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary") end |
||
18 | if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end |
||
19 | |||
20 | if not AceLibrary:HasInstance("AceOO-2.0") then error(MAJOR_VERSION .. " requires AceOO-2.0") end |
||
21 | |||
22 | local AceOO = AceLibrary:GetInstance("AceOO-2.0") |
||
23 | local AceModuleCore = AceOO.Mixin { |
||
24 | "NewModule", |
||
25 | "HasModule", |
||
26 | "GetModule", |
||
27 | "IsModule", |
||
28 | "IterateModules", |
||
29 | "SetModuleMixins", |
||
30 | "SetModuleClass", |
||
31 | "IsModuleActive", |
||
32 | "ToggleModuleActive" |
||
33 | } |
||
34 | |||
35 | local Compost = AceLibrary:HasInstance("Compost-2.0") and AceLibrary("Compost-2.0") |
||
36 | |||
37 | local function getlibrary(lib) |
||
38 | if type(lib) == "string" then |
||
39 | return AceLibrary(lib) |
||
40 | else |
||
41 | return lib |
||
42 | end |
||
43 | end |
||
44 | |||
45 | local tmp |
||
46 | function AceModuleCore:NewModule(name, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
||
47 | if not self.modules then |
||
48 | AceModuleCore:error("CreatePrototype() must be called before attempting to create a new module.", 2) |
||
49 | end |
||
50 | AceModuleCore:argCheck(name, 2, "string") |
||
51 | if string.len(name) == 0 then |
||
52 | AceModuleCore:error("Bad argument #2 to `NewModule`, string must not be empty") |
||
53 | end |
||
54 | if self.modules[name] then |
||
55 | AceModuleCore:error("The module %q has already been registered", name) |
||
56 | end |
||
57 | |||
58 | if not tmp then |
||
59 | tmp = {} |
||
60 | end |
||
61 | if a1 then table.insert(tmp, a1) |
||
62 | if a2 then table.insert(tmp, a2) |
||
63 | if a3 then table.insert(tmp, a3) |
||
64 | if a4 then table.insert(tmp, a4) |
||
65 | if a5 then table.insert(tmp, a5) |
||
66 | if a6 then table.insert(tmp, a6) |
||
67 | if a7 then table.insert(tmp, a7) |
||
68 | if a8 then table.insert(tmp, a8) |
||
69 | if a9 then table.insert(tmp, a9) |
||
70 | if a10 then table.insert(tmp, a10) |
||
71 | if a11 then table.insert(tmp, a11) |
||
72 | if a12 then table.insert(tmp, a12) |
||
73 | if a13 then table.insert(tmp, a13) |
||
74 | if a14 then table.insert(tmp, a14) |
||
75 | if a15 then table.insert(tmp, a15) |
||
76 | if a16 then table.insert(tmp, a16) |
||
77 | if a17 then table.insert(tmp, a17) |
||
78 | if a18 then table.insert(tmp, a18) |
||
79 | if a19 then table.insert(tmp, a19) |
||
80 | if a20 then table.insert(tmp, a20) |
||
81 | end end end end end end end end end end end end end end end end end end end end |
||
82 | for k,v in ipairs(tmp) do |
||
83 | tmp[k] = getlibrary(v) |
||
84 | end |
||
85 | |||
86 | if self.moduleMixins then |
||
87 | for _,mixin in ipairs(self.moduleMixins) do |
||
88 | local exists = false |
||
89 | for _,v in ipairs(tmp) do |
||
90 | if mixin == v then |
||
91 | exists = true |
||
92 | break |
||
93 | end |
||
94 | end |
||
95 | if not exists then |
||
96 | table.insert(tmp, mixin) |
||
97 | end |
||
98 | end |
||
99 | end |
||
100 | |||
101 | local module = AceOO.Classpool(self.moduleClass, unpack(tmp)):new(name) |
||
102 | self.modules[name] = module |
||
103 | module.name = name |
||
104 | module.title = name |
||
105 | |||
106 | AceModuleCore.totalModules[module] = self |
||
107 | |||
108 | for k in pairs(tmp) do |
||
109 | tmp[k] = nil |
||
110 | end |
||
111 | table.setn(tmp, 0) |
||
112 | return module |
||
113 | end |
||
114 | |||
115 | function AceModuleCore:HasModule(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
||
116 | if a1 then if not self.modules[a1] then return false end |
||
117 | if a2 then if not self.modules[a2] then return false end |
||
118 | if a3 then if not self.modules[a3] then return false end |
||
119 | if a4 then if not self.modules[a4] then return false end |
||
120 | if a5 then if not self.modules[a5] then return false end |
||
121 | if a6 then if not self.modules[a6] then return false end |
||
122 | if a7 then if not self.modules[a7] then return false end |
||
123 | if a8 then if not self.modules[a8] then return false end |
||
124 | if a9 then if not self.modules[a9] then return false end |
||
125 | if a10 then if not self.modules[a10] then return false end |
||
126 | if a11 then if not self.modules[a11] then return false end |
||
127 | if a12 then if not self.modules[a12] then return false end |
||
128 | if a13 then if not self.modules[a13] then return false end |
||
129 | if a14 then if not self.modules[a14] then return false end |
||
130 | if a15 then if not self.modules[a15] then return false end |
||
131 | if a16 then if not self.modules[a16] then return false end |
||
132 | if a17 then if not self.modules[a17] then return false end |
||
133 | if a18 then if not self.modules[a18] then return false end |
||
134 | if a19 then if not self.modules[a19] then return false end |
||
135 | if a20 then if not self.modules[a20] then return false end |
||
136 | end end end end end end end end end end end end end end end end end end end end |
||
137 | |||
138 | return true |
||
139 | end |
||
140 | |||
141 | function AceModuleCore:GetModule(name) |
||
142 | if not self.modules then |
||
143 | AceModuleCore:error("Error initializing class. Please report error.") |
||
144 | end |
||
145 | if not self.modules[name] then |
||
146 | AceModuleCore:error("Cannot find module %q.", name) |
||
147 | end |
||
148 | return self.modules[name] |
||
149 | end |
||
150 | |||
151 | function AceModuleCore:IsModule(module) |
||
152 | if self == AceModuleCore then |
||
153 | return AceModuleCore.totalModules[module] |
||
154 | else |
||
155 | for k,v in pairs(self.modules) do |
||
156 | if v == module then |
||
157 | return true |
||
158 | end |
||
159 | end |
||
160 | return false |
||
161 | end |
||
162 | end |
||
163 | |||
164 | function AceModuleCore:IterateModules() |
||
165 | return pairs(self.modules) |
||
166 | end |
||
167 | |||
168 | function AceModuleCore:SetModuleMixins(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) |
||
169 | if self.moduleMixins then |
||
170 | AceModuleCore:error('Cannot call "SetModuleMixins" twice') |
||
171 | elseif not self.modules then |
||
172 | AceModuleCore:error("Error initializing class. Please report error.") |
||
173 | elseif next(self.modules) then |
||
174 | AceModuleCore:error('Cannot call "SetModuleMixins" after "NewModule" has been called.') |
||
175 | end |
||
176 | |||
177 | self.moduleMixins = Compost and Compost:Acquire(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20) or {a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20} |
||
178 | for k,v in ipairs(self.moduleMixins) do |
||
179 | self.moduleMixins[k] = getlibrary(v) |
||
180 | end |
||
181 | end |
||
182 | |||
183 | function AceModuleCore:SetModuleClass(class) |
||
184 | class = getlibrary(class) |
||
185 | AceModuleCore:assert(AceOO.inherits(class, AceOO.Class), "Bad argument #2 to `SetModuleClass' (Class expected)") |
||
186 | if not self.modules then |
||
187 | AceModuleCore:error("Error initializing class. Please report error.") |
||
188 | end |
||
189 | if self.customModuleClass then |
||
190 | AceModuleCore:error("Cannot call `SetModuleClass' twice.") |
||
191 | end |
||
192 | self.customModuleClass = true |
||
193 | self.moduleClass = class |
||
194 | self.modulePrototype = class.prototype |
||
195 | end |
||
196 | |||
197 | function AceModuleCore:ToggleModuleActive(module, state) |
||
198 | AceModuleCore:argCheck(module, 2, "table", "string") |
||
199 | AceModuleCore:argCheck(state, 3, "nil", "boolean") |
||
200 | |||
201 | if type(module) == "string" then |
||
202 | if not self:HasModule(module) then |
||
203 | AceModuleCore:error("Cannot find module %q", module) |
||
204 | end |
||
205 | module = self:GetModule(module) |
||
206 | else |
||
207 | if not self:IsModule(module) then |
||
208 | AceModuleCore:error("%q is not a module", module) |
||
209 | end |
||
210 | end |
||
211 | |||
212 | local disable |
||
213 | if state == nil then |
||
214 | disable = self:IsModuleActive(module) |
||
215 | else |
||
216 | disable = not state |
||
217 | if disable ~= self:IsModuleActive(module) then |
||
218 | return |
||
219 | end |
||
220 | end |
||
221 | |||
222 | if type(module.ToggleActive) == "function" then |
||
223 | return module:ToggleActive(not disable) |
||
224 | elseif AceOO.inherits(self, "AceDB-2.0") then |
||
225 | if not self.db or not self.db.raw then |
||
226 | AceModuleCore:error("Cannot toggle a module until `RegisterDB' has been called and `ADDON_LOADED' has been fireed.") |
||
227 | end |
||
228 | if type(self.db.raw.disabledModules) ~= "table" then |
||
229 | self.db.raw.disabledModules = Compost and Compost:Acquire() or {} |
||
230 | end |
||
231 | local _,profile = self:GetProfile() |
||
232 | if type(self.db.raw.disabledModules[profile]) ~= "table" then |
||
233 | self.db.raw.disabledModules[profile] = Compost and Compost:Acquire() or {} |
||
234 | end |
||
235 | if type(self.db.raw.disabledModules[profile][module.name]) ~= "table" then |
||
236 | self.db.raw.disabledModules[profile][module.name] = disable or nil |
||
237 | end |
||
238 | if not disable then |
||
239 | if not next(self.db.raw.disabledModules[profile]) then |
||
240 | if Compost then |
||
241 | Compost:Reclaim(self.db.raw.disabledModules[profile]) |
||
242 | end |
||
243 | self.db.raw.disabledModules[profile] = nil |
||
244 | end |
||
245 | if not next(self.db.raw.disabledModules) then |
||
246 | if Compost then |
||
247 | Compost:Reclaim(self.db.raw.disabledModules) |
||
248 | end |
||
249 | self.db.raw.disabledModules = nil |
||
250 | end |
||
251 | end |
||
252 | else |
||
253 | if type(self.disabledModules) ~= "table" then |
||
254 | self.disabledModules = Compost and Compost:Acquire() or {} |
||
255 | end |
||
256 | self.disabledModules[module.name] = disable or nil |
||
257 | end |
||
258 | if AceOO.inherits(module, "AceAddon-2.0") then |
||
259 | local AceAddon = AceLibrary("AceAddon-2.0") |
||
260 | if not AceAddon.addonsStarted[module] then |
||
261 | return |
||
262 | end |
||
263 | end |
||
264 | if not disable then |
||
265 | if type(module.OnEnable) == "function" then |
||
266 | module:OnEnable() |
||
267 | end |
||
268 | else |
||
269 | local current = module.class |
||
270 | while true do |
||
271 | if current == AceOO.Class then |
||
272 | break |
||
273 | end |
||
274 | if current.mixins then |
||
275 | for mixin in pairs(current.mixins) do |
||
276 | if type(mixin.OnEmbedDisable) == "function" then |
||
277 | mixin:OnEmbedDisable(module) |
||
278 | end |
||
279 | end |
||
280 | end |
||
281 | current = current.super |
||
282 | end |
||
283 | if type(module.OnDisable) == "function" then |
||
284 | module:OnDisable() |
||
285 | end |
||
286 | end |
||
287 | return not disable |
||
288 | end |
||
289 | |||
290 | function AceModuleCore:IsModuleActive(module) |
||
291 | AceModuleCore:argCheck(module, 2, "table", "string") |
||
292 | |||
293 | if AceModuleCore == self then |
||
294 | self:argCheck(module, 2, "table") |
||
295 | |||
296 | local core = AceModuleCore.totalModules[module] |
||
297 | if not core then |
||
298 | self:error("Bad argument #2 to `IsModuleActive'. Not a module") |
||
299 | end |
||
300 | return core:IsModuleActive(module) |
||
301 | end |
||
302 | |||
303 | if type(module) == "string" then |
||
304 | if not self:HasModule(module) then |
||
305 | AceModuleCore:error("Cannot find module %q", module) |
||
306 | end |
||
307 | module = self:GetModule(module) |
||
308 | else |
||
309 | if not self:IsModule(module) then |
||
310 | AceModuleCore:error("%q is not a module", module) |
||
311 | end |
||
312 | end |
||
313 | |||
314 | if type(module.IsActive) == "function" then |
||
315 | return module:IsActive() |
||
316 | elseif AceOO.inherits(self, "AceDB-2.0") then |
||
317 | local _,profile = self:GetProfile() |
||
318 | return not self.db or not self.db.raw or not self.db.raw.disabledModules or not self.db.raw.disabledModules[profile] or not self.db.raw.disabledModules[profile][module.name] |
||
319 | else |
||
320 | return not self.disabledModules or not self.disabledModules[module.name] |
||
321 | end |
||
322 | end |
||
323 | |||
324 | function AceModuleCore:OnInstanceInit(target) |
||
325 | if target.modules then |
||
326 | AceModuleCore:error("OnInstanceInit cannot be called twice") |
||
327 | end |
||
328 | target.modules = Compost and Compost:Acquire() or {} |
||
329 | |||
330 | target.moduleClass = AceOO.Class("AceAddon-2.0") |
||
331 | target.modulePrototype = target.moduleClass.prototype |
||
332 | end |
||
333 | |||
334 | AceModuleCore.OnManualEmbed = AceModuleCore.OnInstanceInit |
||
335 | |||
336 | local function activate(self, oldLib, oldDeactivate) |
||
337 | AceModuleCore = self |
||
338 | |||
339 | self.super.activate(self, oldLib, oldDeactivate) |
||
340 | |||
341 | if oldLib then |
||
342 | self.totalModules = oldLib.totalModules |
||
343 | end |
||
344 | if not self.totalModules then |
||
345 | self.totalModules = {} |
||
346 | end |
||
347 | end |
||
348 | |||
349 | local function external(self, major, instance) |
||
350 | if major == "Compost-2.0" then |
||
351 | Compost = instance |
||
352 | end |
||
353 | end |
||
354 | |||
355 | AceLibrary:Register(AceModuleCore, MAJOR_VERSION, MINOR_VERSION, activate) |
||
356 | AceModuleCore = AceLibrary(MAJOR_VERSION) |