Zerotorescue@0: --- AceConfigRegistry-3.0 handles central registration of options tables in use by addons and modules.\\ Zerotorescue@0: -- Options tables can be registered as raw tables, OR as function refs that return a table.\\ Zerotorescue@0: -- Such functions receive three arguments: "uiType", "uiName", "appName". \\ Zerotorescue@0: -- * Valid **uiTypes**: "cmd", "dropdown", "dialog". This is verified by the library at call time. \\ Zerotorescue@0: -- * 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.\\ Zerotorescue@0: -- * The **appName** field is the options table name as given at registration time \\ Zerotorescue@0: -- Zerotorescue@0: -- :IterateOptionsTables() (and :GetOptionsTable() if only given one argument) return a function reference that the requesting config handling addon must call with valid "uiType", "uiName". Zerotorescue@0: -- @class file Zerotorescue@0: -- @name AceConfigRegistry-3.0 Zerotorescue@0: -- @release $Id: AceConfigRegistry-3.0.lua 921 2010-05-09 15:49:14Z nevcairiel $ Zerotorescue@0: local MAJOR, MINOR = "AceConfigRegistry-3.0", 12 Zerotorescue@0: local AceConfigRegistry = LibStub:NewLibrary(MAJOR, MINOR) Zerotorescue@0: Zerotorescue@0: if not AceConfigRegistry then return end Zerotorescue@0: Zerotorescue@0: AceConfigRegistry.tables = AceConfigRegistry.tables or {} Zerotorescue@0: Zerotorescue@0: local CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0") Zerotorescue@0: Zerotorescue@0: if not AceConfigRegistry.callbacks then Zerotorescue@0: AceConfigRegistry.callbacks = CallbackHandler:New(AceConfigRegistry) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- Lua APIs Zerotorescue@0: local tinsert, tconcat = table.insert, table.concat Zerotorescue@0: local strfind, strmatch = string.find, string.match Zerotorescue@0: local type, tostring, select, pairs = type, tostring, select, pairs Zerotorescue@0: local error, assert = error, assert Zerotorescue@0: Zerotorescue@0: ----------------------------------------------------------------------- Zerotorescue@0: -- Validating options table consistency: Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: AceConfigRegistry.validated = { Zerotorescue@0: -- list of options table names ran through :ValidateOptionsTable automatically. Zerotorescue@0: -- CLEARED ON PURPOSE, since newer versions may have newer validators Zerotorescue@0: cmd = {}, Zerotorescue@0: dropdown = {}, Zerotorescue@0: dialog = {}, Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: local function err(msg, errlvl, ...) Zerotorescue@0: local t = {} Zerotorescue@0: for i=select("#",...),1,-1 do Zerotorescue@0: tinsert(t, (select(i, ...))) Zerotorescue@0: end Zerotorescue@0: error(MAJOR..":ValidateOptionsTable(): "..tconcat(t,".")..msg, errlvl+2) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: local isstring={["string"]=true, _="string"} Zerotorescue@0: local isstringfunc={["string"]=true,["function"]=true, _="string or funcref"} Zerotorescue@0: local istable={["table"]=true, _="table"} Zerotorescue@0: local ismethodtable={["table"]=true,["string"]=true,["function"]=true, _="methodname, funcref or table"} Zerotorescue@0: local optstring={["nil"]=true,["string"]=true, _="string"} Zerotorescue@0: local optstringfunc={["nil"]=true,["string"]=true,["function"]=true, _="string or funcref"} Zerotorescue@0: local optnumber={["nil"]=true,["number"]=true, _="number"} Zerotorescue@0: local optmethod={["nil"]=true,["string"]=true,["function"]=true, _="methodname or funcref"} Zerotorescue@0: local optmethodfalse={["nil"]=true,["string"]=true,["function"]=true,["boolean"]={[false]=true}, _="methodname, funcref or false"} Zerotorescue@0: local optmethodnumber={["nil"]=true,["string"]=true,["function"]=true,["number"]=true, _="methodname, funcref or number"} Zerotorescue@0: local optmethodtable={["nil"]=true,["string"]=true,["function"]=true,["table"]=true, _="methodname, funcref or table"} Zerotorescue@0: local optmethodbool={["nil"]=true,["string"]=true,["function"]=true,["boolean"]=true, _="methodname, funcref or boolean"} Zerotorescue@0: local opttable={["nil"]=true,["table"]=true, _="table"} Zerotorescue@0: local optbool={["nil"]=true,["boolean"]=true, _="boolean"} Zerotorescue@0: local optboolnumber={["nil"]=true,["boolean"]=true,["number"]=true, _="boolean or number"} Zerotorescue@0: Zerotorescue@0: local basekeys={ Zerotorescue@0: type=isstring, Zerotorescue@0: name=isstringfunc, Zerotorescue@0: desc=optstringfunc, Zerotorescue@0: descStyle=optstring, Zerotorescue@0: order=optmethodnumber, Zerotorescue@0: validate=optmethodfalse, Zerotorescue@0: confirm=optmethodbool, Zerotorescue@0: confirmText=optstring, Zerotorescue@0: disabled=optmethodbool, Zerotorescue@0: hidden=optmethodbool, Zerotorescue@0: guiHidden=optmethodbool, Zerotorescue@0: dialogHidden=optmethodbool, Zerotorescue@0: dropdownHidden=optmethodbool, Zerotorescue@0: cmdHidden=optmethodbool, Zerotorescue@0: icon=optstringfunc, Zerotorescue@0: iconCoords=optmethodtable, Zerotorescue@0: handler=opttable, Zerotorescue@0: get=optmethodfalse, Zerotorescue@0: set=optmethodfalse, Zerotorescue@0: func=optmethodfalse, Zerotorescue@0: arg={["*"]=true}, Zerotorescue@0: width=optstring, Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: local typedkeys={ Zerotorescue@0: header={}, Zerotorescue@0: description={ Zerotorescue@0: image=optstringfunc, Zerotorescue@0: imageCoords=optmethodtable, Zerotorescue@0: imageHeight=optnumber, Zerotorescue@0: imageWidth=optnumber, Zerotorescue@0: fontSize=optstringfunc, Zerotorescue@0: }, Zerotorescue@0: group={ Zerotorescue@0: args=istable, Zerotorescue@0: plugins=opttable, Zerotorescue@0: inline=optbool, Zerotorescue@0: cmdInline=optbool, Zerotorescue@0: guiInline=optbool, Zerotorescue@0: dropdownInline=optbool, Zerotorescue@0: dialogInline=optbool, Zerotorescue@0: childGroups=optstring, Zerotorescue@0: }, Zerotorescue@0: execute={ Zerotorescue@0: image=optstringfunc, Zerotorescue@0: imageCoords=optmethodtable, Zerotorescue@0: imageHeight=optnumber, Zerotorescue@0: imageWidth=optnumber, Zerotorescue@0: }, Zerotorescue@0: input={ Zerotorescue@0: pattern=optstring, Zerotorescue@0: usage=optstring, Zerotorescue@0: control=optstring, Zerotorescue@0: dialogControl=optstring, Zerotorescue@0: dropdownControl=optstring, Zerotorescue@0: multiline=optboolnumber, Zerotorescue@0: }, Zerotorescue@0: toggle={ Zerotorescue@0: tristate=optbool, Zerotorescue@0: image=optstringfunc, Zerotorescue@0: imageCoords=optmethodtable, Zerotorescue@0: }, Zerotorescue@0: tristate={ Zerotorescue@0: }, Zerotorescue@0: range={ Zerotorescue@0: min=optnumber, Zerotorescue@0: softMin=optnumber, Zerotorescue@0: max=optnumber, Zerotorescue@0: softMax=optnumber, Zerotorescue@0: step=optnumber, Zerotorescue@0: bigStep=optnumber, Zerotorescue@0: isPercent=optbool, Zerotorescue@0: }, Zerotorescue@0: select={ Zerotorescue@0: values=ismethodtable, Zerotorescue@0: style={ Zerotorescue@0: ["nil"]=true, Zerotorescue@0: ["string"]={dropdown=true,radio=true}, Zerotorescue@0: _="string: 'dropdown' or 'radio'" Zerotorescue@0: }, Zerotorescue@0: control=optstring, Zerotorescue@0: dialogControl=optstring, Zerotorescue@0: dropdownControl=optstring, Zerotorescue@0: }, Zerotorescue@0: multiselect={ Zerotorescue@0: values=ismethodtable, Zerotorescue@0: style=optstring, Zerotorescue@0: tristate=optbool, Zerotorescue@0: control=optstring, Zerotorescue@0: dialogControl=optstring, Zerotorescue@0: dropdownControl=optstring, Zerotorescue@0: }, Zerotorescue@0: color={ Zerotorescue@0: hasAlpha=optbool, Zerotorescue@0: }, Zerotorescue@0: keybinding={ Zerotorescue@0: -- TODO Zerotorescue@0: }, Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: local function validateKey(k,errlvl,...) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: if type(k)~="string" then Zerotorescue@0: err("["..tostring(k).."] - key is not a string", errlvl,...) Zerotorescue@0: end Zerotorescue@0: if strfind(k, "[%c\127]") then Zerotorescue@0: err("["..tostring(k).."] - key name contained control characters", errlvl,...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function validateVal(v, oktypes, errlvl,...) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: local isok=oktypes[type(v)] or oktypes["*"] Zerotorescue@0: Zerotorescue@0: if not isok then Zerotorescue@0: err(": expected a "..oktypes._..", got '"..tostring(v).."'", errlvl,...) Zerotorescue@0: end Zerotorescue@0: if type(isok)=="table" then -- isok was a table containing specific values to be tested for! Zerotorescue@0: if not isok[v] then Zerotorescue@0: err(": did not expect "..type(v).." value '"..tostring(v).."'", errlvl,...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function validate(options,errlvl,...) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: -- basic consistency Zerotorescue@0: if type(options)~="table" then Zerotorescue@0: err(": expected a table, got a "..type(options), errlvl,...) Zerotorescue@0: end Zerotorescue@0: if type(options.type)~="string" then Zerotorescue@0: err(".type: expected a string, got a "..type(options.type), errlvl,...) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- get type and 'typedkeys' member Zerotorescue@0: local tk = typedkeys[options.type] Zerotorescue@0: if not tk then Zerotorescue@0: err(".type: unknown type '"..options.type.."'", errlvl,...) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- make sure that all options[] are known parameters Zerotorescue@0: for k,v in pairs(options) do Zerotorescue@0: if not (tk[k] or basekeys[k]) then Zerotorescue@0: err(": unknown parameter", errlvl,tostring(k),...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- verify that required params are there, and that everything is the right type Zerotorescue@0: for k,oktypes in pairs(basekeys) do Zerotorescue@0: validateVal(options[k], oktypes, errlvl,k,...) Zerotorescue@0: end Zerotorescue@0: for k,oktypes in pairs(tk) do Zerotorescue@0: validateVal(options[k], oktypes, errlvl,k,...) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- extra logic for groups Zerotorescue@0: if options.type=="group" then Zerotorescue@0: for k,v in pairs(options.args) do Zerotorescue@0: validateKey(k,errlvl,"args",...) Zerotorescue@0: validate(v, errlvl,k,"args",...) Zerotorescue@0: end Zerotorescue@0: if options.plugins then Zerotorescue@0: for plugname,plugin in pairs(options.plugins) do Zerotorescue@0: if type(plugin)~="table" then Zerotorescue@0: err(": expected a table, got '"..tostring(plugin).."'", errlvl,tostring(plugname),"plugins",...) Zerotorescue@0: end Zerotorescue@0: for k,v in pairs(plugin) do Zerotorescue@0: validateKey(k,errlvl,tostring(plugname),"plugins",...) Zerotorescue@0: validate(v, errlvl,k,tostring(plugname),"plugins",...) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: --- Validates basic structure and integrity of an options table \\ Zerotorescue@0: -- Does NOT verify that get/set etc actually exist, since they can be defined at any depth Zerotorescue@0: -- @param options The table to be validated Zerotorescue@0: -- @param name The name of the table to be validated (shown in any error message) Zerotorescue@0: -- @param errlvl (optional number) error level offset, default 0 (=errors point to the function calling :ValidateOptionsTable) Zerotorescue@0: function AceConfigRegistry:ValidateOptionsTable(options,name,errlvl) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: name = name or "Optionstable" Zerotorescue@0: if not options.name then Zerotorescue@0: options.name=name -- bit of a hack, the root level doesn't really need a .name :-/ Zerotorescue@0: end Zerotorescue@0: validate(options,errlvl,name) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Fires a "ConfigTableChange" callback for those listening in on it, allowing config GUIs to refresh. Zerotorescue@0: -- You should call this function if your options table changed from any outside event, like a game event Zerotorescue@0: -- or a timer. Zerotorescue@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Zerotorescue@0: function AceConfigRegistry:NotifyChange(appName) Zerotorescue@0: if not AceConfigRegistry.tables[appName] then return end Zerotorescue@0: AceConfigRegistry.callbacks:Fire("ConfigTableChange", appName) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: -- ------------------------------------------------------------------- Zerotorescue@0: -- Registering and retreiving options tables: Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: -- validateGetterArgs: helper function for :GetOptionsTable (or, rather, the getter functions returned by it) Zerotorescue@0: Zerotorescue@0: local function validateGetterArgs(uiType, uiName, errlvl) Zerotorescue@0: errlvl=(errlvl or 0)+2 Zerotorescue@0: if uiType~="cmd" and uiType~="dropdown" and uiType~="dialog" then Zerotorescue@0: error(MAJOR..": Requesting options table: 'uiType' - invalid configuration UI type, expected 'cmd', 'dropdown' or 'dialog'", errlvl) Zerotorescue@0: end Zerotorescue@0: if not strmatch(uiName, "[A-Za-z]%-[0-9]") then -- Expecting e.g. "MyLib-1.2" Zerotorescue@0: error(MAJOR..": Requesting options table: 'uiName' - badly formatted or missing version number. Expected e.g. 'MyLib-1.2'", errlvl) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Register an options table with the config registry. Zerotorescue@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Zerotorescue@0: -- @param options The options table, OR a function reference that generates it on demand. \\ Zerotorescue@0: -- See the top of the page for info on arguments passed to such functions. Zerotorescue@0: function AceConfigRegistry:RegisterOptionsTable(appName, options) Zerotorescue@0: if type(options)=="table" then Zerotorescue@0: if options.type~="group" then -- quick sanity checker Zerotorescue@0: error(MAJOR..": RegisterOptionsTable(appName, options): 'options' - missing type='group' member in root group", 2) Zerotorescue@0: end Zerotorescue@0: AceConfigRegistry.tables[appName] = function(uiType, uiName, errlvl) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: validateGetterArgs(uiType, uiName, errlvl) Zerotorescue@0: if not AceConfigRegistry.validated[uiType][appName] then Zerotorescue@0: AceConfigRegistry:ValidateOptionsTable(options, appName, errlvl) -- upgradable Zerotorescue@0: AceConfigRegistry.validated[uiType][appName] = true Zerotorescue@0: end Zerotorescue@0: return options Zerotorescue@0: end Zerotorescue@0: elseif type(options)=="function" then Zerotorescue@0: AceConfigRegistry.tables[appName] = function(uiType, uiName, errlvl) Zerotorescue@0: errlvl=(errlvl or 0)+1 Zerotorescue@0: validateGetterArgs(uiType, uiName, errlvl) Zerotorescue@0: local tab = assert(options(uiType, uiName, appName)) Zerotorescue@0: if not AceConfigRegistry.validated[uiType][appName] then Zerotorescue@0: AceConfigRegistry:ValidateOptionsTable(tab, appName, errlvl) -- upgradable Zerotorescue@0: AceConfigRegistry.validated[uiType][appName] = true Zerotorescue@0: end Zerotorescue@0: return tab Zerotorescue@0: end Zerotorescue@0: else Zerotorescue@0: error(MAJOR..": RegisterOptionsTable(appName, options): 'options' - expected table or function reference", 2) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --- Returns an iterator of ["appName"]=funcref pairs Zerotorescue@0: function AceConfigRegistry:IterateOptionsTables() Zerotorescue@0: return pairs(AceConfigRegistry.tables) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: Zerotorescue@0: --- Query the registry for a specific options table. Zerotorescue@0: -- If only appName is given, a function is returned which you Zerotorescue@0: -- can call with (uiType,uiName) to get the table.\\ Zerotorescue@0: -- If uiType&uiName are given, the table is returned. Zerotorescue@0: -- @param appName The application name as given to `:RegisterOptionsTable()` Zerotorescue@0: -- @param uiType The type of UI to get the table for, one of "cmd", "dropdown", "dialog" Zerotorescue@0: -- @param uiName The name of the library/addon querying for the table, e.g. "MyLib-1.0" Zerotorescue@0: function AceConfigRegistry:GetOptionsTable(appName, uiType, uiName) Zerotorescue@0: local f = AceConfigRegistry.tables[appName] Zerotorescue@0: if not f then Zerotorescue@0: return nil Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if uiType then Zerotorescue@0: return f(uiType,uiName,1) -- get the table for us Zerotorescue@0: else Zerotorescue@0: return f -- return the function Zerotorescue@0: end Zerotorescue@0: end