Asa@0
|
1 --- AceConfigRegistry-3.0 handles central registration of options tables in use by addons and modules.\\
|
Asa@0
|
2 -- Options tables can be registered as raw tables, OR as function refs that return a table.\\
|
Asa@0
|
3 -- Such functions receive three arguments: "uiType", "uiName", "appName". \\
|
Asa@0
|
4 -- * Valid **uiTypes**: "cmd", "dropdown", "dialog". This is verified by the library at call time. \\
|
Asa@0
|
5 -- * The **uiName** field is expected to contain the full name of the calling addon, including version, e.g. "FooBar-1.0". This is verified by the library at call time.\\
|
Asa@0
|
6 -- * The **appName** field is the options table name as given at registration time \\
|
Asa@0
|
7 --
|
Asa@0
|
8 -- :IterateOptionsTables() (and :GetOptionsTable() if only given one argument) return a function reference that the requesting config handling addon must call with valid "uiType", "uiName".
|
Asa@0
|
9 -- @class file
|
Asa@0
|
10 -- @name AceConfigRegistry-3.0
|
Asa@0
|
11 -- @release $Id: AceConfigRegistry-3.0.lua 890 2009-12-06 12:50:05Z nevcairiel $
|
Asa@0
|
12 local MAJOR, MINOR = "AceConfigRegistry-3.0", 11
|
Asa@0
|
13 local AceConfigRegistry = LibStub:NewLibrary(MAJOR, MINOR)
|
Asa@0
|
14
|
Asa@0
|
15 if not AceConfigRegistry then return end
|
Asa@0
|
16
|
Asa@0
|
17 AceConfigRegistry.tables = AceConfigRegistry.tables or {}
|
Asa@0
|
18
|
Asa@0
|
19 local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0")
|
Asa@0
|
20
|
Asa@0
|
21 if not AceConfigRegistry.callbacks then
|
Asa@0
|
22 AceConfigRegistry.callbacks = CallbackHandler:New(AceConfigRegistry)
|
Asa@0
|
23 end
|
Asa@0
|
24
|
Asa@0
|
25 -- Lua APIs
|
Asa@0
|
26 local tinsert, tconcat = table.insert, table.concat
|
Asa@0
|
27 local strfind, strmatch = string.find, string.match
|
Asa@0
|
28 local type, tostring, select, pairs = type, tostring, select, pairs
|
Asa@0
|
29 local error, assert = error, assert
|
Asa@0
|
30
|
Asa@0
|
31 -----------------------------------------------------------------------
|
Asa@0
|
32 -- Validating options table consistency:
|
Asa@0
|
33
|
Asa@0
|
34
|
Asa@0
|
35 AceConfigRegistry.validated = {
|
Asa@0
|
36 -- list of options table names ran through :ValidateOptionsTable automatically.
|
Asa@0
|
37 -- CLEARED ON PURPOSE, since newer versions may have newer validators
|
Asa@0
|
38 cmd = {},
|
Asa@0
|
39 dropdown = {},
|
Asa@0
|
40 dialog = {},
|
Asa@0
|
41 }
|
Asa@0
|
42
|
Asa@0
|
43
|
Asa@0
|
44
|
Asa@0
|
45 local function err(msg, errlvl, ...)
|
Asa@0
|
46 local t = {}
|
Asa@0
|
47 for i=select("#",...),1,-1 do
|
Asa@0
|
48 tinsert(t, (select(i, ...)))
|
Asa@0
|
49 end
|
Asa@0
|
50 error(MAJOR..":ValidateOptionsTable(): "..tconcat(t,".")..msg, errlvl+2)
|
Asa@0
|
51 end
|
Asa@0
|
52
|
Asa@0
|
53
|
Asa@0
|
54 local isstring={["string"]=true, _="string"}
|
Asa@0
|
55 local isstringfunc={["string"]=true,["function"]=true, _="string or funcref"}
|
Asa@0
|
56 local istable={["table"]=true, _="table"}
|
Asa@0
|
57 local ismethodtable={["table"]=true,["string"]=true,["function"]=true, _="methodname, funcref or table"}
|
Asa@0
|
58 local optstring={["nil"]=true,["string"]=true, _="string"}
|
Asa@0
|
59 local optstringfunc={["nil"]=true,["string"]=true,["function"]=true, _="string or funcref"}
|
Asa@0
|
60 local optnumber={["nil"]=true,["number"]=true, _="number"}
|
Asa@0
|
61 local optmethod={["nil"]=true,["string"]=true,["function"]=true, _="methodname or funcref"}
|
Asa@0
|
62 local optmethodfalse={["nil"]=true,["string"]=true,["function"]=true,["boolean"]={[false]=true}, _="methodname, funcref or false"}
|
Asa@0
|
63 local optmethodnumber={["nil"]=true,["string"]=true,["function"]=true,["number"]=true, _="methodname, funcref or number"}
|
Asa@0
|
64 local optmethodtable={["nil"]=true,["string"]=true,["function"]=true,["table"]=true, _="methodname, funcref or table"}
|
Asa@0
|
65 local optmethodbool={["nil"]=true,["string"]=true,["function"]=true,["boolean"]=true, _="methodname, funcref or boolean"}
|
Asa@0
|
66 local opttable={["nil"]=true,["table"]=true, _="table"}
|
Asa@0
|
67 local optbool={["nil"]=true,["boolean"]=true, _="boolean"}
|
Asa@0
|
68 local optboolnumber={["nil"]=true,["boolean"]=true,["number"]=true, _="boolean or number"}
|
Asa@0
|
69
|
Asa@0
|
70 local basekeys={
|
Asa@0
|
71 type=isstring,
|
Asa@0
|
72 name=isstringfunc,
|
Asa@0
|
73 desc=optstringfunc,
|
Asa@0
|
74 descStyle=optstring,
|
Asa@0
|
75 order=optmethodnumber,
|
Asa@0
|
76 validate=optmethodfalse,
|
Asa@0
|
77 confirm=optmethodbool,
|
Asa@0
|
78 confirmText=optstring,
|
Asa@0
|
79 disabled=optmethodbool,
|
Asa@0
|
80 hidden=optmethodbool,
|
Asa@0
|
81 guiHidden=optmethodbool,
|
Asa@0
|
82 dialogHidden=optmethodbool,
|
Asa@0
|
83 dropdownHidden=optmethodbool,
|
Asa@0
|
84 cmdHidden=optmethodbool,
|
Asa@0
|
85 icon=optstringfunc,
|
Asa@0
|
86 iconCoords=optmethodtable,
|
Asa@0
|
87 handler=opttable,
|
Asa@0
|
88 get=optmethodfalse,
|
Asa@0
|
89 set=optmethodfalse,
|
Asa@0
|
90 func=optmethodfalse,
|
Asa@0
|
91 arg={["*"]=true},
|
Asa@0
|
92 width=optstring,
|
Asa@0
|
93 }
|
Asa@0
|
94
|
Asa@0
|
95 local typedkeys={
|
Asa@0
|
96 header={},
|
Asa@0
|
97 description={
|
Asa@0
|
98 image=optstringfunc,
|
Asa@0
|
99 imageCoords=optmethodtable,
|
Asa@0
|
100 imageHeight=optnumber,
|
Asa@0
|
101 imageWidth=optnumber,
|
Asa@0
|
102 fontSize=optstringfunc,
|
Asa@0
|
103 },
|
Asa@0
|
104 group={
|
Asa@0
|
105 args=istable,
|
Asa@0
|
106 plugins=opttable,
|
Asa@0
|
107 inline=optbool,
|
Asa@0
|
108 cmdInline=optbool,
|
Asa@0
|
109 guiInline=optbool,
|
Asa@0
|
110 dropdownInline=optbool,
|
Asa@0
|
111 dialogInline=optbool,
|
Asa@0
|
112 childGroups=optstring,
|
Asa@0
|
113 },
|
Asa@0
|
114 execute={
|
Asa@0
|
115 image=optstringfunc,
|
Asa@0
|
116 imageCoords=optmethodtable,
|
Asa@0
|
117 imageHeight=optnumber,
|
Asa@0
|
118 imageWidth=optnumber,
|
Asa@0
|
119 },
|
Asa@0
|
120 input={
|
Asa@0
|
121 pattern=optstring,
|
Asa@0
|
122 usage=optstring,
|
Asa@0
|
123 control=optstring,
|
Asa@0
|
124 dialogControl=optstring,
|
Asa@0
|
125 dropdownControl=optstring,
|
Asa@0
|
126 multiline=optboolnumber,
|
Asa@0
|
127 },
|
Asa@0
|
128 toggle={
|
Asa@0
|
129 tristate=optbool,
|
Asa@0
|
130 image=optstringfunc,
|
Asa@0
|
131 imageCoords=optmethodtable,
|
Asa@0
|
132 },
|
Asa@0
|
133 tristate={
|
Asa@0
|
134 },
|
Asa@0
|
135 range={
|
Asa@0
|
136 min=optnumber,
|
Asa@0
|
137 max=optnumber,
|
Asa@0
|
138 step=optnumber,
|
Asa@0
|
139 bigStep=optnumber,
|
Asa@0
|
140 isPercent=optbool,
|
Asa@0
|
141 },
|
Asa@0
|
142 select={
|
Asa@0
|
143 values=ismethodtable,
|
Asa@0
|
144 style={
|
Asa@0
|
145 ["nil"]=true,
|
Asa@0
|
146 ["string"]={dropdown=true,radio=true},
|
Asa@0
|
147 _="string: 'dropdown' or 'radio'"
|
Asa@0
|
148 },
|
Asa@0
|
149 control=optstring,
|
Asa@0
|
150 dialogControl=optstring,
|
Asa@0
|
151 dropdownControl=optstring,
|
Asa@0
|
152 },
|
Asa@0
|
153 multiselect={
|
Asa@0
|
154 values=ismethodtable,
|
Asa@0
|
155 style=optstring,
|
Asa@0
|
156 tristate=optbool,
|
Asa@0
|
157 control=optstring,
|
Asa@0
|
158 dialogControl=optstring,
|
Asa@0
|
159 dropdownControl=optstring,
|
Asa@0
|
160 },
|
Asa@0
|
161 color={
|
Asa@0
|
162 hasAlpha=optbool,
|
Asa@0
|
163 },
|
Asa@0
|
164 keybinding={
|
Asa@0
|
165 -- TODO
|
Asa@0
|
166 },
|
Asa@0
|
167 }
|
Asa@0
|
168
|
Asa@0
|
169 local function validateKey(k,errlvl,...)
|
Asa@0
|
170 errlvl=(errlvl or 0)+1
|
Asa@0
|
171 if type(k)~="string" then
|
Asa@0
|
172 err("["..tostring(k).."] - key is not a string", errlvl,...)
|
Asa@0
|
173 end
|
Asa@0
|
174 if strfind(k, "[%c\127]") then
|
Asa@0
|
175 err("["..tostring(k).."] - key name contained control characters", errlvl,...)
|
Asa@0
|
176 end
|
Asa@0
|
177 end
|
Asa@0
|
178
|
Asa@0
|
179 local function validateVal(v, oktypes, errlvl,...)
|
Asa@0
|
180 errlvl=(errlvl or 0)+1
|
Asa@0
|
181 local isok=oktypes[type(v)] or oktypes["*"]
|
Asa@0
|
182
|
Asa@0
|
183 if not isok then
|
Asa@0
|
184 err(": expected a "..oktypes._..", got '"..tostring(v).."'", errlvl,...)
|
Asa@0
|
185 end
|
Asa@0
|
186 if type(isok)=="table" then -- isok was a table containing specific values to be tested for!
|
Asa@0
|
187 if not isok[v] then
|
Asa@0
|
188 err(": did not expect "..type(v).." value '"..tostring(v).."'", errlvl,...)
|
Asa@0
|
189 end
|
Asa@0
|
190 end
|
Asa@0
|
191 end
|
Asa@0
|
192
|
Asa@0
|
193 local function validate(options,errlvl,...)
|
Asa@0
|
194 errlvl=(errlvl or 0)+1
|
Asa@0
|
195 -- basic consistency
|
Asa@0
|
196 if type(options)~="table" then
|
Asa@0
|
197 err(": expected a table, got a "..type(options), errlvl,...)
|
Asa@0
|
198 end
|
Asa@0
|
199 if type(options.type)~="string" then
|
Asa@0
|
200 err(".type: expected a string, got a "..type(options.type), errlvl,...)
|
Asa@0
|
201 end
|
Asa@0
|
202
|
Asa@0
|
203 -- get type and 'typedkeys' member
|
Asa@0
|
204 local tk = typedkeys[options.type]
|
Asa@0
|
205 if not tk then
|
Asa@0
|
206 err(".type: unknown type '"..options.type.."'", errlvl,...)
|
Asa@0
|
207 end
|
Asa@0
|
208
|
Asa@0
|
209 -- make sure that all options[] are known parameters
|
Asa@0
|
210 for k,v in pairs(options) do
|
Asa@0
|
211 if not (tk[k] or basekeys[k]) then
|
Asa@0
|
212 err(": unknown parameter", errlvl,tostring(k),...)
|
Asa@0
|
213 end
|
Asa@0
|
214 end
|
Asa@0
|
215
|
Asa@0
|
216 -- verify that required params are there, and that everything is the right type
|
Asa@0
|
217 for k,oktypes in pairs(basekeys) do
|
Asa@0
|
218 validateVal(options[k], oktypes, errlvl,k,...)
|
Asa@0
|
219 end
|
Asa@0
|
220 for k,oktypes in pairs(tk) do
|
Asa@0
|
221 validateVal(options[k], oktypes, errlvl,k,...)
|
Asa@0
|
222 end
|
Asa@0
|
223
|
Asa@0
|
224 -- extra logic for groups
|
Asa@0
|
225 if options.type=="group" then
|
Asa@0
|
226 for k,v in pairs(options.args) do
|
Asa@0
|
227 validateKey(k,errlvl,"args",...)
|
Asa@0
|
228 validate(v, errlvl,k,"args",...)
|
Asa@0
|
229 end
|
Asa@0
|
230 if options.plugins then
|
Asa@0
|
231 for plugname,plugin in pairs(options.plugins) do
|
Asa@0
|
232 if type(plugin)~="table" then
|
Asa@0
|
233 err(": expected a table, got '"..tostring(plugin).."'", errlvl,tostring(plugname),"plugins",...)
|
Asa@0
|
234 end
|
Asa@0
|
235 for k,v in pairs(plugin) do
|
Asa@0
|
236 validateKey(k,errlvl,tostring(plugname),"plugins",...)
|
Asa@0
|
237 validate(v, errlvl,k,tostring(plugname),"plugins",...)
|
Asa@0
|
238 end
|
Asa@0
|
239 end
|
Asa@0
|
240 end
|
Asa@0
|
241 end
|
Asa@0
|
242 end
|
Asa@0
|
243
|
Asa@0
|
244
|
Asa@0
|
245 --- Validates basic structure and integrity of an options table \\
|
Asa@0
|
246 -- Does NOT verify that get/set etc actually exist, since they can be defined at any depth
|
Asa@0
|
247 -- @param options The table to be validated
|
Asa@0
|
248 -- @param name The name of the table to be validated (shown in any error message)
|
Asa@0
|
249 -- @param errlvl (optional number) error level offset, default 0 (=errors point to the function calling :ValidateOptionsTable)
|
Asa@0
|
250 function AceConfigRegistry:ValidateOptionsTable(options,name,errlvl)
|
Asa@0
|
251 errlvl=(errlvl or 0)+1
|
Asa@0
|
252 name = name or "Optionstable"
|
Asa@0
|
253 if not options.name then
|
Asa@0
|
254 options.name=name -- bit of a hack, the root level doesn't really need a .name :-/
|
Asa@0
|
255 end
|
Asa@0
|
256 validate(options,errlvl,name)
|
Asa@0
|
257 end
|
Asa@0
|
258
|
Asa@0
|
259 --- Fires a "ConfigTableChange" callback for those listening in on it, allowing config GUIs to refresh.
|
Asa@0
|
260 -- You should call this function if your options table changed from any outside event, like a game event
|
Asa@0
|
261 -- or a timer.
|
Asa@0
|
262 -- @param appName The application name as given to `:RegisterOptionsTable()`
|
Asa@0
|
263 function AceConfigRegistry:NotifyChange(appName)
|
Asa@0
|
264 if not AceConfigRegistry.tables[appName] then return end
|
Asa@0
|
265 AceConfigRegistry.callbacks:Fire("ConfigTableChange", appName)
|
Asa@0
|
266 end
|
Asa@0
|
267
|
Asa@0
|
268 -- -------------------------------------------------------------------
|
Asa@0
|
269 -- Registering and retreiving options tables:
|
Asa@0
|
270
|
Asa@0
|
271
|
Asa@0
|
272 -- validateGetterArgs: helper function for :GetOptionsTable (or, rather, the getter functions returned by it)
|
Asa@0
|
273
|
Asa@0
|
274 local function validateGetterArgs(uiType, uiName, errlvl)
|
Asa@0
|
275 errlvl=(errlvl or 0)+2
|
Asa@0
|
276 if uiType~="cmd" and uiType~="dropdown" and uiType~="dialog" then
|
Asa@0
|
277 error(MAJOR..": Requesting options table: 'uiType' - invalid configuration UI type, expected 'cmd', 'dropdown' or 'dialog'", errlvl)
|
Asa@0
|
278 end
|
Asa@0
|
279 if not strmatch(uiName, "[A-Za-z]%-[0-9]") then -- Expecting e.g. "MyLib-1.2"
|
Asa@0
|
280 error(MAJOR..": Requesting options table: 'uiName' - badly formatted or missing version number. Expected e.g. 'MyLib-1.2'", errlvl)
|
Asa@0
|
281 end
|
Asa@0
|
282 end
|
Asa@0
|
283
|
Asa@0
|
284 --- Register an options table with the config registry.
|
Asa@0
|
285 -- @param appName The application name as given to `:RegisterOptionsTable()`
|
Asa@0
|
286 -- @param options The options table, OR a function reference that generates it on demand. \\
|
Asa@0
|
287 -- See the top of the page for info on arguments passed to such functions.
|
Asa@0
|
288 function AceConfigRegistry:RegisterOptionsTable(appName, options)
|
Asa@0
|
289 if type(options)=="table" then
|
Asa@0
|
290 if options.type~="group" then -- quick sanity checker
|
Asa@0
|
291 error(MAJOR..": RegisterOptionsTable(appName, options): 'options' - missing type='group' member in root group", 2)
|
Asa@0
|
292 end
|
Asa@0
|
293 AceConfigRegistry.tables[appName] = function(uiType, uiName, errlvl)
|
Asa@0
|
294 errlvl=(errlvl or 0)+1
|
Asa@0
|
295 validateGetterArgs(uiType, uiName, errlvl)
|
Asa@0
|
296 if not AceConfigRegistry.validated[uiType][appName] then
|
Asa@0
|
297 AceConfigRegistry:ValidateOptionsTable(options, appName, errlvl) -- upgradable
|
Asa@0
|
298 AceConfigRegistry.validated[uiType][appName] = true
|
Asa@0
|
299 end
|
Asa@0
|
300 return options
|
Asa@0
|
301 end
|
Asa@0
|
302 elseif type(options)=="function" then
|
Asa@0
|
303 AceConfigRegistry.tables[appName] = function(uiType, uiName, errlvl)
|
Asa@0
|
304 errlvl=(errlvl or 0)+1
|
Asa@0
|
305 validateGetterArgs(uiType, uiName, errlvl)
|
Asa@0
|
306 local tab = assert(options(uiType, uiName, appName))
|
Asa@0
|
307 if not AceConfigRegistry.validated[uiType][appName] then
|
Asa@0
|
308 AceConfigRegistry:ValidateOptionsTable(tab, appName, errlvl) -- upgradable
|
Asa@0
|
309 AceConfigRegistry.validated[uiType][appName] = true
|
Asa@0
|
310 end
|
Asa@0
|
311 return tab
|
Asa@0
|
312 end
|
Asa@0
|
313 else
|
Asa@0
|
314 error(MAJOR..": RegisterOptionsTable(appName, options): 'options' - expected table or function reference", 2)
|
Asa@0
|
315 end
|
Asa@0
|
316 end
|
Asa@0
|
317
|
Asa@0
|
318 --- Returns an iterator of ["appName"]=funcref pairs
|
Asa@0
|
319 function AceConfigRegistry:IterateOptionsTables()
|
Asa@0
|
320 return pairs(AceConfigRegistry.tables)
|
Asa@0
|
321 end
|
Asa@0
|
322
|
Asa@0
|
323
|
Asa@0
|
324
|
Asa@0
|
325
|
Asa@0
|
326 --- Query the registry for a specific options table.
|
Asa@0
|
327 -- If only appName is given, a function is returned which you
|
Asa@0
|
328 -- can call with (uiType,uiName) to get the table.\\
|
Asa@0
|
329 -- If uiType&uiName are given, the table is returned.
|
Asa@0
|
330 -- @param appName The application name as given to `:RegisterOptionsTable()`
|
Asa@0
|
331 -- @param uiType The type of UI to get the table for, one of "cmd", "dropdown", "dialog"
|
Asa@0
|
332 -- @param uiName The name of the library/addon querying for the table, e.g. "MyLib-1.0"
|
Asa@0
|
333 function AceConfigRegistry:GetOptionsTable(appName, uiType, uiName)
|
Asa@0
|
334 local f = AceConfigRegistry.tables[appName]
|
Asa@0
|
335 if not f then
|
Asa@0
|
336 return nil
|
Asa@0
|
337 end
|
Asa@0
|
338
|
Asa@0
|
339 if uiType then
|
Asa@0
|
340 return f(uiType,uiName,1) -- get the table for us
|
Asa@0
|
341 else
|
Asa@0
|
342 return f -- return the function
|
Asa@0
|
343 end
|
Asa@0
|
344 end
|