Mercurial > wow > itemauditor
comparison Libs/AceDB-3.0/AceDB-3.0.lua @ 0:169f5211fc7f
First public revision.
At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
| author | Asa Ayers <Asa.Ayers@Gmail.com> |
|---|---|
| date | Thu, 20 May 2010 19:22:19 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:169f5211fc7f |
|---|---|
| 1 --- **AceDB-3.0** manages the SavedVariables of your addon. | |
| 2 -- It offers profile management, smart defaults and namespaces for modules.\\ | |
| 3 -- Data can be saved in different data-types, depending on its intended usage. | |
| 4 -- The most common data-type is the `profile` type, which allows the user to choose | |
| 5 -- the active profile, and manage the profiles of all of his characters.\\ | |
| 6 -- The following data types are available: | |
| 7 -- * **char** Character-specific data. Every character has its own database. | |
| 8 -- * **realm** Realm-specific data. All of the players characters on the same realm share this database. | |
| 9 -- * **class** Class-specific data. All of the players characters of the same class share this database. | |
| 10 -- * **race** Race-specific data. All of the players characters of the same race share this database. | |
| 11 -- * **faction** Faction-specific data. All of the players characters of the same faction share this database. | |
| 12 -- * **factionrealm** Faction and realm specific data. All of the players characters on the same realm and of the same faction share this database. | |
| 13 -- * **global** Global Data. All characters on the same account share this database. | |
| 14 -- * **profile** Profile-specific data. All characters using the same profile share this database. The user can control which profile should be used. | |
| 15 -- | |
| 16 -- Creating a new Database using the `:New` function will return a new DBObject. A database will inherit all functions | |
| 17 -- of the DBObjectLib listed here. \\ | |
| 18 -- If you create a new namespaced child-database (`:RegisterNamespace`), you'll get a DBObject as well, but note | |
| 19 -- that the child-databases cannot individually change their profile, and are linked to their parents profile - and because of that, | |
| 20 -- the profile related APIs are not available. Only `:RegisterDefaults` and `:ResetProfile` are available on child-databases. | |
| 21 -- | |
| 22 -- For more details on how to use AceDB-3.0, see the [[AceDB-3.0 Tutorial]]. | |
| 23 -- | |
| 24 -- You may also be interested in [[libdualspec-1-0|LibDualSpec-1.0]] to do profile switching automatically when switching specs. | |
| 25 -- | |
| 26 -- @usage | |
| 27 -- MyAddon = LibStub("AceAddon-3.0"):NewAddon("DBExample") | |
| 28 -- | |
| 29 -- -- declare defaults to be used in the DB | |
| 30 -- local defaults = { | |
| 31 -- profile = { | |
| 32 -- setting = true, | |
| 33 -- } | |
| 34 -- } | |
| 35 -- | |
| 36 -- function MyAddon:OnInitialize() | |
| 37 -- -- Assuming the .toc says ## SavedVariables: MyAddonDB | |
| 38 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true) | |
| 39 -- end | |
| 40 -- @class file | |
| 41 -- @name AceDB-3.0.lua | |
| 42 -- @release $Id: AceDB-3.0.lua 877 2009-11-02 15:56:50Z nevcairiel $ | |
| 43 local ACEDB_MAJOR, ACEDB_MINOR = "AceDB-3.0", 19 | |
| 44 local AceDB, oldminor = LibStub:NewLibrary(ACEDB_MAJOR, ACEDB_MINOR) | |
| 45 | |
| 46 if not AceDB then return end -- No upgrade needed | |
| 47 | |
| 48 -- Lua APIs | |
| 49 local type, pairs, next, error = type, pairs, next, error | |
| 50 local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget | |
| 51 | |
| 52 -- WoW APIs | |
| 53 local _G = _G | |
| 54 | |
| 55 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded | |
| 56 -- List them here for Mikk's FindGlobals script | |
| 57 -- GLOBALS: LibStub | |
| 58 | |
| 59 AceDB.db_registry = AceDB.db_registry or {} | |
| 60 AceDB.frame = AceDB.frame or CreateFrame("Frame") | |
| 61 | |
| 62 local CallbackHandler | |
| 63 local CallbackDummy = { Fire = function() end } | |
| 64 | |
| 65 local DBObjectLib = {} | |
| 66 | |
| 67 --[[------------------------------------------------------------------------- | |
| 68 AceDB Utility Functions | |
| 69 ---------------------------------------------------------------------------]] | |
| 70 | |
| 71 -- Simple shallow copy for copying defaults | |
| 72 local function copyTable(src, dest) | |
| 73 if type(dest) ~= "table" then dest = {} end | |
| 74 if type(src) == "table" then | |
| 75 for k,v in pairs(src) do | |
| 76 if type(v) == "table" then | |
| 77 -- try to index the key first so that the metatable creates the defaults, if set, and use that table | |
| 78 v = copyTable(v, dest[k]) | |
| 79 end | |
| 80 dest[k] = v | |
| 81 end | |
| 82 end | |
| 83 return dest | |
| 84 end | |
| 85 | |
| 86 -- Called to add defaults to a section of the database | |
| 87 -- | |
| 88 -- When a ["*"] default section is indexed with a new key, a table is returned | |
| 89 -- and set in the host table. These tables must be cleaned up by removeDefaults | |
| 90 -- in order to ensure we don't write empty default tables. | |
| 91 local function copyDefaults(dest, src) | |
| 92 -- this happens if some value in the SV overwrites our default value with a non-table | |
| 93 --if type(dest) ~= "table" then return end | |
| 94 for k, v in pairs(src) do | |
| 95 if k == "*" or k == "**" then | |
| 96 if type(v) == "table" then | |
| 97 -- This is a metatable used for table defaults | |
| 98 local mt = { | |
| 99 -- This handles the lookup and creation of new subtables | |
| 100 __index = function(t,k) | |
| 101 if k == nil then return nil end | |
| 102 local tbl = {} | |
| 103 copyDefaults(tbl, v) | |
| 104 rawset(t, k, tbl) | |
| 105 return tbl | |
| 106 end, | |
| 107 } | |
| 108 setmetatable(dest, mt) | |
| 109 -- handle already existing tables in the SV | |
| 110 for dk, dv in pairs(dest) do | |
| 111 if not rawget(src, dk) and type(dv) == "table" then | |
| 112 copyDefaults(dv, v) | |
| 113 end | |
| 114 end | |
| 115 else | |
| 116 -- Values are not tables, so this is just a simple return | |
| 117 local mt = {__index = function(t,k) return k~=nil and v or nil end} | |
| 118 setmetatable(dest, mt) | |
| 119 end | |
| 120 elseif type(v) == "table" then | |
| 121 if not rawget(dest, k) then rawset(dest, k, {}) end | |
| 122 if type(dest[k]) == "table" then | |
| 123 copyDefaults(dest[k], v) | |
| 124 if src['**'] then | |
| 125 copyDefaults(dest[k], src['**']) | |
| 126 end | |
| 127 end | |
| 128 else | |
| 129 if rawget(dest, k) == nil then | |
| 130 rawset(dest, k, v) | |
| 131 end | |
| 132 end | |
| 133 end | |
| 134 end | |
| 135 | |
| 136 -- Called to remove all defaults in the default table from the database | |
| 137 local function removeDefaults(db, defaults, blocker) | |
| 138 -- remove all metatables from the db, so we don't accidentally create new sub-tables through them | |
| 139 setmetatable(db, nil) | |
| 140 -- loop through the defaults and remove their content | |
| 141 for k,v in pairs(defaults) do | |
| 142 if k == "*" or k == "**" then | |
| 143 if type(v) == "table" then | |
| 144 -- Loop through all the actual k,v pairs and remove | |
| 145 for key, value in pairs(db) do | |
| 146 if type(value) == "table" then | |
| 147 -- if the key was not explicitly specified in the defaults table, just strip everything from * and ** tables | |
| 148 if defaults[key] == nil and (not blocker or blocker[key] == nil) then | |
| 149 removeDefaults(value, v) | |
| 150 -- if the table is empty afterwards, remove it | |
| 151 if next(value) == nil then | |
| 152 db[key] = nil | |
| 153 end | |
| 154 -- if it was specified, only strip ** content, but block values which were set in the key table | |
| 155 elseif k == "**" then | |
| 156 removeDefaults(value, v, defaults[key]) | |
| 157 end | |
| 158 end | |
| 159 end | |
| 160 elseif k == "*" then | |
| 161 -- check for non-table default | |
| 162 for key, value in pairs(db) do | |
| 163 if defaults[key] == nil and v == value then | |
| 164 db[key] = nil | |
| 165 end | |
| 166 end | |
| 167 end | |
| 168 elseif type(v) == "table" and type(db[k]) == "table" then | |
| 169 -- if a blocker was set, dive into it, to allow multi-level defaults | |
| 170 removeDefaults(db[k], v, blocker and blocker[k]) | |
| 171 if next(db[k]) == nil then | |
| 172 db[k] = nil | |
| 173 end | |
| 174 else | |
| 175 -- check if the current value matches the default, and that its not blocked by another defaults table | |
| 176 if db[k] == defaults[k] and (not blocker or blocker[k] == nil) then | |
| 177 db[k] = nil | |
| 178 end | |
| 179 end | |
| 180 end | |
| 181 end | |
| 182 | |
| 183 -- This is called when a table section is first accessed, to set up the defaults | |
| 184 local function initSection(db, section, svstore, key, defaults) | |
| 185 local sv = rawget(db, "sv") | |
| 186 | |
| 187 local tableCreated | |
| 188 if not sv[svstore] then sv[svstore] = {} end | |
| 189 if not sv[svstore][key] then | |
| 190 sv[svstore][key] = {} | |
| 191 tableCreated = true | |
| 192 end | |
| 193 | |
| 194 local tbl = sv[svstore][key] | |
| 195 | |
| 196 if defaults then | |
| 197 copyDefaults(tbl, defaults) | |
| 198 end | |
| 199 rawset(db, section, tbl) | |
| 200 | |
| 201 return tableCreated, tbl | |
| 202 end | |
| 203 | |
| 204 -- Metatable to handle the dynamic creation of sections and copying of sections. | |
| 205 local dbmt = { | |
| 206 __index = function(t, section) | |
| 207 local keys = rawget(t, "keys") | |
| 208 local key = keys[section] | |
| 209 if key then | |
| 210 local defaultTbl = rawget(t, "defaults") | |
| 211 local defaults = defaultTbl and defaultTbl[section] | |
| 212 | |
| 213 if section == "profile" then | |
| 214 local new = initSection(t, section, "profiles", key, defaults) | |
| 215 if new then | |
| 216 -- Callback: OnNewProfile, database, newProfileKey | |
| 217 t.callbacks:Fire("OnNewProfile", t, key) | |
| 218 end | |
| 219 elseif section == "profiles" then | |
| 220 local sv = rawget(t, "sv") | |
| 221 if not sv.profiles then sv.profiles = {} end | |
| 222 rawset(t, "profiles", sv.profiles) | |
| 223 elseif section == "global" then | |
| 224 local sv = rawget(t, "sv") | |
| 225 if not sv.global then sv.global = {} end | |
| 226 if defaults then | |
| 227 copyDefaults(sv.global, defaults) | |
| 228 end | |
| 229 rawset(t, section, sv.global) | |
| 230 else | |
| 231 initSection(t, section, section, key, defaults) | |
| 232 end | |
| 233 end | |
| 234 | |
| 235 return rawget(t, section) | |
| 236 end | |
| 237 } | |
| 238 | |
| 239 local function validateDefaults(defaults, keyTbl, offset) | |
| 240 if not defaults then return end | |
| 241 offset = offset or 0 | |
| 242 for k in pairs(defaults) do | |
| 243 if not keyTbl[k] or k == "profiles" then | |
| 244 error(("Usage: AceDBObject:RegisterDefaults(defaults): '%s' is not a valid datatype."):format(k), 3 + offset) | |
| 245 end | |
| 246 end | |
| 247 end | |
| 248 | |
| 249 local preserve_keys = { | |
| 250 ["callbacks"] = true, | |
| 251 ["RegisterCallback"] = true, | |
| 252 ["UnregisterCallback"] = true, | |
| 253 ["UnregisterAllCallbacks"] = true, | |
| 254 ["children"] = true, | |
| 255 } | |
| 256 | |
| 257 local realmKey = GetRealmName() | |
| 258 local charKey = UnitName("player") .. " - " .. realmKey | |
| 259 local _, classKey = UnitClass("player") | |
| 260 local _, raceKey = UnitRace("player") | |
| 261 local factionKey = UnitFactionGroup("player") | |
| 262 local factionrealmKey = factionKey .. " - " .. realmKey | |
| 263 -- Actual database initialization function | |
| 264 local function initdb(sv, defaults, defaultProfile, olddb, parent) | |
| 265 -- Generate the database keys for each section | |
| 266 | |
| 267 -- map "true" to our "Default" profile | |
| 268 if defaultProfile == true then defaultProfile = "Default" end | |
| 269 | |
| 270 local profileKey | |
| 271 if not parent then | |
| 272 -- Make a container for profile keys | |
| 273 if not sv.profileKeys then sv.profileKeys = {} end | |
| 274 | |
| 275 -- Try to get the profile selected from the char db | |
| 276 profileKey = sv.profileKeys[charKey] or defaultProfile or charKey | |
| 277 | |
| 278 -- save the selected profile for later | |
| 279 sv.profileKeys[charKey] = profileKey | |
| 280 else | |
| 281 -- Use the profile of the parents DB | |
| 282 profileKey = parent.keys.profile or defaultProfile or charKey | |
| 283 | |
| 284 -- clear the profileKeys in the DB, namespaces don't need to store them | |
| 285 sv.profileKeys = nil | |
| 286 end | |
| 287 | |
| 288 -- This table contains keys that enable the dynamic creation | |
| 289 -- of each section of the table. The 'global' and 'profiles' | |
| 290 -- have a key of true, since they are handled in a special case | |
| 291 local keyTbl= { | |
| 292 ["char"] = charKey, | |
| 293 ["realm"] = realmKey, | |
| 294 ["class"] = classKey, | |
| 295 ["race"] = raceKey, | |
| 296 ["faction"] = factionKey, | |
| 297 ["factionrealm"] = factionrealmKey, | |
| 298 ["profile"] = profileKey, | |
| 299 ["global"] = true, | |
| 300 ["profiles"] = true, | |
| 301 } | |
| 302 | |
| 303 validateDefaults(defaults, keyTbl, 1) | |
| 304 | |
| 305 -- This allows us to use this function to reset an entire database | |
| 306 -- Clear out the old database | |
| 307 if olddb then | |
| 308 for k,v in pairs(olddb) do if not preserve_keys[k] then olddb[k] = nil end end | |
| 309 end | |
| 310 | |
| 311 -- Give this database the metatable so it initializes dynamically | |
| 312 local db = setmetatable(olddb or {}, dbmt) | |
| 313 | |
| 314 if not rawget(db, "callbacks") then | |
| 315 -- try to load CallbackHandler-1.0 if it loaded after our library | |
| 316 if not CallbackHandler then CallbackHandler = LibStub:GetLibrary("CallbackHandler-1.0", true) end | |
| 317 db.callbacks = CallbackHandler and CallbackHandler:New(db) or CallbackDummy | |
| 318 end | |
| 319 | |
| 320 -- Copy methods locally into the database object, to avoid hitting | |
| 321 -- the metatable when calling methods | |
| 322 | |
| 323 if not parent then | |
| 324 for name, func in pairs(DBObjectLib) do | |
| 325 db[name] = func | |
| 326 end | |
| 327 else | |
| 328 -- hack this one in | |
| 329 db.RegisterDefaults = DBObjectLib.RegisterDefaults | |
| 330 db.ResetProfile = DBObjectLib.ResetProfile | |
| 331 end | |
| 332 | |
| 333 -- Set some properties in the database object | |
| 334 db.profiles = sv.profiles | |
| 335 db.keys = keyTbl | |
| 336 db.sv = sv | |
| 337 --db.sv_name = name | |
| 338 db.defaults = defaults | |
| 339 db.parent = parent | |
| 340 | |
| 341 -- store the DB in the registry | |
| 342 AceDB.db_registry[db] = true | |
| 343 | |
| 344 return db | |
| 345 end | |
| 346 | |
| 347 -- handle PLAYER_LOGOUT | |
| 348 -- strip all defaults from all databases | |
| 349 local function logoutHandler(frame, event) | |
| 350 if event == "PLAYER_LOGOUT" then | |
| 351 for db in pairs(AceDB.db_registry) do | |
| 352 db.callbacks:Fire("OnDatabaseShutdown", db) | |
| 353 for section, key in pairs(db.keys) do | |
| 354 if db.defaults and db.defaults[section] and rawget(db, section) then | |
| 355 removeDefaults(db[section], db.defaults[section]) | |
| 356 end | |
| 357 end | |
| 358 end | |
| 359 end | |
| 360 end | |
| 361 | |
| 362 AceDB.frame:RegisterEvent("PLAYER_LOGOUT") | |
| 363 AceDB.frame:SetScript("OnEvent", logoutHandler) | |
| 364 | |
| 365 | |
| 366 --[[------------------------------------------------------------------------- | |
| 367 AceDB Object Method Definitions | |
| 368 ---------------------------------------------------------------------------]] | |
| 369 | |
| 370 --- Sets the defaults table for the given database object by clearing any | |
| 371 -- that are currently set, and then setting the new defaults. | |
| 372 -- @param defaults A table of defaults for this database | |
| 373 function DBObjectLib:RegisterDefaults(defaults) | |
| 374 if defaults and type(defaults) ~= "table" then | |
| 375 error("Usage: AceDBObject:RegisterDefaults(defaults): 'defaults' - table or nil expected.", 2) | |
| 376 end | |
| 377 | |
| 378 validateDefaults(defaults, self.keys) | |
| 379 | |
| 380 -- Remove any currently set defaults | |
| 381 if self.defaults then | |
| 382 for section,key in pairs(self.keys) do | |
| 383 if self.defaults[section] and rawget(self, section) then | |
| 384 removeDefaults(self[section], self.defaults[section]) | |
| 385 end | |
| 386 end | |
| 387 end | |
| 388 | |
| 389 -- Set the DBObject.defaults table | |
| 390 self.defaults = defaults | |
| 391 | |
| 392 -- Copy in any defaults, only touching those sections already created | |
| 393 if defaults then | |
| 394 for section,key in pairs(self.keys) do | |
| 395 if defaults[section] and rawget(self, section) then | |
| 396 copyDefaults(self[section], defaults[section]) | |
| 397 end | |
| 398 end | |
| 399 end | |
| 400 end | |
| 401 | |
| 402 --- Changes the profile of the database and all of it's namespaces to the | |
| 403 -- supplied named profile | |
| 404 -- @param name The name of the profile to set as the current profile | |
| 405 function DBObjectLib:SetProfile(name) | |
| 406 if type(name) ~= "string" then | |
| 407 error("Usage: AceDBObject:SetProfile(name): 'name' - string expected.", 2) | |
| 408 end | |
| 409 | |
| 410 -- changing to the same profile, dont do anything | |
| 411 if name == self.keys.profile then return end | |
| 412 | |
| 413 local oldProfile = self.profile | |
| 414 local defaults = self.defaults and self.defaults.profile | |
| 415 | |
| 416 -- Callback: OnProfileShutdown, database | |
| 417 self.callbacks:Fire("OnProfileShutdown", self) | |
| 418 | |
| 419 if oldProfile and defaults then | |
| 420 -- Remove the defaults from the old profile | |
| 421 removeDefaults(oldProfile, defaults) | |
| 422 end | |
| 423 | |
| 424 self.profile = nil | |
| 425 self.keys["profile"] = name | |
| 426 | |
| 427 -- if the storage exists, save the new profile | |
| 428 -- this won't exist on namespaces. | |
| 429 if self.sv.profileKeys then | |
| 430 self.sv.profileKeys[charKey] = name | |
| 431 end | |
| 432 | |
| 433 -- populate to child namespaces | |
| 434 if self.children then | |
| 435 for _, db in pairs(self.children) do | |
| 436 DBObjectLib.SetProfile(db, name) | |
| 437 end | |
| 438 end | |
| 439 | |
| 440 -- Callback: OnProfileChanged, database, newProfileKey | |
| 441 self.callbacks:Fire("OnProfileChanged", self, name) | |
| 442 end | |
| 443 | |
| 444 --- Returns a table with the names of the existing profiles in the database. | |
| 445 -- You can optionally supply a table to re-use for this purpose. | |
| 446 -- @param tbl A table to store the profile names in (optional) | |
| 447 function DBObjectLib:GetProfiles(tbl) | |
| 448 if tbl and type(tbl) ~= "table" then | |
| 449 error("Usage: AceDBObject:GetProfiles(tbl): 'tbl' - table or nil expected.", 2) | |
| 450 end | |
| 451 | |
| 452 -- Clear the container table | |
| 453 if tbl then | |
| 454 for k,v in pairs(tbl) do tbl[k] = nil end | |
| 455 else | |
| 456 tbl = {} | |
| 457 end | |
| 458 | |
| 459 local curProfile = self.keys.profile | |
| 460 | |
| 461 local i = 0 | |
| 462 for profileKey in pairs(self.profiles) do | |
| 463 i = i + 1 | |
| 464 tbl[i] = profileKey | |
| 465 if curProfile and profileKey == curProfile then curProfile = nil end | |
| 466 end | |
| 467 | |
| 468 -- Add the current profile, if it hasn't been created yet | |
| 469 if curProfile then | |
| 470 i = i + 1 | |
| 471 tbl[i] = curProfile | |
| 472 end | |
| 473 | |
| 474 return tbl, i | |
| 475 end | |
| 476 | |
| 477 --- Returns the current profile name used by the database | |
| 478 function DBObjectLib:GetCurrentProfile() | |
| 479 return self.keys.profile | |
| 480 end | |
| 481 | |
| 482 --- Deletes a named profile. This profile must not be the active profile. | |
| 483 -- @param name The name of the profile to be deleted | |
| 484 -- @param silent If true, do not raise an error when the profile does not exist | |
| 485 function DBObjectLib:DeleteProfile(name, silent) | |
| 486 if type(name) ~= "string" then | |
| 487 error("Usage: AceDBObject:DeleteProfile(name): 'name' - string expected.", 2) | |
| 488 end | |
| 489 | |
| 490 if self.keys.profile == name then | |
| 491 error("Cannot delete the active profile in an AceDBObject.", 2) | |
| 492 end | |
| 493 | |
| 494 if not rawget(self.sv.profiles, name) and not silent then | |
| 495 error("Cannot delete profile '" .. name .. "'. It does not exist.", 2) | |
| 496 end | |
| 497 | |
| 498 self.sv.profiles[name] = nil | |
| 499 | |
| 500 -- populate to child namespaces | |
| 501 if self.children then | |
| 502 for _, db in pairs(self.children) do | |
| 503 DBObjectLib.DeleteProfile(db, name, true) | |
| 504 end | |
| 505 end | |
| 506 | |
| 507 -- Callback: OnProfileDeleted, database, profileKey | |
| 508 self.callbacks:Fire("OnProfileDeleted", self, name) | |
| 509 end | |
| 510 | |
| 511 --- Copies a named profile into the current profile, overwriting any conflicting | |
| 512 -- settings. | |
| 513 -- @param name The name of the profile to be copied into the current profile | |
| 514 -- @param silent If true, do not raise an error when the profile does not exist | |
| 515 function DBObjectLib:CopyProfile(name, silent) | |
| 516 if type(name) ~= "string" then | |
| 517 error("Usage: AceDBObject:CopyProfile(name): 'name' - string expected.", 2) | |
| 518 end | |
| 519 | |
| 520 if name == self.keys.profile then | |
| 521 error("Cannot have the same source and destination profiles.", 2) | |
| 522 end | |
| 523 | |
| 524 if not rawget(self.sv.profiles, name) and not silent then | |
| 525 error("Cannot copy profile '" .. name .. "'. It does not exist.", 2) | |
| 526 end | |
| 527 | |
| 528 -- Reset the profile before copying | |
| 529 DBObjectLib.ResetProfile(self, nil, true) | |
| 530 | |
| 531 local profile = self.profile | |
| 532 local source = self.sv.profiles[name] | |
| 533 | |
| 534 copyTable(source, profile) | |
| 535 | |
| 536 -- populate to child namespaces | |
| 537 if self.children then | |
| 538 for _, db in pairs(self.children) do | |
| 539 DBObjectLib.CopyProfile(db, name, true) | |
| 540 end | |
| 541 end | |
| 542 | |
| 543 -- Callback: OnProfileCopied, database, sourceProfileKey | |
| 544 self.callbacks:Fire("OnProfileCopied", self, name) | |
| 545 end | |
| 546 | |
| 547 --- Resets the current profile to the default values (if specified). | |
| 548 -- @param noChildren if set to true, the reset will not be populated to the child namespaces of this DB object | |
| 549 -- @param noCallbacks if set to true, won't fire the OnProfileReset callback | |
| 550 function DBObjectLib:ResetProfile(noChildren, noCallbacks) | |
| 551 local profile = self.profile | |
| 552 | |
| 553 for k,v in pairs(profile) do | |
| 554 profile[k] = nil | |
| 555 end | |
| 556 | |
| 557 local defaults = self.defaults and self.defaults.profile | |
| 558 if defaults then | |
| 559 copyDefaults(profile, defaults) | |
| 560 end | |
| 561 | |
| 562 -- populate to child namespaces | |
| 563 if self.children and not noChildren then | |
| 564 for _, db in pairs(self.children) do | |
| 565 DBObjectLib.ResetProfile(db, nil, noCallbacks) | |
| 566 end | |
| 567 end | |
| 568 | |
| 569 -- Callback: OnProfileReset, database | |
| 570 if not noCallbacks then | |
| 571 self.callbacks:Fire("OnProfileReset", self) | |
| 572 end | |
| 573 end | |
| 574 | |
| 575 --- Resets the entire database, using the string defaultProfile as the new default | |
| 576 -- profile. | |
| 577 -- @param defaultProfile The profile name to use as the default | |
| 578 function DBObjectLib:ResetDB(defaultProfile) | |
| 579 if defaultProfile and type(defaultProfile) ~= "string" then | |
| 580 error("Usage: AceDBObject:ResetDB(defaultProfile): 'defaultProfile' - string or nil expected.", 2) | |
| 581 end | |
| 582 | |
| 583 local sv = self.sv | |
| 584 for k,v in pairs(sv) do | |
| 585 sv[k] = nil | |
| 586 end | |
| 587 | |
| 588 local parent = self.parent | |
| 589 | |
| 590 initdb(sv, self.defaults, defaultProfile, self) | |
| 591 | |
| 592 -- fix the child namespaces | |
| 593 if self.children then | |
| 594 if not sv.namespaces then sv.namespaces = {} end | |
| 595 for name, db in pairs(self.children) do | |
| 596 if not sv.namespaces[name] then sv.namespaces[name] = {} end | |
| 597 initdb(sv.namespaces[name], db.defaults, self.keys.profile, db, self) | |
| 598 end | |
| 599 end | |
| 600 | |
| 601 -- Callback: OnDatabaseReset, database | |
| 602 self.callbacks:Fire("OnDatabaseReset", self) | |
| 603 -- Callback: OnProfileChanged, database, profileKey | |
| 604 self.callbacks:Fire("OnProfileChanged", self, self.keys["profile"]) | |
| 605 | |
| 606 return self | |
| 607 end | |
| 608 | |
| 609 --- Creates a new database namespace, directly tied to the database. This | |
| 610 -- is a full scale database in it's own rights other than the fact that | |
| 611 -- it cannot control its profile individually | |
| 612 -- @param name The name of the new namespace | |
| 613 -- @param defaults A table of values to use as defaults | |
| 614 function DBObjectLib:RegisterNamespace(name, defaults) | |
| 615 if type(name) ~= "string" then | |
| 616 error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - string expected.", 2) | |
| 617 end | |
| 618 if defaults and type(defaults) ~= "table" then | |
| 619 error("Usage: AceDBObject:RegisterNamespace(name, defaults): 'defaults' - table or nil expected.", 2) | |
| 620 end | |
| 621 if self.children and self.children[name] then | |
| 622 error ("Usage: AceDBObject:RegisterNamespace(name, defaults): 'name' - a namespace with that name already exists.", 2) | |
| 623 end | |
| 624 | |
| 625 local sv = self.sv | |
| 626 if not sv.namespaces then sv.namespaces = {} end | |
| 627 if not sv.namespaces[name] then | |
| 628 sv.namespaces[name] = {} | |
| 629 end | |
| 630 | |
| 631 local newDB = initdb(sv.namespaces[name], defaults, self.keys.profile, nil, self) | |
| 632 | |
| 633 if not self.children then self.children = {} end | |
| 634 self.children[name] = newDB | |
| 635 return newDB | |
| 636 end | |
| 637 | |
| 638 --- Returns an already existing namespace from the database object. | |
| 639 -- @param name The name of the new namespace | |
| 640 -- @param silent if true, the addon is optional, silently return nil if its not found | |
| 641 -- @usage | |
| 642 -- local namespace = self.db:GetNamespace('namespace') | |
| 643 -- @return the namespace object if found | |
| 644 function DBObjectLib:GetNamespace(name, silent) | |
| 645 if type(name) ~= "string" then | |
| 646 error("Usage: AceDBObject:GetNamespace(name): 'name' - string expected.", 2) | |
| 647 end | |
| 648 if not silent and not (self.children and self.children[name]) then | |
| 649 error ("Usage: AceDBObject:GetNamespace(name): 'name' - namespace does not exist.", 2) | |
| 650 end | |
| 651 if not self.children then self.children = {} end | |
| 652 return self.children[name] | |
| 653 end | |
| 654 | |
| 655 --[[------------------------------------------------------------------------- | |
| 656 AceDB Exposed Methods | |
| 657 ---------------------------------------------------------------------------]] | |
| 658 | |
| 659 --- Creates a new database object that can be used to handle database settings and profiles. | |
| 660 -- By default, an empty DB is created, using a character specific profile. | |
| 661 -- | |
| 662 -- You can override the default profile used by passing any profile name as the third argument, | |
| 663 -- or by passing //true// as the third argument to use a globally shared profile called "Default". | |
| 664 -- | |
| 665 -- Note that there is no token replacement in the default profile name, passing a defaultProfile as "char" | |
| 666 -- will use a profile named "char", and not a character-specific profile. | |
| 667 -- @param tbl The name of variable, or table to use for the database | |
| 668 -- @param defaults A table of database defaults | |
| 669 -- @param defaultProfile The name of the default profile. If not set, a character specific profile will be used as the default. | |
| 670 -- You can also pass //true// to use a shared global profile called "Default". | |
| 671 -- @usage | |
| 672 -- -- Create an empty DB using a character-specific default profile. | |
| 673 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB") | |
| 674 -- @usage | |
| 675 -- -- Create a DB using defaults and using a shared default profile | |
| 676 -- self.db = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true) | |
| 677 function AceDB:New(tbl, defaults, defaultProfile) | |
| 678 if type(tbl) == "string" then | |
| 679 local name = tbl | |
| 680 tbl = _G[name] | |
| 681 if not tbl then | |
| 682 tbl = {} | |
| 683 _G[name] = tbl | |
| 684 end | |
| 685 end | |
| 686 | |
| 687 if type(tbl) ~= "table" then | |
| 688 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'tbl' - table expected.", 2) | |
| 689 end | |
| 690 | |
| 691 if defaults and type(defaults) ~= "table" then | |
| 692 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaults' - table expected.", 2) | |
| 693 end | |
| 694 | |
| 695 if defaultProfile and type(defaultProfile) ~= "string" and defaultProfile ~= true then | |
| 696 error("Usage: AceDB:New(tbl, defaults, defaultProfile): 'defaultProfile' - string or true expected.", 2) | |
| 697 end | |
| 698 | |
| 699 return initdb(tbl, defaults, defaultProfile) | |
| 700 end | |
| 701 | |
| 702 -- upgrade existing databases | |
| 703 for db in pairs(AceDB.db_registry) do | |
| 704 if not db.parent then | |
| 705 for name,func in pairs(DBObjectLib) do | |
| 706 db[name] = func | |
| 707 end | |
| 708 else | |
| 709 db.RegisterDefaults = DBObjectLib.RegisterDefaults | |
| 710 db.ResetProfile = DBObjectLib.ResetProfile | |
| 711 end | |
| 712 end |
