Mercurial > wow > libmoduledbshare-1-0
comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 28:085d93d62782
Added support for LibDualSpec.
| author | Andrew Knoll <andrewtknoll@gmail.com> |
|---|---|
| date | Sun, 17 Mar 2013 01:56:31 -0400 |
| parents | 0739db0c99ac |
| children | d8dd617017de |
comparison
equal
deleted
inserted
replaced
| 27:0739db0c99ac | 28:085d93d62782 |
|---|---|
| 17 local AceDB = LibStub("AceDB-3.0"); | 17 local AceDB = LibStub("AceDB-3.0"); |
| 18 local AceDBOptions = LibStub("AceDBOptions-3.0"); | 18 local AceDBOptions = LibStub("AceDBOptions-3.0"); |
| 19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0"); | 19 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0"); |
| 20 local AceConfigDialog = LibStub("AceConfigDialog-3.0"); | 20 local AceConfigDialog = LibStub("AceConfigDialog-3.0"); |
| 21 | 21 |
| 22 -- Optional Libraries | |
| 23 local LibDualSpec = LibStub("LibDualSpec-1.0", true); | |
| 24 | |
| 22 LibModuleDBShare.groups = LibModuleDBShare.groups or {}; | 25 LibModuleDBShare.groups = LibModuleDBShare.groups or {}; |
| 23 | 26 |
| 24 local DBGroup = {}; | 27 local DBGroup = {}; |
| 25 | 28 |
| 26 --- Creates a new DB group. | 29 --- Creates a new DB group. |
| 27 -- @param groupName The name of the new DB group. | 30 -- @param groupName The name of the new DB group. |
| 28 -- @param groupDescription A description of the group to be shown in the root options panel. | 31 -- @param groupDescription A description of the group to be shown in the root options panel. |
| 29 -- @param initialDB The first DB to add to the group. | 32 -- @param initialDB The first DB to add to the group. |
| 30 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI) | 33 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. |
| 31 -- @usage | 34 -- @usage |
| 32 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true) | 35 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true) |
| 33 -- @return the new DB group object | 36 -- @return the new DB group object |
| 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) | 37 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) |
| 35 -- verify parameters | 38 -- verify parameters |
| 39 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2); | 42 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2); |
| 40 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then | 43 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then |
| 41 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2); | 44 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2); |
| 42 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initialDB] then | 45 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initialDB] then |
| 43 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB' must be an AceDB-3.0 database.", 2); | 46 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB' must be an AceDB-3.0 database.", 2); |
| 47 elseif type(usesDualSpec) ~= "boolean" and type(usesDualSpec) ~= "nil" then | |
| 48 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' must be a boolean or nil.", 2); | |
| 49 elseif usesDualSpec and not LibDualSpec then | |
| 50 error("LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' cannot be true without LibDualSpec-1.0 installed.", 2); | |
| 44 end | 51 end |
| 45 -- create group | 52 -- create group |
| 46 local group = {} | 53 local group = {} |
| 47 group.name = groupName; | 54 group.name = groupName; |
| 48 group.members = {}; | 55 group.members = {}; |
| 61 AceConfigDialog:AddToBlizOptions(groupName); | 68 AceConfigDialog:AddToBlizOptions(groupName); |
| 62 -- create sync DB and profile options page | 69 -- create sync DB and profile options page |
| 63 group.syncDBTable = {}; | 70 group.syncDBTable = {}; |
| 64 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile()); | 71 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile()); |
| 65 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false); | 72 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false); |
| 73 if usesDualSpec then | |
| 74 LibDualSpec:EnhanceDatabase(group.syncDB, groupName); | |
| 75 LibDualSpec:EnhanceOptions(group.profileOptionsTable, group.syncDB); | |
| 76 end | |
| 66 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable); | 77 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable); |
| 67 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName); | 78 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName); |
| 68 -- add all profiles from initialDB to syncDB | 79 -- add all profiles from initialDB to syncDB |
| 69 for i, profile in pairs(initialDB:GetProfiles()) do | 80 for i, profile in pairs(initialDB:GetProfiles()) do |
| 70 group.syncDB:SetProfile(profile); | 81 group.syncDB:SetProfile(profile); |
| 71 end | 82 end |
| 83 -- load profile info from initialDB | |
| 72 group.syncDB:SetProfile(initialDB:GetCurrentProfile()); | 84 group.syncDB:SetProfile(initialDB:GetCurrentProfile()); |
| 73 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR); | 85 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR); |
| 74 if type(group.members[initialDB].char.logoutTimestamp) == "number" then | 86 if type(group.members[initialDB].char.logoutTimestamp) == "number" then |
| 75 group.profileTimestamp = group.members[initialDB].char.logoutTimestamp; | 87 group.profileTimestamp = group.members[initialDB].char.logoutTimestamp; |
| 76 else | 88 else |
| 77 group.profileTimestamp = 0; | 89 group.profileTimestamp = 0; |
| 78 end | 90 end |
| 91 if usesDualSpec then | |
| 92 local dualSpecNamespace = group.syncDB:GetNamespace("LibDualSpec-1.0"); | |
| 93 dualSpecNamespace.char.profile = group.members[initialDB].char.profile; | |
| 94 dualSpecNamespace.char.enabled = group.members[initialDB].char.enabled; | |
| 95 dualSpecNamespace.char.specGroup = group.members[initialDB].char.specGroup; | |
| 96 group.syncDB:CheckDualSpecState(); | |
| 97 end | |
| 79 -- add methods and callbacks | 98 -- add methods and callbacks |
| 80 for k, v in pairs(DBGroup) do | 99 for k, v in pairs(DBGroup) do |
| 81 group[k] = v; | 100 group[k] = v; |
| 82 end | 101 end |
| 83 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged"); | 102 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged"); |
| 84 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted"); | 103 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted"); |
| 85 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied"); | 104 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied"); |
| 86 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset"); | 105 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset"); |
| 87 initialDB.RegisterCallback(group, "OnDatabaseShutdown", "OnDatabaseShutdown"); | 106 group.syncDB.RegisterCallback(group, "OnDatabaseShutdown", "OnSyncShutdown"); |
| 107 initialDB.RegisterCallback(group, "OnDatabaseShutdown", "OnMemberShutdown"); | |
| 88 group.squelchCallbacks = false; | 108 group.squelchCallbacks = false; |
| 89 LibModuleDBShare.groups[groupName] = group; | 109 LibModuleDBShare.groups[groupName] = group; |
| 90 return group; | 110 return group; |
| 91 end | 111 end |
| 92 | 112 |
| 124 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR); | 144 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR); |
| 125 if type(namespace.char.logoutTimestamp) == "number" and namespace.char.logoutTimestamp > self.profileTimestamp then | 145 if type(namespace.char.logoutTimestamp) == "number" and namespace.char.logoutTimestamp > self.profileTimestamp then |
| 126 self.squelchCallbacks = false; | 146 self.squelchCallbacks = false; |
| 127 self.syncDB:SetProfile(newDB:GetCurrentProfile()); | 147 self.syncDB:SetProfile(newDB:GetCurrentProfile()); |
| 128 self.profileTimestamp = namespace.character.logoutTimestamp; | 148 self.profileTimestamp = namespace.character.logoutTimestamp; |
| 149 local dualSpecNamespace = self.syncDB:GetNamespace("LibDualSpec-1.0", true); | |
| 150 if dualSpecNamespace then | |
| 151 dualSpecNamespace.char.profile = namespace.char.profile; | |
| 152 dualSpecNamespace.char.enabled = namespace.char.enabled; | |
| 153 dualSpecNamespace.char.specGroup = namespace.char.specGroup; | |
| 154 group.syncDB:CheckDualSpecState(); | |
| 155 end | |
| 129 else | 156 else |
| 130 self.syncDB:SetProfile(syncProfile); | 157 self.syncDB:SetProfile(syncProfile); |
| 131 newDB:SetProfile(syncProfile); | 158 newDB:SetProfile(syncProfile); |
| 132 self.squelchCallbacks = false; | 159 self.squelchCallbacks = false; |
| 133 end | 160 end |
| 134 -- add to members list | 161 -- add to members list |
| 135 self.members[newDB] = namespace; | 162 self.members[newDB] = namespace; |
| 136 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown"); | 163 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnMemberShutdown"); |
| 137 end | 164 end |
| 138 | 165 |
| 139 -- callback handlers (new profiles are handled by OnProfileChanged) | 166 -- callback handlers (new profiles are handled by OnProfileChanged) |
| 140 | 167 |
| 141 function DBGroup:OnProfileChanged(callback, syncDB, profile) | 168 function DBGroup:OnProfileChanged(callback, syncDB, profile) |
| 162 for db, _ in pairs(self.members) do | 189 for db, _ in pairs(self.members) do |
| 163 db:ResetProfile(false, false); | 190 db:ResetProfile(false, false); |
| 164 end | 191 end |
| 165 end | 192 end |
| 166 | 193 |
| 194 local profile = nil; | |
| 195 local enabled = nil; | |
| 196 local specGroup = nil; | |
| 197 | |
| 198 function DBGroup:OnSyncShutdown(callback, syncDB) | |
| 199 if not profile then | |
| 200 local dualSpecNamespace = syncDB:GetNamespace("LibDualSpec-1.0"); | |
| 201 profile = dualSpecNamespace.char.profile; | |
| 202 enabled = dualSpecNamespace.char.enabled; | |
| 203 specGroup = dualSpecNamespace.char.specGroup; | |
| 204 end | |
| 205 end | |
| 206 | |
| 167 local timestamp = nil; | 207 local timestamp = nil; |
| 168 | 208 |
| 169 function DBGroup:OnDatabaseShutdown(callback, db) | 209 function DBGroup:OnMemberShutdown(callback, db) |
| 170 if not timestamp then -- ensure uniform timestamps to minimize | 210 if not timestamp then -- ensure uniform timestamps to minimize |
| 171 timestamp = time(); -- calls to SetProfile in NewGroup | 211 timestamp = time(); -- calls to SetProfile in NewGroup |
| 172 end | 212 end |
| 213 if not profile then | |
| 214 local dualSpecNamespace = syncDB:GetNamespace("LibDualSpec-1.0"); | |
| 215 profile = dualSpecNamespace.char.profile; | |
| 216 enabled = dualSpecNamespace.char.enabled; | |
| 217 specGroup = dualSpecNamespace.char.specGroup; | |
| 218 end | |
| 173 self.members[db].char.logoutTimestamp = timestamp; | 219 self.members[db].char.logoutTimestamp = timestamp; |
| 174 end | 220 self.members[db].char.profile = profile; |
| 221 self.members[db].char.enabled = enabled; | |
| 222 self.members[db].char.specGroup = specGroup; | |
| 223 end |
