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