annotate LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 42:71f3aad48c72

Corrected errors in parameter checks for :AddSecondaryCommand()
author Andrew Knoll <andrewtknoll@gmail.com>
date Wed, 10 Apr 2013 15:12:43 -0400
parents 047d80e6aadc
children 288987af9e66
rev   line source
andrewtknoll@33 1 --- **LibModuleDBShare-1.0** provides a shared profile manager for addons without a central core.
andrewtknoll@33 2 -- A basic options panel for the group is added to the Blizzard options panel, as well as a
andrewtknoll@33 3 -- standard profile manager as a subpanel. Changes through the profiles panel are propagated
andrewtknoll@33 4 -- to member databases. The root panel can be used as a parent for your module config panels,
andrewtknoll@33 5 -- to keep all your addon's config in one place. The root panel's name is the same as the group's
andrewtknoll@33 6 -- name.\\
andrewtknoll@33 7 -- \\
andrewtknoll@35 8 -- A group can be created using the ':NewGroup' library method. The returned object inherits all
andrewtknoll@35 9 -- methods of the DBGroup object described below.\\
andrewtknoll@33 10 -- \\
andrewtknoll@33 11 -- **LibDualSpec Support**\\
andrewtknoll@33 12 -- LibModuleDBShare can use LibDualSpec to manage automatic profile switching with talent spec
andrewtknoll@33 13 -- changes. This integration is handled by the library; there is no need to use LibDualSpec
andrewtknoll@40 14 -- on member databases directly.\\
andrewtknoll@40 15 -- \\
andrewtknoll@40 16 -- **Slash Command Support**\\
andrewtknoll@40 17 -- LibModuleDBShare can associate a slash command with a DBGroup. The default handler function
andrewtknoll@40 18 -- for the slash command opens the root options panel.\\
andrewtknoll@40 19 -- Additional handler functions can be registered to respond to specific arguments given to the
andrewtknoll@40 20 -- slash command.
>@5 21 --
>@5 22 -- @usage
andrewtknoll@33 23 -- local database;
andrewtknoll@33 24 -- -- this function is called after the ADDON_LOADED event fires
andrewtknoll@33 25 -- function initializeDB()
andrewtknoll@33 26 -- database = LibStub("AceDB-3.0"):New("MyAddonDB", defaults, true);
andrewtknoll@33 27 -- local group = LibStub("LibModuleDBShare-1.0"):GetGroup("Group Name");
andrewtknoll@33 28 -- if not group then
andrewtknoll@33 29 -- group = LibStub("LibModuleDBShare-1.0"):NewGroup("Group Name", "A description for this group.", database);
andrewtknoll@33 30 -- else
andrewtknoll@33 31 -- group:AddDB(database);
andrewtknoll@33 32 -- end
andrewtknoll@40 33 -- -- if you want to add a slash command
andrewtknoll@40 34 -- if not group:HasSlashCommand() then
andrewtknoll@40 35 -- group:EnableSlashCommand("COMMAND_NAME", "/groupname");
andrewtknoll@40 36 -- end
andrewtknoll@33 37 -- end
>@5 38 -- @class file
andrewtknoll@33 39 -- @name LibModuleDBShare-1.0
andrewtknoll@39 40 local MAJOR, MINOR = "LibModuleDBShare-1.0", 5
>@3 41 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
>@3 42
>@4 43 if not LibModuleDBShare then return end -- No upgrade needed
>@4 44
andrewtknoll@29 45 -- Lua functions
andrewtknoll@26 46 local error, type, pairs, time = error, type, pairs, time;
andrewtknoll@15 47
andrewtknoll@17 48 -- Required Libraries
@12 49 local AceDB = LibStub("AceDB-3.0");
andrewtknoll@17 50 local AceDBOptions = LibStub("AceDBOptions-3.0");
andrewtknoll@17 51 local AceConfigRegistry = LibStub("AceConfigRegistry-3.0");
andrewtknoll@17 52 local AceConfigDialog = LibStub("AceConfigDialog-3.0");
@12 53
andrewtknoll@28 54 -- Optional Libraries
andrewtknoll@28 55 local LibDualSpec = LibStub("LibDualSpec-1.0", true);
andrewtknoll@28 56
>@4 57 LibModuleDBShare.groups = LibModuleDBShare.groups or {};
>@4 58
>@5 59 local DBGroup = {};
>@5 60
>@5 61 --- Creates a new DB group.
andrewtknoll@42 62 -- @paramsig groupName, groupDescription, initialDB[, usesDualSpec]
andrewtknoll@40 63 -- @param groupName The name of the new DB group, as shown in the options panel. (string)
andrewtknoll@40 64 -- @param groupDescription A description of the group to be shown in the root options panel. (string)
andrewtknoll@40 65 -- @param initialDB The first DB to add to the group. (table)
andrewtknoll@40 66 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (boolean or nil)
>@5 67 -- @return the new DB group object
andrewtknoll@21 68 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec)
andrewtknoll@35 69 -- check to see if LibDualSpec has been loaded
andrewtknoll@35 70 if not LibDualSpec then
andrewtknoll@35 71 LibDualSpec = LibStub("LibDualSpec-1.0", true);
andrewtknoll@35 72 end
andrewtknoll@21 73 -- verify parameters
andrewtknoll@26 74 if type(groupName) ~= "string" then
andrewtknoll@26 75 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupName' must be a string.", 2);
andrewtknoll@26 76 elseif type(groupDescription) ~= "string" then
andrewtknoll@26 77 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'groupDescription' must be a string.", 2);
andrewtknoll@26 78 elseif type(LibModuleDBShare.groups[groupName]) ~= "nil" then
andrewtknoll@31 79 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): group '"..groupName.."' already exists.", 2);
andrewtknoll@27 80 elseif type(initialDB) ~= "table" or not AceDB.db_registry[initialDB] then
andrewtknoll@31 81 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initialDB' must be an AceDB-3.0 database.", 2);
andrewtknoll@31 82 elseif initialDB.parent then
andrewtknoll@31 83 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'initialDB' must not be a namespace.", 2)
andrewtknoll@28 84 elseif type(usesDualSpec) ~= "boolean" and type(usesDualSpec) ~= "nil" then
andrewtknoll@31 85 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' must be a boolean or nil.", 2);
andrewtknoll@28 86 elseif usesDualSpec and not LibDualSpec then
andrewtknoll@31 87 error("Usage: LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec): 'usesDualSpec' cannot be true without LibDualSpec-1.0 installed.", 2);
andrewtknoll@26 88 end
andrewtknoll@21 89 -- create group
@12 90 local group = {}
@12 91 group.name = groupName;
andrewtknoll@21 92 group.members = {};
andrewtknoll@21 93 -- create root option panel for group
andrewtknoll@17 94 group.rootOptionsTable = {
andrewtknoll@17 95 type = "group",
andrewtknoll@17 96 name = groupName,
andrewtknoll@17 97 args = {
andrewtknoll@17 98 text = {
andrewtknoll@17 99 type = "description",
andrewtknoll@21 100 name = groupDescription,
andrewtknoll@17 101 },
andrewtknoll@17 102 },
andrewtknoll@17 103 };
andrewtknoll@17 104 AceConfigRegistry:RegisterOptionsTable(groupName, group.rootOptionsTable);
andrewtknoll@17 105 AceConfigDialog:AddToBlizOptions(groupName);
andrewtknoll@21 106 -- create sync DB and profile options page
@12 107 group.syncDBTable = {};
andrewtknoll@21 108 group.syncDB = AceDB:New(group.syncDBTable, nil, initialDB:GetCurrentProfile());
andrewtknoll@17 109 group.profileOptionsTable = AceDBOptions:GetOptionsTable(group.syncDB, false);
andrewtknoll@28 110 if usesDualSpec then
andrewtknoll@29 111 group.usesDualSpec = true;
andrewtknoll@28 112 LibDualSpec:EnhanceDatabase(group.syncDB, groupName);
andrewtknoll@28 113 LibDualSpec:EnhanceOptions(group.profileOptionsTable, group.syncDB);
andrewtknoll@29 114 else
andrewtknoll@29 115 group.usesDualSpec = false;
andrewtknoll@28 116 end
andrewtknoll@17 117 AceConfigRegistry:RegisterOptionsTable(groupName.."Profiles", group.profileOptionsTable);
andrewtknoll@18 118 AceConfigDialog:AddToBlizOptions(groupName.."Profiles", group.profileOptionsTable.name, groupName);
andrewtknoll@21 119 -- add all profiles from initialDB to syncDB
andrewtknoll@21 120 for i, profile in pairs(initialDB:GetProfiles()) do
andrewtknoll@21 121 group.syncDB:SetProfile(profile);
andrewtknoll@21 122 end
andrewtknoll@28 123 -- load profile info from initialDB
andrewtknoll@21 124 group.syncDB:SetProfile(initialDB:GetCurrentProfile());
andrewtknoll@27 125 group.members[initialDB] = initialDB:GetNamespace(MAJOR, true) or initialDB:RegisterNamespace(MAJOR);
andrewtknoll@29 126 local storedData = group.members[initialDB].char;
andrewtknoll@29 127 if type(storedData.logoutTimestamp) == "number" then
andrewtknoll@29 128 group.profileTimestamp = storedData.logoutTimestamp;
andrewtknoll@22 129 else
andrewtknoll@22 130 group.profileTimestamp = 0;
andrewtknoll@22 131 end
andrewtknoll@35 132 if usesDualSpec then
andrewtknoll@35 133 local LDSnamespace = group.syncDB:GetNamespace("LibDualSpec-1.0");
andrewtknoll@35 134 LDSnamespace.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@35 135 LDSnamespace.char.profile = storedData.altProfile;
andrewtknoll@35 136 LDSnamespace.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@30 137 group.syncDB:CheckDualSpecState();
andrewtknoll@35 138 else
andrewtknoll@35 139 group.syncDB.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@35 140 group.syncDB.char.profile = storedData.altProfile;
andrewtknoll@35 141 group.syncDB.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@28 142 end
andrewtknoll@21 143 -- add methods and callbacks
@12 144 for k, v in pairs(DBGroup) do
@12 145 group[k] = v;
@12 146 end
andrewtknoll@19 147 group.syncDB.RegisterCallback(group, "OnProfileChanged", "OnProfileChanged");
andrewtknoll@19 148 group.syncDB.RegisterCallback(group, "OnProfileDeleted", "OnProfileDeleted");
andrewtknoll@19 149 group.syncDB.RegisterCallback(group, "OnProfileCopied", "OnProfileCopied");
andrewtknoll@19 150 group.syncDB.RegisterCallback(group, "OnProfileReset", "OnProfileReset");
andrewtknoll@28 151 group.syncDB.RegisterCallback(group, "OnDatabaseShutdown", "OnSyncShutdown");
andrewtknoll@41 152 group.members[initialDB].RegisterCallback(group, "OnDatabaseShutdown", "OnMemberShutdown"); -- register the namespace, not the base db
andrewtknoll@20 153 group.squelchCallbacks = false;
andrewtknoll@19 154 LibModuleDBShare.groups[groupName] = group;
@12 155 return group;
>@4 156 end
>@4 157
>@5 158 --- Retrieves an existing DB group.
andrewtknoll@40 159 -- @param groupName The name of the DB group to retrieve. (string)
andrewtknoll@35 160 -- @return the DB group object, or ##nil## if not found
>@5 161 function LibModuleDBShare:GetGroup(groupName)
andrewtknoll@27 162 if type(groupName) ~= "string" then
andrewtknoll@27 163 error("Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.", 2);
andrewtknoll@27 164 end
@12 165 return LibModuleDBShare.groups[groupName];
>@4 166 end
>@5 167
>@5 168 --- Adds a database to the group.
andrewtknoll@40 169 -- @param newDB The database to add. (table)
andrewtknoll@22 170 function DBGroup:AddDB(newDB)
andrewtknoll@22 171 -- verify parameters
andrewtknoll@26 172 if type(newDB) ~= "table" or not AceDB.db_registry[newDB] then
andrewtknoll@31 173 error("Usage: DBGroup:AddDB(newDB): 'newDB' must be an AceDB-3.0 database.", 2);
andrewtknoll@31 174 elseif newDB.parent then
andrewtknoll@31 175 error("Usage: DBGroup:AddDB(newDB): 'newDB' must not be a namespace.", 2)
andrewtknoll@26 176 elseif type(self.members[newDB]) ~= "nil" then
andrewtknoll@31 177 error("Usage: DBGroup:AddDB(newDB): 'newDB' is already a member of DBGroup.", 2);
andrewtknoll@31 178 end
andrewtknoll@31 179 for groupName, group in pairs(LibModuleDBShare.groups) do
andrewtknoll@31 180 if group.members[newDB] ~= nil then
andrewtknoll@31 181 error("Usage: DBGroup:AddDB(newDB): 'newDB' is already a member of group '"..groupName.."'.", 2);
andrewtknoll@31 182 end
andrewtknoll@26 183 end
andrewtknoll@22 184 -- record current profile
andrewtknoll@20 185 local syncProfile = self.syncDB:GetCurrentProfile();
andrewtknoll@22 186 -- add new profiles to syncDB
andrewtknoll@20 187 self.squelchCallbacks = true;
andrewtknoll@22 188 for i, profile in pairs(newDB:GetProfiles()) do
andrewtknoll@20 189 self.syncDB:SetProfile(profile);
andrewtknoll@20 190 end
andrewtknoll@22 191 -- set current profile based on timestamps
andrewtknoll@27 192 local namespace = newDB:GetNamespace(MAJOR, true) or newDB:RegisterNamespace(MAJOR);
andrewtknoll@29 193 local storedData = namespace.char;
andrewtknoll@29 194 if type(storedData.logoutTimestamp) == "number" and storedData.logoutTimestamp > self.profileTimestamp then
andrewtknoll@22 195 self.squelchCallbacks = false;
andrewtknoll@22 196 self.syncDB:SetProfile(newDB:GetCurrentProfile());
andrewtknoll@29 197 self.profileTimestamp = storedData.logoutTimestamp;
andrewtknoll@35 198 if self.usesDualSpec and storedData.altProfile then
andrewtknoll@35 199 local LDSnamespace = group.syncDB:GetNamespace("LibDualSpec-1.0");
andrewtknoll@35 200 LDSnamespace.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@35 201 LDSnamespace.char.profile = storedData.altProfile;
andrewtknoll@35 202 LDSnamespace.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@30 203 group.syncDB:CheckDualSpecState();
andrewtknoll@35 204 elseif storedData.altProfile then
andrewtknoll@35 205 self.syncDB.char.enabled = storedData.dualSpecEnabled;
andrewtknoll@35 206 self.syncDB.char.profile = storedData.altProfile;
andrewtknoll@35 207 self.syncDB.char.specGroup = storedData.activeSpecGroup;
andrewtknoll@28 208 end
andrewtknoll@20 209 else
andrewtknoll@20 210 self.syncDB:SetProfile(syncProfile);
andrewtknoll@22 211 newDB:SetProfile(syncProfile);
andrewtknoll@22 212 self.squelchCallbacks = false;
andrewtknoll@20 213 end
andrewtknoll@22 214 -- add to members list
andrewtknoll@27 215 self.members[newDB] = namespace;
andrewtknoll@41 216 namespace.RegisterCallback(self, "OnDatabaseShutdown", "OnMemberShutdown"); -- register the namespace, not the base db
>@5 217 end
andrewtknoll@18 218
andrewtknoll@39 219 -- LibDualSpec support
andrewtknoll@39 220
andrewtknoll@35 221 --- Checks to see if this group uses LibDualSpec.
andrewtknoll@35 222 -- @return ##true## if this group uses LibDualSpec, ##false## otherwise
andrewtknoll@35 223 function DBGroup:IsUsingDualSpec()
andrewtknoll@35 224 return self.usesDualSpec;
andrewtknoll@35 225 end
andrewtknoll@35 226
andrewtknoll@35 227 --- Enables dual spec support if not already enabled.
andrewtknoll@35 228 function DBGroup:EnableDualSpec()
andrewtknoll@35 229 if not LibDualSpec then
andrewtknoll@35 230 LibDualSpec = LibStub("LibDualSpec-1.0"); -- this will error if LDS isn't found
andrewtknoll@35 231 end
andrewtknoll@35 232 if not self.usesDualSpec then
andrewtknoll@35 233 LibDualSpec:EnhanceDatabase(self.syncDB, self.name);
andrewtknoll@35 234 LibDualSpec:EnhanceOptions(self.profileOptionsTable, self.syncDB);
andrewtknoll@37 235 AceConfigRegistry:NotifyChange(self.name.."Profiles");
andrewtknoll@35 236 self.usesDualSpec = true;
andrewtknoll@35 237 local namespace = self.syncDB:GetNamespace("LibDualSpec-1.0");
andrewtknoll@35 238 namespace.char.enabled = self.syncDB.char.enabled;
andrewtknoll@35 239 namespace.char.profile = self.syncDB.char.profile;
andrewtknoll@35 240 namespace.char.specGroup = self.syncDB.char.specGroup;
andrewtknoll@35 241 self.syncDB:CheckDualSpecState();
andrewtknoll@35 242 end
andrewtknoll@35 243 end
andrewtknoll@35 244
andrewtknoll@39 245 -- slash command support
andrewtknoll@39 246
andrewtknoll@39 247 --- Adds a slash command to the group.
andrewtknoll@42 248 -- @paramsig slug, commandList[, handler]
andrewtknoll@40 249 -- @param slug The base identifier to use for the slash command. (string)
andrewtknoll@40 250 -- @param commandList The command itself, or a list of commands to use. (string or table)
andrewtknoll@40 251 -- @param handler A handler function for the command. If nil, defaults to a function that
andrewtknoll@40 252 -- calls the appropriate secondary command, or opens the root options panel. (function)
andrewtknoll@39 253 function DBGroup:EnableSlashCommand(slug, commandList, handler)
andrewtknoll@39 254 if self.slug then
andrewtknoll@39 255 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): group already has a slash command.", 2);
andrewtknoll@39 256 elseif type(slug) ~= "string" then
andrewtknoll@39 257 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'slug' must be a string.", 2);
andrewtknoll@39 258 elseif type(commandList) ~= "string" and type(commandList) ~= "table" then
andrewtknoll@39 259 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'commandList' must be a string or table.", 2);
andrewtknoll@39 260 elseif handler and type(handler) ~= "function" then
andrewtknoll@39 261 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'handler' must be nil or a function.", 2);
andrewtknoll@39 262 elseif type(commandList) == "table" then
andrewtknoll@39 263 for i = 1, #commandList do
andrewtknoll@39 264 if type(commandList[i]) ~= "string" then
andrewtknoll@39 265 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'commandList' must contain only strings.", 2);
andrewtknoll@39 266 end
andrewtknoll@39 267 end
andrewtknoll@39 268 end
andrewtknoll@39 269
andrewtknoll@39 270 self.slug = slug;
andrewtknoll@39 271 self.subCmdList = {};
andrewtknoll@39 272 if type(commandList) == "string" then
andrewtknoll@39 273 _G["SLASH_"..slug.."1"] = commandList;
andrewtknoll@39 274 else
andrewtknoll@39 275 for i = 1, #commandList do
andrewtknoll@39 276 _G["SLASH_"..slug..i] = commandList[i];
andrewtknoll@39 277 end
andrewtknoll@39 278 end
andrewtknoll@39 279
andrewtknoll@40 280 if handler then
andrewtknoll@40 281 SlashCmdList[slug] = handler;
andrewtknoll@40 282 else
andrewtknoll@40 283 SlashCmdList[slug] = function(msg, editBox)
andrewtknoll@40 284 for cmd, func in pairs(self.subCmdList) do
andrewtknoll@40 285 if msg == cmd then
andrewtknoll@40 286 func("", editBox);
andrewtknoll@39 287 return;
andrewtknoll@40 288 elseif msg:len() > cmd:len() then
andrewtknoll@40 289 if msg:sub(1, cmd:len() + 1) == (cmd.." ") then
andrewtknoll@40 290 func(msg:sub(cmd:len() + 2), editBox);
andrewtknoll@40 291 return;
andrewtknoll@40 292 end
andrewtknoll@39 293 end
andrewtknoll@39 294 end
andrewtknoll@39 295
andrewtknoll@40 296 for k, button in pairs(InterfaceOptionsFrameAddOns.buttons) do
andrewtknoll@40 297 if button.element.name == self.name and button.element.collapsed then
andrewtknoll@40 298 OptionsListButtonToggle_OnClick(button.toggle);
andrewtknoll@40 299 break;
andrewtknoll@40 300 end
andrewtknoll@39 301 end
andrewtknoll@40 302 InterfaceOptionsFrame_OpenToCategory(self.name);
andrewtknoll@40 303 end;
andrewtknoll@40 304 end
andrewtknoll@39 305 end
andrewtknoll@39 306
andrewtknoll@40 307 --- Checks to see if this group has a slash command.
andrewtknoll@40 308 -- @return ##true## if this group has a slash command, ##false## otherwise
andrewtknoll@40 309 function DBGroup:HasSlashCommand()
andrewtknoll@40 310 if self.slug then
andrewtknoll@40 311 return true;
andrewtknoll@40 312 else
andrewtknoll@40 313 return false;
andrewtknoll@40 314 end
andrewtknoll@40 315 end
andrewtknoll@40 316
andrewtknoll@40 317 --- Adds an alias for the slash command.
andrewtknoll@40 318 -- @param alias The alternate name for the slash command. (string)
andrewtknoll@39 319 function DBGroup:AddSlashCommandAlias(alias)
andrewtknoll@39 320 if type(alias) ~= "string" then
andrewtknoll@39 321 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' must be a string.", 2);
andrewtknoll@39 322 elseif not self.slug then
andrewtknoll@39 323 error("Usage: DBGroup:AddSlashCommandAlias(alias): slash commands for this group have not be enabled.", 2);
andrewtknoll@39 324 end
andrewtknoll@39 325
andrewtknoll@39 326 local i = 1;
andrewtknoll@39 327 while _G["SLASH_"..self.slug..i] do
andrewtknoll@39 328 if _G["SLASH_"..self.slug..i] == alias then
andrewtknoll@40 329 error("Usage: DBGroup:AddSlashCommandAlias(alias): alias '"..alias.."' is already in use by this command.", 2);
andrewtknoll@39 330 end
andrewtknoll@39 331 i = i + 1;
andrewtknoll@39 332 end
andrewtknoll@39 333
andrewtknoll@39 334 _G["SLASH_"..self.slug..i] = alias;
andrewtknoll@39 335 end
andrewtknoll@39 336
andrewtknoll@39 337 --- Adds a secondary command handler to the slash command for this group.
andrewtknoll@40 338 -- This handler will be called if the argument to the slash command matches the name provided.
andrewtknoll@42 339 -- @paramsig name, handler[, silent]
andrewtknoll@40 340 -- @param name The name of the secondary command. (string)
andrewtknoll@40 341 -- @param handler The function to handle the command. (function)
andrewtknoll@42 342 -- @param silent ##True## if you want to replace the currently registered command, ##false##
andrewtknoll@40 343 -- otherwise. (boolean)
andrewtknoll@42 344 function DBGroup:AddSecondaryCommand(name, handler, silent)
andrewtknoll@39 345 if type(name) ~= "string" then
andrewtknoll@40 346 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'name' must be a string.", 2);
andrewtknoll@42 347 elseif type(handler) ~= "function" then
andrewtknoll@40 348 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'handler' must be a function.", 2);
andrewtknoll@42 349 elseif not self.slug then
andrewtknoll@40 350 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): slash commands for this group have not be enabled.", 2);
andrewtknoll@40 351 elseif type(overwrite) ~= "boolean" and type(overwrite) ~= "nil" then
andrewtknoll@40 352 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'overwrite' must be a boolean or nil", 2);
andrewtknoll@39 353 end
andrewtknoll@42 354 if not silent then
andrewtknoll@40 355 for k, v in pairs(self.subCmdList) do
andrewtknoll@40 356 if k == name then
andrewtknoll@40 357 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): command '"..name.."' already exists.", 2);
andrewtknoll@40 358 end
andrewtknoll@39 359 end
andrewtknoll@39 360 end
andrewtknoll@40 361
andrewtknoll@39 362 self.subCmdList[name] = handler;
andrewtknoll@39 363 end
andrewtknoll@39 364
andrewtknoll@19 365 -- callback handlers (new profiles are handled by OnProfileChanged)
andrewtknoll@19 366
andrewtknoll@24 367 function DBGroup:OnProfileChanged(callback, syncDB, profile)
andrewtknoll@24 368 if not self.squelchCallbacks then
andrewtknoll@24 369 for db, _ in pairs(self.members) do
andrewtknoll@25 370 db:SetProfile(profile);
andrewtknoll@24 371 end
andrewtknoll@24 372 end
andrewtknoll@18 373 end
andrewtknoll@18 374
andrewtknoll@24 375 function DBGroup:OnProfileDeleted(callback, syncDB, profile)
andrewtknoll@24 376 for db, _ in pairs(self.members) do
andrewtknoll@24 377 db:DeleteProfile(profile, true);
andrewtknoll@24 378 end
andrewtknoll@18 379 end
andrewtknoll@18 380
andrewtknoll@24 381 function DBGroup:OnProfileCopied(callback, syncDB, profile)
andrewtknoll@24 382 for db, _ in pairs(self.members) do
andrewtknoll@24 383 db:CopyProfile(profile, true);
andrewtknoll@24 384 end
andrewtknoll@18 385 end
andrewtknoll@18 386
andrewtknoll@24 387 function DBGroup:OnProfileReset(callback, syncDB)
andrewtknoll@24 388 for db, _ in pairs(self.members) do
andrewtknoll@24 389 db:ResetProfile(false, false);
andrewtknoll@24 390 end
andrewtknoll@18 391 end
andrewtknoll@23 392
andrewtknoll@42 393 -- shutdown handling
andrewtknoll@42 394
andrewtknoll@29 395 local altProfile = nil;
andrewtknoll@29 396 local dualSpecEnabled = nil;
andrewtknoll@29 397 local activeSpecGroup = nil;
andrewtknoll@28 398
andrewtknoll@28 399 function DBGroup:OnSyncShutdown(callback, syncDB)
andrewtknoll@29 400 if self.usesDualSpec and not altProfile then
andrewtknoll@29 401 altProfile = syncDB:GetDualSpecProfile();
andrewtknoll@29 402 dualSpecEnabled = syncDB:IsDualSpecEnabled();
andrewtknoll@29 403 activeSpecGroup = GetActiveSpecGroup();
andrewtknoll@28 404 end
andrewtknoll@28 405 end
andrewtknoll@28 406
andrewtknoll@24 407 local timestamp = nil;
andrewtknoll@24 408
andrewtknoll@28 409 function DBGroup:OnMemberShutdown(callback, db)
andrewtknoll@27 410 if not timestamp then -- ensure uniform timestamps to minimize
andrewtknoll@27 411 timestamp = time(); -- calls to SetProfile in NewGroup
andrewtknoll@24 412 end
andrewtknoll@41 413 db.char.logoutTimestamp = timestamp; -- namespace is registered for callback, not base db
andrewtknoll@29 414 if self.usesDualSpec then
andrewtknoll@29 415 if not altProfile then
andrewtknoll@29 416 altProfile = self.syncDB:GetDualSpecProfile();
andrewtknoll@29 417 dualSpecEnabled = self.syncDB:IsDualSpecEnabled();
andrewtknoll@29 418 activeSpecGroup = GetActiveSpecGroup();
andrewtknoll@29 419 end
andrewtknoll@41 420 db.char.altProfile = altProfile;
andrewtknoll@41 421 db.char.dualSpecEnabled = dualSpecEnabled;
andrewtknoll@41 422 db.char.activeSpecGroup = activeSpecGroup;
andrewtknoll@28 423 end
andrewtknoll@23 424 end
andrewtknoll@33 425
andrewtknoll@33 426 -- update existing groups
andrewtknoll@33 427 for groupName, group in pairs(LibModuleDBShare.groups) do
andrewtknoll@33 428 for funcName, func in pairs(DBGroup) do
andrewtknoll@33 429 group[funcName] = func;
andrewtknoll@33 430 end
andrewtknoll@33 431 end