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