-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombatLog.lua
More file actions
executable file
·504 lines (444 loc) · 15.5 KB
/
Copy pathCombatLog.lua
File metadata and controls
executable file
·504 lines (444 loc) · 15.5 KB
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
---@class AddOn
local AddOn = (select(2, ...))
local COMBAT_LOG_FILTER_PET = AddOn.COMBAT_LOG_FILTER_PET
local MAX_CACHED_EVENTS_PER_FRAME = AddOn.MAX_CACHED_EVENTS_PER_FRAME
local next = next
local strsplit = strsplit
local ActiveSegments = AddOn.ActiveSegments
local GUIDIsPlayer = C_PlayerInfo.GUIDIsPlayer
local GetPlayerClass = AddOn.GetPlayerClass
local GetPlayerName = AddOn.GetPlayerName
local SafePack = AddOn.SafePack
local SafeUnpack = AddOn.SafeUnpack
local StartCombat = AddOn.StartCombat
local UnitClassBase = UnitClassBase
local UnitGUID = UnitGUID
local UnitName = UnitName
local UnitTokenFromGUID = UnitTokenFromGUID
local function nop() end
---@alias CheckCombatHandler fun(timestamp:number, hideCaster:boolean, sourceGUID?:string, sourceName?:string, sourceFlags?:number, sourceRaidFlags?:number, destGUID?:string, destName?:string, destFlags?:number, destRaidFlags?:number, ...:any):boolean?
---@type table<string, CheckCombatHandler>
local checkCombatHandlers = setmetatable({}, {
---@param t table<string, CheckCombatHandler>
---@param k string
---@return CheckCombatHandler
__index = function(t, k)
local v = nop
t[k] = v
return v
end,
})
AddOn.CheckCombatHandlers = checkCombatHandlers
---@param timestamp number
---@param subEvent string
---@param ... any
local function checkCombat(timestamp, subEvent, ...)
if checkCombatHandlers[subEvent](timestamp, ...) then StartCombat(timestamp) end
end
---@alias StaticCombatLogHandler fun(timestamp: number, hideCaster: boolean, sourceGUID?: string, sourceName?: string, sourceFlags?: number, sourceRaidFlags?: number, destGUID?: string, destName?: string, destFlags?: number, destRaidFlags?: number, ...: any)
---@type table<string, StaticCombatLogHandler>
local staticCombatLogHandlers = setmetatable({}, {
---@param t table<string, StaticCombatLogHandler>
---@param k string
---@return StaticCombatLogHandler
__index = function(t, k)
local v = nop
t[k] = v
return v
end,
})
AddOn.StaticCombatLogHandlers = staticCombatLogHandlers
---@param timestamp number
---@param subEvent string
---@param ... any
local function handleStaticEvent(timestamp, subEvent, ...) staticCombatLogHandlers[subEvent](timestamp, ...) end
---@alias CombatLogHandler fun(segment: Segment, timestamp: number, hideCaster: boolean, sourceGUID?: string, sourceName?: string, sourceFlags?: number, sourceRaidFlags?: number, destGUID?: string, destName?: string, destFlags?: number, destRaidFlags?: number, ...: any)
---@type table<string, CombatLogHandler>
local combatLogHandlers = setmetatable({}, {
---@param t table<string, CombatLogHandler>
---@param k string
---@return CombatLogHandler
__index = function(t, k)
local v = nop
t[k] = v
return v
end,
})
AddOn.CombatLogHandlers = combatLogHandlers
---@param segment Segment
---@param timestamp number
---@param subEvent string
---@param ... any
local function handleCombatLogEvent(segment, timestamp, subEvent, ...)
combatLogHandlers[subEvent](segment, timestamp, ...)
end
---@type table<string, string?>
local names = {}
---@param unitToken string
local function updateName(unitToken)
local name, realm = UnitName(unitToken)
if name and name ~= "" then
if realm and realm ~= "" then
names[UnitGUID(unitToken)] = name .. " - " .. realm
else
names[UnitGUID(unitToken)] = name
end
end
end
---@param guid string
---@param default? string
---@return string
local function getName(guid, default)
local name = names[guid]
if not name then
local unitToken = UnitTokenFromGUID(guid)
if unitToken then
name = UnitName(unitToken)
names[guid] = name
end
if not name and GUIDIsPlayer(guid) then
local name2, fullName, shortName = GetPlayerName(guid)
names[guid] = fullName
end
end
return name or default or "UNKNOWN"
end
AddOn.GetName = getName
---@type table<string, ClassFile?>
local classes = {}
---@param unitToken string
local function updateClass(unitToken)
if UnitExists(unitToken) then
local class = UnitClassBase(unitToken)
if class then
local guid = UnitGUID(unitToken)
if guid then
classes[guid] = class
local unitType, _, serverID, instanceID, zoneUID, npcID, spawnUID = strsplit("-", guid)
if unitType == "Creature" then if npcID then classes[npcID] = class end end
end
local name = UnitName(unitToken)
if name then classes[name] = class end
end
end
end
---@param guid string
---@return ClassFile?
local function getClass(guid)
local class = classes[guid]
if not class then
local unitType, _, serverID, instanceID, zoneUID, npcID, spawnUID = strsplit("-", guid)
if unitType == "Creature" then
if npcID then
class = classes[npcID]
if class then return class end
end
else
npcID = nil
end
local unitToken = UnitTokenFromGUID(guid)
if unitToken then
class = UnitClassBase(unitToken)
if npcID then
classes[npcID] = class
return class
end
local name = UnitName(unitToken)
if name then
class = classes[name]
if not class then
class = UnitClassBase(unitToken)
classes[name] = class
end
end
end
if not class and GUIDIsPlayer(guid) then
class = GetPlayerClass(guid)
classes[guid] = class
end
end
return class
end
AddOn.GetClass = getClass
---@type table<string, string?>
local petOwners = {}
---@param unitToken string
local function updatePet(unitToken)
local petToken = unitToken .. "pet"
local petGUID = UnitExists(petToken) and UnitGUID(petToken)
if petGUID then
petOwners[petGUID] = UnitGUID(unitToken)
updateClass(petToken)
updateName(petToken)
end
end
---@type fun(petGUID:string):string
local findPetOwner
do -- findPetOwner
---@type GameTooltip
---@diagnostic disable-next-line:assign-type-mismatch
local tooltip = CreateFrame("GameTooltip", nil, nil, "GameTooltipTemplate")
tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
local unitTooltipDataType = Enum.TooltipDataType.Unit
---@param petGUID string
---@return string
function findPetOwner(petGUID)
tooltip:ClearLines()
tooltip:SetHyperlink("unit:" .. petGUID)
--[[ tooltip:Show() ]]
if tooltip:IsTooltipType(unitTooltipDataType) then ---@diagnostic disable-line:undefined-field
local tooltipData = tooltip:GetPrimaryTooltipData() ---@diagnostic disable-line:undefined-field
if tooltipData then
local lines = tooltipData.lines
if lines then
for i = 1, #lines, 1 do
local guid = lines[i].guid
if guid and guid ~= petGUID then
petOwners[petGUID] = guid
tooltip:Hide()
return guid
end
end
end
end
end
tooltip:Hide()
return petGUID
end
end
---@param petGUID string
---@return string
local function getPetOwner(petGUID) return petOwners[petGUID] or findPetOwner(petGUID) or petGUID end
AddOn.GetPetOwner = getPetOwner
local frame = CreateFrame("Frame")
---@type fun(timestamp:number, subEvent:string, ...)
local onCombatLogEvent = function() end
local combatLogGetCurrentEventInfo = CombatLogGetCurrentEventInfo
frame:SetScript("OnEvent", --
---@param self Frame
---@param event WowEvent
---@param ... any
function(self, event, ...)
if event == "COMBAT_LOG_EVENT_UNFILTERED" then
onCombatLogEvent(combatLogGetCurrentEventInfo()) ---@diagnostic disable-line:param-type-mismatch
elseif event == "PLAYER_TARGET_CHANGED" then
updateClass("target")
elseif event == "UNIT_PET" then
---@type UnitToken
local unitTarget = ...
updatePet(unitTarget)
elseif event == "GROUP_ROSTER_UPDATE" then
elseif event == "PLAYER_ENTERING_WORLD" then
local isInitialLogin, isReloadingUi = ...
if isInitialLogin or isReloadingUi then
updateClass("player")
updateName("player")
updatePet("player")
end
else
self:UnregisterEvent(event)
end
end)
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
frame:RegisterEvent("PLAYER_TARGET_CHANGED")
frame:RegisterEvent("UNIT_PET")
frame:RegisterEvent("GROUP_ROSTER_UPDATE")
frame:RegisterEvent("PLAYER_ENTERING_WORLD")
---@class CombatLogEventQueue
local eventQueue = {}
do -- CombatLogEventQueue
local first = 0
local last = -1
local list = {}
---@param value SafeTable
function eventQueue.Push(value)
last = last + 1
list[last] = value
end
---@return SafeTable?
function eventQueue.Pop()
if first <= last then
local value = list[first]
list[first] = nil
first = first + 1
return value
end
end
---@param offset? number
---@return SafeTable?
function eventQueue.Peek(offset)
local index = first - (offset or 0)
if index <= last then return list[index] end
end
end
frame:SetScript("OnUpdate", --
---@param self Frame
---@param elapsed number
function(self, elapsed)
local processedEvents = 0
local event = eventQueue.Pop()
while event do
handleCombatLogEvent(SafeUnpack(event))
processedEvents = processedEvents + 1
if processedEvents < MAX_CACHED_EVENTS_PER_FRAME then
event = eventQueue.Pop()
else
break
end
end
end)
local cacheEnabled
---@param enabled boolean
function AddOn.SetCacheEnabled(enabled)
if enabled ~= cacheEnabled then
cacheEnabled = enabled
if enabled then
onCombatLogEvent = function(timestamp, subEvent, ...)
if not ActiveSegments.combat then checkCombat(timestamp, subEvent, ...) end
handleStaticEvent(timestamp, subEvent, ...)
for key, segment in next, ActiveSegments, nil do
eventQueue.Push(SafePack(segment, timestamp, subEvent, ...))
end
end
else
local event = eventQueue.Pop()
while event do
handleCombatLogEvent(SafeUnpack(event))
event = eventQueue.Pop()
end
onCombatLogEvent = function(timestamp, subEvent, ...)
if not ActiveSegments.combat then checkCombat(timestamp, subEvent, ...) end
handleStaticEvent(timestamp, subEvent, ...)
for key, segment in next, ActiveSegments, nil do
handleCombatLogEvent(segment, timestamp, subEvent, ...)
end
end
end
end
end
AddOn.SetCacheEnabled(false)
---@return boolean
function AddOn.IsCacheEnabled() return cacheEnabled end
---@param timestamp number
---@param hideCaster boolean
---@param sourceGUID? string
---@param sourceName? string
---@param sourceFlags? number
---@param sourceRaidFlags? number
---@param destGUID? string
---@param destName? string
---@param destFlags? number
---@param destRaidFlags? number
---@param spellId number
---@param spellName string
---@param spellSchool number
function staticCombatLogHandlers.SPELL_SUMMON(timestamp, hideCaster, sourceGUID, sourceName, sourceFlags,
sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, spellId,
spellName, spellSchool)
if sourceGUID and destGUID and sourceGUID ~= "" and destGUID ~= "" then petOwners[destGUID] = sourceGUID end
end
local combatLog_Object_IsA = CombatLog_Object_IsA
---@param sources table<string, UnitTable>
---@param sourceGUID? string
---@param sourceName? string
---@param sourceFlags? number
---@return UnitTable
---@return boolean
---@return boolean
---@return string?
function AddOn.GetSource(sources, sourceGUID, sourceName, sourceFlags)
local isNew = false
local isPet = false
if sourceFlags then isPet = combatLog_Object_IsA(sourceFlags, COMBAT_LOG_FILTER_PET) end
local source = sourceGUID and sources[sourceGUID] or sourceName and sources[sourceName]
if source then
if not source.class and sourceGUID then source.class = getClass(sourceGUID) end
return source, false, isPet, sourceName
end
sourceGUID = (sourceGUID and sourceGUID ~= "") and sourceGUID or nil
local key
if isPet and sourceGUID then
local ownerGUID = getPetOwner(sourceGUID)
if ownerGUID ~= sourceGUID then
source = ownerGUID and sources[ownerGUID]
if not source then
source = {name = getName(ownerGUID), class = getClass(ownerGUID)}
key = ownerGUID
isNew = true
end
end
elseif sourceGUID then
source = {name = getName(sourceGUID, sourceName), class = getClass(sourceGUID)}
key = GUIDIsPlayer(sourceGUID) and sourceGUID or sourceName or "UNKNOWN"
isNew = true
end
if not source then
source = {name = sourceName or "UNKNOWN"}
key = sourceName or "UNKNOWN"
isNew = true
end
if isNew then sources[key] = source end
return source, isNew, isPet, sourceName
end
---@param spells table<string|number, SpellTable>
---@param spellId number|string
---@param spellSchool number
---@return SpellTable
---@return boolean isNew
function AddOn.GetSpell(spells, spellId, spellSchool)
local isNew = false
local spell = spells[spellId]
if not spell then
---@class SpellTable
spell = {school = spellSchool}
isNew = true
end
if isNew then spells[spellId] = spell end
return spell, isNew
end
---@param targets table<string, UnitTable>
---@param destGUID? string
---@param destName? string
---@param destFlags? number
---@return UnitTable
---@return boolean
function AddOn.GetTarget(targets, destGUID, destName, destFlags)
local isNew = false
local key
local target = destGUID and targets[destGUID]
if target then
if not target.class and destGUID then target.class = getClass(destGUID) end
else
if destGUID and GUIDIsPlayer(destGUID) then
target = {name = getName(destGUID, destName), class = getClass(destGUID)}
isNew = true
key = destGUID
elseif destGUID then
local name = getName(destGUID, destName) or "UNKNOWN"
target = targets[name]
if not target then
target = {name = name, class = getClass(destGUID)}
isNew = true
key = name
elseif not target.class then
target.class = getClass(destGUID)
end
elseif destName then
target = targets[destName]
if not target then
target = {name = destName}
isNew = true
key = destName
end
else
target = targets["UNKNOWN"]
if not target then
target = {name = "UNKNOWN"}
isNew = true
key = "UNKNOWN"
end
end
end
if isNew then targets[key] = target end
return target, isNew
end