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