>@5: --- **LibModuleDBShare-1.0**\\ >@5: -- A description will eventually be here. >@5: -- >@5: -- @usage >@5: -- Also coming soon. >@5: -- @class file >@5: -- @name LibModuleDBShare-1.0.lua >@3: local MAJOR, MINOR = "LibModuleDBShare-1.0", 1 >@3: local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) >@3: >@4: if not LibModuleDBShare then return end -- No upgrade needed >@4: andrewtknoll@15: -- Lua APIs andrewtknoll@21: local assert, type, pairs = assert, type, pairs; andrewtknoll@15: andrewtknoll@17: -- Required Libraries @12: local AceDB = LibStub("AceDB-3.0"); andrewtknoll@17: local AceDBOptions = LibStub("AceDBOptions-3.0"); andrewtknoll@17: local AceConfigRegistry = LibStub("AceConfigRegistry-3.0"); andrewtknoll@17: local AceConfigDialog = LibStub("AceConfigDialog-3.0"); @12: >@4: LibModuleDBShare.groups = LibModuleDBShare.groups or {}; >@4: >@5: local DBGroup = {}; >@5: >@5: --- Creates a new DB group. >@5: -- @param groupName The name of the new DB group. andrewtknoll@21: -- @param groupDescription A description of the group to be shown in the root options panel. andrewtknoll@21: -- @param initialDB The first DB to add to the group. @12: -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI) >@5: -- @usage >@5: -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true) >@5: -- @return the new DB group object andrewtknoll@21: function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) andrewtknoll@21: -- verify parameters andrewtknoll@21: assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string."); andrewtknoll@21: assert(type(groupDescription) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string."); andrewtknoll@22: assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists."); andrewtknoll@21: assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table."); andrewtknoll@21: -- create group @12: local group = {} @12: group.name = groupName; andrewtknoll@21: group.members = {}; andrewtknoll@21: -- create root option panel for group andrewtknoll@17: group.rootOptionsTable = { andrewtknoll@17: type = "group", andrewtknoll@17: name = groupName, andrewtknoll@17: args = { andrewtknoll@17: text = { andrewtknoll@17: type = "description", andrewtknoll@21: name = groupDescription, andrewtknoll@17: }, andrewtknoll@17: }, andrewtknoll@17: }; andrewtknoll@17: AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable); andrewtknoll@17: AceConfigDialog:AddToBlizOptions(groupName); andrewtknoll@21: -- create sync DB and profile options page @12: group.syncDBTable = {}; andrewtknoll@21: group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile()); andrewtknoll@17: group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false); andrewtknoll@17: AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable); andrewtknoll@18: AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName); andrewtknoll@21: -- add all profiles from initialDB to syncDB andrewtknoll@21: for i, profile in pairs(initialDB:GetProfiles()) do andrewtknoll@21: group.syncDB:SetProfile(profile); andrewtknoll@21: end andrewtknoll@21: group.syncDB:SetProfile(initialDB:GetCurrentProfile()); andrewtknoll@21: group.members[initialDB] = true; andrewtknoll@23: if type(initialDB.char.logoutTimestamp) == "number" then andrewtknoll@23: group.profileTimestamp = initialDB.char.logoutTimestamp; andrewtknoll@22: else andrewtknoll@22: group.profileTimestamp = 0; andrewtknoll@22: end andrewtknoll@21: -- add methods and callbacks @12: for k, v in pairs(DBGroup) do @12: group[k] = v; @12: end andrewtknoll@19: group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged"); andrewtknoll@19: group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted"); andrewtknoll@19: group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied"); andrewtknoll@19: group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset"); andrewtknoll@23: initialDB.RegisterCallback(group, "OnDatabaseShutdown", "OnDatabaseShutdown"); andrewtknoll@20: group.squelchCallbacks = false; andrewtknoll@19: LibModuleDBShare.groups[groupName] = group; @12: return group; >@4: end >@4: >@5: --- Retrieves an existing DB group. >@5: -- @param groupName The name of the DB group to retrieve. >@5: -- @usage >@5: -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName") >@5: -- @return the DB group object, or nil if not found >@5: function LibModuleDBShare:GetGroup(groupName) andrewtknoll@22: assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string."); @12: return LibModuleDBShare.groups[groupName]; >@4: end >@5: >@5: --- Adds a database to the group. andrewtknoll@22: -- @param newDB The database to add. >@5: -- @usage >@5: -- myAddonDBGroup:AddDB(MyAddon.db) andrewtknoll@22: function DBGroup:AddDB(newDB) andrewtknoll@22: -- verify parameters andrewtknoll@22: assert(type(newDB) == "table", "Usage: DBGroup:AddDB(newDB): 'newDB' must be a table."); andrewtknoll@22: assert(type(self.members[newDB]) == "nil", "DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup."); andrewtknoll@22: -- record current profile andrewtknoll@20: local syncProfile = self.syncDB:GetCurrentProfile(); andrewtknoll@22: -- add new profiles to syncDB andrewtknoll@20: self.squelchCallbacks = true; andrewtknoll@22: for i, profile in pairs(newDB:GetProfiles()) do andrewtknoll@20: self.syncDB:SetProfile(profile); andrewtknoll@20: end andrewtknoll@22: -- set current profile based on timestamps andrewtknoll@23: if type(newDB.char.logoutTimestamp) == "number" and newDB.char.logoutTimestamp > self.profileTimestamp then andrewtknoll@22: self.squelchCallbacks = false; andrewtknoll@22: self.syncDB:SetProfile(newDB:GetCurrentProfile()); andrewtknoll@22: self.profileTimestamp = newDB.character.logoutTimestamp; andrewtknoll@20: else andrewtknoll@20: self.syncDB:SetProfile(syncProfile); andrewtknoll@22: newDB:SetProfile(syncProfile); andrewtknoll@22: self.squelchCallbacks = false; andrewtknoll@20: end andrewtknoll@22: -- add to members list andrewtknoll@22: self.members[newDB] = true; andrewtknoll@23: newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnDatabaseShutdown"); >@5: end andrewtknoll@18: andrewtknoll@19: -- callback handlers (new profiles are handled by OnProfileChanged) andrewtknoll@19: andrewtknoll@19: function DBGroup:OnProfileChanged(callback, db, profile) andrewtknoll@19: print("Profile Changed"); andrewtknoll@19: print(self.name); andrewtknoll@18: print(type(profile)); andrewtknoll@18: print(tostring(profile)); andrewtknoll@18: end andrewtknoll@18: andrewtknoll@19: function DBGroup:OnProfileDeleted(callback, db, profile) andrewtknoll@19: print("Profile Deleted"); andrewtknoll@19: print(self.name); andrewtknoll@18: print(type(profile)); andrewtknoll@18: print(tostring(profile)); andrewtknoll@18: end andrewtknoll@18: andrewtknoll@19: function DBGroup:OnProfileCopied(callback, db, profile) andrewtknoll@19: print("Profile Copied"); andrewtknoll@19: print(self.name); andrewtknoll@18: print(type(profile)); andrewtknoll@18: print(tostring(profile)); andrewtknoll@18: end andrewtknoll@18: andrewtknoll@19: function DBGroup:OnProfileReset(callback, db) andrewtknoll@19: print("Profile Reset"); andrewtknoll@19: print(self.name); andrewtknoll@18: end andrewtknoll@23: andrewtknoll@23: function DBGroup:OnDatabaseShutdown(callback, db) andrewtknoll@23: print("Database Shutdown"); andrewtknoll@23: end