comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 29:d8dd617017de

Rewrote LibDualSpec support to use public APIs instead of directly accessing data.
author Andrew Knoll <andrewtknoll@gmail.com>
date Sun, 17 Mar 2013 03:05:25 -0400
parents 085d93d62782
children 5c7143d32287
comparison
equal deleted inserted replaced
28:085d93d62782 29:d8dd617017de
8 local MAJOR, MINOR = "LibModuleDBShare-1.0", 1 8 local MAJOR, MINOR = "LibModuleDBShare-1.0", 1
9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) 9 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
10 10
11 if not LibModuleDBShare then return end -- No upgrade needed 11 if not LibModuleDBShare then return end -- No upgrade needed
12 12
13 -- Lua APIs 13 -- Lua functions
14 local error, type, pairs, time = error, type, pairs, time; 14 local error, type, pairs, time = error, type, pairs, time;
15
16 -- Blizzard functions
17 local GetActiveSpecGroup = GetActiveSpecGroup;
15 18
16 -- Required Libraries 19 -- Required Libraries
17 local AceDB = LibStub("AceDB-3.0"); 20 local AceDB = LibStub("AceDB-3.0");
18 local AceDBOptions = LibStub("AceDBOptions-3.0"); 21 local AceDBOptions = LibStub("AceDBOptions-3.0");
19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0"); 22 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
69 -- create sync DB and profile options page 72 -- create sync DB and profile options page
70 group.syncDBTable = {}; 73 group.syncDBTable = {};
71 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile()); 74 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
72 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false); 75 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
73 if usesDualSpec then 76 if usesDualSpec then
77 group.usesDualSpec = true;
74 LibDualSpec:EnhanceDatabase(group.syncDB, groupName); 78 LibDualSpec:EnhanceDatabase(group.syncDB, groupName);
75 LibDualSpec:EnhanceOptions(group.profileOptionsTable, group.syncDB); 79 LibDualSpec:EnhanceOptions(group.profileOptionsTable, group.syncDB);
80 else
81 group.usesDualSpec = false;
76 end 82 end
77 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable); 83 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
78 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName); 84 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
79 -- add all profiles from initialDB to syncDB 85 -- add all profiles from initialDB to syncDB
80 for i, profile in pairs(initialDB:GetProfiles()) do 86 for i, profile in pairs(initialDB:GetProfiles()) do
81 group.syncDB:SetProfile(profile); 87 group.syncDB:SetProfile(profile);
82 end 88 end
83 -- load profile info from initialDB 89 -- load profile info from initialDB
84 group.syncDB:SetProfile(initialDB:GetCurrentProfile()); 90 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
85 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR); 91 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR);
86 if type(group.members[initialDB].char.logoutTimestamp) == "number" then 92 local storedData = group.members[initialDB].char;
87 group.profileTimestamp = group.members[initialDB].char.logoutTimestamp; 93 if type(storedData.logoutTimestamp) == "number" then
94 group.profileTimestamp = storedData.logoutTimestamp;
88 else 95 else
89 group.profileTimestamp = 0; 96 group.profileTimestamp = 0;
90 end 97 end
91 if usesDualSpec then 98 if usesDualSpec and storedData.altProfile then
92 local dualSpecNamespace = group.syncDB:GetNamespace("LibDualSpec-1.0"); 99 group.syncDB:SetDualSpecProfile(storedData.altProfile);
93 dualSpecNamespace.char.profile = group.members[initialDB].char.profile; 100 group.syncDB:SetDualSpecEnabled(storedData.dualSpecEnabled);
94 dualSpecNamespace.char.enabled = group.members[initialDB].char.enabled; 101 if storedData.dualSpecEnabled and storedData.activeSpecGroup ~= GetActiveSpecGroup() then
95 dualSpecNamespace.char.specGroup = group.members[initialDB].char.specGroup; 102 group.syncDB:SetDualSpecProfile(group.syncDB:GetCurrentProfile());
96 group.syncDB:CheckDualSpecState(); 103 group.syncDB:SetProfile(storedData.altProfile);
104 initialDB:SetProfile(storedData.altProfile);
105 end
97 end 106 end
98 -- add methods and callbacks 107 -- add methods and callbacks
99 for k, v in pairs(DBGroup) do 108 for k, v in pairs(DBGroup) do
100 group[k] = v; 109 group[k] = v;
101 end 110 end
140 for i, profile in pairs(newDB:GetProfiles()) do 149 for i, profile in pairs(newDB:GetProfiles()) do
141 self.syncDB:SetProfile(profile); 150 self.syncDB:SetProfile(profile);
142 end 151 end
143 -- set current profile based on timestamps 152 -- set current profile based on timestamps
144 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR); 153 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR);
145 if type(namespace.char.logoutTimestamp) == "number" and namespace.char.logoutTimestamp > self.profileTimestamp then 154 local storedData = namespace.char;
155 if type(storedData.logoutTimestamp) == "number" and storedData.logoutTimestamp > self.profileTimestamp then
146 self.squelchCallbacks = false; 156 self.squelchCallbacks = false;
147 self.syncDB:SetProfile(newDB:GetCurrentProfile()); 157 self.syncDB:SetProfile(newDB:GetCurrentProfile());
148 self.profileTimestamp = namespace.character.logoutTimestamp; 158 self.profileTimestamp = storedData.logoutTimestamp;
149 local dualSpecNamespace = self.syncDB:GetNamespace("LibDualSpec-1.0", true); 159 if self.usesDualSpec and storedData.altProfile then
150 if dualSpecNamespace then 160 self.syncDB:SetDualSpecProfile(storedData.altProfile);
151 dualSpecNamespace.char.profile = namespace.char.profile; 161 self.syncDB:SetDualSpecEnabled(storedData.dualSpecEnabled);
152 dualSpecNamespace.char.enabled = namespace.char.enabled; 162 if storedData.dualSpecEnabled and storedData.activeSpecGroup ~= GetActiveSpecGroup() then
153 dualSpecNamespace.char.specGroup = namespace.char.specGroup; 163 self.syncDB:SetDualSpecProfile(self.syncDB:GetCurrentProfile());
154 group.syncDB:CheckDualSpecState(); 164 self.syncDB:SetProfile(storedData.altProfile);
165 newDB:SetProfile(storedData.altProfile);
166 end
155 end 167 end
156 else 168 else
157 self.syncDB:SetProfile(syncProfile); 169 self.syncDB:SetProfile(syncProfile);
158 newDB:SetProfile(syncProfile); 170 newDB:SetProfile(syncProfile);
159 self.squelchCallbacks = false; 171 self.squelchCallbacks = false;
189 for db, _ in pairs(self.members) do 201 for db, _ in pairs(self.members) do
190 db:ResetProfile(false, false); 202 db:ResetProfile(false, false);
191 end 203 end
192 end 204 end
193 205
194 local profile = nil; 206 local altProfile = nil;
195 local enabled = nil; 207 local dualSpecEnabled = nil;
196 local specGroup = nil; 208 local activeSpecGroup = nil;
197 209
198 function DBGroup:OnSyncShutdown(callback, syncDB) 210 function DBGroup:OnSyncShutdown(callback, syncDB)
199 if not profile then 211 if self.usesDualSpec and not altProfile then
200 local dualSpecNamespace = syncDB:GetNamespace("LibDualSpec-1.0"); 212 altProfile = syncDB:GetDualSpecProfile();
201 profile = dualSpecNamespace.char.profile; 213 dualSpecEnabled = syncDB:IsDualSpecEnabled();
202 enabled = dualSpecNamespace.char.enabled; 214 activeSpecGroup = GetActiveSpecGroup();
203 specGroup = dualSpecNamespace.char.specGroup;
204 end 215 end
205 end 216 end
206 217
207 local timestamp = nil; 218 local timestamp = nil;
208 219
209 function DBGroup:OnMemberShutdown(callback, db) 220 function DBGroup:OnMemberShutdown(callback, db)
210 if not timestamp then -- ensure uniform timestamps to minimize 221 if not timestamp then -- ensure uniform timestamps to minimize
211 timestamp = time(); -- calls to SetProfile in NewGroup 222 timestamp = time(); -- calls to SetProfile in NewGroup
212 end 223 end
213 if not profile then 224 if self.usesDualSpec then
214 local dualSpecNamespace = syncDB:GetNamespace("LibDualSpec-1.0"); 225 if not altProfile then
215 profile = dualSpecNamespace.char.profile; 226 altProfile = self.syncDB:GetDualSpecProfile();
216 enabled = dualSpecNamespace.char.enabled; 227 dualSpecEnabled = self.syncDB:IsDualSpecEnabled();
217 specGroup = dualSpecNamespace.char.specGroup; 228 activeSpecGroup = GetActiveSpecGroup();
218 end 229 end
219 self.members[db].char.logoutTimestamp = timestamp; 230 self.members[db].char.logoutTimestamp = timestamp;
220 self.members[db].char.profile = profile; 231 self.members[db].char.altProfile = altProfile;
221 self.members[db].char.enabled = enabled; 232 self.members[db].char.dualSpecEnabled = dualSpecEnabled;
222 self.members[db].char.specGroup = specGroup; 233 self.members[db].char.activeSpecGroup = activeSpecGroup;
223 end 234 end
235 end