annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 22:4f5fba631b99

Corrected some assertions. Corrected initial timestamp handling in LMDBS:NewGroup(). Rewrote DBG:AddDB() to remove first DB handling.
author Andrew Knoll <andrewtknoll@gmail.com>
date Fri, 15 Mar 2013 20:42:26 -0400
parents f2038b9d4d9a
children 91ae8cfc63f2
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@15 13 -- Lua APIs
andrewtknoll@21 14 local assert, type, pairs = assert, type, pairs;
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
>@4 22 LibModuleDBShare.groups = LibModuleDBShare.groups or {};
>@4 23
>@5 24 local DBGroup = {};
>@5 25
>@5 26 --- Creates a new DB group.
>@5 27 -- @param groupName The name of the new DB group.
andrewtknoll@21 28 -- @param groupDescription A description of the group to be shown in the root options panel.
andrewtknoll@21 29 -- @param initialDB The first DB to add to the group.
@12 30 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (NYI)
>@5 31 -- @usage
>@5 32 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", true)
>@5 33 -- @return the new DB group object
andrewtknoll@21 34 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
andrewtknoll@21 35 -- verify parameters
andrewtknoll@21 36 assert(type(groupName) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.");
andrewtknoll@21 37 assert(type(groupDescription) == "string", "Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.");
andrewtknoll@22 38 assert(type(LibModuleDBShare.groups[groupName]) == "nil", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.");
andrewtknoll@21 39 assert(type(initialDB) == "table", "LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initalDB must be a table.");
andrewtknoll@21 40 -- create group
@12 41 local group = {}
@12 42 group.name = groupName;
andrewtknoll@21 43 group.members = {};
andrewtknoll@21 44 -- create root option panel for group
andrewtknoll@17 45 group.rootOptionsTable = {
andrewtknoll@17 46 type = "group",
andrewtknoll@17 47 name = groupName,
andrewtknoll@17 48 args = {
andrewtknoll@17 49 text = {
andrewtknoll@17 50 type = "description",
andrewtknoll@21 51 name = groupDescription,
andrewtknoll@17 52 },
andrewtknoll@17 53 },
andrewtknoll@17 54 };
andrewtknoll@17 55 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 56 AceConfigDialog:AddToBlizOptions(groupName);
andrewtknoll@21 57 -- create sync DB and profile options page
@12 58 group.syncDBTable = {};
andrewtknoll@21 59 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
andrewtknoll@17 60 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@17 61 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 62 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@21 63 -- add all profiles from initialDB to syncDB
andrewtknoll@21 64 for i, profile in pairs(initialDB:GetProfiles()) do
andrewtknoll@21 65 group.syncDB:SetProfile(profile);
andrewtknoll@21 66 end
andrewtknoll@21 67 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
andrewtknoll@21 68 group.members[initialDB] = true;
andrewtknoll@22 69 if type(initialDB.character.logoutTimestamp) == "number" then
andrewtknoll@22 70 group.profileTimestamp = initialDB.character.logoutTimestamp;
andrewtknoll@22 71 else
andrewtknoll@22 72 group.profileTimestamp = 0;
andrewtknoll@22 73 end
andrewtknoll@21 74 -- add methods and callbacks
@12 75 for k, v in pairs(DBGroup) do
@12 76 group[k] = v;
@12 77 end
andrewtknoll@19 78 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 79 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 80 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 81 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@20 82 group.squelchCallbacks = false;
andrewtknoll@19 83 LibModuleDBShare.groups[groupName] = group;
@12 84 return group;
>@4 85 end
>@4 86
>@5 87 --- Retrieves an existing DB group.
>@5 88 -- @param groupName The name of the DB group to retrieve.
>@5 89 -- @usage
>@5 90 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName")
>@5 91 -- @return the DB group object, or nil if not found
>@5 92 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@22 93 assert(type(groupName) == "string", "Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.");
@12 94 return LibModuleDBShare.groups[groupName];
>@4 95 end
>@5 96
>@5 97 --- Adds a database to the group.
andrewtknoll@22 98 -- @param newDB The database to add.
>@5 99 -- @usage
>@5 100 -- myAddonDBGroup:AddDB(MyAddon.db)
andrewtknoll@22 101 function DBGroup:AddDB(newDB)
andrewtknoll@22 102 -- verify parameters
andrewtknoll@22 103 assert(type(newDB) == "table", "Usage: DBGroup:AddDB(newDB): 'newDB' must be a table.");
andrewtknoll@22 104 assert(type(self.members[newDB]) == "nil", "DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup.");
andrewtknoll@22 105 -- record current profile
andrewtknoll@20 106 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@22 107 -- add new profiles to syncDB
andrewtknoll@20 108 self.squelchCallbacks = true;
andrewtknoll@22 109 for i, profile in pairs(newDB:GetProfiles()) do
andrewtknoll@20 110 self.syncDB:SetProfile(profile);
andrewtknoll@20 111 end
andrewtknoll@22 112 -- set current profile based on timestamps
andrewtknoll@22 113 if type(newDB.character.logoutTimestamp) == "number" and newDB.character.logoutTimestamp > self.profileTimestamp then
andrewtknoll@22 114 self.squelchCallbacks = false;
andrewtknoll@22 115 self.syncDB:SetProfile(newDB:GetCurrentProfile());
andrewtknoll@22 116 self.profileTimestamp = newDB.character.logoutTimestamp;
andrewtknoll@20 117 else
andrewtknoll@20 118 self.syncDB:SetProfile(syncProfile);
andrewtknoll@22 119 newDB:SetProfile(syncProfile);
andrewtknoll@22 120 self.squelchCallbacks = false;
andrewtknoll@20 121 end
andrewtknoll@22 122 -- add to members list
andrewtknoll@22 123 self.members[newDB] = true;
>@5 124 end
andrewtknoll@18 125
andrewtknoll@19 126 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 127
andrewtknoll@19 128 function DBGroup:OnProfileChanged(callback, db, profile)
andrewtknoll@19 129 print("Profile Changed");
andrewtknoll@19 130 print(self.name);
andrewtknoll@18 131 print(type(profile));
andrewtknoll@18 132 print(tostring(profile));
andrewtknoll@18 133 end
andrewtknoll@18 134
andrewtknoll@19 135 function DBGroup:OnProfileDeleted(callback, db, profile)
andrewtknoll@19 136 print("Profile Deleted");
andrewtknoll@19 137 print(self.name);
andrewtknoll@18 138 print(type(profile));
andrewtknoll@18 139 print(tostring(profile));
andrewtknoll@18 140 end
andrewtknoll@18 141
andrewtknoll@19 142 function DBGroup:OnProfileCopied(callback, db, profile)
andrewtknoll@19 143 print("Profile Copied");
andrewtknoll@19 144 print(self.name);
andrewtknoll@18 145 print(type(profile));
andrewtknoll@18 146 print(tostring(profile));
andrewtknoll@18 147 end
andrewtknoll@18 148
andrewtknoll@19 149 function DBGroup:OnProfileReset(callback, db)
andrewtknoll@19 150 print("Profile Reset");
andrewtknoll@19 151 print(self.name);
andrewtknoll@18 152 end