annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 25:e825492d4edd

Removed print statements from callback forwarders. Corrected profile changed forwarder.
author Andrew Knoll <andrewtknoll@gmail.com>
date Fri, 15 Mar 2013 23:33:28 -0400
parents efdeebcef96e
children 4bc47e7b549d
rev   line source
>@5 1 --- **LibModuleDBShare-1.0**\\
>@5 2 -- A description will eventually be here.
>@5 3 --
>@5 4 -- @usage
>@5 5 -- Also coming soon.
>@5 6 -- @class file
>@5 7 -- @name LibModuleDBShare-1.0.lua
>@3 8 local MAJOR, MINOR = "LibModuleDBShare-1.0", 1
>@3 9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
>@3 10
>@4 11 if not LibModuleDBShare then return end -- No upgrade needed
>@4 12
andrewtknoll@15 13 -- Lua APIs
andrewtknoll@24 14 local assert, type, pairs, time = assert, type, pairs, time;
andrewtknoll@15 15
andrewtknoll@17 16 -- Required Libraries
@12 17 local AceDB = LibStub("AceDB-3.0");
andrewtknoll@17 18 local AceDBOptions = LibStub("AceDBOptions-3.0");
andrewtknoll@17 19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
andrewtknoll@17 20 local AceConfigDialog = LibStub("AceConfigDialog-3.0");
@12 21
>@4 22 LibModuleDBShare.groups = LibModuleDBShare.groups or {};
>@4 23
>@5 24 local DBGroup = {};
>@5 25
>@5 26 --- Creates a new DB group.
>@5 27 -- @param groupName The name of the new DB group.
andrewtknoll@21 28 -- @param groupDescription A description of the group to be shown in the root options panel.
andrewtknoll@21 29 -- @param initialDB The first DB to add to the group.
@12 30 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI)
>@5 31 -- @usage
>@5 32 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
>@5 33 -- @return the new DB group object
andrewtknoll@21 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
andrewtknoll@21 35 -- verify parameters
andrewtknoll@21 36 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.");
andrewtknoll@21 37 assert(type(groupDescription) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.");
andrewtknoll@22 38 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.");
andrewtknoll@21 39 assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table.");
andrewtknoll@21 40 -- create group
@12 41 local group = {}
@12 42 group.name = groupName;
andrewtknoll@21 43 group.members = {};
andrewtknoll@21 44 -- create root option panel for group
andrewtknoll@17 45 group.rootOptionsTable = {
andrewtknoll@17 46 type = "group",
andrewtknoll@17 47 name = groupName,
andrewtknoll@17 48 args = {
andrewtknoll@17 49 text = {
andrewtknoll@17 50 type = "description",
andrewtknoll@21 51 name = groupDescription,
andrewtknoll@17 52 },
andrewtknoll@17 53 },
andrewtknoll@17 54 };
andrewtknoll@17 55 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 56 AceConfigDialog:AddToBlizOptions(groupName);
andrewtknoll@21 57 -- create sync DB and profile options page
@12 58 group.syncDBTable = {};
andrewtknoll@21 59 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
andrewtknoll@17 60 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@17 61 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 62 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@21 63 -- add all profiles from initialDB to syncDB
andrewtknoll@21 64 for i, profile in pairs(initialDB:GetProfiles()) do
andrewtknoll@21 65 group.syncDB:SetProfile(profile);
andrewtknoll@21 66 end
andrewtknoll@21 67 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
andrewtknoll@21 68 group.members[initialDB] = true;
andrewtknoll@23 69 if type(initialDB.char.logoutTimestamp) == "number" then
andrewtknoll@23 70 group.profileTimestamp = initialDB.char.logoutTimestamp;
andrewtknoll@22 71 else
andrewtknoll@22 72 group.profileTimestamp = 0;
andrewtknoll@22 73 end
andrewtknoll@21 74 -- add methods and callbacks
@12 75 for k, v in pairs(DBGroup) do
@12 76 group[k] = v;
@12 77 end
andrewtknoll@19 78 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 79 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 80 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 81 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@23 82 initialDB.RegisterCallback(group, "OnDatabaseShutdown", "OnDatabaseShutdown");
andrewtknoll@20 83 group.squelchCallbacks = false;
andrewtknoll@19 84 LibModuleDBShare.groups[groupName] = group;
@12 85 return group;
>@4 86 end
>@4 87
>@5 88 --- Retrieves an existing DB group.
>@5 89 -- @param groupName The name of the DB group to retrieve.
>@5 90 -- @usage
>@5 91 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 92 -- @return the DB group object, or nil if not found
>@5 93 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@22 94 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.");
@12 95 return LibModuleDBShare.groups[groupName];
>@4 96 end
>@5 97
>@5 98 --- Adds a database to the group.
andrewtknoll@22 99 -- @param newDB The database to add.
>@5 100 -- @usage
>@5 101 -- myAddonDBGroup:AddDB(MyAddon.db)
andrewtknoll@22 102 function DBGroup:AddDB(newDB)
andrewtknoll@22 103 -- verify parameters
andrewtknoll@22 104 assert(type(newDB) == "table", "Usage: DBGroup:AddDB(newDB): 'newDB' must be a table.");
andrewtknoll@22 105 assert(type(self.members[newDB]) == "nil", "DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup.");
andrewtknoll@22 106 -- record current profile
andrewtknoll@20 107 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@22 108 -- add new profiles to syncDB
andrewtknoll@20 109 self.squelchCallbacks = true;
andrewtknoll@22 110 for i, profile in pairs(newDB:GetProfiles()) do
andrewtknoll@20 111 self.syncDB:SetProfile(profile);
andrewtknoll@20 112 end
andrewtknoll@22 113 -- set current profile based on timestamps
andrewtknoll@23 114 if type(newDB.char.logoutTimestamp) == "number" and newDB.char.logoutTimestamp > self.profileTimestamp then
andrewtknoll@22 115 self.squelchCallbacks = false;
andrewtknoll@22 116 self.syncDB:SetProfile(newDB:GetCurrentProfile());
andrewtknoll@22 117 self.profileTimestamp = newDB.character.logoutTimestamp;
andrewtknoll@20 118 else
andrewtknoll@20 119 self.syncDB:SetProfile(syncProfile);
andrewtknoll@22 120 newDB:SetProfile(syncProfile);
andrewtknoll@22 121 self.squelchCallbacks = false;
andrewtknoll@20 122 end
andrewtknoll@22 123 -- add to members list
andrewtknoll@22 124 self.members[newDB] = true;
andrewtknoll@23 125 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown");
>@5 126 end
andrewtknoll@18 127
andrewtknoll@19 128 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 129
andrewtknoll@24 130 function DBGroup:OnProfileChanged(callback, syncDB, profile)
andrewtknoll@24 131 if not self.squelchCallbacks then
andrewtknoll@24 132 for db, _ in pairs(self.members) do
andrewtknoll@25 133 db:SetProfile(profile);
andrewtknoll@24 134 end
andrewtknoll@24 135 end
andrewtknoll@18 136 end
andrewtknoll@18 137
andrewtknoll@24 138 function DBGroup:OnProfileDeleted(callback, syncDB, profile)
andrewtknoll@24 139 for db, _ in pairs(self.members) do
andrewtknoll@24 140 db:DeleteProfile(profile, true);
andrewtknoll@24 141 end
andrewtknoll@18 142 end
andrewtknoll@18 143
andrewtknoll@24 144 function DBGroup:OnProfileCopied(callback, syncDB, profile)
andrewtknoll@24 145 for db, _ in pairs(self.members) do
andrewtknoll@24 146 db:CopyProfile(profile, true);
andrewtknoll@24 147 end
andrewtknoll@18 148 end
andrewtknoll@18 149
andrewtknoll@24 150 function DBGroup:OnProfileReset(callback, syncDB)
andrewtknoll@24 151 for db, _ in pairs(self.members) do
andrewtknoll@24 152 db:ResetProfile(false, false);
andrewtknoll@24 153 end
andrewtknoll@18 154 end
andrewtknoll@23 155
andrewtknoll@24 156 local timestamp = nil;
andrewtknoll@24 157
andrewtknoll@23 158 function DBGroup:OnDatabaseShutdown(callback, db)
andrewtknoll@24 159 if not timestamp then -- ensures uniform timestamps so
andrewtknoll@24 160 timestamp = time();
andrewtknoll@24 161 end
andrewtknoll@24 162 db.char.logoutTimestamp = timestamp;
andrewtknoll@23 163 end