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