annotate Core.lua @ 159:a3507735dfd9 v75

Fixed a rendering issue with auto-logging checkboxes.
author yellowfive
date Wed, 24 Apr 2019 18:00:42 -0700
parents df1596b1a744
children 35612aee8e15
rev   line source
yellowfive@57 1 AskMrRobot = LibStub("AceAddon-3.0"):NewAddon("AskMrRobot", "AceEvent-3.0", "AceComm-3.0", "AceConsole-3.0", "AceSerializer-3.0")
yellowfive@57 2 local Amr = AskMrRobot
yellowfive@57 3 Amr.Serializer = LibStub("AskMrRobot-Serializer")
yellowfive@57 4
yellowfive@57 5 Amr.ADDON_NAME = "AskMrRobot"
yellowfive@57 6
yellowfive@57 7 -- types of inter-addon messages that we receive, used to parcel them out to the proper handlers
yellowfive@57 8 Amr.MessageTypes = {
yellowfive@57 9 Version = "_V",
yellowfive@124 10 VersionRequest = "_VR"
yellowfive@57 11 }
yellowfive@57 12
yellowfive@57 13 local L = LibStub("AceLocale-3.0"):GetLocale("AskMrRobot", true)
yellowfive@57 14 local AceGUI = LibStub("AceGUI-3.0")
yellowfive@57 15
yellowfive@57 16 -- minimap icon and LDB support
yellowfive@57 17 local _amrLDB = LibStub("LibDataBroker-1.1"):NewDataObject(Amr.ADDON_NAME, {
yellowfive@57 18 type = "launcher",
yellowfive@57 19 text = "Ask Mr. Robot",
yellowfive@57 20 icon = "Interface\\AddOns\\" .. Amr.ADDON_NAME .. "\\Media\\icon",
yellowfive@57 21 OnClick = function(self, button, down)
yellowfive@57 22 if button == "LeftButton" then
yellowfive@124 23 --if IsControlKeyDown() then
yellowfive@124 24 -- Amr:Wipe()
yellowfive@124 25 --else
yellowfive@57 26 Amr:Toggle()
yellowfive@124 27 --end
yellowfive@57 28 elseif button == "RightButton" then
yellowfive@57 29 Amr:EquipGearSet()
yellowfive@57 30 end
yellowfive@57 31 end,
yellowfive@57 32 OnTooltipShow = function(tt)
yellowfive@57 33 tt:AddLine("Ask Mr. Robot", 1, 1, 1);
yellowfive@57 34 tt:AddLine(" ");
yellowfive@57 35 tt:AddLine(L.MinimapTooltip)
yellowfive@57 36 end
yellowfive@57 37 })
yellowfive@57 38 local _icon = LibStub("LibDBIcon-1.0")
yellowfive@57 39
yellowfive@57 40
yellowfive@57 41 -- initialize the database
yellowfive@57 42 local function initializeDb()
yellowfive@57 43
yellowfive@57 44 local defaults = {
yellowfive@139 45 char = {
yellowfive@139 46 LastVersion = 0, -- used to clean out old stuff
yellowfive@129 47 FirstUse = true, -- true if this is first time use, gets cleared after seeing the export help splash window
yellowfive@129 48 Talents = {}, -- for each spec, selected talents
yellowfive@129 49 Equipped = {}, -- for each spec, slot id to item info
yellowfive@129 50 BagItems = {}, -- list of item info for bags
yellowfive@129 51 BankItems = {}, -- list of item info for bank
yellowfive@129 52 BagItemsAndCounts = {}, -- used mainly for the shopping list
yellowfive@129 53 BankItemsAndCounts = {}, -- used mainly for the shopping list
yellowfive@139 54 GearSetups = {}, -- imported gear sets
yellowfive@129 55 ExtraEnchantData = {}, -- enchant id to enchant display information and material information
yellowfive@129 56 Logging = { -- character logging settings
yellowfive@129 57 Enabled = false, -- whether logging is currently on or not
yellowfive@129 58 LastZone = nil, -- last zone the player was in
yellowfive@129 59 LastDiff = nil, -- last difficulty for the last zone the player was in
yellowfive@129 60 LastWipe = nil -- last time a wipe was called by this player
yellowfive@129 61 }
yellowfive@129 62 },
yellowfive@57 63 profile = {
yellowfive@57 64 minimap = { -- minimap hide/show and position settings
yellowfive@57 65 hide = false
yellowfive@57 66 },
yellowfive@57 67 window = {}, -- main window position settings
yellowfive@57 68 shopWindow = {}, -- shopping list window position settings
yellowfive@57 69 options = {
yellowfive@57 70 autoGear = false, -- auto-equip saved gear sets when changing specs
yellowfive@61 71 shopAh = false, -- auto-show shopping list at AH
yellowfive@91 72 disableEm = false, -- disable auto-creation of equipment manager sets
yellowfive@61 73 uiScale = 1 -- custom scale for AMR UI
yellowfive@57 74 },
yellowfive@57 75 Logging = { -- global logging settings
yellowfive@57 76 Auto = {} -- for each instanceId, for each difficultyId, true if auto-logging enabled
yellowfive@57 77 }
yellowfive@57 78 },
yellowfive@57 79 global = {
yellowfive@57 80 Region = nil, -- region that this user is in, all characters on the same account should be the same region
yellowfive@139 81 Shopping2 = {}, -- shopping list data stored globally for access on any character
yellowfive@57 82 Logging = { -- a lot of log data is stored globally for simplicity, can only be raiding with one character at a time
yellowfive@57 83 Wipes = {}, -- times that a wipe was called
yellowfive@57 84 PlayerData = {}, -- player data gathered at fight start
yellowfive@57 85 PlayerExtras = {} -- player extra data like auras, gathered at fight start
yellowfive@57 86 }
yellowfive@57 87 }
yellowfive@57 88 }
yellowfive@57 89
yellowfive@124 90 Amr.db = LibStub("AceDB-3.0"):New("AskMrRobotDb4", defaults)
yellowfive@110 91
yellowfive@110 92 -- set defaults for auto logging; if a new zone is added and some other stuff was turned on, turn on the new zone too
yellowfive@110 93 local hasSomeLogging = false
yellowfive@110 94 local addedLogging = {}
yellowfive@57 95 for i, instanceId in ipairs(Amr.InstanceIdsOrdered) do
yellowfive@110 96 local byDiff = Amr.db.profile.Logging.Auto[instanceId]
yellowfive@57 97 if not byDiff then
yellowfive@57 98 byDiff = {}
yellowfive@110 99 Amr.db.profile.Logging.Auto[instanceId] = byDiff
yellowfive@110 100 addedLogging[instanceId] = byDiff
yellowfive@57 101 end
yellowfive@57 102
yellowfive@57 103 for k, difficultyId in pairs(Amr.Difficulties) do
yellowfive@110 104 if not byDiff[difficultyId] then
yellowfive@57 105 byDiff[difficultyId] = false
yellowfive@110 106 else
yellowfive@110 107 hasSomeLogging = true
yellowfive@57 108 end
yellowfive@57 109 end
yellowfive@137 110 end
yellowfive@137 111
yellowfive@137 112 for k,v in pairs(Amr.db.profile.Logging.Auto) do
yellowfive@137 113 if not Amr.IsSupportedInstanceId(k) then
yellowfive@137 114 Amr.db.profile.Logging.Auto[k] = nil
yellowfive@137 115 end
yellowfive@57 116 end
yellowfive@57 117
yellowfive@110 118 if hasSomeLogging then
yellowfive@110 119 for instanceId, byDiff in pairs(addedLogging) do
yellowfive@110 120 for k, difficultyId in pairs(Amr.Difficulties) do
yellowfive@110 121 byDiff[difficultyId] = true
yellowfive@110 122 end
yellowfive@110 123 end
yellowfive@110 124 end
yellowfive@110 125
yellowfive@139 126 -- upgrade old gear set info to new format
yellowfive@139 127 if Amr.db.char.GearSets then
yellowfive@139 128 Amr.db.char.GearSets = nil
yellowfive@139 129 end
yellowfive@139 130
yellowfive@139 131 if not Amr.db.char.GearSetups then
yellowfive@139 132 Amr.db.char.GearSetups = {}
yellowfive@139 133 end
yellowfive@139 134
yellowfive@139 135 if Amr.db.global.Shopping then
yellowfive@139 136 Amr.db.global.Shopping = nil
yellowfive@139 137 end
yellowfive@57 138
yellowfive@57 139 Amr.db.RegisterCallback(Amr, "OnProfileChanged", "RefreshConfig")
yellowfive@57 140 Amr.db.RegisterCallback(Amr, "OnProfileCopied", "RefreshConfig")
yellowfive@57 141 Amr.db.RegisterCallback(Amr, "OnProfileReset", "RefreshConfig")
yellowfive@57 142 end
yellowfive@57 143
yellowfive@57 144 function Amr:OnInitialize()
yellowfive@57 145
yellowfive@57 146 initializeDb()
yellowfive@57 147
yellowfive@57 148 Amr:RegisterChatCommand("amr", "SlashCommand")
yellowfive@57 149
yellowfive@57 150 _icon:Register(Amr.ADDON_NAME, _amrLDB, self.db.profile.minimap)
yellowfive@57 151
yellowfive@57 152 -- listen for inter-addon communication
yellowfive@139 153 self:RegisterComm(Amr.ChatPrefix, "OnCommReceived")
yellowfive@57 154 end
yellowfive@57 155
yellowfive@57 156 local _enteredWorld = false
yellowfive@57 157 local _pendingInit = false
yellowfive@57 158
yellowfive@139 159 -- upgrade some stuff from old to new formats
yellowfive@139 160 local function upgradeFromOld()
yellowfive@139 161
yellowfive@139 162 local currentVersion = tonumber(GetAddOnMetadata(Amr.ADDON_NAME, "Version"))
yellowfive@139 163 if Amr.db.char.LastVersion < 65 then
yellowfive@143 164
yellowfive@143 165 if not Amr.db.profile.options.disableEm then
yellowfive@143 166 for i = 1,GetNumSpecializations() do
yellowfive@143 167 local _, specName = GetSpecializationInfo(i)
yellowfive@143 168 if specName then
yellowfive@143 169 local setid = C_EquipmentSet.GetEquipmentSetID("AMR " .. specName)
yellowfive@143 170 if setid then
yellowfive@143 171 C_EquipmentSet.DeleteEquipmentSet(setid)
yellowfive@143 172 end
yellowfive@139 173 end
yellowfive@139 174 end
yellowfive@139 175 end
yellowfive@143 176
yellowfive@139 177 end
yellowfive@139 178 Amr.db.char.LastVersion = currentVersion
yellowfive@139 179
yellowfive@139 180 end
yellowfive@139 181
yellowfive@133 182 local function finishInitialize()
yellowfive@57 183
yellowfive@57 184 -- record region, the only thing that we still can't get from the log file
yellowfive@57 185 Amr.db.global.Region = Amr.RegionNames[GetCurrentRegion()]
yellowfive@57 186
yellowfive@57 187 -- make sure that some initialization is deferred until after PLAYER_ENTERING_WORLD event so that data we need is available;
yellowfive@57 188 -- also delay this initialization for a few extra seconds to deal with some event spam that is otherwise hard to identify and ignore when a player logs in
yellowfive@57 189 Amr.Wait(5, function()
yellowfive@145 190 --Amr:InitializeVersions()
yellowfive@57 191 Amr:InitializeGear()
yellowfive@57 192 Amr:InitializeExport()
yellowfive@57 193 Amr:InitializeCombatLog()
yellowfive@139 194
yellowfive@139 195 upgradeFromOld()
yellowfive@57 196 end)
yellowfive@57 197 end
yellowfive@57 198
yellowfive@133 199 local function onPlayerEnteringWorld()
yellowfive@57 200
yellowfive@57 201 _enteredWorld = true
yellowfive@57 202
yellowfive@57 203 if _pendingInit then
yellowfive@57 204 finishInitialize()
yellowfive@57 205 _pendingInit = false
yellowfive@57 206 end
yellowfive@57 207 end
yellowfive@57 208
yellowfive@57 209 function Amr:OnEnable()
yellowfive@57 210
yellowfive@81 211 --[[
yellowfive@57 212 -- listen for changes to the snapshot enable state, and always make sure it is enabled if using the core AskMrRobot addon
yellowfive@57 213 self:RegisterMessage("AMR_SNAPSHOT_STATE_CHANGED", function(eventName, isEnabled)
yellowfive@57 214 if not isEnabled then
yellowfive@57 215 -- immediately re-enable on any attempt to disable
yellowfive@57 216 Amr.Serializer:EnableSnapshots()
yellowfive@57 217 end
yellowfive@57 218 end)
yellowfive@57 219 self.Serializer:EnableSnapshots()
yellowfive@81 220 ]]
yellowfive@57 221
yellowfive@57 222 -- update based on current configuration whenever enabled
yellowfive@57 223 self:RefreshConfig()
yellowfive@57 224
yellowfive@57 225 -- if we have fully entered the world, do initialization; otherwise wait for PLAYER_ENTERING_WORLD to continue
yellowfive@57 226 if not _enteredWorld then
yellowfive@57 227 _pendingInit = true
yellowfive@57 228 else
yellowfive@57 229 _pendingInit = false
yellowfive@57 230 finishInitialize()
yellowfive@57 231 end
yellowfive@57 232 end
yellowfive@57 233
yellowfive@57 234 function Amr:OnDisable()
yellowfive@57 235 -- disabling is not supported
yellowfive@57 236 end
yellowfive@57 237
yellowfive@57 238
yellowfive@57 239 ----------------------------------------------------------------------------------------
yellowfive@57 240 -- Slash Commands
yellowfive@57 241 ----------------------------------------------------------------------------------------
yellowfive@57 242 local _slashMethods = {
yellowfive@57 243 hide = "Hide",
yellowfive@57 244 show = "Show",
yellowfive@57 245 toggle = "Toggle",
yellowfive@124 246 equip = "EquipGearSet",
yellowfive@57 247 version = "PrintVersions",
yellowfive@124 248 --wipe = "Wipe",
yellowfive@124 249 --undowipe = "UndoWipe",
yellowfive@61 250 reset = "Reset",
yellowfive@57 251 test = "Test"
yellowfive@57 252 }
yellowfive@57 253
yellowfive@57 254 function Amr:SlashCommand(input)
yellowfive@57 255 input = string.lower(input)
yellowfive@57 256 local parts = {}
yellowfive@57 257 for w in input:gmatch("%S+") do
yellowfive@57 258 table.insert(parts, w)
yellowfive@57 259 end
yellowfive@57 260
yellowfive@57 261 if #parts == 0 then return end
yellowfive@57 262
yellowfive@57 263 local func = _slashMethods[parts[1]]
yellowfive@57 264 if not func then return end
yellowfive@57 265
yellowfive@57 266 local funcArgs = {}
yellowfive@57 267 for i = 2, #parts do
yellowfive@57 268 table.insert(funcArgs, parts[i])
yellowfive@57 269 end
yellowfive@57 270
yellowfive@57 271 Amr[func](Amr, unpack(funcArgs))
yellowfive@57 272 end
yellowfive@57 273
yellowfive@57 274
yellowfive@57 275 ----------------------------------------------------------------------------------------
yellowfive@57 276 -- Configuration
yellowfive@57 277 ----------------------------------------------------------------------------------------
yellowfive@57 278
yellowfive@57 279 -- refresh all state based on the current values of configuration options
yellowfive@57 280 function Amr:RefreshConfig()
yellowfive@57 281
yellowfive@57 282 self:UpdateMinimap()
yellowfive@57 283 self:RefreshOptionsUi()
yellowfive@57 284 self:RefreshLogUi()
yellowfive@57 285 end
yellowfive@57 286
yellowfive@57 287 function Amr:UpdateMinimap()
yellowfive@57 288
yellowfive@57 289 if self.db.profile.minimap.hide or not Amr:IsEnabled() then
yellowfive@57 290 _icon:Hide(Amr.ADDON_NAME)
yellowfive@57 291 else
yellowfive@57 292 -- change icon color if logging
yellowfive@57 293 if Amr:IsLogging() then
yellowfive@57 294 _amrLDB.icon = 'Interface\\AddOns\\AskMrRobot\\Media\\icon_green'
yellowfive@57 295 else
yellowfive@57 296 _amrLDB.icon = 'Interface\\AddOns\\AskMrRobot\\Media\\icon'
yellowfive@57 297 end
yellowfive@57 298
yellowfive@57 299 _icon:Show(Amr.ADDON_NAME)
yellowfive@57 300 end
yellowfive@57 301 end
yellowfive@57 302
yellowfive@57 303
yellowfive@57 304 ----------------------------------------------------------------------------------------
yellowfive@57 305 -- Version Checking
yellowfive@57 306 ----------------------------------------------------------------------------------------
yellowfive@57 307
yellowfive@57 308 -- version of addon being run by each person in the player's raid or group
yellowfive@57 309 Amr.GroupVersions = {}
yellowfive@57 310
yellowfive@57 311 local function toGroupVersionKey(realm, name)
yellowfive@57 312 realm = string.gsub(realm, "%s+", "")
yellowfive@57 313 return name .. "-" .. realm
yellowfive@57 314 end
yellowfive@57 315
yellowfive@57 316 -- prune out version information for players no longer in the current raid group
yellowfive@57 317 local function pruneVersionInfo()
yellowfive@57 318
yellowfive@57 319 local newVersions = {}
yellowfive@57 320 local units = Amr:GetGroupUnitIdentifiers()
yellowfive@57 321
yellowfive@57 322 for i, unitId in ipairs(units) do
yellowfive@57 323 local realm, name = Amr:GetRealmAndName(unitId)
yellowfive@57 324 if realm then
yellowfive@57 325 local key = toGroupVersionKey(realm, name)
yellowfive@57 326 newVersions[key] = Amr.GroupVersions[key]
yellowfive@57 327 end
yellowfive@57 328 end
yellowfive@57 329
yellowfive@57 330 Amr.GroupVersions = newVersions
yellowfive@57 331 end
yellowfive@57 332
yellowfive@57 333 -- send version information to other people in the same raid group
yellowfive@57 334 local function sendVersionInfo()
yellowfive@57 335
yellowfive@57 336 local realm = GetRealmName()
yellowfive@57 337 local name = UnitName("player")
yellowfive@57 338 local ver = GetAddOnMetadata(Amr.ADDON_NAME, "Version")
yellowfive@57 339
yellowfive@57 340 local msg = string.format("%s\n%s\n%s\n%s", Amr.MessageTypes.Version, realm, name, ver)
yellowfive@57 341 Amr:SendAmrCommMessage(msg)
yellowfive@57 342 end
yellowfive@57 343
yellowfive@57 344 local function onVersionInfoReceived(message)
yellowfive@57 345
yellowfive@57 346 -- message will be of format: realm\nname\nversion
yellowfive@57 347 local parts = {}
yellowfive@57 348 for part in string.gmatch(message, "([^\n]+)") do
yellowfive@57 349 table.insert(parts, part)
yellowfive@57 350 end
yellowfive@57 351
yellowfive@57 352 local key = toGroupVersionKey(parts[2], parts[3])
yellowfive@57 353 local ver = parts[4]
yellowfive@57 354
yellowfive@57 355 Amr.GroupVersions[key] = tonumber(ver)
yellowfive@57 356
yellowfive@57 357 -- make sure that versions are properly pruned in case this message arrived late and the player has since been removed from the group
yellowfive@57 358 pruneVersionInfo()
yellowfive@57 359 end
yellowfive@57 360
yellowfive@57 361 -- get the addon version another person in the player's raid/group is running, or 0 if they are not running the addon
yellowfive@57 362 function Amr:GetAddonVersion(realm, name)
yellowfive@57 363 local ver = Amr.GroupVersions[toGroupVersionKey(realm, name)]
yellowfive@57 364 return ver or 0
yellowfive@57 365 end
yellowfive@57 366
yellowfive@57 367 function Amr:PrintVersions()
yellowfive@57 368
yellowfive@57 369 if not IsInGroup() and not IsInRaid() then
yellowfive@57 370 self:Print(L.VersionChatNotGrouped)
yellowfive@57 371 return
yellowfive@57 372 end
yellowfive@57 373
yellowfive@57 374 local units = self:GetGroupUnitIdentifiers()
yellowfive@57 375
yellowfive@57 376 local msg = {}
yellowfive@57 377 table.insert(msg, L.VersionChatTitle)
yellowfive@57 378
yellowfive@57 379 for i, unitId in ipairs(units) do
yellowfive@57 380 local realm, name = self:GetRealmAndName(unitId)
yellowfive@57 381 if realm then
yellowfive@57 382 local key = toGroupVersionKey(realm, name)
yellowfive@57 383 local ver = Amr.GroupVersions[key]
yellowfive@57 384 if not ver then
yellowfive@57 385 table.insert(msg, key .. " |cFFFF0000" .. L.VersionChatNotInstalled .. "|r")
yellowfive@57 386 else
yellowfive@57 387 table.insert(msg, key .. " v" .. ver)
yellowfive@57 388 end
yellowfive@57 389 end
yellowfive@57 390 end
yellowfive@57 391
yellowfive@57 392 msg = table.concat(msg, "\n")
yellowfive@57 393 print(msg)
yellowfive@57 394 end
yellowfive@57 395
yellowfive@57 396 function Amr:InitializeVersions()
yellowfive@57 397 Amr:AddEventHandler("GROUP_ROSTER_UPDATE", pruneVersionInfo)
yellowfive@57 398 Amr:AddEventHandler("GROUP_ROSTER_UPDATE", sendVersionInfo)
yellowfive@57 399
yellowfive@57 400 -- request version information from anyone in my group upon initialization
yellowfive@57 401 if IsInGroup() or IsInRaid() then
yellowfive@57 402 Amr:SendAmrCommMessage(Amr.MessageTypes.VersionRequest)
yellowfive@57 403 end
yellowfive@57 404 end
yellowfive@57 405
yellowfive@57 406
yellowfive@57 407 ----------------------------------------------------------------------------------------
yellowfive@57 408 -- Generic Helpers
yellowfive@57 409 ----------------------------------------------------------------------------------------
yellowfive@57 410
yellowfive@57 411 local _waitTable = {}
yellowfive@57 412 local _waitFrame = nil
yellowfive@57 413
yellowfive@57 414 -- execute the specified function after the specified delay (in seconds)
yellowfive@57 415 function Amr.Wait(delay, func, ...)
yellowfive@57 416 if not _waitFrame then
yellowfive@57 417 _waitFrame = CreateFrame("Frame", "AmrWaitFrame", UIParent)
yellowfive@57 418 _waitFrame:SetScript("OnUpdate", function (self, elapse)
yellowfive@57 419 local count = #_waitTable
yellowfive@57 420 local i = 1
yellowfive@57 421 while(i <= count) do
yellowfive@57 422 local waitRecord = table.remove(_waitTable, i)
yellowfive@57 423 local d = table.remove(waitRecord, 1)
yellowfive@57 424 local f = table.remove(waitRecord, 1)
yellowfive@57 425 local p = table.remove(waitRecord, 1)
yellowfive@57 426 if d > elapse then
yellowfive@57 427 table.insert(_waitTable, i, { d-elapse, f, p })
yellowfive@57 428 i = i + 1
yellowfive@57 429 else
yellowfive@57 430 count = count - 1
yellowfive@57 431 f(unpack(p))
yellowfive@57 432 end
yellowfive@57 433 end
yellowfive@57 434 end)
yellowfive@57 435 end
yellowfive@57 436 table.insert(_waitTable, { delay, func, {...} })
yellowfive@57 437 return true
yellowfive@57 438 end
yellowfive@57 439
yellowfive@57 440 -- helper to iterate over a table in order by its keys
yellowfive@57 441 function Amr.spairs(t, order)
yellowfive@57 442 -- collect the keys
yellowfive@57 443 local keys = {}
yellowfive@57 444 for k in pairs(t) do keys[#keys+1] = k end
yellowfive@57 445
yellowfive@57 446 -- if order function given, sort by it by passing the table and keys a, b,
yellowfive@57 447 -- otherwise just sort the keys
yellowfive@57 448 if order then
yellowfive@57 449 table.sort(keys, function(a,b) return order(t, a, b) end)
yellowfive@57 450 else
yellowfive@57 451 table.sort(keys)
yellowfive@57 452 end
yellowfive@57 453
yellowfive@57 454 -- return the iterator function
yellowfive@57 455 local i = 0
yellowfive@57 456 return function()
yellowfive@57 457 i = i + 1
yellowfive@57 458 if keys[i] then
yellowfive@57 459 return keys[i], t[keys[i]]
yellowfive@57 460 end
yellowfive@57 461 end
yellowfive@57 462 end
yellowfive@57 463
yellowfive@57 464 function Amr.StartsWith(str, prefix)
yellowfive@57 465 if string.len(str) < string.len(prefix) then return false end
yellowfive@57 466 return string.sub(str, 1, string.len(prefix)) == prefix
yellowfive@57 467 end
yellowfive@57 468
yellowfive@124 469 function Amr.IsEmpty(table)
yellowfive@124 470 return next(table) == nil
yellowfive@124 471 end
yellowfive@124 472
yellowfive@124 473 function Amr.Contains(table, value)
yellowfive@124 474 if not table then return false end
yellowfive@124 475 for k,v in pairs(table) do
yellowfive@124 476 if v == value then
yellowfive@124 477 return true
yellowfive@124 478 end
yellowfive@124 479 end
yellowfive@124 480 return false
yellowfive@124 481 end
yellowfive@124 482
yellowfive@57 483 -- helper to get the unit identifiers (e.g. to pass to GetUnitName) for all members of the player's current group/raid
yellowfive@57 484 function Amr:GetGroupUnitIdentifiers()
yellowfive@57 485
yellowfive@57 486 local units = {}
yellowfive@57 487 if IsInRaid() then
yellowfive@57 488 for i = 1,40 do
yellowfive@57 489 table.insert(units, "raid" .. i)
yellowfive@57 490 end
yellowfive@57 491 elseif IsInGroup() then
yellowfive@57 492 table.insert(units, "player")
yellowfive@57 493 for i = 1,4 do
yellowfive@57 494 table.insert(units, "party" .. i)
yellowfive@57 495 end
yellowfive@57 496 else
yellowfive@57 497 table.insert(units, "player")
yellowfive@57 498 end
yellowfive@57 499
yellowfive@57 500 return units
yellowfive@57 501 end
yellowfive@57 502
yellowfive@57 503 -- helper to get the realm and name from a unitId (e.g. "player" or "raid1")
yellowfive@57 504 function Amr:GetRealmAndName(unitId)
yellowfive@57 505
yellowfive@57 506 local name = GetUnitName(unitId, true)
yellowfive@57 507 if not name then return end
yellowfive@57 508
yellowfive@57 509 local realm = GetRealmName()
yellowfive@57 510 local splitPos = string.find(name, "-")
yellowfive@57 511 if splitPos ~= nil then
yellowfive@57 512 realm = string.sub(name, splitPos + 1)
yellowfive@57 513 name = string.sub(name, 1, splitPos - 1)
yellowfive@57 514 end
yellowfive@57 515
yellowfive@57 516 return realm, name
yellowfive@57 517 end
yellowfive@57 518
yellowfive@57 519 -- find the unitid of a player given the name and realm... this comes from the server so the realm will be in english...
yellowfive@57 520 -- TODO: more robust handling of players with same name but different realms in the same group on non-english clients
yellowfive@57 521 function Amr:GetUnitId(unitRealm, unitName)
yellowfive@57 522
yellowfive@57 523 local nameMatches = {}
yellowfive@57 524
yellowfive@57 525 local units = Amr:GetGroupUnitIdentifiers()
yellowfive@57 526 for i, unitId in ipairs(units) do
yellowfive@57 527 local realm, name = Amr:GetRealmAndName(unitId)
yellowfive@57 528 if realm then
yellowfive@57 529 -- remove spaces to ensure proper matches
yellowfive@57 530 realm = string.gsub(realm, "%s+", "")
yellowfive@57 531 unitRealm = string.gsub(unitRealm, "%s+", "")
yellowfive@57 532
yellowfive@57 533 if unitRealm == realm and unitName == name then return unitId end
yellowfive@57 534 if unitName == name then
yellowfive@57 535 table.insert(nameMatches, unitId)
yellowfive@57 536 end
yellowfive@57 537 end
yellowfive@57 538 end
yellowfive@57 539
yellowfive@57 540 -- only one player with same name, must be the player of interest
yellowfive@57 541 if #nameMatches == 1 then return nameMatches[1] end
yellowfive@57 542
yellowfive@57 543 -- could not find or ambiguous
yellowfive@57 544 return nil
yellowfive@57 545 end
yellowfive@57 546
yellowfive@133 547 --[[
yellowfive@57 548 -- search the tooltip for txt, returns true if it is encountered on any line
yellowfive@57 549 function Amr:IsTextInTooltip(tt, txt)
yellowfive@57 550 local regions = { tt:GetRegions() }
yellowfive@57 551 for i, region in ipairs(regions) do
yellowfive@57 552 if region and region:GetObjectType() == "FontString" then
yellowfive@57 553 if region:GetText() == txt then
yellowfive@57 554 return true
yellowfive@57 555 end
yellowfive@57 556 end
yellowfive@57 557 end
yellowfive@57 558 return false
yellowfive@57 559 end
yellowfive@133 560 ]]
yellowfive@57 561
yellowfive@124 562 -- helper to determine if we can equip an item (it is soulbound)
yellowfive@59 563 function Amr:CanEquip(bagId, slotId)
yellowfive@124 564 local item = Item:CreateFromBagAndSlot(bagId, slotId)
yellowfive@124 565 if item then
yellowfive@124 566 local loc = item:GetItemLocation()
yellowfive@124 567 return C_Item.IsBound(loc)
yellowfive@124 568 else
yellowfive@124 569 -- for now just return true if we can't find the item... will get an error trying to equip if it isn't bound
yellowfive@124 570 return true
yellowfive@124 571 end
yellowfive@124 572
yellowfive@124 573 --local tt = Amr.GetItemTooltip(bagId, slotId)
yellowfive@124 574 --if self:IsTextInTooltip(tt, ITEM_SOULBOUND) then return true end
yellowfive@124 575 --if self:IsTextInTooltip(tt, ITEM_BNETACCOUNTBOUND) then return true end
yellowfive@124 576 --if self:IsTextInTooltip(tt, ITEM_ACCOUNTBOUND) then return true end
yellowfive@57 577 end
yellowfive@57 578
yellowfive@57 579 -- helper to determine if an item has a unique constraint
yellowfive@133 580 --[[
yellowfive@57 581 function Amr:IsUnique(bagId, slotId)
yellowfive@81 582 local tt = Amr.GetItemTooltip(bagId, slotId)
yellowfive@57 583 if self:IsTextInTooltip(tt, ITEM_UNIQUE_EQUIPPABLE) then return true end
yellowfive@57 584 if self:IsTextInTooltip(tt, ITEM_UNIQUE) then return true end
yellowfive@57 585 return false
yellowfive@57 586 end
yellowfive@133 587 ]]
yellowfive@57 588
yellowfive@57 589
yellowfive@57 590 ----------------------------------------------------------------------------------------
yellowfive@57 591 -- Inter-Addon Communication
yellowfive@57 592 ----------------------------------------------------------------------------------------
yellowfive@57 593 function Amr:SendAmrCommMessage(message, channel)
yellowfive@57 594 -- prepend version to all messages
yellowfive@57 595 local v = GetAddOnMetadata(Amr.ADDON_NAME, "Version")
yellowfive@57 596 message = v .. "\r" .. message
yellowfive@57 597
yellowfive@79 598 Amr:SendCommMessage(Amr.ChatPrefix, message, channel or (IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or "RAID"))
yellowfive@57 599 end
yellowfive@57 600
yellowfive@57 601 function Amr:OnCommReceived(prefix, message, distribution, sender)
yellowfive@57 602
yellowfive@57 603 local parts = {}
yellowfive@57 604 for part in string.gmatch(message, "([^\r]+)") do
yellowfive@57 605 table.insert(parts, part)
yellowfive@57 606 end
yellowfive@57 607
yellowfive@57 608 local ver = parts[1]
yellowfive@57 609 if ver then ver = tonumber(ver) end
yellowfive@57 610 if ver then
yellowfive@57 611 -- newest versions of the addon start all messages with a version number
yellowfive@57 612 message = parts[2]
yellowfive@57 613 end
yellowfive@57 614
yellowfive@57 615 -- we always allow version checks, even from old versions of the addon that aren't otherwise compatible
yellowfive@57 616 if Amr.StartsWith(message, Amr.MessageTypes.Version) then
yellowfive@57 617 -- version checking between group members
yellowfive@57 618 if Amr.StartsWith(message, Amr.MessageTypes.VersionRequest) then
yellowfive@57 619 sendVersionInfo()
yellowfive@57 620 else
yellowfive@57 621 onVersionInfoReceived(message)
yellowfive@57 622 end
yellowfive@57 623
yellowfive@57 624 return
yellowfive@57 625 end
yellowfive@57 626
yellowfive@57 627 -- any other kind of message is ignored if the version is too old
yellowfive@57 628 if not ver or ver < Amr.MIN_ADDON_VERSION then return end
yellowfive@57 629
yellowfive@124 630 --[[
yellowfive@57 631 if Amr.StartsWith(message, Amr.MessageTypes.Team) then
yellowfive@57 632 -- if fully initialized, process team optimizer messages
yellowfive@57 633 if Amr["ProcessTeamMessage"] then
yellowfive@57 634 Amr:ProcessTeamMessage(message)
yellowfive@57 635 end
yellowfive@57 636 else
yellowfive@57 637 -- if we are fully loaded, process a player snapshot when it is received (combat logging)
yellowfive@57 638 if Amr["ProcessPlayerSnapshot"] then
yellowfive@57 639 self:ProcessPlayerSnapshot(message)
yellowfive@57 640 end
yellowfive@57 641 end
yellowfive@124 642 ]]
yellowfive@57 643 end
yellowfive@57 644
yellowfive@57 645
yellowfive@57 646 ----------------------------------------------------------------------------------------
yellowfive@57 647 -- Events
yellowfive@57 648 ----------------------------------------------------------------------------------------
yellowfive@57 649 local _eventHandlers = {}
yellowfive@57 650
yellowfive@57 651 local function handleEvent(eventName, ...)
yellowfive@57 652 local list = _eventHandlers[eventName]
yellowfive@57 653 if list then
yellowfive@57 654 --print(eventName .. " handled")
yellowfive@57 655 for i, handler in ipairs(list) do
yellowfive@57 656 if type(handler) == "function" then
yellowfive@57 657 handler(select(1, ...))
yellowfive@57 658 else
yellowfive@57 659 Amr[handler](Amr, select(1, ...))
yellowfive@57 660 end
yellowfive@57 661 end
yellowfive@57 662 end
yellowfive@57 663 end
yellowfive@57 664
yellowfive@57 665 -- WoW and Ace seem to work on a "one handler" kind of approach to events (as far as I can tell from the sparse documentation of both).
yellowfive@57 666 -- This is a simple wrapper to allow adding multiple handlers to the same event, thus allowing better encapsulation of code from file to file.
yellowfive@57 667 function Amr:AddEventHandler(eventName, methodOrName)
yellowfive@57 668 local list = _eventHandlers[eventName]
yellowfive@57 669 if not list then
yellowfive@57 670 list = {}
yellowfive@57 671 _eventHandlers[eventName] = list
yellowfive@57 672 Amr:RegisterEvent(eventName, handleEvent)
yellowfive@57 673 end
yellowfive@57 674 table.insert(list, methodOrName)
yellowfive@57 675 end
yellowfive@57 676
yellowfive@57 677 Amr:AddEventHandler("PLAYER_ENTERING_WORLD", onPlayerEnteringWorld)
yellowfive@57 678
yellowfive@57 679
yellowfive@57 680 ----------------------------------------------------------------------------------------
yellowfive@57 681 -- Debugging
yellowfive@57 682 ----------------------------------------------------------------------------------------
yellowfive@124 683 function Amr:dump(o)
yellowfive@124 684 if type(o) == 'table' then
yellowfive@124 685 local s = '{ '
yellowfive@124 686 for k,v in pairs(o) do
yellowfive@124 687 if type(k) ~= 'number' then k = '"'..k..'"' end
yellowfive@124 688 s = s .. '['..k..'] = ' .. Amr:dump(v) .. ','
yellowfive@124 689 end
yellowfive@124 690 return s .. '} '
yellowfive@124 691 else
yellowfive@124 692 return tostring(o)
yellowfive@124 693 end
yellowfive@124 694 end
yellowfive@124 695
yellowfive@69 696 function Amr:Test()
yellowfive@124 697
yellowfive@69 698 end