annotate Core.lua @ 178:38cc9221202d

Added a test page used to determine loading times.
author Zerotorescue
date Sun, 30 Jan 2011 15:40:16 +0100
parents 26c750a10b14
children a44a4147b34f
rev   line source
Zerotorescue@178 1 --@debug@
Zerotorescue@178 2 IMStartLoadTime = GetTime();
Zerotorescue@178 3 collectgarbage("collect");
Zerotorescue@178 4 IMStartLoadMemory = collectgarbage("count");
Zerotorescue@178 5 --@end-debug@
Zerotorescue@178 6
Zerotorescue@11 7 -- You can access this addon's object through: LibStub("AceAddon-3.0"):GetAddon("Inventorium")
Zerotorescue@17 8 local addon = select(2, ...);
Zerotorescue@17 9 addon = LibStub("AceAddon-3.0"):NewAddon(addon, "Inventorium", "AceEvent-3.0");
Zerotorescue@1 10
Zerotorescue@122 11 local _G = _G;
Zerotorescue@122 12 local sformat, ssplit, slower, strim, smatch = _G.string.format, _G.string.split, _G.string.lower, _G.string.trim, _G.string.match;
Zerotorescue@128 13 local floor, print, pairs, tonumber = _G.floor, _G.print, _G.pairs, _G.tonumber;
Zerotorescue@122 14
Zerotorescue@61 15 --@debug@
Zerotorescue@61 16 local addonRevision = 1;
Zerotorescue@61 17 --@end-debug@
Zerotorescue@61 18 --[===[@non-debug@
Zerotorescue@61 19 local addonRevision = @project-revision@;
Zerotorescue@61 20 --@end-non-debug@]===]
Zerotorescue@61 21
Zerotorescue@62 22 -- All modules must be able to retrieve our supported addons database, thus keep it a part of the addon object rather than local
Zerotorescue@13 23 addon.supportedAddons = {};
Zerotorescue@13 24 addon.supportedAddons.auctionPricing = {};
Zerotorescue@13 25 addon.supportedAddons.itemCount = {};
Zerotorescue@13 26 addon.supportedAddons.crafting = {};
Zerotorescue@0 27
Zerotorescue@120 28 addon.Locations = {
Zerotorescue@120 29 ["Bag"] = "Bag",
Zerotorescue@120 30 ["Bank"] = "Bank",
Zerotorescue@120 31 ["Guild"] = "Guild",
Zerotorescue@120 32 ["Mailbox"] = "Mailbox",
Zerotorescue@120 33 ["Merchant"] = "Merchant",
Zerotorescue@120 34 };
Zerotorescue@120 35
Zerotorescue@0 36 function addon:OnInitialize()
Zerotorescue@0 37 -- SAVED VARIABLES
Zerotorescue@0 38
Zerotorescue@0 39 local defaults = {
Zerotorescue@0 40 global = {
Zerotorescue@61 41 version = nil,
Zerotorescue@61 42 },
Zerotorescue@61 43 profile = {
Zerotorescue@0 44 defaults = {
Zerotorescue@157 45 -- General
Zerotorescue@13 46 auctionPricingAddon = "Auctioneer",
Zerotorescue@13 47 itemCountAddon = "Altoholic",
Zerotorescue@13 48 craftingAddon = "AdvancedTradeSkillWindow",
Zerotorescue@157 49 trackAtCharacters = { },
Zerotorescue@176 50 dontAlertAtCharacters = { },
Zerotorescue@157 51 localItemData = {
Zerotorescue@157 52 ["Bag"] = true,
Zerotorescue@157 53 ["Auction House"] = true,
Zerotorescue@157 54 },
Zerotorescue@157 55
Zerotorescue@157 56 -- Minimumm stock
Zerotorescue@61 57 minLocalStock = 20,
Zerotorescue@57 58 alertBelowLocalMinimum = true,
Zerotorescue@82 59 autoRefill = true,
Zerotorescue@101 60 autoRefillSkipConfirm = false,
Zerotorescue@61 61 minGlobalStock = 60,
Zerotorescue@61 62 alertBelowGlobalMinimum = true,
Zerotorescue@157 63
Zerotorescue@157 64 -- Queueing
Zerotorescue@0 65 restockTarget = 60,
Zerotorescue@0 66 minCraftingQueue = 0.05,
Zerotorescue@0 67 bonusQueue = 0.1,
Zerotorescue@0 68 priceThreshold = 0,
Zerotorescue@157 69
Zerotorescue@157 70 -- Summary
Zerotorescue@157 71 summaryThresholdShow = 10,
Zerotorescue@13 72 summaryHidePriceThreshold = false,
Zerotorescue@157 73
Zerotorescue@157 74 -- Global (can't be overridden)
Zerotorescue@106 75 hideHelp = false,
Zerotorescue@176 76 scanInterval = "0.1", -- string because the associated select requires it to be
Zerotorescue@13 77 summary = {
Zerotorescue@13 78 speed = 5,
Zerotorescue@74 79 width = 700,
Zerotorescue@13 80 height = 600,
Zerotorescue@13 81 },
Zerotorescue@0 82 colors = {
Zerotorescue@17 83 red = 0,
Zerotorescue@17 84 orange = 0.3,
Zerotorescue@17 85 yellow = 0.6,
Zerotorescue@17 86 green = 0.95,
Zerotorescue@0 87 },
Zerotorescue@157 88 itemCountGuildsExcluded = { },
Zerotorescue@0 89 },
Zerotorescue@61 90 groups = {
Zerotorescue@140 91 -- items = {},
Zerotorescue@140 92 -- isVirtual = nil,
Zerotorescue@61 93 },
Zerotorescue@0 94 },
Zerotorescue@0 95 factionrealm = {
Zerotorescue@40 96 characters = {
Zerotorescue@40 97 },
Zerotorescue@0 98 },
Zerotorescue@0 99 };
Zerotorescue@0 100
Zerotorescue@0 101 -- Register our saved variables database
Zerotorescue@11 102 self.db = LibStub("AceDB-3.0"):New("InventoriumDB", defaults, true);
Zerotorescue@61 103
Zerotorescue@62 104 -- SLASH COMMANDS
Zerotorescue@62 105
Zerotorescue@62 106 -- Disable the AddonLoader slash commands
Zerotorescue@62 107 SLASH_INVENTORIUM1 = nil;
Zerotorescue@62 108 SLASH_IM1 = nil;
Zerotorescue@62 109
Zerotorescue@62 110 -- Register our own slash commands
Zerotorescue@62 111 SLASH_INVENTORIUM1 = "/inventorium";
Zerotorescue@62 112 SLASH_INVENTORIUM2 = "/im";
Zerotorescue@62 113 SlashCmdList["INVENTORIUM"] = function(msg)
Zerotorescue@62 114 addon:CommandHandler(msg);
Zerotorescue@62 115 end;
Zerotorescue@62 116
Zerotorescue@62 117 -- Debug command handling
Zerotorescue@62 118 self:RegisterSlash(function(this)
Zerotorescue@62 119 this.debugChannel = false;
Zerotorescue@62 120 for i = 1, NUM_CHAT_WINDOWS do
Zerotorescue@62 121 local name = GetChatWindowInfo(i);
Zerotorescue@62 122
Zerotorescue@176 123 if string.upper(name) == "IMDEBUG" then
Zerotorescue@128 124 addon:Print("A debug channel already exists, removing the old one... (" .. i .. ")");
Zerotorescue@128 125 FCF_Close(_G["ChatFrame" .. i]);
Zerotorescue@62 126 end
Zerotorescue@62 127 end
Zerotorescue@62 128
Zerotorescue@62 129 if not this.debugChannel then
Zerotorescue@62 130 -- Create a new debug channel
Zerotorescue@176 131 local chatFrame = FCF_OpenNewWindow('IMDebug');
Zerotorescue@62 132 ChatFrame_RemoveAllMessageGroups(chatFrame);
Zerotorescue@62 133 this.debugChannel = chatFrame;
Zerotorescue@62 134
Zerotorescue@98 135 addon:Print("New debug channel created.");
Zerotorescue@62 136 end
Zerotorescue@176 137 end, { "d", "debug", "imdebug" });
Zerotorescue@62 138
Zerotorescue@62 139 -- Remember this character is on this account
Zerotorescue@62 140 local playerName = UnitName("player");
Zerotorescue@62 141 if not self.db.factionrealm.characters[playerName] then
Zerotorescue@62 142 self.db.factionrealm.characters[playerName] = true;
Zerotorescue@62 143
Zerotorescue@62 144 -- Default to tracking on all chars, untracking is a convenience, not tracking by default would probably get multiple issue reports.
Zerotorescue@62 145 self.db.profile.defaults.trackAtCharacters[playerName] = true;
Zerotorescue@62 146 end
Zerotorescue@66 147
Zerotorescue@66 148 self:UpdateDatabase();
Zerotorescue@62 149 end
Zerotorescue@62 150
Zerotorescue@65 151
Zerotorescue@65 152
Zerotorescue@65 153
Zerotorescue@65 154
Zerotorescue@65 155 -- Database patching after new revisions
Zerotorescue@65 156
Zerotorescue@62 157 function addon:UpdateDatabase()
Zerotorescue@61 158 if not self.db.global.version or self.db.global.version < addonRevision then
Zerotorescue@61 159 -- Is our database outdated? Then patch it.
Zerotorescue@61 160
Zerotorescue@61 161 if not self.db.global.version then
Zerotorescue@61 162 -- Old version was before version was saved, many changes were done in that revision
Zerotorescue@61 163
Zerotorescue@98 164 addon:Print("Updating Inventorium database from version " .. (self.db.global.version or "Unknown") .. " to version " .. addonRevision .. "...");
Zerotorescue@61 165
Zerotorescue@61 166 if self.db.global and self.db.global.defaults then
Zerotorescue@98 167 addon:Print("Moving all global data into your current profile...");
Zerotorescue@61 168
Zerotorescue@61 169 -- All data mustn't be global but profile-based
Zerotorescue@61 170 self.db.profile.defaults = CopyTable(self.db.global.defaults);
Zerotorescue@61 171 self.db.profile.groups = CopyTable(self.db.global.groups);
Zerotorescue@61 172
Zerotorescue@61 173 self.db.global.defaults = nil;
Zerotorescue@61 174 self.db.global.groups = nil;
Zerotorescue@61 175
Zerotorescue@62 176 self.CommandHandler = function()
Zerotorescue@62 177 message("You must /reload once to finalize the Inventorium database updates. This will only be required once during the BETA.");
Zerotorescue@61 178 end;
Zerotorescue@62 179 self:CommandHandler();
Zerotorescue@61 180 end
Zerotorescue@61 181
Zerotorescue@61 182 if self.db.profile.defaults.minimumStock then
Zerotorescue@98 183 addon:Print("Copying the minimum stock value into the minimum global stock...");
Zerotorescue@61 184
Zerotorescue@61 185 -- We added another stock option and renamed the old to be more obvious about what it means
Zerotorescue@61 186 self.db.profile.defaults.minGlobalStock = self.db.profile.defaults.minimumStock;
Zerotorescue@61 187 self.db.profile.defaults.minimumStock = nil;
Zerotorescue@61 188 end
Zerotorescue@61 189
Zerotorescue@61 190 if self.db.profile.defaults.minimumLocalStock then
Zerotorescue@98 191 addon:Print("Renaming the minimum local stock property...");
Zerotorescue@61 192
Zerotorescue@61 193 -- We added another stock option and then renamed it
Zerotorescue@61 194 self.db.profile.defaults.minLocalStock = self.db.profile.defaults.minimumLocalStock;
Zerotorescue@61 195 self.db.profile.defaults.minimumLocalStock = nil;
Zerotorescue@61 196 end
Zerotorescue@61 197
Zerotorescue@61 198 if self.db.profile.defaults.alertBelowMinimum then
Zerotorescue@98 199 addon:Print("Copying the alert below minimum value into the alert below global minimum value...");
Zerotorescue@61 200
Zerotorescue@61 201 -- We added another stock option and then renamed it
Zerotorescue@61 202 self.db.profile.defaults.alertBelowGlobalMinimum = self.db.profile.defaults.alertBelowMinimum;
Zerotorescue@61 203 self.db.profile.defaults.alertBelowMinimum = nil;
Zerotorescue@61 204 end
Zerotorescue@61 205
Zerotorescue@61 206 -- Go through all groups to see if there's one with the above two renamed variables
Zerotorescue@61 207 for groupName, values in pairs(self.db.profile.groups) do
Zerotorescue@61 208 if values.minimumStock then
Zerotorescue@61 209 values.minGlobalStock = values.minimumStock;
Zerotorescue@61 210 values.minimumStock = nil;
Zerotorescue@61 211 end
Zerotorescue@61 212 end
Zerotorescue@61 213 end
Zerotorescue@61 214
Zerotorescue@61 215 -- Remember the version of our database
Zerotorescue@61 216 self.db.global.version = addonRevision;
Zerotorescue@61 217 end
Zerotorescue@46 218 end
Zerotorescue@46 219
Zerotorescue@62 220 function addon:GetOptionByKey(groupName, optionName, noDefault)
Zerotorescue@62 221 if groupName and addon.db.profile.groups[groupName] and addon.db.profile.groups[groupName][optionName] ~= nil then
Zerotorescue@62 222 -- If this option exists within the settings of this group
Zerotorescue@62 223
Zerotorescue@62 224 return addon.db.profile.groups[groupName][optionName];
Zerotorescue@62 225 elseif groupName and addon.db.profile.groups[groupName] and addon.db.profile.groups[groupName].virtualGroup ~= "" and not noDefault then
Zerotorescue@62 226 -- If a virtual group was selected
Zerotorescue@62 227
Zerotorescue@62 228 return self:GetOptionByKey(addon.db.profile.groups[groupName].virtualGroup, optionName, noDefault);
Zerotorescue@62 229 elseif addon.db.profile.defaults[optionName] and not noDefault then
Zerotorescue@62 230 return addon.db.profile.defaults[optionName];
Zerotorescue@62 231 else
Zerotorescue@62 232 return nil;
Zerotorescue@0 233 end
Zerotorescue@0 234 end
Zerotorescue@0 235
Zerotorescue@35 236 function addon:GetItemCountAddon(group)
Zerotorescue@35 237 local selectedExternalAddon = self:GetOptionByKey(group, "itemCountAddon");
Zerotorescue@35 238
Zerotorescue@35 239 if self.supportedAddons.itemCount[selectedExternalAddon] and self.supportedAddons.itemCount[selectedExternalAddon].IsEnabled() then
Zerotorescue@35 240 -- Try to use the default item count addon
Zerotorescue@35 241
Zerotorescue@157 242 if self.supportedAddons.itemCount[selectedExternalAddon].SetGuildState then
Zerotorescue@157 243 self.supportedAddons.itemCount[selectedExternalAddon].SetGuildState(self.db.profile.defaults.itemCountGuildsExcluded);
Zerotorescue@157 244 end
Zerotorescue@157 245
Zerotorescue@35 246 return self.supportedAddons.itemCount[selectedExternalAddon], selectedExternalAddon;
Zerotorescue@35 247 else
Zerotorescue@35 248 -- Default not available, get the first one then
Zerotorescue@35 249
Zerotorescue@35 250 for name, value in pairs(self.supportedAddons.itemCount) do
Zerotorescue@35 251 if value.IsEnabled() then
Zerotorescue@157 252 if value.SetGuildState then
Zerotorescue@157 253 value.SetGuildState(self.db.profile.defaults.itemCountGuildsExcluded);
Zerotorescue@157 254 end
Zerotorescue@157 255
Zerotorescue@35 256 return value, name;
Zerotorescue@35 257 end
Zerotorescue@35 258 end
Zerotorescue@35 259 end
Zerotorescue@35 260
Zerotorescue@35 261 return;
Zerotorescue@35 262 end
Zerotorescue@35 263
Zerotorescue@23 264 function addon:GetItemCount(itemId, group)
Zerotorescue@13 265 itemId = tonumber(itemId);
Zerotorescue@13 266
Zerotorescue@13 267 if not itemId then return; end
Zerotorescue@13 268
Zerotorescue@35 269 local itemCountAddon = self:GetItemCountAddon(group);
Zerotorescue@23 270
Zerotorescue@35 271 return (itemCountAddon and itemCountAddon.GetTotalCount(itemId)) or -1;
Zerotorescue@0 272 end
Zerotorescue@0 273
Zerotorescue@50 274 function addon:GetLocalItemCount(itemId, group)
Zerotorescue@50 275 itemId = tonumber(itemId);
Zerotorescue@50 276
Zerotorescue@50 277 if not itemId then return; end
Zerotorescue@50 278
Zerotorescue@50 279 local itemCountAddon = self:GetItemCountAddon(group);
Zerotorescue@50 280
Zerotorescue@50 281 local currentItemCount;
Zerotorescue@50 282
Zerotorescue@50 283 if itemCountAddon and itemCountAddon.GetCharacterCount then
Zerotorescue@50 284 local bag, bank, auctionHouse, mail = itemCountAddon.GetCharacterCount(itemId);
Zerotorescue@50 285
Zerotorescue@50 286 local selectedLocalItemCountSources = self:GetOptionByKey(group, "localItemData");
Zerotorescue@50 287
Zerotorescue@50 288 currentItemCount = 0;
Zerotorescue@50 289 if selectedLocalItemCountSources["Bag"] then
Zerotorescue@50 290 currentItemCount = currentItemCount + bag;
Zerotorescue@50 291 end
Zerotorescue@50 292 if selectedLocalItemCountSources["Bank"] then
Zerotorescue@50 293 currentItemCount = currentItemCount + bank;
Zerotorescue@50 294 end
Zerotorescue@50 295 if selectedLocalItemCountSources["Auction House"] then
Zerotorescue@50 296 currentItemCount = currentItemCount + auctionHouse;
Zerotorescue@50 297 end
Zerotorescue@50 298 if selectedLocalItemCountSources["Mailbox"] then
Zerotorescue@50 299 currentItemCount = currentItemCount + mail;
Zerotorescue@50 300 end
Zerotorescue@50 301 end
Zerotorescue@50 302
Zerotorescue@50 303 return currentItemCount or -1;
Zerotorescue@50 304 end
Zerotorescue@50 305
Zerotorescue@23 306 function addon:GetAuctionValue(itemLink, group)
Zerotorescue@23 307 if not itemLink then return -5; end
Zerotorescue@13 308
Zerotorescue@23 309 local selectedExternalAddon = self:GetOptionByKey(group, "auctionPricingAddon");
Zerotorescue@23 310
Zerotorescue@23 311 if self.supportedAddons.auctionPricing[selectedExternalAddon] and self.supportedAddons.auctionPricing[selectedExternalAddon].IsEnabled() then
Zerotorescue@13 312 -- Try to use the default auction pricing addon
Zerotorescue@1 313
Zerotorescue@23 314 return self.supportedAddons.auctionPricing[selectedExternalAddon].GetValue(itemLink);
Zerotorescue@13 315 else
Zerotorescue@13 316 -- Default not available, get the first one then
Zerotorescue@1 317
Zerotorescue@13 318 for name, value in pairs(self.supportedAddons.auctionPricing) do
Zerotorescue@13 319 if value.IsEnabled() then
Zerotorescue@13 320 return value.GetValue(itemLink);
Zerotorescue@13 321 end
Zerotorescue@1 322 end
Zerotorescue@1 323 end
Zerotorescue@7 324
Zerotorescue@7 325 return -2;
Zerotorescue@1 326 end
Zerotorescue@1 327
Zerotorescue@65 328
Zerotorescue@65 329
Zerotorescue@65 330
Zerotorescue@65 331
Zerotorescue@62 332 -- Slash commands
Zerotorescue@62 333
Zerotorescue@62 334 local slashArgs = {};
Zerotorescue@62 335 local slashError = "Wrong argument, the following arguments are available:";
Zerotorescue@62 336
Zerotorescue@62 337 function addon:CommandHandler(message)
Zerotorescue@122 338 local cmd, arg = ssplit(" ", (message or ""), 2);
Zerotorescue@122 339 cmd = slower(cmd);
Zerotorescue@62 340
Zerotorescue@62 341 if slashArgs[cmd] then
Zerotorescue@62 342 -- Pass a reference to the addon (to be used as "self") and the provided arg
Zerotorescue@62 343 slashArgs[cmd](addon, arg);
Zerotorescue@62 344 else
Zerotorescue@98 345 addon:Print(slashError);
Zerotorescue@62 346 end
Zerotorescue@62 347 end
Zerotorescue@62 348
Zerotorescue@62 349 function addon:RegisterSlash(func, args, description)
Zerotorescue@62 350 for _, arg in pairs(args) do
Zerotorescue@62 351 slashArgs[arg] = func;
Zerotorescue@62 352 end
Zerotorescue@62 353
Zerotorescue@62 354 if description then
Zerotorescue@62 355 slashError = slashError .. "\n" .. description;
Zerotorescue@62 356 end
Zerotorescue@62 357 end
Zerotorescue@62 358
Zerotorescue@65 359
Zerotorescue@65 360
Zerotorescue@65 361
Zerotorescue@65 362
Zerotorescue@106 363 function addon:ColorCode(num, required)
Zerotorescue@106 364 local percentage = ( num / required );
Zerotorescue@106 365
Zerotorescue@106 366 if percentage >= addon.db.profile.defaults.colors.green then
Zerotorescue@106 367 return sformat("|cff00ff00%d|r", num);
Zerotorescue@106 368 elseif percentage >= addon.db.profile.defaults.colors.yellow then
Zerotorescue@106 369 return sformat("|cffffff00%d|r", num);
Zerotorescue@106 370 elseif percentage >= addon.db.profile.defaults.colors.orange then
Zerotorescue@106 371 return sformat("|cffff9933%d|r", num);
Zerotorescue@106 372 elseif percentage >= addon.db.profile.defaults.colors.red then
Zerotorescue@106 373 return sformat("|cffff0000%d|r", num);
Zerotorescue@106 374 else
Zerotorescue@106 375 return num;
Zerotorescue@106 376 end
Zerotorescue@106 377 end
Zerotorescue@106 378
Zerotorescue@106 379 function addon:DisplayItemCount(value, minimumStock)
Zerotorescue@106 380 if value == -1 then
Zerotorescue@106 381 return "|cffffff00Unknown|r";
Zerotorescue@106 382 elseif value == -3 then
Zerotorescue@106 383 return "|cffffff00Unknown|r";
Zerotorescue@176 384 elseif value == -4 then
Zerotorescue@176 385 return "|cffffff00-|r";
Zerotorescue@106 386 else
Zerotorescue@106 387 return sformat("%s / %d", self:ColorCode(value, minimumStock), minimumStock);
Zerotorescue@106 388 end
Zerotorescue@106 389 end
Zerotorescue@106 390
Zerotorescue@62 391 -- Readable money
Zerotorescue@62 392
Zerotorescue@62 393 local goldText = "%s%d|cffffd700g|r ";
Zerotorescue@62 394 local silverText = "%s%d|cffc7c7cfs|r ";
Zerotorescue@62 395 local copperText = "%s%d|cffeda55fc|r";
Zerotorescue@62 396
Zerotorescue@62 397 function addon:ReadableMoney(copper, clean)
Zerotorescue@62 398 local text = "";
Zerotorescue@62 399
Zerotorescue@128 400 local gold = floor( copper / COPPER_PER_GOLD );
Zerotorescue@62 401 if gold > 0 then
Zerotorescue@106 402 text = sformat(goldText, text, gold);
Zerotorescue@62 403 end
Zerotorescue@62 404
Zerotorescue@62 405 if not clean or (not gold or gold < 10) then
Zerotorescue@128 406 local silver = floor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER );
Zerotorescue@62 407 if silver > 0 then
Zerotorescue@106 408 text = sformat(silverText, text, silver);
Zerotorescue@62 409 end
Zerotorescue@62 410
Zerotorescue@62 411 if not clean or (not gold or gold < 1) then
Zerotorescue@128 412 local copper = floor( copper % COPPER_PER_SILVER );
Zerotorescue@62 413 if copper > 0 or text == "" then
Zerotorescue@106 414 text = sformat(copperText, text, copper);
Zerotorescue@62 415 end
Zerotorescue@62 416 end
Zerotorescue@62 417 end
Zerotorescue@62 418
Zerotorescue@62 419
Zerotorescue@122 420 return strim(text);
Zerotorescue@62 421 end
Zerotorescue@62 422
Zerotorescue@62 423 function addon:ReadableMoneyToCopper(value)
Zerotorescue@62 424 -- If a player enters a value it will be filled without color codes
Zerotorescue@62 425 -- If it is retrieved from the database, it will be colored coded
Zerotorescue@62 426 -- Thus we look for both
Zerotorescue@122 427 local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
Zerotorescue@122 428 local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
Zerotorescue@122 429 local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
Zerotorescue@62 430
Zerotorescue@62 431 return ( (gold or 0) * COPPER_PER_GOLD ) + ( (silver or 0) * COPPER_PER_SILVER ) + (copper or 0);
Zerotorescue@62 432 end
Zerotorescue@62 433
Zerotorescue@62 434 function addon:ValidateReadableMoney(info, value)
Zerotorescue@62 435 -- If a player enters a value it will be filled without color codes
Zerotorescue@62 436 -- If it is retrieved from the database, it will be colored coded
Zerotorescue@62 437 -- Thus we look for both
Zerotorescue@122 438 local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
Zerotorescue@122 439 local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
Zerotorescue@122 440 local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
Zerotorescue@62 441
Zerotorescue@62 442 if not gold and not silver and not copper then
Zerotorescue@62 443 return "The provided amount of money is invalid. Please provide the amount of money as #g#s#c, e.g. 591617g24s43c.";
Zerotorescue@62 444 else
Zerotorescue@62 445 return true;
Zerotorescue@62 446 end
Zerotorescue@62 447 end
Zerotorescue@62 448
Zerotorescue@0 449
Zerotorescue@0 450
Zerotorescue@65 451
Zerotorescue@65 452
Zerotorescue@13 453 -- Public
Zerotorescue@13 454
Zerotorescue@36 455 function IMRegisterPricingAddon(name, get, enabled, onSelect)
Zerotorescue@13 456 addon.supportedAddons.auctionPricing[name] = {
Zerotorescue@156 457 ["GetValue"] = get,
Zerotorescue@156 458 ["IsEnabled"] = enabled,
Zerotorescue@156 459 ["OnSelect"] = onSelect,
Zerotorescue@13 460 };
Zerotorescue@13 461 end
Zerotorescue@13 462
Zerotorescue@156 463 function IMRegisterItemCountAddon(name, getTotal, getCharacter, enabled, onSelect, getGuildNames, setGuildState)
Zerotorescue@13 464 addon.supportedAddons.itemCount[name] = {
Zerotorescue@156 465 ["GetTotalCount"] = getTotal,
Zerotorescue@156 466 ["GetCharacterCount"] = getCharacter,
Zerotorescue@156 467 ["IsEnabled"] = enabled,
Zerotorescue@156 468 ["OnSelect"] = onSelect,
Zerotorescue@156 469 ["GetGuildNames"] = getGuildNames,
Zerotorescue@156 470 ["SetGuildState"] = setGuildState,
Zerotorescue@13 471 };
Zerotorescue@13 472 end
Zerotorescue@13 473
Zerotorescue@50 474 function IMRegisterCraftingAddon(name, queue, enabled, onSelect)
Zerotorescue@13 475 addon.supportedAddons.crafting[name] = {
Zerotorescue@156 476 ["Queue"] = queue,
Zerotorescue@156 477 ["IsEnabled"] = enabled,
Zerotorescue@156 478 ["OnSelect"] = onSelect,
Zerotorescue@13 479 };
Zerotorescue@13 480 end
Zerotorescue@13 481
Zerotorescue@62 482 -- We need a global command handler for our chat-links
Zerotorescue@62 483 function InventoriumCommandHandler(msg)
Zerotorescue@62 484 addon:CommandHandler(msg);
Zerotorescue@62 485 end
Zerotorescue@62 486
Zerotorescue@13 487
Zerotorescue@13 488
Zerotorescue@65 489
Zerotorescue@65 490
Zerotorescue@76 491 -- General
Zerotorescue@76 492
Zerotorescue@76 493 addon.Colors = {
Zerotorescue@157 494 ["Red"] = { 1, 0, 0 },
Zerotorescue@157 495 ["Orange"] = { 1, .46, .1 },
Zerotorescue@157 496 ["Green"] = { 0, 1, 0 },
Zerotorescue@157 497 ["Blue"] = { 0, 0, 1 },
Zerotorescue@157 498 ["Yellow"] = { 1, 1, 0 },
Zerotorescue@157 499 ["Cyan"] = { 0, 1, 1 },
Zerotorescue@76 500 }; -- easy to extend if more colors are needed
Zerotorescue@76 501 function addon:Print(text, color)
Zerotorescue@76 502 local red, green, blue;
Zerotorescue@76 503
Zerotorescue@76 504 if color then
Zerotorescue@76 505 red, green, blue = color[1], color[2], color[3];
Zerotorescue@76 506 end
Zerotorescue@76 507
Zerotorescue@76 508 DEFAULT_CHAT_FRAME:AddMessage(text or "", red, green, blue, nil, 5);
Zerotorescue@76 509 end
Zerotorescue@76 510
Zerotorescue@95 511 function addon:GetItemId(itemLink)
Zerotorescue@84 512 itemLink = itemLink and itemLink:match("|Hitem:([-0-9]+):"); -- if itemLink is nil, it won't execute the second part
Zerotorescue@84 513 itemLink = itemLink and tonumber(itemLink);
Zerotorescue@84 514
Zerotorescue@84 515 return itemLink;
Zerotorescue@84 516 end
Zerotorescue@84 517
Zerotorescue@13 518 -- Debug
Zerotorescue@0 519
Zerotorescue@176 520 local function ReadableTable(t, includeKeys, jumps)
Zerotorescue@176 521 local tabs = "";
Zerotorescue@176 522 for i = 1, (jumps or 0) do
Zerotorescue@176 523 tabs = tabs .. " ";
Zerotorescue@176 524 end
Zerotorescue@176 525
Zerotorescue@176 526 local temp = "{\n";
Zerotorescue@176 527
Zerotorescue@169 528 for i, v in pairs(t) do
Zerotorescue@169 529 if type(v) == "table" then
Zerotorescue@176 530 if includeKeys then
Zerotorescue@176 531 local key = (type(i) == "number" and tostring(i)) or string.format("\"%s\"", tostring(i));
Zerotorescue@176 532
Zerotorescue@176 533 temp = string.format("%s%s [%s] => %s,\n", temp, tabs, key, ReadableTable(v, includeKeys, (jumps or 0) + 1));
Zerotorescue@176 534 else
Zerotorescue@176 535 temp = string.format("%s%s %s,\n", temp, tabs, ReadableTable(v, includeKeys, (jumps or 0) + 1));
Zerotorescue@176 536 end
Zerotorescue@169 537 else
Zerotorescue@176 538 if includeKeys then
Zerotorescue@176 539 local key = (type(i) == "number" and tostring(i)) or string.format("\"%s\"", tostring(i));
Zerotorescue@176 540 local value = (type(v) == "number" and tostring(v)) or string.format("\"%s\"", tostring(v));
Zerotorescue@176 541
Zerotorescue@176 542 temp = string.format("%s%s [%s] => %s,\n", temp, tabs, key, value);
Zerotorescue@176 543 else
Zerotorescue@176 544 local value = (type(v) == "number" and tostring(v)) or string.format("\"%s\"", tostring(v));
Zerotorescue@176 545
Zerotorescue@176 546 temp = string.format("%s%s %s,\n", temp, tabs, value);
Zerotorescue@176 547 end
Zerotorescue@169 548 end
Zerotorescue@169 549 end
Zerotorescue@176 550 temp = temp .. tabs .. "}";
Zerotorescue@176 551
Zerotorescue@169 552 return temp;
Zerotorescue@169 553 end
Zerotorescue@169 554
Zerotorescue@89 555 function addon:Debug(t, ...)
Zerotorescue@0 556 if not self.debugChannel and self.debugChannel ~= false then
Zerotorescue@0 557 -- We want to check just once, so if you add a debug channel later just do a /reload (registering an event for this is wasted resources)
Zerotorescue@0 558 self.debugChannel = false;
Zerotorescue@0 559
Zerotorescue@0 560 for i = 1, NUM_CHAT_WINDOWS do
Zerotorescue@0 561 local name = GetChatWindowInfo(i);
Zerotorescue@0 562
Zerotorescue@176 563 if name:upper() == "IMDEBUG" then
Zerotorescue@0 564 self.debugChannel = _G["ChatFrame" .. i];
Zerotorescue@0 565 end
Zerotorescue@0 566 end
Zerotorescue@0 567 end
Zerotorescue@0 568
Zerotorescue@0 569 if self.debugChannel then
Zerotorescue@169 570 if type(t) == "table" then
Zerotorescue@176 571 t = ReadableTable(t, true);
Zerotorescue@169 572 end
Zerotorescue@169 573
Zerotorescue@110 574 self.debugChannel:AddMessage("|cffffff00Inventorium|r:" .. sformat(t, ...));
Zerotorescue@0 575 end
Zerotorescue@0 576 end