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