/
MooSpec-1.0.lua
531 lines (474 loc) · 14.6 KB
/
MooSpec-1.0.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
--[[--------------------------------------------------------------------
Copyright (C) 2018 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
-- GLOBALS: assert
-- GLOBALS: LibStub
local MAJOR, MINOR = "MooSpec-1.0", 9
assert(LibStub, MAJOR .. " requires LibStub")
assert(LibStub("CallbackHandler-1.0", true), MAJOR .. " requires CallbackHandler-1.0")
assert(LibStub("MooInspect-1.0", true), MAJOR .. " requires MooInspect-1.0")
assert(LibStub("MooUnit-1.0", true), MAJOR .. " requires MooUnit-1.0")
local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end
------------------------------------------------------------------------
local format = string.format
local next = next
local pairs = pairs
local setmetatable = setmetatable
local strfind = string.find
local strjoin = strjoin
local strmatch = string.match
local tonumber = tonumber
local tostring = tostring
local tostringall = tostringall
local type = type
local wipe = wipe
-- GLOBALS: _G
-- GLOBALS: GetAddOnMetadata
local FONT_COLOR_CODE_CLOSE = FONT_COLOR_CODE_CLOSE
local GREEN_FONT_COLOR_CODE = GREEN_FONT_COLOR_CODE
local NORMAL_FONT_COLOR_CODE = NORMAL_FONT_COLOR_CODE
local MooInspect = LibStub("MooInspect-1.0")
local MooUnit = LibStub("MooUnit-1.0")
--[[--------------------------------------------------------------------
Debugging code from LibResInfo-1.0 by Phanx.
https://github.com/Phanx/LibResInfo
--]]--------------------------------------------------------------------
local isAddon = GetAddOnMetadata(MAJOR, "Version")
local DEBUG_LEVEL = isAddon and 2 or 0
local DEBUG_FRAME = ChatFrame3
local function debug(level, text, ...)
if level <= DEBUG_LEVEL then
if ... then
if type(text) == "string" and strfind(text, "%%[dfqsx%d%.]") then
text = format(text, ...)
else
text = strjoin(" ", tostringall(text, ...))
end
else
text = tostring(text)
end
DEBUG_FRAME:AddMessage(GREEN_FONT_COLOR_CODE .. MAJOR .. FONT_COLOR_CODE_CLOSE .. " " .. text)
end
end
if isAddon then
-- GLOBALS: SLASH_MOOSPEC1
-- GLOBALS: SlashCmdList
SLASH_MOOSPEC1 = "/moospec"
SlashCmdList.MOOSPEC = function(input)
input = tostring(input or "")
local CURRENT_CHAT_FRAME
for i = 1, 10 do
local cf = _G["ChatFrame"..i]
if cf and cf:IsVisible() then
CURRENT_CHAT_FRAME = cf
break
end
end
local of = DEBUG_FRAME
DEBUG_FRAME = CURRENT_CHAT_FRAME
if strmatch(input, "^%s*[0-9]%s*$") then
local v = tonumber(input)
debug(0, "Debug level set to", input)
DEBUG_LEVEL = v
DEBUG_FRAME = of
return
end
local f = _G[input]
if type(f) == "table" and type(f.AddMessage) == "function" then
debug(0, "Debug frame set to", input)
DEBUG_FRAME = f
return
end
debug(0, "Version " .. MINOR .. " loaded. Usage:")
debug(0, format("%s%s %s%s - change debug verbosity, valid range is 0-6",
NORMAL_FONT_COLOR_CODE, SLASH_MOOSPEC1, DEBUG_LEVEL, FONT_COLOR_CODE_CLOSE))
debug(0, format("%s%s %s%s -- change debug output frame",
NORMAL_FONT_COLOR_CODE, SLASH_MOOSPEC1, of:GetName(), FONT_COLOR_CODE_CLOSE))
DEBUG_FRAME = of
end
end
------------------------------------------------------------------------
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.callbacksInUse = lib.callbacksInUse or {}
local eventFrame = lib.eventFrame or CreateFrame("Frame")
lib.eventFrame = eventFrame
eventFrame:UnregisterAllEvents()
local function OnEvent(frame, event, ...)
return frame[event] and frame[event](frame, event, ...)
end
eventFrame:SetScript("OnEvent", OnEvent)
function lib.callbacks:OnUsed(lib, callback)
if not next(lib.callbacksInUse) then
debug(1, "Callbacks in use! Starting up...")
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
eventFrame:RegisterEvent("ROLE_CHANGED_INFORM")
MooInspect.RegisterCallback(eventFrame, "MooInspect_InspectReady", "OnInspectReady")
MooUnit.RegisterCallback(eventFrame, "MooUnit_UnitChanged", "OnUnitChanged")
MooUnit.RegisterCallback(eventFrame, "MooUnit_UnitJoined", "OnUnitJoined")
MooUnit.RegisterCallback(eventFrame, "MooUnit_UnitLeft", "OnUnitLeft")
end
lib.callbacksInUse[callback] = true
end
function lib.callbacks:OnUnused(lib, callback)
lib.callbacksInUse[callback] = nil
if not next(lib.callbacksInUse) then
debug(1, "No callbacks in use. Shutting down...")
eventFrame:UnregisterAllEvents()
MooInspect.UnregisterCallback(eventFrame, "MooInspect_InspectReady")
MooUnit.UnregisterCallback(eventFrame, "MooUnit_UnitChanged")
MooUnit.UnregisterCallback(eventFrame, "MooUnit_UnitJoined")
MooUnit.UnregisterCallback(eventFrame, "MooUnit_UnitLeft")
end
end
------------------------------------------------------------------------
-- GLOBALS: GetInspectSpecialization
-- GLOBALS: GetSpecialization
-- GLOBALS: GetSpecializationInfo
-- GLOBALS: UnitClass
-- GLOBALS: UnitGroupRolesAssigned
-- GLOBALS: UnitGUID
-- GLOBALS: UnitIsPlayer
-- Preserve mappings across library upgrades.
local blizzardRoleByGUID = lib.blizzardRoleByGUID or {}
local classByGUID = lib.classByGUID or {}
local roleByGUID = lib.roleByGUID or {}
local specializationByGUID = lib.specializationByGUID or {}
lib.blizzardRoleByGUID = blizzardRoleByGUID
lib.classByGUID = classByGUID
lib.roleByGUID = roleByGUID
lib.specializationByGUID = specializationByGUID
local playerGUID = UnitGUID("player")
-- Default roles for each class.
local roleByClass = {
DEATHKNIGHT = "melee",
DEMONHUNTER = "melee",
DRUID = "ranged",
HUNTER = "ranged",
MAGE = "ranged",
MONK = "melee",
PALADIN = "melee",
PRIEST = "ranged",
ROGUE = "melee",
SHAMAN = "ranged",
WARLOCK = "ranged",
WARRIOR = "melee",
}
-- Map return values from GetInspectSpecialization() to roles.
-- ref: https://www.wowpedia.org/API_GetInspectSpecialization
local roleBySpecialization = {
-- Death Knight
[250] = "tank", -- Blood
[251] = "melee", -- Frost
[252] = "melee", -- Unholy
-- Demon Hunter
[577] = "melee", -- Havoc
[581] = "tank", -- Vengeance
-- Druid
[102] = "ranged", -- Balance
[103] = "melee", -- Feral
[104] = "tank", -- Guardian
[105] = "healer", -- Restoration
-- Hunter
[253] = "ranged", -- Beast Mastery
[254] = "ranged", -- Marksmanship
[255] = "melee", -- Survival
-- Mage
[62] = "ranged", -- Arcane
[63] = "ranged", -- Fire
[64] = "ranged", -- Frost
-- Monk
[268] = "tank", -- Brewmaster
[270] = "healer", -- Mistweaver
[269] = "melee", -- Windwalker
-- Paladin
[65] = "healer", -- "Holy
[66] = "tank", -- "Protection
[70] = "melee", -- "Retribution
-- Priest
[256] = "healer", -- Discipline
[257] = "healer", -- Holy
[258] = "ranged", -- Shadow
-- Rogue
[259] = "melee", -- Assassination
[260] = "melee", -- Outlaw
[261] = "melee", -- Subtlety
-- Shaman
[262] = "ranged", -- Elemental
[263] = "melee", -- Enhancement
[264] = "healer", -- Restoration
-- Warlock
[265] = "ranged", -- Affliction
[266] = "ranged", -- Demonology
[267] = "ranged", -- Destruction
-- Warrior
[71] = "melee", -- Arms
[72] = "melee", -- Fury
[73] = "tank", -- Protection
}
-- Map return values from GetInspectSpecialization() to names.
local specializationName = {
-- Death Knight
[250] = "blood",
[251] = "frost",
[252] = "unholy",
-- Demon Hunter
[577] = "havoc",
[581] = "vengeance",
-- Druid
[102] = "balance",
[103] = "feral",
[104] = "guardian",
[105] = "restoration",
-- Hunter
[253] = "beast_mastery",
[254] = "marksmanship",
[255] = "survival",
-- Mage
[62] = "arcane",
[63] = "fire",
[64] = "frost",
-- Monk
[268] = "brewmaster",
[270] = "mistweaver",
[269] = "windwalker",
-- Paladin
[65] = "holy",
[66] = "protection",
[70] = "retribution",
-- Priest
[256] = "discipline",
[257] = "holy",
[258] = "shadow",
-- Rogue
[259] = "assassination",
[260] = "outlaw",
[261] = "subtlety",
-- Shaman
[262] = "elemental",
[263] = "enhancement",
[264] = "restoration",
-- Warlock
[265] = "affliction",
[266] = "demonology",
[267] = "destruction",
-- Warrior
[71] = "arms",
[72] = "fury",
[73] = "protection",
}
local blizzardRoleByRole = {
tank = "TANK",
healer = "HEALER",
melee = "DAMAGER",
ranged = "DAMAGER",
none = "NONE",
}
local function UpdateBlizzardRole(guid, unit, role)
local oldRole = blizzardRoleByGUID[guid] or "NONE"
if oldRole ~= role then
blizzardRoleByGUID[guid] = role
debug(2, "MooSpec_UnitBlizzardRoleChanged", guid, unit, oldRole, role)
lib.callbacks:Fire("MooSpec_UnitBlizzardRoleChanged", guid, unit, oldRole, role)
end
end
local function UpdateRole(guid, unit, role)
local oldRole = roleByGUID[guid] or "none"
if role and role ~= "none" then
-- Only update the role if it's not "none".
if oldRole ~= role then
roleByGUID[guid] = role
debug(2, "MooSpec_UnitRoleChanged", guid, unit, oldRole, role)
lib.callbacks:Fire("MooSpec_UnitRoleChanged", guid, unit, oldRole, role)
end
end
end
local function UpdateSpecialization(guid, unit, specialization)
local oldSpecialization = specializationByGUID[guid] or 0 -- zero seems to mean "can't get specialization"
if specialization and roleBySpecialization[specialization] then
-- Only update the specialization if it can be validated as a possible specialization ID.
if oldSpecialization ~= specialization then
specializationByGUID[guid] = specialization
debug(2, "MooSpec_UnitSpecializationChanged", guid, unit, oldSpecialization, specialization)
lib.callbacks:Fire("MooSpec_UnitSpecializationChanged", guid, unit, oldSpecialization, specialization)
end
end
end
local function UpdateClass(guid, unit)
-- Only update class if it hasn't been determined yet.
if not classByGUID[guid] then
unit = unit or MooUnit:GetUnitByGUID(guid)
if unit then
local _, class = UnitClass(unit)
if class then
classByGUID[guid] = class
-- Set a default role if this unit had no previous role.
if not roleByGUID[guid] then
local role = roleByClass[class]
UpdateRole(guid, unit, role)
end
debug(2, "MooSpec_UnitClass", guid, unit, class)
lib.callbacks:Fire("MooSpec_UnitClass", guid, unit, class)
end
end
end
end
local function UpdateUnit(guid, unit)
unit = unit or MooUnit:GetUnitByGUID(guid)
if unit then
local specialization
if guid == playerGUID then
-- The player's specialization information doesn't need to come through the
-- inspection API -- use GetSpecialization() directly.
local index = GetSpecialization()
if index then
specialization = GetSpecializationInfo(index)
end
elseif UnitIsPlayer(unit) then
specialization = GetInspectSpecialization(unit)
end
if specialization then
debug(3, "UpdateUnit", guid, unit)
-- Validate the return value against the table of possible specialization IDs.
if roleBySpecialization[specialization] then
UpdateSpecialization(guid, unit, specialization)
local role = roleBySpecialization[specialization]
UpdateRole(guid, unit, role)
local blizzardRole = lib:GetBlizzardRole(guid)
UpdateBlizzardRole(guid, unit, blizzardRole)
else
local class = lib:GetClass(guid)
debug(1, "Unknown player specialization:", guid, class, specialization)
end
end
else
-- GUID no longer maps to a usable unit ID.
eventFrame:OnUnitLeft("UpdateUnit", guid)
end
end
function lib:GetClass(guid)
UpdateClass(guid)
return classByGUID[guid]
end
function lib:GetBlizzardRole(guid)
if not blizzardRoleByGUID[guid] then
local unit = MooUnit:GetUnitByGUID(guid)
if unit then
blizzardRoleByGUID[guid] = UnitGroupRolesAssigned(unit)
end
end
return blizzardRoleByGUID[guid] or "NONE"
end
function lib:GetRole(guid)
UpdateClass(guid)
return roleByGUID[guid] or "none"
end
function lib:GetSpecialization(guid)
local specialization = specializationByGUID[guid]
if specialization then
local name = self:GetSpecializationName(specialization)
return specialization, name
end
return nil
end
function lib:GetSpecializationName(specialization)
return specializationName[specialization]
end
------------------------------------------------------------------------
-- GLOBALS: UnitGUID
-- GLOBALS: UnitIsPlayer
-- GLOBALS: UnitIsUnit
local function OnUnitSpecializationChanged(event, unit)
debug(3, "OnUnitSpecializationChanged", event, unit)
if unit == "player" or UnitIsUnit(unit, "player") then
-- No need to inspect the player as the specialization info is already available.
UpdateClass(playerGUID, "player")
UpdateUnit(playerGUID, "player")
elseif UnitIsPlayer(unit) then
local guid = UnitGUID(unit)
if guid then
MooInspect:QueueInspect(guid)
end
end
end
function eventFrame:PLAYER_ENTERING_WORLD(event)
OnUnitSpecializationChanged(event, "player")
end
function eventFrame:PLAYER_SPECIALIZATION_CHANGED(event, unit)
OnUnitSpecializationChanged(event, unit)
end
function eventFrame:ROLE_CHANGED_INFORM(event, changedPlayer, changedBy, oldRole, newRole)
debug(3, event, changedPlayer, changedBy, oldRole, newRole)
local guid = MooUnit:GetGUIDByName(changedPlayer)
if guid then
local unit = MooUnit:GetUnitByGUID(guid)
if unit then
UpdateBlizzardRole(guid, unit, newRole)
else
-- GUID no longer maps to a usable unit ID.
self:OnUnitLeft(event, guid)
end
end
end
function eventFrame:OnInspectReady(event, guid)
debug(3, event, guid)
UpdateClass(guid)
UpdateUnit(guid)
end
local function QueueUnit(guid, unit)
-- Set a default role if this unit had no previous role.
UpdateClass(guid, unit)
local role = lib:GetRole(guid)
if role == "none" then
local class = lib:GetClass(guid)
if class then
role = roleByClass[class]
UpdateRole(guid, unit, role)
end
end
MooInspect:QueueInspect(guid)
end
function eventFrame:OnUnitJoined(event, guid, unit)
if UnitIsPlayer(unit) then
debug(3, "OnUnitJoined", event, guid, unit)
UpdateClass(guid, unit)
QueueUnit(guid, unit)
end
end
function eventFrame:OnUnitLeft(event, guid)
debug(3, "OnUnitLeft", event, guid)
MooInspect:CancelInspect(guid)
blizzardRoleByGUID[guid] = nil
classByGUID[guid] = nil
roleByGUID[guid] = nil
specializationByGUID[guid] = nil
end
function eventFrame:OnUnitChanged(event, guid, unit, name)
if MooUnit:IsGroupUnit(unit) and UnitIsPlayer(unit) then
debug(3, "OnUnitChanged", event, guid, unit, name)
UpdateClass(guid, unit)
QueueUnit(guid, unit)
end
end
------------------------------------------------------------------------
-- GLOBALS: UnitGUID
-- GLOBALS: UnitIsPlayer
function lib:InspectUnit(unit)
if UnitIsPlayer(unit) then
local guid = UnitGUID(unit)
if guid then
UpdateClass(guid, unit)
QueueUnit(guid, unit)
end
end
end
function lib:InspectRoster()
for guid, unit in MooUnit:IterateRoster() do
if UnitIsPlayer(unit) then
UpdateClass(guid, unit)
QueueUnit(guid, unit)
end
end
end