-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMSA_BuffBridge.lua
More file actions
1432 lines (1247 loc) · 48.7 KB
/
MSA_BuffBridge.lua
File metadata and controls
1432 lines (1247 loc) · 48.7 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
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ########################################################
-- MSA_BuffBridge.lua (v5 - EQoL-style event-driven cache)
--
-- Central aura data layer for BUFF_AURA mode icons.
-- Replaces per-tick GetUnitAuras polling with event-driven
-- dirty-flag cache. CDM frame scanner uses dirty-flag too.
--
-- Architecture:
-- UNIT_AURA(player/target) -> set dirty flag
-- Next read from UpdateEngine -> rebuild cache once
-- All subsequent reads in same tick -> cached O(1)
--
-- Secret-safe: no comparisons on tainted values.
-- Zero idle CPU: no OnUpdate, no timers, pure event relay.
-- ########################################################
local MSWA = _G.MSWA
if type(MSWA) ~= "table" then return end
local type, tostring, tonumber = type, tostring, tonumber
local pcall = pcall
local GetTime = GetTime
local wipe = wipe or table.wipe
-----------------------------------------------------------
-- API capability detection (once at load time)
-----------------------------------------------------------
local C_UnitAuras = C_UnitAuras
local C_Spell = C_Spell
local hasGetUnitAuras = C_UnitAuras and C_UnitAuras.GetUnitAuras
local hasGetPlayerAura = C_UnitAuras and C_UnitAuras.GetPlayerAuraBySpellID
local hasGetCDAura = C_UnitAuras and C_UnitAuras.GetCooldownAuraBySpellID
local hasGetAuraCount = C_UnitAuras and C_UnitAuras.GetAuraApplicationDisplayCount
local _issecretvalue = _G.issecretvalue
-----------------------------------------------------------
-- Player aura cache (event-driven, not per-tick)
--
-- _playerDirty: true = cache stale, needs rebuild
-- _playerCache[spellId] = auraData
-- Rebuilt ONLY on first read after UNIT_AURA fires.
-----------------------------------------------------------
local _playerCache = {} -- [spellId] = auraData
local _playerDirty = true -- start dirty to force initial build
local function RebuildPlayerCache()
if not _playerDirty then return end
_playerDirty = false
wipe(_playerCache)
if not hasGetUnitAuras then return end
-- EQoL pattern: GetUnitAuras returns all auras with
-- whitelisted/readable fields (Midnight 12.0 safe)
local auras = C_UnitAuras.GetUnitAuras("player", "HELPFUL")
if type(auras) ~= "table" then return end
for i = 1, #auras do
local a = auras[i]
if a then
local sid = a.spellId
-- Only cache if spellId is readable (not secret)
if sid and not (_issecretvalue and _issecretvalue(sid)) then
if not _playerCache[sid] then
_playerCache[sid] = a
end
end
end
end
end
-----------------------------------------------------------
-- Target aura cache (same pattern, separate dirty flag)
-----------------------------------------------------------
local _targetCache = {}
local _targetDirty = true
local function RebuildTargetCache()
if not _targetDirty then return end
_targetDirty = false
wipe(_targetCache)
if not hasGetUnitAuras then return end
local auras = C_UnitAuras.GetUnitAuras("target", "HELPFUL")
if type(auras) ~= "table" then return end
for i = 1, #auras do
local a = auras[i]
if a then
local sid = a.spellId
if sid and not (_issecretvalue and _issecretvalue(sid)) then
if not _targetCache[sid] then
_targetCache[sid] = a
end
end
end
end
-- Also scan HARMFUL for target debuffs
local debuffs = C_UnitAuras.GetUnitAuras("target", "HARMFUL")
if type(debuffs) ~= "table" then return end
for i = 1, #debuffs do
local a = debuffs[i]
if a then
local sid = a.spellId
if sid and not (_issecretvalue and _issecretvalue(sid)) then
if not _targetCache[sid] then
_targetCache[sid] = a
end
end
end
end
end
-----------------------------------------------------------
-- Public aura API
-----------------------------------------------------------
--- Invalidate player buff cache (called from events)
function MSWA_InvalidateBuffCache()
_playerDirty = true
end
--- Invalidate target cache
function MSWA_InvalidateTargetCache()
_targetDirty = true
end
--- Get player aura data by spell ID (event-driven cache)
function MSWA_GetPlayerAuraDataBySpellID(spellID)
if not spellID then return nil end
-- Primary: event-driven cache (EQoL Midnight pattern)
if hasGetUnitAuras then
RebuildPlayerCache()
local cached = _playerCache[spellID]
if cached then return cached end
end
-- Fallback: GetPlayerAuraBySpellID (works for some buffs)
if hasGetPlayerAura then
local data = C_UnitAuras.GetPlayerAuraBySpellID(spellID)
if data then return data end
end
-- Last resort: GetCooldownAuraBySpellID (CD-only auras)
if hasGetCDAura then
local ok, data = pcall(C_UnitAuras.GetCooldownAuraBySpellID, spellID)
if ok and type(data) == "table" then return data end
end
return nil
end
--- Get aura data for arbitrary unit by spell ID
function MSWA_GetAuraDataForUnit(unit, spellID)
if not unit or not spellID then return nil end
if unit == "player" then
return MSWA_GetPlayerAuraDataBySpellID(spellID)
end
if unit == "target" then
if hasGetUnitAuras then
RebuildTargetCache()
local cached = _targetCache[spellID]
if cached then return cached end
end
return nil
end
-- Generic unit: direct API call (no cache)
if hasGetUnitAuras then
local auras = C_UnitAuras.GetUnitAuras(unit, "HELPFUL")
if type(auras) == "table" then
for i = 1, #auras do
local a = auras[i]
if a and a.spellId == spellID then return a end
end
end
end
return nil
end
--- Check if a value is a Midnight secret value
function MSWA_IsSecretValue(val)
if val == nil then return false end
if _issecretvalue and _issecretvalue(val) then return true end
return false
end
--- Safe field read: returns nil for secret values
function MSWA_SafeAuraField(auraData, fieldName)
if not auraData then return nil end
local val = auraData[fieldName]
if val == nil then return nil end
if _issecretvalue and _issecretvalue(val) then return nil end
return val
end
--- Stack text (EQoL signature: GetAuraApplicationDisplayCount)
function MSWA_GetAuraStackText(auraData, minCount)
if not auraData or not hasGetAuraCount then return nil end
minCount = minCount or 2
-- EQoL signature: GetAuraApplicationDisplayCount(unit, instanceID, min, max)
local instanceID = auraData.auraInstanceID
if instanceID then
local ok, s = pcall(C_UnitAuras.GetAuraApplicationDisplayCount,
"player", instanceID, minCount, 1000)
if ok and s then return tostring(s) end
end
-- Fallback: read applications directly if not secret
local apps = auraData.applications
if apps and not (_issecretvalue and _issecretvalue(apps)) then
if apps >= minCount then return tostring(apps) end
end
return nil
end
-----------------------------------------------------------
-- CDM (Cooldown Manager) Frame Scanner
-- Dirty-flag based: only rescans when CDM frames change.
-- Secret-safe: tostring() comparison on cooldownIDs.
-----------------------------------------------------------
local _cdmFrameCache = {} -- [tostring(cooldownID)] = frame
local _cdmDirty = true
local _cdmScratch = {} -- reusable select() buffer
local _cdmCycleCache = {} -- cleared once per UpdateSpells() cycle
function MSWA_BeginCDMAuraCycle()
wipe(_cdmCycleCache)
end
-- Capture vararg into reusable scratch table
local function CaptureCDMChildren(buf, ...)
wipe(buf)
local n = select("#", ...)
for i = 1, n do buf[i] = select(i, ...) end
return n
end
local function RebuildCDMFrameCache()
if not _cdmDirty then return end
_cdmDirty = false
wipe(_cdmFrameCache)
local viewers = { "BuffIconCooldownViewer", "BuffBarCooldownViewer" }
for vi = 1, #viewers do
local viewer = _G[viewers[vi]]
if viewer then
-- Collect from GetChildren
if viewer.GetChildren then
local childCount = CaptureCDMChildren(_cdmScratch, viewer:GetChildren())
for ci = 1, childCount do
local child = _cdmScratch[ci]
if child then
local cdID = child.cooldownID
if not cdID and child.cooldownInfo then cdID = child.cooldownInfo.cooldownID end
if cdID then _cdmFrameCache[tostring(cdID)] = child end
end
end
end
-- Also check layoutChildren (some viewers use this)
local lc = viewer.layoutChildren
if type(lc) == "table" then
for _, child in pairs(lc) do
if type(child) == "table" then
local cdID = child.cooldownID
if not cdID and child.cooldownInfo then cdID = child.cooldownInfo.cooldownID end
if cdID and not _cdmFrameCache[tostring(cdID)] then
_cdmFrameCache[tostring(cdID)] = child
end
end
end
end
end
end
end
--- Invalidate CDM cache (called on viewer changes)
function MSWA_InvalidateCDMCache()
_cdmDirty = true
wipe(_cdmCycleCache)
end
-- Check if an auraInstanceID is usable (non-secret, positive number)
local function IsUsableAuraInstanceID(v)
return type(v) == "number" and not (_issecretvalue and _issecretvalue(v)) and v > 0
end
--- Get aura data from a CDM viewer frame by cooldownID.
--- Falls back to spell-based lookup if frame scanning yields nothing.
--- @param cooldownID number|string stored cdmCooldownID
--- @param fallbackSID number|nil spell ID for fallback
--- @return table|nil auraData (Blizzard struct or synthetic)
function MSWA_GetCDMFrameAuraData(cooldownID, fallbackSID)
if not cooldownID then
if fallbackSID then return MSWA_GetPlayerAuraDataBySpellID(fallbackSID) end
return nil
end
local cacheKey = tostring(cooldownID)
local cycleEntry = _cdmCycleCache[cacheKey]
if cycleEntry ~= nil then
return cycleEntry ~= false and cycleEntry or nil
end
RebuildCDMFrameCache()
local frame = _cdmFrameCache[cacheKey]
if frame then
-- 1) Read auraInstanceID from frame -> GetAuraDataByAuraInstanceID
local auraInstanceID = frame.auraInstanceID
if IsUsableAuraInstanceID(auraInstanceID) then
local auraUnit
if type(frame.GetAuraDataUnit) == "function" then
local ok, u = pcall(frame.GetAuraDataUnit, frame)
if ok and type(u) == "string" and u ~= "" then auraUnit = u end
end
if not auraUnit and type(frame.auraDataUnit) == "string" then
auraUnit = frame.auraDataUnit
end
if not auraUnit then auraUnit = "player" end
if C_UnitAuras and C_UnitAuras.GetAuraDataByAuraInstanceID then
local auraData = C_UnitAuras.GetAuraDataByAuraInstanceID(auraUnit, auraInstanceID)
if auraData then
_cdmCycleCache[cacheKey] = auraData
return auraData
end
end
end
-- 2) Totem fallback (shamans etc.)
if frame.totemData ~= nil and GetTotemInfo then
local slot = frame.preferredTotemUpdateSlot
if not slot and frame.totemData then
local ok, s = pcall(function() return frame.totemData.slot end)
if ok then slot = s end
end
if slot then
local _, _, startTime, duration = GetTotemInfo(slot)
if duration and duration > 0 then
local iconTex
if frame.Icon and frame.Icon.Icon and frame.Icon.Icon.GetTexture then
iconTex = frame.Icon.Icon:GetTexture()
end
local synthetic = {
duration = duration,
expirationTime = startTime + duration,
timeMod = 1,
applications = 0,
icon = iconTex,
_isCDMTotem = true,
}
_cdmCycleCache[cacheKey] = synthetic
return synthetic
end
end
end
end
-- 3) Fallback: standard spellID-based lookup
if fallbackSID then
local auraData = MSWA_GetPlayerAuraDataBySpellID(fallbackSID)
_cdmCycleCache[cacheKey] = auraData or false
return auraData
end
_cdmCycleCache[cacheKey] = false
return nil
end
-----------------------------------------------------------
-- Reminder threshold helper for BUFF_AURA
-- Returns true if aura should be HIDDEN (remaining > threshold).
-- Secret values -> never hide. Absent -> never hide.
-----------------------------------------------------------
function MSWA_ShouldHideByThreshold(s, auraData, now)
if not s or not s.reminderThresholdMin then return false end
local thresh = tonumber(s.reminderThresholdMin)
if not thresh or thresh <= 0 then return false end
if not auraData then return false end
local exp = auraData.expirationTime
local dur = auraData.duration
-- Secret values -> can't check, always show
if _issecretvalue then
if (exp and _issecretvalue(exp)) or (dur and _issecretvalue(dur)) then
return false
end
end
-- Permanent buff (duration=0) -> always show
if not dur or dur == 0 then return false end
if not exp or exp == 0 then return false end
local remaining = exp - now
if remaining <= 0 then return false end
-- Hide if remaining time is ABOVE threshold (buff still healthy)
return remaining > (thresh * 60)
end
-----------------------------------------------------------
-- Resolve aura data for a BUFF_AURA entry
-- Central helper: CDM path or direct player lookup.
-- @param s spellSettings entry
-- @param fallbackID spellID or itemID to use if s.auraSpellID is nil
-- @return auraData, buffActive
-----------------------------------------------------------
function MSWA_ResolveBuffAura(s, fallbackID)
local buffSID = s.auraSpellID or fallbackID
local cdmID = s.cdmCooldownID
local auraData
if cdmID then
auraData = MSWA_GetCDMFrameAuraData(cdmID, buffSID)
else
auraData = MSWA_GetPlayerAuraDataBySpellID(buffSID)
end
return auraData, (auraData ~= nil)
end
-----------------------------------------------------------
-- Apply cooldown sweep for aura data (secret-safe)
-- Shared by icon render + bar info collection.
-----------------------------------------------------------
function MSWA_ApplyAuraCooldown(cd, auraData)
if not cd or not auraData then
if cd then MSWA_ClearCooldownFrame(cd) end
return
end
local dur = auraData.duration
local exp = auraData.expirationTime
local isSecret = _issecretvalue and (
(dur and _issecretvalue(dur)) or
(exp and _issecretvalue(exp))
)
if isSecret then
if cd.SetCooldownFromExpirationTime then
cd:SetCooldownFromExpirationTime(exp, dur, auraData.timeMod)
cd.__mswaSet = true
end
elseif dur and dur > 0 and exp then
MSWA_ApplyCooldownFrame(cd, exp - dur, dur, auraData.timeMod or 1, exp)
else
MSWA_ClearCooldownFrame(cd)
end
end
-----------------------------------------------------------
-- Collect bar info for BUFF_AURA mode
-- Fills bInfo fields from aura data. Called from
-- UpdateEngine bar post-processing loop.
-----------------------------------------------------------
function MSWA_CollectBuffAuraBarInfo(bInfo, bs, bkey, previewMode)
local sid = bs.auraSpellID or tonumber(bkey)
local cdmID = bs.cdmCooldownID
local ad
if cdmID then
ad = MSWA_GetCDMFrameAuraData(cdmID, sid)
elseif sid then
ad = MSWA_GetPlayerAuraDataBySpellID(sid)
end
if ad then
local e = ad.expirationTime
local d = ad.duration
if e and d then
if _issecretvalue and ((_issecretvalue(e)) or (_issecretvalue(d))) then
bInfo.isSecret = true
else
bInfo.expires = e
bInfo.duration = d
end
end
bInfo.stacks = MSWA_GetAuraStackText(ad, 2)
else
bInfo.isActive = (bs.showWhenAbsent == true or previewMode)
if not bInfo.isActive then bInfo.isActive = false end
bInfo.absentAlpha = tonumber(bs.alphaOnAbsent) or 0.45
end
end
-----------------------------------------------------------
-- Event frame: UNIT_AURA relay + target changes
-- Drives dirty-flag invalidation for all caches.
-----------------------------------------------------------
local eventFrame = CreateFrame("Frame", "MSWA_BuffEventFrame", UIParent)
if eventFrame.RegisterUnitEvent then
eventFrame:RegisterUnitEvent("UNIT_AURA", "player", "target")
else
eventFrame:RegisterEvent("UNIT_AURA")
end
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
eventFrame:RegisterEvent("PLAYER_EQUIPMENT_CHANGED")
-- Named handler (no closure allocation)
local function OnEvent(_, event, arg1, arg2)
if event == "UNIT_AURA" then
if arg1 == "player" then
_playerDirty = true
wipe(_cdmCycleCache)
if MSWA_RequestUpdateSpells then MSWA_RequestUpdateSpells() end
elseif arg1 == "target" then
_targetDirty = true
if MSWA_RequestUpdateSpells then MSWA_RequestUpdateSpells() end
end
return
end
if event == "PLAYER_TARGET_CHANGED" then
_targetDirty = true
_cdmDirty = true
wipe(_cdmCycleCache)
if MSWA_RequestUpdateSpells then MSWA_RequestUpdateSpells() end
return
end
if event == "PLAYER_EQUIPMENT_CHANGED" then
-- arg1 = equipmentSlot. Only care about trinket slots 13/14.
if arg1 == 13 or arg1 == 14 then
-- Invalidate icon cache for trinket keys so texture updates
MSWA_InvalidateTrinketIcons()
if MSWA_RequestUpdateSpells then MSWA_RequestUpdateSpells() end
end
return
end
-- PLAYER_ENTERING_WORLD: full invalidation
_playerDirty = true
_targetDirty = true
_cdmDirty = true
wipe(_cdmCycleCache)
if MSWA_RequestUpdateSpells then MSWA_RequestUpdateSpells() end
end
eventFrame:SetScript("OnEvent", OnEvent)
-----------------------------------------------------------
-- Blizzard Cooldown Viewer visibility controller
-- EQoL-style integration for hiding/fading Blizzard viewers
-- while MSA tracks the same buffs/cooldowns.
-----------------------------------------------------------
local CV_RULES = {
IN_COMBAT = "IN_COMBAT",
WHILE_MOUNTED = "WHILE_MOUNTED",
WHILE_NOT_MOUNTED = "WHILE_NOT_MOUNTED",
MOUSEOVER = "MOUSEOVER",
PLAYER_HAS_TARGET = "PLAYER_HAS_TARGET",
PLAYER_CASTING = "PLAYER_CASTING",
PLAYER_IN_GROUP = "PLAYER_IN_GROUP",
ALWAYS_HIDDEN = "ALWAYS_HIDDEN",
}
local CV_FRAME_NAMES = {
"EssentialCooldownViewer",
"UtilityCooldownViewer",
"BuffBarCooldownViewer",
"BuffIconCooldownViewer",
}
MSWA.COOLDOWN_VIEWER_VIS_RULES = CV_RULES
function MSWA_GetCooldownViewerFrameNames()
return CV_FRAME_NAMES
end
local _msaEssentialAttachCache = { viewer = nil, at = 0, width = 36, height = 36, spacing = 4, rows = nil }
local _msaEssentialAttachHosts = {}
local _msaEssentialActiveAttachments = {}
local _msaMSUFBridgeState = { proxy = nil, viewer = nil, extraLeft = 0, extraRight = 0, active = false, revision = 0 }
local _msaMSUFBridgeNotifyPending = false
local function CV_ScheduleMSUFBridgeNotify()
if _msaMSUFBridgeNotifyPending then return end
_msaMSUFBridgeNotifyPending = true
local function _flush()
_msaMSUFBridgeNotifyPending = false
local cb = _G and _G.MSUF_OnCDMExtensionChanged
if type(cb) == "function" then
cb()
end
end
if C_Timer and C_Timer.After then
C_Timer.After(0, _flush)
else
_flush()
end
end
local function CV_GetMSUFBridgeProxy(viewer)
local state = _msaMSUFBridgeState
local proxy = state.proxy
if not proxy then
proxy = CreateFrame("Frame", "MSWA_EssentialBridgeProxy", (viewer and viewer:GetParent()) or UIParent)
proxy:EnableMouse(false)
proxy:Hide()
state.proxy = proxy
end
local desiredParent = (viewer and viewer:GetParent()) or UIParent
if proxy:GetParent() ~= desiredParent then
proxy:SetParent(desiredParent)
end
if viewer and viewer.GetFrameStrata and proxy.SetFrameStrata then
proxy:SetFrameStrata(viewer:GetFrameStrata())
end
if viewer and viewer.GetFrameLevel and proxy.SetFrameLevel then
proxy:SetFrameLevel((viewer:GetFrameLevel() or 0) + 1)
end
return proxy
end
local function CV_UpdateMSUFBridge(viewer, extraLeft, extraRight)
local state = _msaMSUFBridgeState
local active = viewer and ((extraLeft or 0) + (extraRight or 0)) > 0 and true or false
local changed = false
if active and viewer then
local proxy = CV_GetMSUFBridgeProxy(viewer)
proxy:ClearAllPoints()
proxy:SetPoint("TOPLEFT", viewer, "TOPLEFT", -(extraLeft or 0), 0)
proxy:SetPoint("BOTTOMRIGHT", viewer, "BOTTOMRIGHT", (extraRight or 0), 0)
proxy:Show()
elseif state.proxy then
state.proxy:ClearAllPoints()
state.proxy:Hide()
end
if state.viewer ~= viewer then changed = true end
if math.abs((state.extraLeft or 0) - (extraLeft or 0)) > 0.5 then changed = true end
if math.abs((state.extraRight or 0) - (extraRight or 0)) > 0.5 then changed = true end
if (state.active and true or false) ~= active then changed = true end
state.viewer = viewer
state.extraLeft = extraLeft or 0
state.extraRight = extraRight or 0
state.active = active and true or false
if changed then
state.revision = (state.revision or 0) + 1
CV_ScheduleMSUFBridgeNotify()
end
end
function MSWA_GetOrCreateEssentialBridgeProxy()
local viewer = _G["EssentialCooldownViewer"] or _G["CooldownManager"]
if viewer and (not viewer.IsForbidden or not viewer:IsForbidden()) then
return CV_GetMSUFBridgeProxy(viewer)
end
return _msaMSUFBridgeState.proxy
end
function MSWA_GetEssentialBridgeFrame()
local state = _msaMSUFBridgeState
if state and state.active and state.proxy then
return state.proxy
end
return MSWA_GetEssentialCooldownViewerFrame and MSWA_GetEssentialCooldownViewerFrame() or (_G["EssentialCooldownViewer"] or _G["CooldownManager"])
end
function MSWA_GetEssentialBridgeRevision()
return (_msaMSUFBridgeState and _msaMSUFBridgeState.revision) or 0
end
local function CV_GetAttachHostToken(key)
if MSWA_KeyToTrinketSlot then
local slot = MSWA_KeyToTrinketSlot(key)
if slot then return tostring(slot) end
end
return tostring(key or "msa")
end
local function CV_GetFirstPointTuple(frame)
if not frame or not frame.GetPoint then return nil end
local point, relativeTo, relativePoint, xOfs, yOfs = frame:GetPoint(1)
return point, relativeTo, relativePoint, xOfs, yOfs
end
local function CV_PointHasSide(point, token)
return type(point) == "string" and point:find(token, 1, true) ~= nil
end
local function CV_ApplyViewerBounds(viewer)
if not viewer or (viewer.IsForbidden and viewer:IsForbidden()) then return end
local extraLeft, extraRight = 0, 0
local viewerLeft = viewer.GetLeft and viewer:GetLeft() or nil
local viewerRight = viewer.GetRight and viewer:GetRight() or nil
for _, entry in pairs(_msaEssentialActiveAttachments) do
if entry and entry.viewer == viewer then
local width = entry.width or 0
local offsetX = entry.offsetX or 0
local row = entry.row
if entry.side == "START" then
local baseLeft = nil
if row and row.leftButton and row.leftButton.left then
baseLeft = row.leftButton.left
elseif type(viewerLeft) == "number" then
baseLeft = viewerLeft
end
if type(baseLeft) == "number" and type(viewerLeft) == "number" then
local outerLeft = baseLeft + offsetX - width
local need = viewerLeft - outerLeft
if type(need) == "number" and need > extraLeft then extraLeft = need end
end
else
local baseRight = nil
if row and row.rightButton and row.rightButton.right then
baseRight = row.rightButton.right
elseif type(viewerRight) == "number" then
baseRight = viewerRight
end
if type(baseRight) == "number" and type(viewerRight) == "number" then
local outerRight = baseRight + offsetX + width
local need = outerRight - viewerRight
if type(need) == "number" and need > extraRight then extraRight = need end
end
end
end
end
if extraLeft < 0 then extraLeft = 0 end
if extraRight < 0 then extraRight = 0 end
CV_UpdateMSUFBridge(viewer, extraLeft, extraRight)
end
function MSWA_RegisterActiveEssentialAttachment(key, layout)
if not key or type(layout) ~= "table" or not layout.viewer then return end
_msaEssentialActiveAttachments[tostring(key)] = {
viewer = layout.viewer,
side = layout.side or "END",
row = layout.row,
offsetX = layout.offsetX or 0,
width = layout.width or 0,
}
CV_ApplyViewerBounds(layout.viewer)
end
function MSWA_UnregisterActiveEssentialAttachment(key)
if not key then return end
local token = tostring(key)
local entry = _msaEssentialActiveAttachments[token]
_msaEssentialActiveAttachments[token] = nil
if entry and entry.viewer then
CV_ApplyViewerBounds(entry.viewer)
end
end
function MSWA_GetEssentialAttachHost(key, viewer)
if not key then return nil end
viewer = viewer or MSWA_GetEssentialCooldownViewerFrame()
if not viewer then return nil end
local token = CV_GetAttachHostToken(key)
local host = _msaEssentialAttachHosts[token]
if not host then
host = CreateFrame("Frame", nil, viewer)
host:SetSize(1, 1)
host:EnableMouse(false)
host:Hide()
_msaEssentialAttachHosts[token] = host
elseif host:GetParent() ~= viewer then
host:SetParent(viewer)
end
if viewer.GetFrameStrata and host.SetFrameStrata then
host:SetFrameStrata(viewer:GetFrameStrata())
end
if viewer.GetFrameLevel and host.SetFrameLevel then
host:SetFrameLevel((viewer:GetFrameLevel() or 0) + 5)
end
return host
end
function MSWA_HideEssentialAttachHost(key)
if not key then return end
local token = CV_GetAttachHostToken(key)
local host = _msaEssentialAttachHosts[token]
if host then
host:ClearAllPoints()
host:Hide()
end
MSWA_UnregisterActiveEssentialAttachment(key)
end
local function CV_SafeBool(val)
if val == nil then return false end
if _issecretvalue and _issecretvalue(val) then return false end
return val == true
end
local function CV_SafeIsShown(frame)
if not frame or not frame.IsShown then return false end
local ok, shown = pcall(frame.IsShown, frame)
if not ok then return false end
return CV_SafeBool(shown)
end
local function CV_SafeObjectType(frame)
if not frame or not frame.GetObjectType then return nil end
local ok, objectType = pcall(frame.GetObjectType, frame)
if not ok or (_issecretvalue and _issecretvalue(objectType)) then return nil end
if type(objectType) ~= "string" then return nil end
return objectType
end
local function CV_SafeNumberMethod(frame, methodName)
local method = frame and frame[methodName]
if not method then return nil end
local ok, value = pcall(method, frame)
if not ok or (_issecretvalue and _issecretvalue(value)) then return nil end
if type(value) ~= "number" then return nil end
return value
end
local function CV_SafeCenter(frame)
if not frame or not frame.GetCenter then return nil, nil end
local ok, x, y = pcall(frame.GetCenter, frame)
if not ok then return nil, nil end
if (_issecretvalue and ((_issecretvalue(x)) or (_issecretvalue(y)))) then return nil, nil end
if type(x) ~= "number" or type(y) ~= "number" then return nil, nil end
return x, y
end
local function CV_IsMSAAttachButton(frame)
if not frame then return false end
if frame.spellID ~= nil then return true end
local name = (frame.GetName and frame:GetName()) or nil
return type(name) == "string" and name:find("MidnightSimpleAurasIcon", 1, true) == 1
end
local function CV_FindVisibleButtonMetrics(root, depth)
if not root or depth < 0 or not root.GetChildren then return nil, nil end
local kids = { root:GetChildren() }
for i = 1, #kids do
local child = kids[i]
if child and (not CV_IsMSAAttachButton(child)) and CV_SafeObjectType(child) == "Button" and CV_SafeIsShown(child) then
local w = CV_SafeNumberMethod(child, "GetWidth")
local h = CV_SafeNumberMethod(child, "GetHeight")
if type(w) == "number" and type(h) == "number" and w >= 18 and w <= 80 and h >= 18 and h <= 80 then
return w, h
end
end
end
if depth <= 0 then return nil, nil end
for i = 1, #kids do
local w, h = CV_FindVisibleButtonMetrics(kids[i], depth - 1)
if w and h then return w, h end
end
return nil, nil
end
local function CV_CollectVisibleButtons(root, depth, out)
if not root or depth < 0 or not root.GetChildren then return out end
out = out or {}
local kids = { root:GetChildren() }
for i = 1, #kids do
local child = kids[i]
if child and (not CV_IsMSAAttachButton(child)) and CV_SafeIsShown(child) and CV_SafeObjectType(child) == "Button" then
local w = CV_SafeNumberMethod(child, "GetWidth")
local h = CV_SafeNumberMethod(child, "GetHeight")
local x, y = CV_SafeCenter(child)
if type(w) == "number" and type(h) == "number" and type(x) == "number" and type(y) == "number" and w >= 18 and w <= 80 and h >= 18 and h <= 80 then
out[#out + 1] = {
frame = child,
width = w,
height = h,
cx = x,
cy = y,
left = x - (w * 0.5),
right = x + (w * 0.5),
top = y + (h * 0.5),
bottom = y - (h * 0.5),
}
end
end
end
if depth <= 0 then return out end
for i = 1, #kids do
CV_CollectVisibleButtons(kids[i], depth - 1, out)
end
return out
end
local function CV_BuildRowsFromButtons(buttons)
if type(buttons) ~= "table" or #buttons == 0 then return nil end
table.sort(buttons, function(a, b)
if a.cy == b.cy then return a.cx < b.cx end
return a.cy > b.cy
end)
local avgH = 0
for i = 1, #buttons do avgH = avgH + (buttons[i].height or 0) end
avgH = (#buttons > 0) and (avgH / #buttons) or 36
local rowThreshold = math.max(8, math.floor((avgH * 0.45) + 0.5))
local rows = {}
for i = 1, #buttons do
local b = buttons[i]
local placed = false
for r = 1, #rows do
local row = rows[r]
if math.abs((b.cy or 0) - (row.cy or 0)) <= rowThreshold then
row.buttons[#row.buttons + 1] = b
row.cy = ((row.cy * row.count) + b.cy) / (row.count + 1)
row.count = row.count + 1
placed = true
break
end
end
if not placed then
rows[#rows + 1] = { buttons = { b }, cy = b.cy, count = 1 }
end
end
table.sort(rows, function(a, b) return (a.cy or 0) > (b.cy or 0) end)
for r = 1, #rows do
local row = rows[r]
table.sort(row.buttons, function(a, b) return (a.cx or 0) < (b.cx or 0) end)
row.index = r
row.leftButton = row.buttons[1]
row.rightButton = row.buttons[#row.buttons]
local width, height = 0, 0
local totalGap, gapCount = 0, 0
for i = 1, #row.buttons do
local b = row.buttons[i]
width = width + (b.width or 0)
height = height + (b.height or 0)
if i > 1 then
local prev = row.buttons[i - 1]
local gap = (b.left or 0) - (prev.right or 0)
if type(gap) == "number" and gap >= 0 and gap <= 40 then
totalGap = totalGap + gap
gapCount = gapCount + 1
end
end
end
row.width = (#row.buttons > 0) and (width / #row.buttons) or 36
row.height = (#row.buttons > 0) and (height / #row.buttons) or 36
row.spacing = (gapCount > 0) and (totalGap / gapCount) or 4
end
return rows
end
local function CV_RoundPixel(v)
v = tonumber(v) or 0
return math.floor(v + (v >= 0 and 0.5 or -0.5))
end
local function CV_ResolveAttachRowIndex(rows, pref)
if type(rows) ~= "table" or #rows == 0 then return 1 end
if pref == "ROW2" and rows[2] then return 2 end
if pref == "ROW1" and rows[1] then return 1 end
return 1
end
function MSWA_GetEssentialCooldownViewerFrame()
local f = _G["EssentialCooldownViewer"] or _G["CooldownManager"]
if not f then return nil end
if CV_SafeIsShown(f) then return f end
-- Keep using the real viewer frame even during transient visibility/layout churn.
-- This avoids attached trinkets falling back to standalone positioning and
-- visually "disappearing" when Blizzard momentarily rebuilds the viewer.
return f
end
function MSWA_GetAttachedTrinketLayout(key)
if not (MSWA_IsTrinketKey and MSWA_IsTrinketKey(key)) then return nil end
local viewer = MSWA_GetEssentialCooldownViewerFrame()