vanilla-wow-addons – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 --[[
2 Name: AceConsole-2.0
3 Revision: $Rev: 11285 $
4 Developed by: The Ace Development Team (http://www.wowace.com/index.php/The_Ace_Development_Team)
5 Inspired By: Ace 1.x by Turan (turan@gryphon.com)
6 Website: http://www.wowace.com/
7 Documentation: http://www.wowace.com/index.php/AceConsole-2.0
8 SVN: http://svn.wowace.com/root/trunk/Ace2/AceConsole-2.0
9 Description: Mixin to allow for input/output capabilities. This uses the
10 AceOptions data table format to determine input.
11 http://wiki.wowace.com/index.php/AceOptions_data_table
12 Dependencies: AceLibrary, AceOO-2.0
13 ]]
14  
15 local MAJOR_VERSION = "AceConsole-2.0"
16 local MINOR_VERSION = "$Revision: 11285 $"
17  
18 if not AceLibrary then error(MAJOR_VERSION .. " requires AceLibrary.") end
19 if not AceLibrary:IsNewVersion(MAJOR_VERSION, MINOR_VERSION) then return end
20  
21 if not AceLibrary:HasInstance("AceOO-2.0") then error(MAJOR_VERSION .. " requires AceOO-2.0.") end
22 -- localize --
23 local MAP_ONOFF = { [false] = "|cffff0000Off|r", [true] = "|cff00ff00On|r" }
24 local USAGE = "Usage"
25 local IS_CURRENTLY_SET_TO = "|cffffff7f%s|r is currently set to |cffffff7f[|r%s|cffffff7f]|r"
26 local IS_NOW_SET_TO = "|cffffff7f%s|r is now set to |cffffff7f[|r%s|cffffff7f]|r"
27 local IS_NOT_A_VALID_OPTION_FOR = "[|cffffff7f%s|r] is not a valid option for |cffffff7f%s|r"
28 local IS_NOT_A_VALID_VALUE_FOR = "[|cffffff7f%s|r] is not a valid value for |cffffff7f%s|r"
29 local NO_OPTIONS_AVAILABLE = "No options available"
30 local OPTION_HANDLER_NOT_FOUND = "Option handler |cffffff7f%q|r not found."
31 local OPTION_HANDLER_NOT_VALID = "Option handler not valid."
32 local OPTION_IS_DISABLED = "Option |cffffff7f%s|r is disabled."
33 -- localize --
34  
35 local NONE = NONE or "None"
36  
37 local AceOO = AceLibrary("AceOO-2.0")
38 local AceEvent
39  
40 local AceConsole = AceOO.Mixin { "Print", "PrintComma", "CustomPrint", "RegisterChatCommand" }
41 local Dewdrop
42  
43 local _G = getfenv(0)
44  
45 local function print(text, name, r, g, b, frame, delay)
46 if not text or string.len(text) == 0 then
47 text = " "
48 end
49 if not name or name == AceConsole then
50 (frame or DEFAULT_CHAT_FRAME):AddMessage(text, r, g, b, nil, delay or 5)
51 else
52 (frame or DEFAULT_CHAT_FRAME):AddMessage("|cffffff78" .. tostring(name) .. ":|r " .. text, r, g, b, nil, delay or 5)
53 end
54 end
55  
56 local tmp
57 function AceConsole:CustomPrint(r, g, b, frame, delay, connector, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
58 a1 = tostring(a1)
59 if string.find(a1, "%%") then
60 print(string.format(a1, tostring(a2), tostring(a3), tostring(a4), tostring(a5), tostring(a6), tostring(a7), tostring(a8), tostring(a9), tostring(a10), tostring(a11), tostring(a12), tostring(a13), tostring(a14), tostring(a15), tostring(a16), tostring(a17), tostring(a18), tostring(a19), tostring(a20)), self, r, g, b, frame or self.printFrame, delay)
61 else
62 if not tmp then
63 tmp = {}
64 end
65 tmp[1] = a1
66 tmp[2] = a2
67 tmp[3] = a3
68 tmp[4] = a4
69 tmp[5] = a5
70 tmp[6] = a6
71 tmp[7] = a7
72 tmp[8] = a8
73 tmp[9] = a9
74 tmp[10] = a10
75 tmp[11] = a11
76 tmp[12] = a12
77 tmp[13] = a13
78 tmp[14] = a14
79 tmp[15] = a15
80 tmp[16] = a16
81 tmp[17] = a17
82 tmp[18] = a18
83 tmp[19] = a19
84 tmp[20] = a20
85 local n = 20
86 while tmp[n] == nil do
87 n = n - 1
88 end
89 table.setn(tmp, n)
90 for k = 1, n do
91 tmp[k] = tostring(tmp[k])
92 end
93 print(table.concat(tmp, connector or " "), self, r, g, b, frame or self.printFrame, delay)
94 for k,v in tmp do
95 tmp[k] = nil
96 end
97 table.setn(tmp, 0)
98 end
99 end
100  
101 function AceConsole:Print(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
102 return AceConsole.CustomPrint(self, nil, nil, nil, nil, nil, " ", a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
103 end
104  
105 function AceConsole:PrintComma(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
106 return AceConsole.CustomPrint(self, nil, nil, nil, nil, nil, ", ", a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20)
107 end
108  
109 local work
110 local argwork
111  
112 local function findTableLevel(self, options, chat, text, index, passTable)
113 if not index then
114 index = 1
115 if work then
116 for k,v in pairs(work) do
117 work[k] = nil
118 end
119 table.setn(work, 0)
120 for k,v in pairs(argwork) do
121 argwork[k] = nil
122 end
123 table.setn(argwork, 0)
124 else
125 work = {}
126 argwork = {}
127 end
128 local len = string.len(text)
129 local count
130 repeat
131 text, count = string.gsub(text, "(|cff%x%x%x%x%x%x|Hitem:%d-:%d-:%d-:%d-|h%[[^%]]-) (.-%]|h|r)", "%1\001%2")
132 until count == 0
133 text = string.gsub(text, "(%]|h|r)(|cff%x%x%x%x%x%x|Hitem:%d-:%d-:%d-:%d-|h%[)", "%1 %2")
134 for token in string.gfind(text, "([^%s]+)") do
135 local token = token
136 local num = tonumber(token)
137 if num then
138 token = num
139 else
140 token = string.gsub(token, "\001", " ")
141 end
142 table.insert(work, token)
143 end
144 end
145  
146 local path = chat
147 for i = 1, index - 1 do
148 path = path .. " " .. tostring(work[i])
149 end
150  
151 if type(options.args) == "table" then
152 local disabled, hidden = options.disabled, options.cmdHidden or options.hidden
153 if hidden then
154 if type(hidden) == "function" then
155 hidden = hidden()
156 elseif type(hidden) == "string" then
157 local handler = options.handler or self
158 if type(handler[hidden]) ~= "function" then
159 AceConsole:error(OPTION_HANDLER_NOT_FOUND, hidden)
160 end
161 hidden = handler[hidden](handler)
162 end
163 end
164 if hidden then
165 disabled = true
166 elseif disabled then
167 if type(disabled) == "function" then
168 disabled = disabled()
169 elseif type(disabled) == "string" then
170 local handler = options.handler or self
171 if type(handler[disabled]) ~= "function" then
172 AceConsole:error(OPTION_HANDLER_NOT_FOUND, disabled)
173 end
174 disabled = handler[disabled](handler)
175 end
176 end
177 if not disabled then
178 local next = work[index] and string.lower(work[index])
179 if next then
180 for k,v in options.args do
181 local good = false
182 if string.lower(k) == next then
183 good = true
184 elseif type(v.aliases) == "table" then
185 for _,alias in ipairs(v.aliases) do
186 if string.lower(alias) == next then
187 good = true
188 break
189 end
190 end
191 elseif type(v.aliases) == "string" and string.lower(v.aliases) == next then
192 good = true
193 end
194 if good then
195 return findTableLevel(options.handler or self, v, chat, text, index + 1, options.pass and options or nil)
196 end
197 end
198 end
199 end
200 end
201 for i = index, table.getn(work) do
202 table.insert(argwork, work[i])
203 end
204 return options, path, argwork, options.handler or self, passTable, passTable and work[index - 1]
205 end
206  
207 local function validateOptionsMethods(self, options, position)
208 if type(options) ~= "table" then
209 return "Options must be a table.", position
210 end
211 self = options.handler or self
212 if options.type == "execute" then
213 if options.func and type(options.func) ~= "string" and type(options.func) ~= "function" then
214 return "func must be a string or function", position
215 end
216 if options.func and type(options.func) == "string" and type(self[options.func]) ~= "function" then
217 return string.format("%q is not a proper function", options.func), position
218 end
219 else
220 if options.get then
221 if type(options.get) ~= "string" and type(options.get) ~= "function" then
222 return "get must be a string or function", position
223 end
224 if type(options.get) == "string" and type(self[options.get]) ~= "function" then
225 return string.format("%q is not a proper function", options.get), position
226 end
227 end
228 if options.set then
229 if type(options.set) ~= "string" and type(options.set) ~= "function" then
230 return "set must be a string or function", position
231 end
232 if type(options.set) == "string" and type(self[options.set]) ~= "function" then
233 return string.format("%q is not a proper function", options.set), position
234 end
235 end
236 if options.validate and type(options.validate) ~= "table" then
237 if type(options.validate) ~= "string" and type(options.validate) ~= "function" then
238 return "validate must be a string or function", position
239 end
240 if type(options.validate) == "string" and type(self[options.validate]) ~= "function" then
241 return string.format("%q is not a proper function", options.validate), position
242 end
243 end
244 end
245 if options.disabled and type(options.disabled) == "string" and type(self[options.disabled]) ~= "function" then
246 return string.format("%q is not a proper function", options.disabled), position
247 end
248 if options.cmdHidden and type(options.cmdHidden) == "string" and type(self[options.cmdHidden]) ~= "function" then
249 return string.format("%q is not a proper function", options.cmdHidden), position
250 end
251 if options.guiHidden and type(options.guiHidden) == "string" and type(self[options.guiHidden]) ~= "function" then
252 return string.format("%q is not a proper function", options.guiHidden), position
253 end
254 if options.hidden and type(options.hidden) == "string" and type(self[options.hidden]) ~= "function" then
255 return string.format("%q is not a proper function", options.hidden), position
256 end
257 if options.type == "group" and type(options.args) == "table" then
258 for k,v in pairs(options.args) do
259 if type(v) == "table" then
260 local newposition
261 if position then
262 newposition = position .. ".args." .. k
263 else
264 newposition = "args." .. k
265 end
266 local err, pos = validateOptionsMethods(self, v, newposition)
267 if err then
268 return err, pos
269 end
270 end
271 end
272 end
273 end
274  
275 local function validateOptions(options, position, baseOptions, fromPass)
276 if not baseOptions then
277 baseOptions = options
278 end
279 if type(options) ~= "table" then
280 return "Options must be a table.", position
281 end
282 local kind = options.type
283 if type(kind) ~= "string" then
284 return '"type" must be a string.', position
285 elseif kind ~= "group" and kind ~= "range" and kind ~= "text" and kind ~= "execute" and kind ~= "toggle" and kind ~= "color" and kind ~= "header" then
286 return '"type" must either be "range", "text", "group", "toggle", "execute", "color", or "header".', position
287 end
288 if options.aliases then
289 if type(options.aliases) ~= "table" and type(options.aliases) ~= "string" then
290 return '"alias" must be a table or string', position
291 end
292 end
293 if not fromPass then
294 if kind == "execute" then
295 if type(options.func) ~= "string" and type(options.func) ~= "function" then
296 return '"func" must be a string or function', position
297 end
298 elseif kind == "range" or kind == "text" or kind == "toggle" then
299 if type(options.set) ~= "string" and type(options.set) ~= "function" then
300 return '"set" must be a string or function', position
301 end
302 if kind == "text" and options.get == false then
303 elseif type(options.get) ~= "string" and type(options.get) ~= "function" then
304 return '"get" must be a string or function', position
305 end
306 elseif kind == "group" and options.pass then
307 if options.pass ~= true then
308 return '"pass" must be either nil, true, or false', position
309 end
310 if not options.func then
311 if type(options.set) ~= "string" and type(options.set) ~= "function" then
312 return '"set" must be a string or function', position
313 end
314 if type(options.get) ~= "string" and type(options.get) ~= "function" then
315 return '"get" must be a string or function', position
316 end
317 elseif type(options.func) ~= "string" and type(options.func) ~= "function" then
318 return '"func" must be a string or function', position
319 end
320 end
321 else
322 if kind == "group" then
323 return 'cannot have "type" = "group" as a subgroup of a passing group', position
324 end
325 end
326 if options ~= baseOptions then
327 if kind == "header" then
328 elseif type(options.desc) ~= "string" then
329 return '"desc" must be a string', position
330 elseif string.len(options.desc) == 0 then
331 return '"desc" cannot be a 0-length string', position
332 end
333 end
334  
335 if options ~= baseOptions or kind == "range" or kind == "text" or kind == "toggle" or kind == "color" then
336 if options.type == "header" and not options.cmdName and not options.name then
337 elseif options.cmdName then
338 if type(options.cmdName) ~= "string" then
339 return '"cmdName" must be a string or nil', position
340 elseif string.len(options.cmdName) == 0 then
341 return '"cmdName" cannot be a 0-length string', position
342 end
343 if type(options.guiName) ~= "string" then
344 if not options.guiNameIsMap then
345 return '"guiName" must be a string or nil', position
346 end
347 elseif string.len(options.guiName) == 0 then
348 return '"guiName" cannot be a 0-length string', position
349 end
350 else
351 if type(options.name) ~= "string" then
352 return '"name" must be a string', position
353 elseif string.len(options.name) == 0 then
354 return '"name" cannot be a 0-length string', position
355 end
356 end
357 end
358 if options.guiNameIsMap then
359 if type(options.guiNameIsMap) ~= "boolean" then
360 return '"guiNameIsMap" must be a boolean or nil', position
361 elseif options.type ~= "toggle" then
362 return 'if "guiNameIsMap" is true, then "type" must be set to \'toggle\'', position
363 elseif type(options.map) ~= "table" then
364 return '"map" must be a table', position
365 end
366 end
367 if options.message and type(options.message) ~= "string" then
368 return '"message" must be a string or nil', position
369 end
370 if options.error and type(options.error) ~= "string" then
371 return '"error" must be a string or nil', position
372 end
373 if options.current and type(options.current) ~= "string" then
374 return '"current" must be a string or nil', position
375 end
376 if options.order then
377 if type(options.order) ~= "number" or (-1 < options.order and options.order < 0.999) then
378 return '"order" must be a non-zero number or nil', position
379 end
380 end
381 if options.disabled then
382 if type(options.disabled) ~= "function" and type(options.disabled) ~= "string" and options.disabled ~= true then
383 return '"disabled" must be a function, string, or boolean', position
384 end
385 end
386 if options.cmdHidden then
387 if type(options.cmdHidden) ~= "function" and type(options.cmdHidden) ~= "string" and options.cmdHidden ~= true then
388 return '"cmdHidden" must be a function, string, or boolean', position
389 end
390 end
391 if options.guiHidden then
392 if type(options.guiHidden) ~= "function" and type(options.guiHidden) ~= "string" and options.guiHidden ~= true then
393 return '"guiHidden" must be a function, string, or boolean', position
394 end
395 end
396 if options.hidden then
397 if type(options.hidden) ~= "function" and type(options.hidden) ~= "string" and options.hidden ~= true then
398 return '"hidden" must be a function, string, or boolean', position
399 end
400 end
401 if kind == "text" then
402 if type(options.validate) == "table" then
403 local t = options.validate
404 local iTable = nil
405 for k,v in pairs(t) do
406 if type(k) == "number" then
407 if iTable == nil then
408 iTable = true
409 elseif not iTable then
410 return '"validate" must either have all keys be indexed numbers or strings', position
411 elseif k < 1 or k > table.getn(t) then
412 return '"validate" numeric keys must be indexed properly. >= 1 and <= table.getn', position
413 end
414 else
415 if iTable == nil then
416 iTable = false
417 elseif iTable then
418 return '"validate" must either have all keys be indexed numbers or strings', position
419 end
420 end
421 if type(v) ~= "string" then
422 return '"validate" values must all be strings', position
423 end
424 end
425 else
426 if type(options.usage) ~= "string" then
427 return '"usage" must be a string', position
428 elseif options.validate and type(options.validate) ~= "string" and type(options.validate) ~= "function" then
429 return '"validate" must be a string, function, or table', position
430 end
431 end
432 elseif kind == "range" then
433 if options.min or options.max then
434 if type(options.min) ~= "number" then
435 return '"min" must be a number', position
436 elseif type(options.max) ~= "number" then
437 return '"max" must be a number', position
438 elseif options.min >= options.max then
439 return '"min" must be less than "max"', position
440 end
441 end
442 if options.step then
443 if type(options.step) ~= "number" then
444 return '"step" must be a number', position
445 elseif options.step < 0 then
446 return '"step" must be nonnegative', position
447 end
448 end
449 if options.isPercent and options.isPercent ~= true then
450 return '"isPercent" must either be nil, true, or false', position
451 end
452 elseif kind == "toggle" then
453 if options.map then
454 if type(options.map) ~= "table" then
455 return '"map" must be a table', position
456 elseif type(options.map[true]) ~= "string" then
457 return '"map[true]" must be a string', position
458 elseif type(options.map[false]) ~= "string" then
459 return '"map[false]" must be a string', position
460 end
461 end
462 elseif kind == "color" then
463 if options.hasAlpha and options.hasAlpha ~= true then
464 return '"hasAlpha" must be nil, true, or false', position
465 end
466 elseif kind == "group" then
467 if options.pass and options.pass ~= true then
468 return '"pass" must be nil, true, or false', position
469 end
470 if type(options.args) ~= "table" then
471 return '"args" must be a table', position
472 end
473 for k,v in pairs(options.args) do
474 if type(k) ~= "string" then
475 return '"args" keys must be strings', position
476 elseif string.find(k, "%s") then
477 return string.format('"args" keys must not include spaces. %q is not appropriate.', k), position
478 elseif string.len(k) == 0 then
479 return '"args" keys must not be 0-length strings.', position
480 end
481 if type(v) ~= "table" then
482 return '"args" values must be tables', position and position .. "." .. k or k
483 end
484 local newposition
485 if position then
486 newposition = position .. ".args." .. k
487 else
488 newposition = "args." .. k
489 end
490 local err, pos = validateOptions(v, newposition, baseOptions, options.pass)
491 if err then
492 return err, pos
493 end
494 end
495 end
496 end
497  
498 local colorTable
499 local colorFunc
500 local colorCancelFunc
501  
502 local order
503  
504 local mysort_args
505 local mysort
506  
507 local function printUsage(self, handler, realOptions, options, path, args, quiet, filter)
508 if filter then
509 filter = "^" .. string.gsub(filter, "([%(%)%.%*%+%-%[%]%?%^%$%%])", "%%%1")
510 end
511 local hidden, disabled = options.cmdHidden or options.hidden, options.disabled
512 if hidden then
513 if type(hidden) == "function" then
514 hidden = hidden()
515 elseif type(hidden) == "string" then
516 if type(handler[handler]) ~= "function" then
517 AceConsole:error(OPTION_HANDLER_NOT_FOUND, handler)
518 end
519 hidden = handler[hidden](handler)
520 end
521 end
522 if hidden then
523 disabled = true
524 elseif disabled then
525 if type(disabled) == "function" then
526 disabled = disabled()
527 elseif type(disabled) == "string" then
528 if type(handler[disabled]) ~= "function" then
529 AceConsole:error(OPTION_HANDLER_NOT_FOUND, disabled)
530 end
531 disabled = handler[disabled](handler)
532 end
533 end
534 local kind = string.lower(options.type or "group")
535 if disabled then
536 print(string.format(OPTION_IS_DISABLED, path), realOptions.cmdName or realOptions.name or self)
537 elseif kind == "text" then
538 local var
539 if passTable then
540 if not passTable.get then
541 elseif type(passTable.get) == "function" then
542 var = passTable.get(passValue)
543 else
544 local handler = passTable.handler or handler
545 if type(handler[passTable.get]) ~= "function" then
546 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
547 end
548 var = handler[passTable.get](handler, passValue)
549 end
550 else
551 if not options.get then
552 elseif type(options.get) == "function" then
553 var = options.get()
554 else
555 local handler = options.handler or handler
556 if type(handler[options.get]) ~= "function" then
557 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
558 end
559 var = handler[options.get](handler)
560 end
561 end
562  
563 local usage
564 if type(options.validate) == "table" then
565 if filter then
566 if not order then
567 order = {}
568 end
569 for k,v in pairs(options.validate) do
570 if string.find(v, filter) then
571 table.insert(order, v)
572 end
573 end
574 usage = "{" .. table.concat(order, " || ") .. "}"
575 for k in pairs(order) do
576 order[k] = nil
577 end
578 table.setn(order, 0)
579 else
580 if not order then
581 order = {}
582 end
583 for k,v in pairs(options.validate) do
584 table.insert(order, v)
585 end
586 usage = "{" .. table.concat(order, " || ") .. "}"
587 for k in pairs(order) do
588 order[k] = nil
589 end
590 table.setn(order, 0)
591 end
592 var = options.validate[var] or var
593 else
594 usage = options.usage or "<value>"
595 end
596 if not quiet then
597 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, usage), realOptions.cmdName or realOptions.name or self)
598 end
599 if (passTable and passTable.get) or options.get then
600 print(string.format(options.current or IS_CURRENTLY_SET_TO, tostring(options.cmdName or options.name), tostring(var or NONE)))
601 end
602 elseif kind == "range" then
603 local var
604 if passTable then
605 if type(passTable.get) == "function" then
606 var = passTable.get(passValue)
607 else
608 local handler = passTable.handler or handler
609 if type(handler[passTable.get]) ~= "function" then
610 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
611 end
612 var = handler[passTable.get](handler, passValue)
613 end
614 else
615 if type(options.get) == "function" then
616 var = options.get()
617 else
618 local handler = options.handler or handler
619 if type(handler[options.get]) ~= "function" then
620 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
621 end
622 var = handler[options.get](handler)
623 end
624 end
625  
626 local usage
627 local min = options.min or 0
628 local max = options.max or 1
629 if options.isPercent then
630 min, max = min * 100, max * 100
631 var = tostring(var * 100) .. "%"
632 end
633 local bit = "-"
634 if min < 0 or max < 0 then
635 bit = " - "
636 end
637 usage = string.format("(%s%s%s)", min, bit, max)
638 if not quiet then
639 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, usage), realOptions.cmdName or realOptions.name or self)
640 end
641 print(string.format(options.current or IS_CURRENTLY_SET_TO, tostring(options.cmdName or options.name), tostring(var or NONE)))
642 elseif kind == "group" then
643 local usage
644 if next(options.args) then
645 if not order then
646 order = {}
647 end
648 for k,v in pairs(options.args) do
649 if v.type ~= "header" then
650 local hidden = v.cmdHidden or v.hidden
651 if hidden then
652 if type(hidden) == "function" then
653 hidden = hidden()
654 elseif type(hidden) == "string" then
655 local handler = v.handler or handler
656 if type(handler[hidden]) ~= "function" then
657 AceConsole:error(OPTION_HANDLER_NOT_FOUND, hidden)
658 end
659 hidden = handler[hidden](handler)
660 end
661 end
662 if not hidden then
663 if filter then
664 if string.find(k, filter) then
665 table.insert(order, k)
666 elseif type(v.aliases) == "table" then
667 for _,bit in ipairs(v.aliases) do
668 if string.find(v.aliases, filter) then
669 table.insert(order, k)
670 break
671 end
672 end
673 elseif type(v.aliases) == "string" then
674 if string.find(v.aliases, filter) then
675 table.insert(order, k)
676 end
677 end
678 else
679 table.insert(order, k)
680 end
681 end
682 end
683 end
684 if not mysort then
685 mysort = function(a, b)
686 local alpha, bravo = mysort_args[a], mysort_args[b]
687 local alpha_order = alpha and alpha.order or 100
688 local bravo_order = bravo and bravo.order or 100
689 if alpha_order == bravo_order then
690 return tostring(a) < tostring(b)
691 else
692 if alpha_order < 0 then
693 if bravo_order > 0 then
694 return false
695 end
696 else
697 if bravo_order < 0 then
698 return true
699 end
700 end
701 if alpha_order > 0 and bravo_order > 0 then
702 return tostring(a) < tostring(b)
703 end
704 return alpha_order < bravo_order
705 end
706 end
707 end
708 mysort_args = options.args
709 table.sort(order, mysort)
710 mysort_args = nil
711 if not quiet then
712 if options == realOptions then
713 if options.desc then
714 print(tostring(options.desc), realOptions.cmdName or realOptions.name or self)
715 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, "{" .. table.concat(order, " || ") .. "}"))
716 elseif self.description or self.notes then
717 print(tostring(self.description or self.notes), realOptions.cmdName or realOptions.name or self)
718 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, "{" .. table.concat(order, " || ") .. "}"))
719 else
720 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, "{" .. table.concat(order, " || ") .. "}"), realOptions.cmdName or realOptions.name or self)
721 end
722 else
723 if options.desc then
724 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, "{" .. table.concat(order, " || ") .. "}"), realOptions.cmdName or realOptions.name or self)
725 print(tostring(options.desc))
726 else
727 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, "{" .. table.concat(order, " || ") .. "}"), realOptions.cmdName or realOptions.name or self)
728 end
729 end
730 end
731 for _,k in ipairs(order) do
732 local v = options.args[k]
733 if v then
734 local disabled = v.disabled
735 if disabled then
736 if type(disabled) == "function" then
737 disabled = disabled()
738 elseif type(disabled) == "string" then
739 local handler = v.handler or handler
740 if type(handler[disabled]) ~= "function" then
741 AceConsole:error(OPTION_HANDLER_NOT_FOUND, disabled)
742 end
743 disabled = handler[disabled](handler)
744 end
745 end
746 if type(v.aliases) == "table" then
747 k = k .. " || " .. table.concat(v.aliases, " || ")
748 elseif type(v.aliases) == "string" then
749 k = k .. " || " .. v.aliases
750 end
751 if v.get then
752 local a1,a2,a3,a4
753 if type(v.get) == "function" then
754 if options.pass then
755 a1,a2,a3,a4 = v.get(k)
756 else
757 a1,a2,a3,a4 = v.get()
758 end
759 else
760 local handler = v.handler or handler
761 if type(handler[v.get]) ~= "function" then
762 AceConsole:error(OPTION_HANDLER_NOT_FOUND, v.get)
763 end
764 if options.pass then
765 a1,a2,a3,a4 = handler[v.get](handler, k)
766 else
767 a1,a2,a3,a4 = handler[v.get](handler)
768 end
769 end
770 if v.type == "color" then
771 if v.hasAlpha then
772 if not a1 or not a2 or not a3 or not a4 then
773 s = NONE
774 else
775 s = string.format("|c%02x%02x%02x%02x%02x%02x%02x%02x|r", a4*255, a1*255, a2*255, a3*255, a4*255, a1*255, a2*255, a3*255)
776 end
777 else
778 if not a1 or not a2 or not a3 then
779 s = NONE
780 else
781 s = string.format("|cff%02x%02x%02x%02x%02x%02x|r", a1*255, a2*255, a3*255, a1*255, a2*255, a3*255)
782 end
783 end
784 elseif v.type == "toggle" then
785 if v.map then
786 s = tostring(v.map[a1 or false] or NONE)
787 else
788 s = tostring(MAP_ONOFF[a1 or false] or NONE)
789 end
790 elseif v.type == "range" then
791 if v.isPercent then
792 s = tostring(a1 * 100) .. "%"
793 else
794 s = tostring(a1)
795 end
796 elseif v.type == "text" and type(v.validate) == "table" then
797 s = tostring(v.validate[a1] or a1)
798 else
799 s = tostring(a1 or NONE)
800 end
801 if disabled then
802 local s = string.gsub(s, "|cff%x%x%x%x%x%x(.-)|r", "%1")
803 local desc = string.gsub(v.desc or NONE, "|cff%x%x%x%x%x%x(.-)|r", "%1")
804 print(string.format("|cffcfcfcf - %s: [%s] %s|r", k, s, desc))
805 else
806 print(string.format(" - |cffffff7f%s: [|r%s|cffffff7f]|r %s", k, s, v.desc or NONE))
807 end
808 else
809 if disabled then
810 local desc = string.gsub(v.desc or NONE, "|cff%x%x%x%x%x%x(.-)|r", "%1")
811 print(string.format("|cffcfcfcf - %s: %s", k, desc))
812 else
813 print(string.format(" - |cffffff7f%s:|r %s", k, v.desc or NONE))
814 end
815 end
816 end
817 end
818 for k in pairs(order) do
819 order[k] = nil
820 end
821 table.setn(order, 0)
822 else
823 if options.desc then
824 desc = options.desc
825 print(string.format("|cffffff7f%s:|r %s", USAGE, path), realOptions.cmdName or realOptions.name or self)
826 print(tostring(options.desc))
827 elseif options == realOptions and (self.description or self.notes) then
828 print(tostring(self.description or self.notes), realOptions.cmdName or realOptions.name or self)
829 print(string.format("|cffffff7f%s:|r %s", USAGE, path))
830 else
831 print(string.format("|cffffff7f%s:|r %s", USAGE, path), realOptions.cmdName or realOptions.name or self)
832 end
833 print(self, NO_OPTIONS_AVAILABLE)
834 end
835 end
836 end
837  
838 local function handlerFunc(self, chat, msg, options)
839 if not msg then
840 msg = ""
841 else
842 msg = string.gsub(msg, "^%s*(.-)%s*$", "%1")
843 msg = string.gsub(msg, "%s+", " ")
844 end
845  
846 local realOptions = options
847 local options, path, args, handler, passTable, passValue = findTableLevel(self, options, chat, msg)
848  
849 local hidden, disabled = options.cmdHidden or options.hidden, options.disabled
850 if hidden then
851 if type(hidden) == "function" then
852 hidden = hidden()
853 elseif type(hidden) == "string" then
854 if type(handler[hidden]) ~= "function" then
855 AceConsole:error(OPTION_HANDLER_NOT_FOUND, hidden)
856 end
857 hidden = handler[hidden](handler)
858 end
859 end
860 if hidden then
861 disabled = true
862 elseif disabled then
863 if type(disabled) == "function" then
864 disabled = disabled()
865 elseif type(disabled) == "string" then
866 if type(handler[disabled]) ~= "function" then
867 AceConsole:error(OPTION_HANDLER_NOT_FOUND, disabled)
868 end
869 disabled = handler[disabled](handler)
870 end
871 end
872 local _G_this = this
873 local kind = string.lower(options.type or "group")
874 if disabled then
875 print(string.format(OPTION_IS_DISABLED, path), realOptions.cmdName or realOptions.name or self)
876 elseif kind == "text" then
877 if table.getn(args) > 0 then
878 if (type(options.validate) == "table" and table.getn(args) > 1) or (type(options.validate) ~= "table" and not options.input) then
879 local arg = table.concat(args, " ")
880 for k,v in pairs(args) do
881 args[k] = nil
882 end
883 table.setn(args, 0)
884 table.insert(args, arg)
885 end
886 if options.validate then
887 local good
888 if type(options.validate) == "function" then
889 good = options.validate(unpack(args))
890 elseif type(options.validate) == "table" then
891 local arg = args[1]
892 arg = string.lower(tostring(arg))
893 for k,v in pairs(options.validate) do
894 if string.lower(v) == arg then
895 args[1] = type(k) == "string" and k or v
896 good = true
897 break
898 end
899 end
900 if not good and type((next(options.validate))) == "string" then
901 for k,v in pairs(options.validate) do
902 if type(k) == "string" and string.lower(k) == arg then
903 args[1] = k
904 good = true
905 break
906 end
907 end
908 end
909 else
910 if type(handler[options.validate]) ~= "function" then
911 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.validate)
912 end
913 good = handler[options.validate](handler, unpack(args))
914 end
915 if not good then
916 local usage
917 if type(options.validate) == "table" then
918 if not order then
919 order = {}
920 end
921 for k,v in pairs(options.validate) do
922 table.insert(order, v)
923 end
924 usage = "{" .. table.concat(order, " || ") .. "}"
925 for k in pairs(order) do
926 order[k] = nil
927 end
928 table.setn(order, 0)
929 else
930 usage = options.usage or "<value>"
931 end
932 print(string.format(options.error or IS_NOT_A_VALID_OPTION_FOR, tostring(table.concat(args, " ")), path), realOptions.cmdName or realOptions.name or self)
933 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, usage))
934 return
935 end
936 end
937  
938 local var
939 if passTable then
940 if not passTable.get then
941 elseif type(passTable.get) == "function" then
942 var = passTable.get(passValue)
943 else
944 if type(handler[passTable.get]) ~= "function" then
945 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
946 end
947 var = handler[passTable.get](handler, passValue)
948 end
949 else
950 if not options.get then
951 elseif type(options.get) == "function" then
952 var = options.get()
953 else
954 if type(handler[options.get]) ~= "function" then
955 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
956 end
957 var = handler[options.get](handler)
958 end
959 end
960  
961 if var ~= args[1] then
962 if passTable then
963 if type(passTable.set) == "function" then
964 passTable.set(passValue, unpack(args))
965 else
966 if type(handler[passTable.set]) ~= "function" then
967 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.set)
968 end
969 handler[passTable.set](handler, passTable.set, unpack(args))
970 end
971 else
972 if type(options.set) == "function" then
973 options.set(unpack(args))
974 else
975 if type(handler[options.set]) ~= "function" then
976 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.set)
977 end
978 handler[options.set](handler, unpack(args))
979 end
980 end
981 end
982 end
983  
984 if table.getn(args) > 0 then
985 local var
986 if passTable then
987 if not passTable.get then
988 elseif type(passTable.get) == "function" then
989 var = passTable.get(passValue)
990 else
991 if type(handler[passTable.get]) ~= "function" then
992 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
993 end
994 var = handler[passTable.get](handler, passValue)
995 end
996 else
997 if not options.get then
998 elseif type(options.get) == "function" then
999 var = options.get()
1000 else
1001 if type(handler[options.get]) ~= "function" then
1002 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1003 end
1004 var = handler[options.get](handler)
1005 end
1006 end
1007 if type(options.validate) == "table" then
1008 var = options.validate[var] or var
1009 end
1010 if (passTable and passTable.get) or options.get then
1011 print(string.format(options.message or IS_NOW_SET_TO, tostring(options.cmdName or options.name), tostring(var or NONE)), realOptions.cmdName or realOptions.name or self)
1012 end
1013 if var == args[1] then
1014 return
1015 end
1016 else
1017 printUsage(self, handler, realOptions, options, path, args)
1018 return
1019 end
1020 elseif kind == "execute" then
1021 if passTable then
1022 if type(passFunc) == "function" then
1023 set(passValue)
1024 else
1025 if type(handler[passFunc]) ~= "function" then
1026 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passFunc)
1027 end
1028 handler[passFunc](handler, passValue)
1029 end
1030 else
1031 local ret, msg
1032 if type(options.func) == "function" then
1033 options.func()
1034 else
1035 local handler = options.handler or self
1036 if type(handler[options.func]) ~= "function" then
1037 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.func)
1038 end
1039 handler[options.func](handler)
1040 end
1041 end
1042 elseif kind == "toggle" then
1043 local var
1044 if passTable then
1045 if type(passTable.get) == "function" then
1046 var = passTable.get(passValue)
1047 else
1048 if type(handler[passTable.get]) ~= "function" then
1049 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
1050 end
1051 var = handler[passTable.get](handler, passValue)
1052 end
1053 if type(passTable.set) == "function" then
1054 passTable.set(passValue, not var)
1055 else
1056 if type(handler[passTable.set]) ~= "function" then
1057 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.set)
1058 end
1059 handler[passTable.set](handler, passValue, not var)
1060 end
1061 if type(passTable.get) == "function" then
1062 var = passTable.get(passValue)
1063 else
1064 var = handler[passTable.get](handler, passValue)
1065 end
1066 else
1067 local handler = options.handler or self
1068 if type(options.get) == "function" then
1069 var = options.get()
1070 else
1071 if type(handler[options.get]) ~= "function" then
1072 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1073 end
1074 var = handler[options.get](handler)
1075 end
1076 if type(options.set) == "function" then
1077 options.set(not var)
1078 else
1079 if type(handler[options.set]) ~= "function" then
1080 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.set)
1081 end
1082 handler[options.set](handler, not var)
1083 end
1084 if type(options.get) == "function" then
1085 var = options.get()
1086 else
1087 var = handler[options.get](handler)
1088 end
1089 end
1090  
1091 print(string.format(options.message or IS_NOW_SET_TO, tostring(options.cmdName or options.name), (options.map or MAP_ONOFF)[var or false] or NONE), realOptions.cmdName or realOptions.name or self)
1092 elseif kind == "range" then
1093 local arg
1094 if table.getn(args) <= 1 then
1095 arg = args[1]
1096 else
1097 arg = table.concat(args, " ")
1098 end
1099  
1100 if arg then
1101 local min = options.min or 0
1102 local max = options.max or 1
1103 local good = false
1104 if type(arg) == "number" then
1105 if options.isPercent then
1106 arg = arg / 100
1107 end
1108  
1109 if arg >= min and arg <= max then
1110 good = true
1111 end
1112  
1113 if good and type(options.step) == "number" and options.step > 0 then
1114 local step = options.step
1115 arg = math.floor((arg - min) / step + 0.5) * step + min
1116 if arg > max then
1117 arg = max
1118 elseif arg < min then
1119 arg = min
1120 end
1121 end
1122 end
1123 if not good then
1124 local usage
1125 local min = options.min or 0
1126 local max = options.max or 1
1127 if options.isPercent then
1128 min, max = min * 100, max * 100
1129 end
1130 local bit = "-"
1131 if min < 0 or max < 0 then
1132 bit = " - "
1133 end
1134 usage = string.format("(%s%s%s)", min, bit, max)
1135 print(string.format(options.error or IS_NOT_A_VALID_VALUE_FOR, tostring(arg), path), realOptions.cmdName or realOptions.name or self)
1136 print(string.format("|cffffff7f%s:|r %s %s", USAGE, path, usage))
1137 return
1138 end
1139  
1140 local var
1141 if passTable then
1142 if type(passTable.get) == "function" then
1143 var = passTable.get(passValue)
1144 else
1145 if type(handler[passTable.get]) ~= "function" then
1146 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
1147 end
1148 var = handler[passTable.get](handler, passValue)
1149 end
1150 else
1151 if type(options.get) == "function" then
1152 var = options.get()
1153 else
1154 local handler = options.handler or self
1155 if type(handler[options.get]) ~= "function" then
1156 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1157 end
1158 var = handler[options.get](handler)
1159 end
1160 end
1161  
1162 if var ~= arg then
1163 if passTable then
1164 if type(passTable.set) == "function" then
1165 passTable.set(passValue, arg)
1166 else
1167 if type(handler[passTable.set]) ~= "function" then
1168 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.set)
1169 end
1170 handler[passTable.set](handler, passValue, arg)
1171 end
1172 else
1173 if type(options.set) == "function" then
1174 options.set(arg)
1175 else
1176 local handler = options.handler or self
1177 if type(handler[options.set]) ~= "function" then
1178 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.set)
1179 end
1180 handler[options.set](handler, arg)
1181 end
1182 end
1183 end
1184 end
1185  
1186 if arg then
1187 local var
1188 if passTable then
1189 if type(passTable.get) == "function" then
1190 var = passTable.get(passValue)
1191 else
1192 if type(handler[passTable.get]) ~= "function" then
1193 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
1194 end
1195 var = handler[passTable.get](handler, passValue)
1196 end
1197 else
1198 if type(options.get) == "function" then
1199 var = options.get()
1200 else
1201 local handler = options.handler or self
1202 if type(handler[options.get]) ~= "function" then
1203 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1204 end
1205 var = handler[options.get](handler)
1206 end
1207 end
1208  
1209 if var and options.isPercent then
1210 var = tostring(var * 100) .. "%"
1211 end
1212 print(string.format(options.message or IS_NOW_SET_TO, tostring(options.cmdName or options.name), tostring(var or NONE)), realOptions.cmdName or realOptions.name or self)
1213 if var == arg then
1214 return
1215 end
1216 else
1217 printUsage(self, handler, realOptions, options, path, args)
1218 return
1219 end
1220 elseif kind == "color" then
1221 if table.getn(args) > 0 then
1222 local r,g,b,a
1223 if table.getn(args) == 1 then
1224 local arg = tostring(args[1])
1225 if options.hasAlpha then
1226 if string.len(arg) == 8 and string.find(arg, "^%x*$") then
1227 r,g,b,a = tonumber(string.sub(arg, 1, 2), 16) / 255, tonumber(string.sub(arg, 3, 4), 16) / 255, tonumber(string.sub(arg, 5, 6), 16) / 255, tonumber(string.sub(arg, 7, 8), 16) / 255
1228 end
1229 else
1230 if string.len(arg) == 6 and string.find(arg, "^%x*$") then
1231 r,g,b = tonumber(string.sub(arg, 1, 2), 16) / 255, tonumber(string.sub(arg, 3, 4), 16) / 255, tonumber(string.sub(arg, 5, 6), 16) / 255
1232 end
1233 end
1234 elseif table.getn(args) == 4 and options.hasAlpha then
1235 local a1,a2,a3,a4 = args[1], args[2], args[3], args[4]
1236 if type(a1) == "number" and type(a2) == "number" and type(a3) == "number" and type(a4) == "number" and a1 <= 1 and a2 <= 1 and a3 <= 1 and a4 <= 1 then
1237 r,g,b,a = a1,a2,a3,a4
1238 elseif (type(a1) == "number" or string.len(a1) == 2) and string.find(a1, "^%x*$") and (type(a2) == "number" or string.len(a2) == 2) and string.find(a2, "^%x*$") and (type(a3) == "number" or string.len(a3) == 2) and string.find(a3, "^%x*$") and (type(a4) == "number" or string.len(a4) == 2) and string.find(a4, "^%x*$") then
1239 r,g,b,a = tonumber(a1, 16) / 255, tonumber(a2, 16) / 255, tonumber(a3, 16) / 255, tonumber(a4, 16) / 255
1240 end
1241 elseif table.getn(args) == 3 and not options.hasAlpha then
1242 local a1,a2,a3 = args[1], args[2], args[3]
1243 if type(a1) == "number" and type(a2) == "number" and type(a3) == "number" and a1 <= 1 and a2 <= 1 and a3 <= 1 then
1244 r,g,b = a1,a2,a3
1245 elseif (type(a1) == "number" or string.len(a1) == 2) and string.find(a1, "^%x*$") and (type(a2) == "number" or string.len(a2) == 2) and string.find(a2, "^%x*$") and (type(a3) == "number" or string.len(a3) == 2) and string.find(a3, "^%x*$") then
1246 r,g,b = tonumber(a1, 16) / 255, tonumber(a2, 16) / 255, tonumber(a3, 16) / 255
1247 end
1248 end
1249 if not r then
1250 print(string.format(options.error or IS_NOT_A_VALID_OPTION_FOR, table.concat(args, ' '), path), realOptions.cmdName or realOptions.name or self)
1251 print(string.format("|cffffff7f%s:|r %s {0-1} {0-1} {0-1}%s", USAGE, path, options.hasAlpha and " {0-1}" or ""))
1252 return
1253 end
1254 if passTable then
1255 if type(passTable.set) == "function" then
1256 passTable.set(passValue, r,g,b,a)
1257 else
1258 if type(handler[passTable.set]) ~= "function" then
1259 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.set)
1260 end
1261 handler[passTable.set](handler, passValue, r,g,b,a)
1262 end
1263 else
1264 if type(options.set) == "function" then
1265 options.set(r,g,b,a)
1266 else
1267 if type(handler[options.set]) ~= "function" then
1268 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.set)
1269 end
1270 handler[options.set](handler, r,g,b,a)
1271 end
1272 end
1273  
1274 local r,g,b,a
1275 if passTable then
1276 if type(passTable.get) == "function" then
1277 r,g,b,a = passTable.get(passValue)
1278 else
1279 if type(handler[passTable.get]) ~= "function" then
1280 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
1281 end
1282 r,g,b,a = handler[passTable.get](handler, passValue)
1283 end
1284 else
1285 if type(options.get) == "function" then
1286 r,g,b,a = options.get()
1287 else
1288 if type(handler[options.get]) ~= "function" then
1289 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1290 end
1291 r,g,b,a = handler[options.get](handler)
1292 end
1293 end
1294  
1295 local s
1296 if type(r) == "number" and type(g) == "number" and type(b) == "number" then
1297 if options.hasAlpha and type(a) == "number" then
1298 s = string.format("|c%02x%02x%02x%02x%02x%02x%02x%02x|r", a*255, r*255, g*255, b*255, r*255, g*255, b*255, a*255)
1299 else
1300 s = string.format("|cff%02x%02x%02x%02x%02x%02x|r", r*255, g*255, b*255, r*255, g*255, b*255)
1301 end
1302 else
1303 s = NONE
1304 end
1305 print(string.format(options.message or IS_NOW_SET_TO, tostring(options.cmdName or options.name), s), realOptions.cmdName or realOptions.name or self)
1306 else
1307 local r,g,b,a
1308 if passTable then
1309 if type(passTable.get) == "function" then
1310 r,g,b,a = passTable.get(passValue)
1311 else
1312 if type(handler[passTable.get]) ~= "function" then
1313 AceConsole:error(OPTION_HANDLER_NOT_FOUND, passTable.get)
1314 end
1315 r,g,b,a = handler[passTable.get](handler, passValue)
1316 end
1317 else
1318 if type(options.get) == "function" then
1319 r,g,b,a = options.get()
1320 else
1321 if type(handler[options.get]) ~= "function" then
1322 AceConsole:error(OPTION_HANDLER_NOT_FOUND, options.get)
1323 end
1324 r,g,b,a = handler[options.get](handler)
1325 end
1326 end
1327  
1328 if not colorTable then
1329 colorTable = {}
1330 local t = colorTable
1331  
1332 if ColorPickerOkayButton then
1333 local ColorPickerOkayButton_OnClick = ColorPickerOkayButton:GetScript("OnClick")
1334 ColorPickerOkayButton:SetScript("OnClick", function()
1335 if ColorPickerOkayButton_OnClick then
1336 ColorPickerOkayButton_OnClick()
1337 end
1338 if t.active then
1339 ColorPickerFrame.cancelFunc = nil
1340 ColorPickerFrame.func = nil
1341 ColorPickerFrame.opacityFunc = nil
1342 local r,g,b,a
1343 if t.passValue then
1344 if type(t.get) == "function" then
1345 r,g,b,a = t.get(t.passValue)
1346 else
1347 if type(t.handler[t.get]) ~= "function" then
1348 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.get)
1349 end
1350 r,g,b,a = t.handler[t.get](t.handler, t.passValue)
1351 end
1352 else
1353 if type(t.get) == "function" then
1354 r,g,b,a = t.get()
1355 else
1356 if type(t.handler[t.get]) ~= "function" then
1357 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.get)
1358 end
1359 r,g,b,a = t.handler[t.get](t.handler)
1360 end
1361 end
1362 if r ~= t.r or g ~= t.g or b ~= t.b or (t.hasAlpha and a ~= t.a) then
1363 local s
1364 if type(r) == "number" and type(g) == "number" and type(b) == "number" then
1365 if t.hasAlpha and type(a) == "number" then
1366 s = string.format("|c%02x%02x%02x%02x%02x%02x%02x%02x|r", a*255, r*255, g*255, b*255, r*255, g*255, b*255, a*255)
1367 else
1368 s = string.format("|cff%02x%02x%02x%02x%02x%02x|r", r*255, g*255, b*255, r*255, g*255, b*255)
1369 end
1370 else
1371 s = NONE
1372 end
1373 print(string.format(t.message, tostring(t.name), s), t.realOptions.cmdName or t.realOptions.name or self)
1374 end
1375 for k,v in pairs(t) do
1376 t[k] = nil
1377 end
1378 end
1379 end)
1380 end
1381 else
1382 for k,v in pairs(colorTable) do
1383 colorTable[k] = nil
1384 end
1385 end
1386  
1387 if type(r) ~= "number" or type(g) ~= "number" or type(b) ~= "number" then
1388 r,g,b = 1, 1, 1
1389 end
1390 if type(a) ~= "number" then
1391 a = 1
1392 end
1393 local t = colorTable
1394 t.r = r
1395 t.g = g
1396 t.b = b
1397 if hasAlpha then
1398 t.a = a
1399 end
1400 t.realOptions = realOptions
1401 t.hasAlpha = options.hasAlpha
1402 t.handler = handler
1403 t.set = passTable and passTable.set or options.set
1404 t.get = passTable and passTable.get or options.get
1405 t.name = options.cmdName or options.name
1406 t.message = options.message or IS_NOW_SET_TO
1407 t.passValue = passValue
1408 t.active = true
1409  
1410 if not colorFunc then
1411 colorFunc = function()
1412 local r,g,b = ColorPickerFrame:GetColorRGB()
1413 if t.hasAlpha then
1414 local a = 1 - OpacitySliderFrame:GetValue()
1415 if type(t.set) == "function" then
1416 if t.passValue then
1417 t.set(t.passValue, r,g,b,a)
1418 else
1419 t.set(r,g,b,a)
1420 end
1421 else
1422 if type(t.handler[t.set]) ~= "function" then
1423 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.set)
1424 end
1425 if t.passValue then
1426 t.handler[t.set](t.handler, t.passValue, r,g,b,a)
1427 else
1428 t.handler[t.set](t.handler, r,g,b,a)
1429 end
1430 end
1431 else
1432 if type(t.set) == "function" then
1433 if t.passValue then
1434 t.set(t.passValue, r,g,b)
1435 else
1436 t.set(r,g,b)
1437 end
1438 else
1439 if type(t.handler[t.set]) ~= "function" then
1440 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.set)
1441 end
1442 if t.passValue then
1443 t.handler[t.set](t.handler, t.passValue, r,g,b)
1444 else
1445 t.handler[t.set](t.handler, r,g,b)
1446 end
1447 end
1448 end
1449 end
1450 end
1451  
1452 ColorPickerFrame.func = colorFunc
1453 ColorPickerFrame.hasOpacity = options.hasAlpha
1454 if options.hasAlpha then
1455 ColorPickerFrame.opacityFunc = ColorPickerFrame.func
1456 ColorPickerFrame.opacity = 1 - a
1457 end
1458 ColorPickerFrame:SetColorRGB(r,g,b)
1459  
1460 if not colorCancelFunc then
1461 colorCancelFunc = function()
1462 if t.hasAlpha then
1463 if type(t.set) == "function" then
1464 if t.passValue then
1465 t.set(t.passValue, t.r,t.g,t.b,t.a)
1466 else
1467 t.set(t.r,t.g,t.b,t.a)
1468 end
1469 else
1470 if type(t.handler[t.get]) ~= "function" then
1471 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.get)
1472 end
1473 if t.passValue then
1474 t.handler[t.set](t.handler, t.passValue, t.r,t.g,t.b,t.a)
1475 else
1476 t.handler[t.set](t.handler, t.r,t.g,t.b,t.a)
1477 end
1478 end
1479 else
1480 if type(t.set) == "function" then
1481 if t.passValue then
1482 t.set(t.passValue, t.r,t.g,t.b)
1483 else
1484 t.set(t.r,t.g,t.b)
1485 end
1486 else
1487 if type(t.handler[t.set]) ~= "function" then
1488 AceConsole:error(OPTION_HANDLER_NOT_FOUND, t.set)
1489 end
1490 if t.passValue then
1491 t.handler[t.set](t.handler, t.passValue, t.r,t.g,t.b)
1492 else
1493 t.handler[t.set](t.handler, t.r,t.g,t.b)
1494 end
1495 end
1496 end
1497 for k,v in pairs(t) do
1498 t[k] = nil
1499 end
1500 ColorPickerFrame.cancelFunc = nil
1501 ColorPickerFrame.func = nil
1502 ColorPickerFrame.opacityFunc = nil
1503 end
1504 end
1505  
1506 ColorPickerFrame.cancelFunc = colorCancelFunc
1507  
1508 ShowUIPanel(ColorPickerFrame)
1509 end
1510 return
1511 elseif kind == "group" then
1512 if table.getn(args) == 0 then
1513 printUsage(self, handler, realOptions, options, path, args)
1514 else
1515 -- invalid argument
1516 print(string.format(options.error or IS_NOT_A_VALID_OPTION_FOR, args[1], path), realOptions.cmdName or realOptions.name or self)
1517 end
1518 return
1519 end
1520 this = _G_this
1521 if Dewdrop then
1522 Dewdrop:Refresh(1)
1523 Dewdrop:Refresh(2)
1524 Dewdrop:Refresh(3)
1525 Dewdrop:Refresh(4)
1526 Dewdrop:Refresh(5)
1527 end
1528 end
1529  
1530 local external
1531 function AceConsole:RegisterChatCommand(slashCommands, options, name)
1532 if type(slashCommands) ~= "table" and slashCommands ~= false then
1533 AceConsole:error("Bad argument #2 to `RegisterChatCommand' (expected table, got %s)", type(slashCommands))
1534 end
1535 if not slashCommands and type(name) ~= "string" then
1536 AceConsole:error("Bad argument #4 to `RegisterChatCommand' (expected string, got %s)", type(name))
1537 end
1538 if type(options) ~= "table" and type(options) ~= "function" and options ~= nil then
1539 AceConsole:error("Bad argument #3 to `RegisterChatCommand' (expected table, function, or nil, got %s)", type(options))
1540 end
1541 if name then
1542 if type(name) ~= "string" then
1543 AceConsole:error("Bad argument #4 to `RegisterChatCommand' (expected string or nil, got %s)", type(name))
1544 elseif not string.find(name, "^%w+$") or string.upper(name) ~= name or string.len(name) == 0 then
1545 AceConsole:error("Argument #4 must be an uppercase, letters-only string with at least 1 character")
1546 end
1547 end
1548 if slashCommands then
1549 if table.getn(slashCommands) == 0 then
1550 AceConsole:error("Argument #2 to `RegisterChatCommand' must include at least one string")
1551 end
1552  
1553 for k,v in pairs(slashCommands) do
1554 if type(k) ~= "number" then
1555 AceConsole:error("All keys in argument #2 to `RegisterChatCommand' must be numbers")
1556 end
1557 if type(v) ~= "string" then
1558 AceConsole:error("All values in argument #2 to `RegisterChatCommand' must be strings")
1559 elseif not string.find(v, "^/[A-Za-z][A-Za-z0-9_]*$") then
1560 AceConsole:error("All values in argument #2 to `RegisterChatCommand' must be in the form of \"/word\"")
1561 end
1562 end
1563 end
1564  
1565 if not options then
1566 options = {
1567 type = 'group',
1568 args = {},
1569 handler = self
1570 }
1571 end
1572  
1573 if type(options) == "table" then
1574 local err, position = validateOptions(options)
1575 if err then
1576 if position then
1577 AceConsole:error(position .. ": " .. err)
1578 else
1579 AceConsole:error(err)
1580 end
1581 end
1582  
1583 if not options.handler then
1584 options.handler = self
1585 end
1586  
1587 if options.handler == self and string.lower(options.type) == "group" and self.class then
1588 AceConsole:InjectAceOptionsTable(self, options)
1589 end
1590 end
1591  
1592 local chat
1593 if slashCommands then
1594 chat = slashCommands[1]
1595 else
1596 chat = _G["SLASH_"..name..1]
1597 end
1598  
1599 local handler
1600 if type(options) == "function" then
1601 handler = options
1602 else
1603 function handler(msg)
1604 handlerFunc(self, chat, msg, options)
1605 end
1606 end
1607  
1608 if not _G.SlashCmdList then
1609 _G.SlashCmdList = {}
1610 end
1611  
1612 if not name then
1613 repeat
1614 name = string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1) .. string.char(math.random(26) + string.byte('A') - 1)
1615 until not _G.SlashCmdList[name]
1616 end
1617  
1618 if slashCommands then
1619 if _G.SlashCmdList[name] then
1620 local i = 0
1621 while true do
1622 i = i + 1
1623 if _G["SLASH_"..name..i] then
1624 _G["SLASH_"..name..i] = nil
1625 else
1626 break
1627 end
1628 end
1629 end
1630  
1631 local i = 0
1632 for _,command in ipairs(slashCommands) do
1633 i = i + 1
1634 _G["SLASH_"..name..i] = command
1635 if string.lower(command) ~= command then
1636 i = i + 1
1637 _G["SLASH_"..name..i] = string.lower(command)
1638 end
1639 end
1640 end
1641 _G.SlashCmdList[name] = handler
1642 if self ~= AceConsole and self.slashCommand == nil then
1643 self.slashCommand = chat
1644 end
1645  
1646 if not AceEvent and AceLibrary:HasInstance("AceEvent-2.0") then
1647 external(AceConsole, "AceEvent-2.0", AceLibrary("AceEvent-2.0"))
1648 end
1649 if AceEvent then
1650 if not AceConsole.nextAddon then
1651 AceConsole.nextAddon = {}
1652 end
1653 if type(options) == "table" then
1654 AceConsole.nextAddon[self] = options
1655 if not self.playerLogin then
1656 AceConsole:RegisterEvent("PLAYER_LOGIN", "PLAYER_LOGIN", true)
1657 end
1658 end
1659 end
1660  
1661 AceConsole.registry[name] = options
1662 end
1663  
1664 function AceConsole:InjectAceOptionsTable(handler, options)
1665 self:argCheck(handler, 2, "table")
1666 self:argCheck(options, 3, "table")
1667 if string.lower(options.type) ~= "group" then
1668 self:error('Cannot inject into options table argument #3 if its type is not "group"')
1669 end
1670 if options.handler ~= nil and options.handler ~= handler then
1671 self:error("Cannot inject into options table argument #3 if it has a different handler than argument #2")
1672 end
1673 options.handler = handler
1674 local class = handler.class
1675 if not class then
1676 self:error("Cannot retrieve AceOptions tables from a non-object argument #2")
1677 end
1678 while class and class ~= AceOO.Class do
1679 if type(class.GetAceOptionsDataTable) == "function" then
1680 local t = class:GetAceOptionsDataTable(handler)
1681 for k,v in pairs(t) do
1682 if type(options.args) ~= "table" then
1683 options.args = {}
1684 end
1685 if options.args[k] == nil then
1686 options.args[k] = v
1687 end
1688 end
1689 end
1690 local mixins = class.mixins
1691 if mixins then
1692 for mixin in pairs(mixins) do
1693 if type(mixin.GetAceOptionsDataTable) == "function" then
1694 local t = mixin:GetAceOptionsDataTable(handler)
1695 for k,v in pairs(t) do
1696 if type(options.args) ~= "table" then
1697 options.args = {}
1698 end
1699 if options.args[k] == nil then
1700 options.args[k] = v
1701 end
1702 end
1703 end
1704 end
1705 end
1706 class = class.super
1707 end
1708 return options
1709 end
1710  
1711 function AceConsole:PLAYER_LOGIN()
1712 self.playerLogin = true
1713 for addon, options in pairs(self.nextAddon) do
1714 local err, position = validateOptionsMethods(addon, options)
1715 if err then
1716 if position then
1717 error(tostring(addon) .. ": AceConsole: " .. position .. ": " .. err)
1718 else
1719 error(tostring(addon) .. ": AceConsole: " .. err)
1720 end
1721 end
1722 self.nextAddon[addon] = nil
1723 end
1724  
1725 self:RegisterChatCommand({ "/reload", "/rl", "/reloadui" }, ReloadUI, "RELOAD")
1726 local tmp
1727 self:RegisterChatCommand({ "/print" }, function(text)
1728 RunScript("local function func(...) for k = 1,table.getn(arg) do arg[k] = tostring(arg[k]) end DEFAULT_CHAT_FRAME:AddMessage(table.concat(arg, ' ')) end func(" .. text .. ")")
1729 end, "PRINT")
1730 end
1731  
1732 function AceConsole:TabCompleteInfo(cmdpath)
1733 local _, _, cmd = string.find(cmdpath, "(/%S+)")
1734 if not cmd then
1735 return
1736 end
1737 local path = string.sub(cmdpath, string.len(cmd) + 2)
1738 for name in pairs(SlashCmdList) do --global
1739 if AceConsole.registry[name] then
1740 local i = 0
1741 while true do
1742 i = i + 1
1743 local scmd = _G["SLASH_"..name..i]
1744 if not scmd then break end
1745 if cmd == scmd then
1746 return name, cmd, path
1747 end
1748 end
1749 end
1750 end
1751 end
1752  
1753 function external(self, major, instance)
1754 if major == "AceEvent-2.0" then
1755 if not AceEvent then
1756 AceEvent = instance
1757  
1758 AceEvent:embed(self)
1759 end
1760 elseif major == "AceTab-2.0" then
1761 instance:RegisterTabCompletion("AceConsole", "%/.*", function(t, cmdpath, pos)
1762 local ac = AceLibrary("AceConsole-2.0")
1763 local name, cmd, path = ac:TabCompleteInfo(string.sub(cmdpath, 1, pos))
1764  
1765 if not ac.registry[name] then
1766 return false
1767 else
1768 local validArgs = findTableLevel(ac, ac.registry[name], cmd, path or "")
1769 if validArgs.args then
1770 for arg in pairs(validArgs.args) do
1771 table.insert(t, arg)
1772 end
1773 end
1774 end
1775 end, function(u, matches, gcs, cmdpath)
1776 local ac = AceLibrary("AceConsole-2.0")
1777 local name, cmd, path = ac:TabCompleteInfo(cmdpath)
1778 if ac.registry[name] then
1779 local validArgs, path2, argwork = findTableLevel(ac, ac.registry[name], cmd, path)
1780 printUsage(ac, validArgs.handler, ac.registry[name], validArgs, path2, argwork, not gcs or gcs ~= "", gcs)
1781 end
1782 end)
1783 elseif major == "Dewdrop-2.0" then
1784 Dewdrop = instance
1785 end
1786 end
1787  
1788 local function activate(self, oldLib, oldDeactivate)
1789 AceConsole = self
1790  
1791 self.super.activate(self, oldLib, oldDeactivate)
1792  
1793 if oldLib then
1794 self.registry = oldLib.registry
1795 self.nextAddon = oldLib.nextAddon
1796 end
1797 if not self.registry then
1798 self.registry = {}
1799 else
1800 for name,options in pairs(self.registry) do
1801 self:RegisterChatCommand(false, options, name)
1802 end
1803 end
1804  
1805 if oldDeactivate then
1806 oldDeactivate(oldLib)
1807 end
1808 end
1809  
1810 AceLibrary:Register(AceConsole, MAJOR_VERSION, MINOR_VERSION, activate, nil, external)
1811 AceConsole = AceLibrary(MAJOR_VERSION)