tercio@0: --- **AceDB-3.0** manages the SavedVariables of your addon. tercio@0: -- It offers profile management, smart defaults and namespaces for modules.\\ tercio@0: -- Data can be saved in different data-types, depending on its intended usage. tercio@0: -- The most common data-type is the `profile` type, which allows the user to choose tercio@0: -- the active profile, and manage the profiles of all of his characters.\\ tercio@0: -- The following data types are available: tercio@0: -- * **char** Character-specific data. Every character has its own database. tercio@0: -- * **realm** Realm-specific data. All of the players characters on the same realm share this database. tercio@0: -- * **class** Class-specific data. All of the players characters of the same class share this database. tercio@0: -- * **race** Race-specific data. All of the players characters of the same race share this database. tercio@0: -- * **faction** Faction-specific data. All of the players characters of the same faction share this database. tercio@0: -- * **factionrealm** Faction and realm specific data. All of the players characters on the same realm and of the same faction share this database. tercio@0: -- * **global** Global Data. All characters on the same account share this database. tercio@0: -- * **profile** Profile-specific data. All characters using the same profile share this database. The user can control which profile should be used. tercio@0: -- tercio@0: -- Creating a new Database using the `:New` function will return a new DBObject. A database will inherit all functions tercio@0: -- of the DBObjectLib listed here. \\ tercio@0: -- If you create a new namespaced child-database (`:RegisterNamespace`), you'll get a DBObject as well, but note tercio@0: -- that the child-databases cannot individually change their profile, and are linked to their parents profile - and because of that, tercio@0: -- the profile related APIs are not available. Only `:RegisterDefaults` and `:ResetProfile` are available on child-databases. tercio@0: -- tercio@0: -- For more details on how to use AceDB-3.0, see the [[AceDB-3.0 Tutorial]]. tercio@0: -- tercio@0: -- You may also be interested in [[libdualspec-1-0|LibDualSpec-1.0]] to do profile switching automatically when switching specs. tercio@0: -- tercio@0: -- @usage tercio@0: -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("DBExample") tercio@0: -- tercio@0: -- -- declare defaults to be used in the DB tercio@0: -- local defaults = { tercio@0: -- profile = { tercio@0: -- setting = true, tercio@0: -- } tercio@0: -- } tercio@0: -- tercio@0: -- function MyAddon:OnInitialize() tercio@0: -- -- Assuming the .toc says ## SavedVariables: MyAddonDB tercio@0: -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true) tercio@0: -- end tercio@0: -- @class file tercio@0: -- @name AceDB-3.0.lua tercio@0: -- @release $Id: AceDB-3.0.lua 1096 2013-09-13 15:05:40Z nevcairiel $ tercio@0: local ACEDB_MAJOR, ACEDB_MINOR = "AceDB-3.0", 23 tercio@0: local AceDB, oldminor = LibStub:NewLibrary(ACEDB_MAJOR, ACEDB_MINOR) tercio@0: tercio@0: if not AceDB then return end -- No upgrade needed tercio@0: tercio@0: -- Lua APIs tercio@0: local type, pairs, next, error = type, pairs, next, error tercio@0: local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget tercio@0: tercio@0: -- WoW APIs tercio@0: local _G = _G tercio@0: tercio@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded tercio@0: -- List them here for Mikk's FindGlobals script tercio@0: -- GLOBALS: LibStub tercio@0: tercio@0: AceDB.db_registry = AceDB.db_registry or {} tercio@0: AceDB.frame = AceDB.frame or CreateFrame("Frame") tercio@0: tercio@0: local CallbackHandler tercio@0: local CallbackDummy = { Fire = function() end } tercio@0: tercio@0: local DBObjectLib = {} tercio@0: tercio@0: --[[------------------------------------------------------------------------- tercio@0: AceDB Utility Functions tercio@0: ---------------------------------------------------------------------------]] tercio@0: tercio@0: -- Simple shallow copy for copying defaults tercio@0: local function copyTable(src, dest) tercio@0: if type(dest) ~= "table" then dest = {} end tercio@0: if type(src) == "table" then tercio@0: for k,v in pairs(src) do tercio@0: if type(v) == "table" then tercio@0: -- try to index the key first so that the metatable creates the defaults, if set, and use that table tercio@0: v = copyTable(v, dest[k]) tercio@0: end tercio@0: dest[k] = v tercio@0: end tercio@0: end tercio@0: return dest tercio@0: end tercio@0: tercio@0: -- Called to add defaults to a section of the database tercio@0: -- tercio@0: -- When a ["*"] default section is indexed with a new key, a table is returned tercio@0: -- and set in the host table. These tables must be cleaned up by removeDefaults tercio@0: -- in order to ensure we don't write empty default tables. tercio@0: local function copyDefaults(dest, src) tercio@0: -- this happens if some value in the SV overwrites our default value with a non-table tercio@0: --if type(dest) ~= "table" then return end tercio@0: for k, v in pairs(src) do tercio@0: if k == "*" or k == "**" then tercio@0: if type(v) == "table" then tercio@0: -- This is a metatable used for table defaults tercio@0: local mt = { tercio@0: -- This handles the lookup and creation of new subtables tercio@0: __index = function(t,k) tercio@0: if k == nil then return nil end tercio@0: local tbl = {} tercio@0: copyDefaults(tbl, v) tercio@0: rawset(t, k, tbl) tercio@0: return tbl tercio@0: end, tercio@0: } tercio@0: setmetatable(dest, mt) tercio@0: -- handle already existing tables in the SV tercio@0: for dk, dv in pairs(dest) do tercio@0: if not rawget(src, dk) and type(dv) == "table" then tercio@0: copyDefaults(dv, v) tercio@0: end tercio@0: end tercio@0: else tercio@0: -- Values are not tables, so this is just a simple return tercio@0: local mt = {__index = function(t,k) return k~=nil and v or nil end} tercio@0: setmetatable(dest, mt) tercio@0: end tercio@0: elseif type(v) == "table" then tercio@0: if not rawget(dest, k) then rawset(dest, k, {}) end tercio@0: if type(dest[k]) == "table" then tercio@0: copyDefaults(dest[k], v) tercio@0: if src['**'] then tercio@0: copyDefaults(dest[k], src['**']) tercio@0: end tercio@0: end tercio@0: else tercio@0: if rawget(dest, k) == nil then tercio@0: rawset(dest, k, v) tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: -- Called to remove all defaults in the default table from the database tercio@0: local function removeDefaults(db, defaults, blocker) tercio@0: -- remove all metatables from the db, so we don't accidentally create new sub-tables through them tercio@0: setmetatable(db, nil) tercio@0: -- loop through the defaults and remove their content tercio@0: for k,v in pairs(defaults) do tercio@0: if k == "*" or k == "**" then tercio@0: if type(v) == "table" then tercio@0: -- Loop through all the actual k,v pairs and remove tercio@0: for key, value in pairs(db) do tercio@0: if type(value) == "table" then tercio@0: -- if the key was not explicitly specified in the defaults table, just strip everything from * and ** tables tercio@0: if defaults[key] == nil and (not blocker or blocker[key] == nil) then tercio@0: removeDefaults(value, v) tercio@0: -- if the table is empty afterwards, remove it tercio@0: if next(value) == nil then tercio@0: db[key] = nil tercio@0: end tercio@0: -- if it was specified, only strip ** content, but block values which were set in the key table tercio@0: elseif k == "**" then tercio@0: removeDefaults(value, v, defaults[key]) tercio@0: end tercio@0: end tercio@0: end tercio@0: elseif k == "*" then tercio@0: -- check for non-table default tercio@0: for key, value in pairs(db) do tercio@0: if defaults[key] == nil and v == value then tercio@0: db[key] = nil tercio@0: end tercio@0: end tercio@0: end tercio@0: elseif type(v) == "table" and type(db[k]) == "table" then tercio@0: -- if a blocker was set, dive into it, to allow multi-level defaults tercio@0: removeDefaults(db[k], v, blocker and blocker[k]) tercio@0: if next(db[k]) == nil then tercio@0: db[k] = nil tercio@0: end tercio@0: else tercio@0: -- check if the current value matches the default, and that its not blocked by another defaults table tercio@0: if db[k] == defaults[k] and (not blocker or blocker[k] == nil) then tercio@0: db[k] = nil tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: -- This is called when a table section is first accessed, to set up the defaults tercio@0: local function initSection(db, section, svstore, key, defaults) tercio@0: local sv = rawget(db, "sv") tercio@0: tercio@0: local tableCreated tercio@0: if not sv[svstore] then sv[svstore] = {} end tercio@0: if not sv[svstore][key] then tercio@0: sv[svstore][key] = {} tercio@0: tableCreated = true tercio@0: end tercio@0: tercio@0: local tbl = sv[svstore][key] tercio@0: tercio@0: if defaults then tercio@0: copyDefaults(tbl, defaults) tercio@0: end tercio@0: rawset(db, section, tbl) tercio@0: tercio@0: return tableCreated, tbl tercio@0: end tercio@0: tercio@0: -- Metatable to handle the dynamic creation of sections and copying of sections. tercio@0: local dbmt = { tercio@0: __index = function(t, section) tercio@0: local keys = rawget(t, "keys") tercio@0: local key = keys[section] tercio@0: if key then tercio@0: local defaultTbl = rawget(t, "defaults") tercio@0: local defaults = defaultTbl and defaultTbl[section] tercio@0: tercio@0: if section == "profile" then tercio@0: local new = initSection(t, section, "profiles", key, defaults) tercio@0: if new then tercio@0: -- Callback: OnNewProfile, database, newProfileKey tercio@0: t.callbacks:Fire("OnNewProfile", t, key) tercio@0: end tercio@0: elseif section == "profiles" then tercio@0: local sv = rawget(t, "sv") tercio@0: if not sv.profiles then sv.profiles = {} end tercio@0: rawset(t, "profiles", sv.profiles) tercio@0: elseif section == "global" then tercio@0: local sv = rawget(t, "sv") tercio@0: if not sv.global then sv.global = {} end tercio@0: if defaults then tercio@0: copyDefaults(sv.global, defaults) tercio@0: end tercio@0: rawset(t, section, sv.global) tercio@0: else tercio@0: initSection(t, section, section, key, defaults) tercio@0: end tercio@0: end tercio@0: tercio@0: return rawget(t, section) tercio@0: end tercio@0: } tercio@0: tercio@0: local function validateDefaults(defaults, keyTbl, offset) tercio@0: if not defaults then return end tercio@0: offset = offset or 0 tercio@0: for k in pairs(defaults) do tercio@0: if not keyTbl[k] or k == "profiles" then tercio@0: error(("Usage: AceDBObject:RegisterDefaults(defaults): '%s' is not a valid datatype."):format(k), 3 + offset) tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: local preserve_keys = { tercio@0: ["callbacks"] = true, tercio@0: ["RegisterCallback"] = true, tercio@0: ["UnregisterCallback"] = true, tercio@0: ["UnregisterAllCallbacks"] = true, tercio@0: ["children"] = true, tercio@0: } tercio@0: tercio@0: local realmKey = GetRealmName() tercio@0: local charKey = UnitName("player") .. " - " .. realmKey tercio@0: local _, classKey = UnitClass("player") tercio@0: local _, raceKey = UnitRace("player") tercio@0: local factionKey = UnitFactionGroup("player") tercio@0: local factionrealmKey = factionKey .. " - " .. realmKey tercio@0: local factionrealmregionKey = factionrealmKey .. " - " .. string.sub(GetCVar("realmList"), 1, 2):upper() tercio@0: local localeKey = GetLocale():lower() tercio@0: tercio@0: -- Actual database initialization function tercio@0: local function initdb(sv, defaults, defaultProfile, olddb, parent) tercio@0: -- Generate the database keys for each section tercio@0: tercio@0: -- map "true" to our "Default" profile tercio@0: if defaultProfile == true then defaultProfile = "Default" end tercio@0: tercio@0: local profileKey tercio@0: if not parent then tercio@0: -- Make a container for profile keys tercio@0: if not sv.profileKeys then sv.profileKeys = {} end tercio@0: tercio@0: -- Try to get the profile selected from the char db tercio@0: profileKey = sv.profileKeys[charKey] or defaultProfile or charKey tercio@0: tercio@0: -- save the selected profile for later tercio@0: sv.profileKeys[charKey] = profileKey tercio@0: else tercio@0: -- Use the profile of the parents DB tercio@0: profileKey = parent.keys.profile or defaultProfile or charKey tercio@0: tercio@0: -- clear the profileKeys in the DB, namespaces don't need to store them tercio@0: sv.profileKeys = nil tercio@0: end tercio@0: tercio@0: -- This table contains keys that enable the dynamic creation tercio@0: -- of each section of the table. The 'global' and 'profiles' tercio@0: -- have a key of true, since they are handled in a special case tercio@0: local keyTbl= { tercio@0: ["char"] = charKey, tercio@0: ["realm"] = realmKey, tercio@0: ["class"] = classKey, tercio@0: ["race"] = raceKey, tercio@0: ["faction"] = factionKey, tercio@0: ["factionrealm"] = factionrealmKey, tercio@0: ["factionrealmregion"] = factionrealmregionKey, tercio@0: ["profile"] = profileKey, tercio@0: ["locale"] = localeKey, tercio@0: ["global"] = true, tercio@0: ["profiles"] = true, tercio@0: } tercio@0: tercio@0: validateDefaults(defaults, keyTbl, 1) tercio@0: tercio@0: -- This allows us to use this function to reset an entire database tercio@0: -- Clear out the old database tercio@0: if olddb then tercio@0: for k,v in pairs(olddb) do if not preserve_keys[k] then olddb[k] = nil end end tercio@0: end tercio@0: tercio@0: -- Give this database the metatable so it initializes dynamically tercio@0: local db = setmetatable(olddb or {}, dbmt) tercio@0: tercio@0: if not rawget(db, "callbacks") then tercio@0: -- try to load CallbackHandler-1.0 if it loaded after our library tercio@0: if not CallbackHandler then CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0", true) end tercio@0: db.callbacks = CallbackHandler and CallbackHandler:New(db) or CallbackDummy tercio@0: end tercio@0: tercio@0: -- Copy methods locally into the database object, to avoid hitting tercio@0: -- the metatable when calling methods tercio@0: tercio@0: if not parent then tercio@0: for name, func in pairs(DBObjectLib) do tercio@0: db[name] = func tercio@0: end tercio@0: else tercio@0: -- hack this one in tercio@0: db.RegisterDefaults = DBObjectLib.RegisterDefaults tercio@0: db.ResetProfile = DBObjectLib.ResetProfile tercio@0: end tercio@0: tercio@0: -- Set some properties in the database object tercio@0: db.profiles = sv.profiles tercio@0: db.keys = keyTbl tercio@0: db.sv = sv tercio@0: --db.sv_name = name tercio@0: db.defaults = defaults tercio@0: db.parent = parent tercio@0: tercio@0: -- store the DB in the registry tercio@0: AceDB.db_registry[db] = true tercio@0: tercio@0: return db tercio@0: end tercio@0: tercio@0: -- handle PLAYER_LOGOUT tercio@0: -- strip all defaults from all databases tercio@0: -- and cleans up empty sections tercio@0: local function logoutHandler(frame, event) tercio@0: if event == "PLAYER_LOGOUT" then tercio@0: for db in pairs(AceDB.db_registry) do tercio@0: db.callbacks:Fire("OnDatabaseShutdown", db) tercio@0: db:RegisterDefaults(nil) tercio@0: tercio@0: -- cleanup sections that are empty without defaults tercio@0: local sv = rawget(db, "sv") tercio@0: for section in pairs(db.keys) do tercio@0: if rawget(sv, section) then tercio@0: -- global is special, all other sections have sub-entrys tercio@0: -- also don't delete empty profiles on main dbs, only on namespaces tercio@0: if section ~= "global" and (section ~= "profiles" or rawget(db, "parent")) then tercio@0: for key in pairs(sv[section]) do tercio@0: if not next(sv[section][key]) then tercio@0: sv[section][key] = nil tercio@0: end tercio@0: end tercio@0: end tercio@0: if not next(sv[section]) then tercio@0: sv[section] = nil tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: AceDB.frame:RegisterEvent("PLAYER_LOGOUT") tercio@0: AceDB.frame:SetScript("OnEvent", logoutHandler) tercio@0: tercio@0: tercio@0: --[[------------------------------------------------------------------------- tercio@0: AceDB Object Method Definitions tercio@0: ---------------------------------------------------------------------------]] tercio@0: tercio@0: --- Sets the defaults table for the given database object by clearing any tercio@0: -- that are currently set, and then setting the new defaults. tercio@0: -- @param defaults A table of defaults for this database tercio@0: function DBObjectLib:RegisterDefaults(defaults) tercio@0: if defaults and type(defaults) ~= "table" then tercio@0: error("Usage: AceDBObject:RegisterDefaults(defaults): 'defaults' - table or nil expected.", 2) tercio@0: end tercio@0: tercio@0: validateDefaults(defaults, self.keys) tercio@0: tercio@0: -- Remove any currently set defaults tercio@0: if self.defaults then tercio@0: for section,key in pairs(self.keys) do tercio@0: if self.defaults[section] and rawget(self, section) then tercio@0: removeDefaults(self[section], self.defaults[section]) tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: -- Set the DBObject.defaults table tercio@0: self.defaults = defaults tercio@0: tercio@0: -- Copy in any defaults, only touching those sections already created tercio@0: if defaults then tercio@0: for section,key in pairs(self.keys) do tercio@0: if defaults[section] and rawget(self, section) then tercio@0: copyDefaults(self[section], defaults[section]) tercio@0: end tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: --- Changes the profile of the database and all of it's namespaces to the tercio@0: -- supplied named profile tercio@0: -- @param name The name of the profile to set as the current profile tercio@0: function DBObjectLib:SetProfile(name) tercio@0: if type(name) ~= "string" then tercio@0: error("Usage: AceDBObject:SetProfile(name): 'name' - string expected.", 2) tercio@0: end tercio@0: tercio@0: -- changing to the same profile, dont do anything tercio@0: if name == self.keys.profile then return end tercio@0: tercio@0: local oldProfile = self.profile tercio@0: local defaults = self.defaults and self.defaults.profile tercio@0: tercio@0: -- Callback: OnProfileShutdown, database tercio@0: self.callbacks:Fire("OnProfileShutdown", self) tercio@0: tercio@0: if oldProfile and defaults then tercio@0: -- Remove the defaults from the old profile tercio@0: removeDefaults(oldProfile, defaults) tercio@0: end tercio@0: tercio@0: self.profile = nil tercio@0: self.keys["profile"] = name tercio@0: tercio@0: -- if the storage exists, save the new profile tercio@0: -- this won't exist on namespaces. tercio@0: if self.sv.profileKeys then tercio@0: self.sv.profileKeys[charKey] = name tercio@0: end tercio@0: tercio@0: -- populate to child namespaces tercio@0: if self.children then tercio@0: for _, db in pairs(self.children) do tercio@0: DBObjectLib.SetProfile(db, name) tercio@0: end tercio@0: end tercio@0: tercio@0: -- Callback: OnProfileChanged, database, newProfileKey tercio@0: self.callbacks:Fire("OnProfileChanged", self, name) tercio@0: end tercio@0: tercio@0: --- Returns a table with the names of the existing profiles in the database. tercio@0: -- You can optionally supply a table to re-use for this purpose. tercio@0: -- @param tbl A table to store the profile names in (optional) tercio@0: function DBObjectLib:GetProfiles(tbl) tercio@0: if tbl and type(tbl) ~= "table" then tercio@0: error("Usage: AceDBObject:GetProfiles(tbl): 'tbl' - table or nil expected.", 2) tercio@0: end tercio@0: tercio@0: -- Clear the container table tercio@0: if tbl then tercio@0: for k,v in pairs(tbl) do tbl[k] = nil end tercio@0: else tercio@0: tbl = {} tercio@0: end tercio@0: tercio@0: local curProfile = self.keys.profile tercio@0: tercio@0: local i = 0 tercio@0: for profileKey in pairs(self.profiles) do tercio@0: i = i + 1 tercio@0: tbl[i] = profileKey tercio@0: if curProfile and profileKey == curProfile then curProfile = nil end tercio@0: end tercio@0: tercio@0: -- Add the current profile, if it hasn't been created yet tercio@0: if curProfile then tercio@0: i = i + 1 tercio@0: tbl[i] = curProfile tercio@0: end tercio@0: tercio@0: return tbl, i tercio@0: end tercio@0: tercio@0: --- Returns the current profile name used by the database tercio@0: function DBObjectLib:GetCurrentProfile() tercio@0: return self.keys.profile tercio@0: end tercio@0: tercio@0: --- Deletes a named profile. This profile must not be the active profile. tercio@0: -- @param name The name of the profile to be deleted tercio@0: -- @param silent If true, do not raise an error when the profile does not exist tercio@0: function DBObjectLib:DeleteProfile(name, silent) tercio@0: if type(name) ~= "string" then tercio@0: error("Usage: AceDBObject:DeleteProfile(name): 'name' - string expected.", 2) tercio@0: end tercio@0: tercio@0: if self.keys.profile == name then tercio@0: error("Cannot delete the active profile in an AceDBObject.", 2) tercio@0: end tercio@0: tercio@0: if not rawget(self.profiles, name) and not silent then tercio@0: error("Cannot delete profile '" .. name .. "'. It does not exist.", 2) tercio@0: end tercio@0: tercio@0: self.profiles[name] = nil tercio@0: tercio@0: -- populate to child namespaces tercio@0: if self.children then tercio@0: for _, db in pairs(self.children) do tercio@0: DBObjectLib.DeleteProfile(db, name, true) tercio@0: end tercio@0: end tercio@0: tercio@0: -- switch all characters that use this profile back to the default tercio@0: if self.sv.profileKeys then tercio@0: for key, profile in pairs(self.sv.profileKeys) do tercio@0: if profile == name then tercio@0: self.sv.profileKeys[key] = nil tercio@0: end tercio@0: end tercio@0: end tercio@0: tercio@0: -- Callback: OnProfileDeleted, database, profileKey tercio@0: self.callbacks:Fire("OnProfileDeleted", self, name) tercio@0: end tercio@0: tercio@0: --- Copies a named profile into the current profile, overwriting any conflicting tercio@0: -- settings. tercio@0: -- @param name The name of the profile to be copied into the current profile tercio@0: -- @param silent If true, do not raise an error when the profile does not exist tercio@0: function DBObjectLib:CopyProfile(name, silent) tercio@0: if type(name) ~= "string" then tercio@0: error("Usage: AceDBObject:CopyProfile(name): 'name' - string expected.", 2) tercio@0: end tercio@0: tercio@0: if name == self.keys.profile then tercio@0: error("Cannot have the same source and destination profiles.", 2) tercio@0: end tercio@0: tercio@0: if not rawget(self.profiles, name) and not silent then tercio@0: error("Cannot copy profile '" .. name .. "'. It does not exist.", 2) tercio@0: end tercio@0: tercio@0: -- Reset the profile before copying tercio@0: DBObjectLib.ResetProfile(self, nil, true) tercio@0: tercio@0: local profile = self.profile tercio@0: local source = self.profiles[name] tercio@0: tercio@0: copyTable(source, profile) tercio@0: tercio@0: -- populate to child namespaces tercio@0: if self.children then tercio@0: for _, db in pairs(self.children) do tercio@0: DBObjectLib.CopyProfile(db, name, true) tercio@0: end tercio@0: end tercio@0: tercio@0: -- Callback: OnProfileCopied, database, sourceProfileKey tercio@0: self.callbacks:Fire("OnProfileCopied", self, name) tercio@0: end tercio@0: tercio@0: --- Resets the current profile to the default values (if specified). tercio@0: -- @param noChildren if set to true, the reset will not be populated to the child namespaces of this DB object tercio@0: -- @param noCallbacks if set to true, won't fire the OnProfileReset callback tercio@0: function DBObjectLib:ResetProfile(noChildren, noCallbacks) tercio@0: local profile = self.profile tercio@0: tercio@0: for k,v in pairs(profile) do tercio@0: profile[k] = nil tercio@0: end tercio@0: tercio@0: local defaults = self.defaults and self.defaults.profile tercio@0: if defaults then tercio@0: copyDefaults(profile, defaults) tercio@0: end tercio@0: tercio@0: -- populate to child namespaces tercio@0: if self.children and not noChildren then tercio@0: for _, db in pairs(self.children) do tercio@0: DBObjectLib.ResetProfile(db, nil, noCallbacks) tercio@0: end tercio@0: end tercio@0: tercio@0: -- Callback: OnProfileReset, database tercio@0: if not noCallbacks then tercio@0: self.callbacks:Fire("OnProfileReset", self) tercio@0: end tercio@0: end tercio@0: tercio@0: --- Resets the entire database, using the string defaultProfile as the new default tercio@0: -- profile. tercio@0: -- @param defaultProfile The profile name to use as the default tercio@0: function DBObjectLib:ResetDB(defaultProfile) tercio@0: if defaultProfile and type(defaultProfile) ~= "string" then tercio@0: error("Usage: AceDBObject:ResetDB(defaultProfile): 'defaultProfile' - string or nil expected.", 2) tercio@0: end tercio@0: tercio@0: local sv = self.sv tercio@0: for k,v in pairs(sv) do tercio@0: sv[k] = nil tercio@0: end tercio@0: tercio@0: local parent = self.parent tercio@0: tercio@0: initdb(sv, self.defaults, defaultProfile, self) tercio@0: tercio@0: -- fix the child namespaces tercio@0: if self.children then tercio@0: if not sv.namespaces then sv.namespaces = {} end tercio@0: for name, db in pairs(self.children) do tercio@0: if not sv.namespaces[name] then sv.namespaces[name] = {} end tercio@0: initdb(sv.namespaces[name], db.defaults, self.keys.profile, db, self) tercio@0: end tercio@0: end tercio@0: tercio@0: -- Callback: OnDatabaseReset, database tercio@0: self.callbacks:Fire("OnDatabaseReset", self) tercio@0: -- Callback: OnProfileChanged, database, profileKey tercio@0: self.callbacks:Fire("OnProfileChanged", self, self.keys["profile"]) tercio@0: tercio@0: return self tercio@0: end tercio@0: tercio@0: --- Creates a new database namespace, directly tied to the database. This tercio@0: -- is a full scale database in it's own rights other than the fact that tercio@0: -- it cannot control its profile individually tercio@0: -- @param name The name of the new namespace tercio@0: -- @param defaults A table of values to use as defaults tercio@0: function DBObjectLib:RegisterNamespace(name, defaults) tercio@0: if type(name) ~= "string" then tercio@0: error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - string expected.", 2) tercio@0: end tercio@0: if defaults and type(defaults) ~= "table" then tercio@0: error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'defaults' - table or nil expected.", 2) tercio@0: end tercio@0: if self.children and self.children[name] then tercio@0: error ("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - a namespace with that name already exists.", 2) tercio@0: end tercio@0: tercio@0: local sv = self.sv tercio@0: if not sv.namespaces then sv.namespaces = {} end tercio@0: if not sv.namespaces[name] then tercio@0: sv.namespaces[name] = {} tercio@0: end tercio@0: tercio@0: local newDB = initdb(sv.namespaces[name], defaults, self.keys.profile, nil, self) tercio@0: tercio@0: if not self.children then self.children = {} end tercio@0: self.children[name] = newDB tercio@0: return newDB tercio@0: end tercio@0: tercio@0: --- Returns an already existing namespace from the database object. tercio@0: -- @param name The name of the new namespace tercio@0: -- @param silent if true, the addon is optional, silently return nil if its not found tercio@0: -- @usage tercio@0: -- local namespace = self.db:GetNamespace('namespace') tercio@0: -- @return the namespace object if found tercio@0: function DBObjectLib:GetNamespace(name, silent) tercio@0: if type(name) ~= "string" then tercio@0: error("Usage: AceDBObject:GetNamespace(name): 'name' - string expected.", 2) tercio@0: end tercio@0: if not silent and not (self.children and self.children[name]) then tercio@0: error ("Usage: AceDBObject:GetNamespace(name): 'name' - namespace does not exist.", 2) tercio@0: end tercio@0: if not self.children then self.children = {} end tercio@0: return self.children[name] tercio@0: end tercio@0: tercio@0: --[[------------------------------------------------------------------------- tercio@0: AceDB Exposed Methods tercio@0: ---------------------------------------------------------------------------]] tercio@0: tercio@0: --- Creates a new database object that can be used to handle database settings and profiles. tercio@0: -- By default, an empty DB is created, using a character specific profile. tercio@0: -- tercio@0: -- You can override the default profile used by passing any profile name as the third argument, tercio@0: -- or by passing //true// as the third argument to use a globally shared profile called "Default". tercio@0: -- tercio@0: -- Note that there is no token replacement in the default profile name, passing a defaultProfile as "char" tercio@0: -- will use a profile named "char", and not a character-specific profile. tercio@0: -- @param tbl The name of variable, or table to use for the database tercio@0: -- @param defaults A table of database defaults tercio@0: -- @param defaultProfile The name of the default profile. If not set, a character specific profile will be used as the default. tercio@0: -- You can also pass //true// to use a shared global profile called "Default". tercio@0: -- @usage tercio@0: -- -- Create an empty DB using a character-specific default profile. tercio@0: -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB") tercio@0: -- @usage tercio@0: -- -- Create a DB using defaults and using a shared default profile tercio@0: -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true) tercio@0: function AceDB:New(tbl, defaults, defaultProfile) tercio@0: if type(tbl) == "string" then tercio@0: local name = tbl tercio@0: tbl = _G[name] tercio@0: if not tbl then tercio@0: tbl = {} tercio@0: _G[name] = tbl tercio@0: end tercio@0: end tercio@0: tercio@0: if type(tbl) ~= "table" then tercio@0: error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'tbl' - table expected.", 2) tercio@0: end tercio@0: tercio@0: if defaults and type(defaults) ~= "table" then tercio@0: error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaults' - table expected.", 2) tercio@0: end tercio@0: tercio@0: if defaultProfile and type(defaultProfile) ~= "string" and defaultProfile ~= true then tercio@0: error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaultProfile' - string or true expected.", 2) tercio@0: end tercio@0: tercio@0: return initdb(tbl, defaults, defaultProfile) tercio@0: end tercio@0: tercio@0: -- upgrade existing databases tercio@0: for db in pairs(AceDB.db_registry) do tercio@0: if not db.parent then tercio@0: for name,func in pairs(DBObjectLib) do tercio@0: db[name] = func tercio@0: end tercio@0: else tercio@0: db.RegisterDefaults = DBObjectLib.RegisterDefaults tercio@0: db.ResetProfile = DBObjectLib.ResetProfile tercio@0: end tercio@0: end