annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 32:a63d1129c0d8

Added tag v0.1 beta for changeset ff0e10fd2a5f
author Andrew Knoll <andrewtknoll@gmail.com>
date Mon, 18 Mar 2013 15:29:49 -0400
parents ff0e10fd2a5f
children 3f329c676eac
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@29 13 -- Lua functions
andrewtknoll@26 14 local error, type, pairs, time = error, 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
andrewtknoll@28 22 -- Optional Libraries
andrewtknoll@28 23 local LibDualSpec = LibStub("LibDualSpec-1.0", true);
andrewtknoll@28 24
>@4 25 LibModuleDBShare.groups = LibModuleDBShare.groups or {};
>@4 26
>@5 27 local DBGroup = {};
>@5 28
>@5 29 --- Creates a new DB group.
>@5 30 -- @param groupName The name of the new DB group.
andrewtknoll@21 31 -- @param groupDescription A description of the group to be shown in the root options panel.
andrewtknoll@21 32 -- @param initialDB The first DB to add to the group.
andrewtknoll@28 33 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise.
>@5 34 -- @usage
>@5 35 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
>@5 36 -- @return the new DB group object
andrewtknoll@21 37 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
andrewtknoll@21 38 -- verify parameters
andrewtknoll@26 39 if type(groupName) ~= "string" then
andrewtknoll@26 40 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.", 2);
andrewtknoll@26 41 elseif type(groupDescription) ~= "string" then
andrewtknoll@26 42 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2);
andrewtknoll@26 43 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then
andrewtknoll@31 44 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2);
andrewtknoll@27 45 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initialDB] then
andrewtknoll@31 46 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initialDB' must be an AceDB-3.0 database.", 2);
andrewtknoll@31 47 elseif initialDB.parent then
andrewtknoll@31 48 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initialDB' must not be a namespace.", 2)
andrewtknoll@28 49 elseif type(usesDualSpec) ~= "boolean" and type(usesDualSpec) ~= "nil" then
andrewtknoll@31 50 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' must be a boolean or nil.", 2);
andrewtknoll@28 51 elseif usesDualSpec and not LibDualSpec then
andrewtknoll@31 52 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' cannot be true without LibDualSpec-1.0 installed.", 2);
andrewtknoll@26 53 end
andrewtknoll@21 54 -- create group
@12 55 local group = {}
@12 56 group.name = groupName;
andrewtknoll@21 57 group.members = {};
andrewtknoll@21 58 -- create root option panel for group
andrewtknoll@17 59 group.rootOptionsTable = {
andrewtknoll@17 60 type = "group",
andrewtknoll@17 61 name = groupName,
andrewtknoll@17 62 args = {
andrewtknoll@17 63 text = {
andrewtknoll@17 64 type = "description",
andrewtknoll@21 65 name = groupDescription,
andrewtknoll@17 66 },
andrewtknoll@17 67 },
andrewtknoll@17 68 };
andrewtknoll@17 69 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 70 AceConfigDialog:AddToBlizOptions(groupName);
andrewtknoll@21 71 -- create sync DB and profile options page
@12 72 group.syncDBTable = {};
andrewtknoll@21 73 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
andrewtknoll@17 74 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@28 75 if usesDualSpec then
andrewtknoll@29 76 group.usesDualSpec = true;
andrewtknoll@28 77 LibDualSpec:EnhanceDatabase(group.syncDB, groupName);
andrewtknoll@28 78 LibDualSpec:EnhanceOptions(group.profileOptionsTable, group.syncDB);
andrewtknoll@29 79 else
andrewtknoll@29 80 group.usesDualSpec = false;
andrewtknoll@28 81 end
andrewtknoll@17 82 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 83 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@21 84 -- add all profiles from initialDB to syncDB
andrewtknoll@21 85 for i, profile in pairs(initialDB:GetProfiles()) do
andrewtknoll@21 86 group.syncDB:SetProfile(profile);
andrewtknoll@21 87 end
andrewtknoll@28 88 -- load profile info from initialDB
andrewtknoll@21 89 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
andrewtknoll@27 90 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR);
andrewtknoll@29 91 local storedData = group.members[initialDB].char;
andrewtknoll@29 92 if type(storedData.logoutTimestamp) == "number" then
andrewtknoll@29 93 group.profileTimestamp = storedData.logoutTimestamp;
andrewtknoll@22 94 else
andrewtknoll@22 95 group.profileTimestamp = 0;
andrewtknoll@22 96 end
andrewtknoll@29 97 if usesDualSpec and storedData.altProfile then
andrewtknoll@30 98 namespace = group.syncDB:GetNamespace("LibDualSpec-1.0");
andrewtknoll@30 99 namespace.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@30 100 namespace.char.profile = storedData.altProfile;
andrewtknoll@30 101 namespace.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@30 102 group.syncDB:CheckDualSpecState();
andrewtknoll@28 103 end
andrewtknoll@21 104 -- add methods and callbacks
@12 105 for k, v in pairs(DBGroup) do
@12 106 group[k] = v;
@12 107 end
andrewtknoll@19 108 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 109 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 110 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 111 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@28 112 group.syncDB.RegisterCallback(group, "OnDatabaseShutdown", "OnSyncShutdown");
andrewtknoll@28 113 initialDB.RegisterCallback(group, "OnDatabaseShutdown", "OnMemberShutdown");
andrewtknoll@20 114 group.squelchCallbacks = false;
andrewtknoll@19 115 LibModuleDBShare.groups[groupName] = group;
@12 116 return group;
>@4 117 end
>@4 118
>@5 119 --- Retrieves an existing DB group.
>@5 120 -- @param groupName The name of the DB group to retrieve.
>@5 121 -- @usage
>@5 122 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 123 -- @return the DB group object, or nil if not found
>@5 124 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@27 125 if type(groupName) ~= "string" then
andrewtknoll@27 126 error("Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.", 2);
andrewtknoll@27 127 end
@12 128 return LibModuleDBShare.groups[groupName];
>@4 129 end
>@5 130
>@5 131 --- Adds a database to the group.
andrewtknoll@22 132 -- @param newDB The database to add.
>@5 133 -- @usage
>@5 134 -- myAddonDBGroup:AddDB(MyAddon.db)
andrewtknoll@22 135 function DBGroup:AddDB(newDB)
andrewtknoll@22 136 -- verify parameters
andrewtknoll@26 137 if type(newDB) ~= "table" or not AceDB.db_registry[newDB] then
andrewtknoll@31 138 error("Usage: DBGroup:AddDB(newDB): 'newDB' must be an AceDB-3.0 database.", 2);
andrewtknoll@31 139 elseif newDB.parent then
andrewtknoll@31 140 error("Usage: DBGroup:AddDB(newDB): 'newDB' must not be a namespace.", 2)
andrewtknoll@26 141 elseif type(self.members[newDB]) ~= "nil" then
andrewtknoll@31 142 error("Usage: DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup.", 2);
andrewtknoll@31 143 end
andrewtknoll@31 144 for groupName, group in pairs(LibModuleDBShare.groups) do
andrewtknoll@31 145 if group.members[newDB] ~= nil then
andrewtknoll@31 146 error("Usage: DBGroup:AddDB(newDB): 'newDB' is already a member of group '"..groupName.."'.", 2);
andrewtknoll@31 147 end
andrewtknoll@26 148 end
andrewtknoll@22 149 -- record current profile
andrewtknoll@20 150 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@22 151 -- add new profiles to syncDB
andrewtknoll@20 152 self.squelchCallbacks = true;
andrewtknoll@22 153 for i, profile in pairs(newDB:GetProfiles()) do
andrewtknoll@20 154 self.syncDB:SetProfile(profile);
andrewtknoll@20 155 end
andrewtknoll@22 156 -- set current profile based on timestamps
andrewtknoll@27 157 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR);
andrewtknoll@29 158 local storedData = namespace.char;
andrewtknoll@29 159 if type(storedData.logoutTimestamp) == "number" and storedData.logoutTimestamp > self.profileTimestamp then
andrewtknoll@22 160 self.squelchCallbacks = false;
andrewtknoll@22 161 self.syncDB:SetProfile(newDB:GetCurrentProfile());
andrewtknoll@29 162 self.profileTimestamp = storedData.logoutTimestamp;
andrewtknoll@30 163 if usesDualSpec and storedData.altProfile then
andrewtknoll@30 164 namespace = group.syncDB:GetNamespace("LibDualSpec-1.0");
andrewtknoll@30 165 namespace.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@30 166 namespace.char.profile = storedData.altProfile;
andrewtknoll@30 167 namespace.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@30 168 group.syncDB:CheckDualSpecState();
andrewtknoll@28 169 end
andrewtknoll@20 170 else
andrewtknoll@20 171 self.syncDB:SetProfile(syncProfile);
andrewtknoll@22 172 newDB:SetProfile(syncProfile);
andrewtknoll@22 173 self.squelchCallbacks = false;
andrewtknoll@20 174 end
andrewtknoll@22 175 -- add to members list
andrewtknoll@27 176 self.members[newDB] = namespace;
andrewtknoll@28 177 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnMemberShutdown");
>@5 178 end
andrewtknoll@18 179
andrewtknoll@19 180 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 181
andrewtknoll@24 182 function DBGroup:OnProfileChanged(callback, syncDB, profile)
andrewtknoll@24 183 if not self.squelchCallbacks then
andrewtknoll@24 184 for db, _ in pairs(self.members) do
andrewtknoll@25 185 db:SetProfile(profile);
andrewtknoll@24 186 end
andrewtknoll@24 187 end
andrewtknoll@18 188 end
andrewtknoll@18 189
andrewtknoll@24 190 function DBGroup:OnProfileDeleted(callback, syncDB, profile)
andrewtknoll@24 191 for db, _ in pairs(self.members) do
andrewtknoll@24 192 db:DeleteProfile(profile, true);
andrewtknoll@24 193 end
andrewtknoll@18 194 end
andrewtknoll@18 195
andrewtknoll@24 196 function DBGroup:OnProfileCopied(callback, syncDB, profile)
andrewtknoll@24 197 for db, _ in pairs(self.members) do
andrewtknoll@24 198 db:CopyProfile(profile, true);
andrewtknoll@24 199 end
andrewtknoll@18 200 end
andrewtknoll@18 201
andrewtknoll@24 202 function DBGroup:OnProfileReset(callback, syncDB)
andrewtknoll@24 203 for db, _ in pairs(self.members) do
andrewtknoll@24 204 db:ResetProfile(false, false);
andrewtknoll@24 205 end
andrewtknoll@18 206 end
andrewtknoll@23 207
andrewtknoll@29 208 local altProfile = nil;
andrewtknoll@29 209 local dualSpecEnabled = nil;
andrewtknoll@29 210 local activeSpecGroup = nil;
andrewtknoll@28 211
andrewtknoll@28 212 function DBGroup:OnSyncShutdown(callback, syncDB)
andrewtknoll@29 213 if self.usesDualSpec and not altProfile then
andrewtknoll@29 214 altProfile = syncDB:GetDualSpecProfile();
andrewtknoll@29 215 dualSpecEnabled = syncDB:IsDualSpecEnabled();
andrewtknoll@29 216 activeSpecGroup = GetActiveSpecGroup();
andrewtknoll@28 217 end
andrewtknoll@28 218 end
andrewtknoll@28 219
andrewtknoll@24 220 local timestamp = nil;
andrewtknoll@24 221
andrewtknoll@28 222 function DBGroup:OnMemberShutdown(callback, db)
andrewtknoll@27 223 if not timestamp then -- ensure uniform timestamps to minimize
andrewtknoll@27 224 timestamp = time(); -- calls to SetProfile in NewGroup
andrewtknoll@24 225 end
andrewtknoll@29 226 if self.usesDualSpec then
andrewtknoll@29 227 if not altProfile then
andrewtknoll@29 228 altProfile = self.syncDB:GetDualSpecProfile();
andrewtknoll@29 229 dualSpecEnabled = self.syncDB:IsDualSpecEnabled();
andrewtknoll@29 230 activeSpecGroup = GetActiveSpecGroup();
andrewtknoll@29 231 end
andrewtknoll@29 232 self.members[db].char.logoutTimestamp = timestamp;
andrewtknoll@29 233 self.members[db].char.altProfile = altProfile;
andrewtknoll@29 234 self.members[db].char.dualSpecEnabled = dualSpecEnabled;
andrewtknoll@29 235 self.members[db].char.activeSpecGroup = activeSpecGroup;
andrewtknoll@28 236 end
andrewtknoll@23 237 end