forked from WeakAuras/WeakAuras2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeakAuras.lua
More file actions
5235 lines (4794 loc) · 169 KB
/
WeakAuras.lua
File metadata and controls
5235 lines (4794 loc) · 169 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
-- Lua APIs
local tinsert, tconcat, tremove, wipe = table.insert, table.concat, table.remove, wipe
local fmt, tostring, string_char = string.format, tostring, string.char
local select, pairs, next, type, unpack = select, pairs, next, type, unpack
local loadstring, assert, error = loadstring, assert, error
local setmetatable, getmetatable, rawset, rawget = setmetatable, getmetatable, rawset, rawget
local bit_band, bit_lshift, bit_rshift = bit.band, bit.lshift, bit.rshift
local ADDON_NAME = "WeakAuras";
local versionString = WeakAuras.versionString;
WeakAurasTimers = setmetatable({}, {__tostring=function() return "WeakAuras" end});
LibStub("AceTimer-3.0"):Embed(WeakAurasTimers);
WeakAurasAceEvents = setmetatable({}, {__tostring=function() return "WeakAuras" end});
LibStub("AceEvent-3.0"):Embed(WeakAurasAceEvents);
local LDB = LibStub:GetLibrary("LibDataBroker-1.1");
local timer = WeakAurasTimers;
local aceEvents = WeakAurasAceEvents
local WeakAuras = WeakAuras;
local L = WeakAuras.L;
local queueshowooc;
function WeakAuras.OpenOptions(msg)
if not(IsAddOnLoaded("WeakAurasOptions")) then
if InCombatLockdown() then
-- inform the user and queue ooc
print("|cff9900FF".."WeakAuras Options"..FONT_COLOR_CODE_CLOSE.." will finish loading after combat.")
queueshowooc = msg or "";
WeakAuras.frames["Addon Initialization Handler"]:RegisterEvent("PLAYER_REGEN_ENABLED")
return;
else
local loaded, reason = LoadAddOn("WeakAurasOptions");
if not(loaded) then
print("|cff9900FF".."WeakAuras Options"..FONT_COLOR_CODE_CLOSE.." could not be loaded: "..RED_FONT_COLOR_CODE.._G["ADDON_"..reason]);
return;
end
end
end
WeakAuras.ToggleOptions(msg);
end
SLASH_WEAKAURAS1, SLASH_WEAKAURAS2 = "/weakauras", "/wa";
function SlashCmdList.WEAKAURAS(msg)
WeakAuras.OpenOptions(msg);
end
local db;
local registeredFromAddons;
WeakAuras.addons = {};
local addons = WeakAuras.addons;
WeakAuras.tutorials = {};
local tutorials = WeakAuras.tutorials;
WeakAuras.collisions = {};
local collisions = WeakAuras.collisions;
local paused = true;
local squelch_actions = true;
WeakAuras.regions = {};
local regions = WeakAuras.regions;
WeakAuras.auras = {};
local auras = WeakAuras.auras;
WeakAuras.events = {};
local events = WeakAuras.events;
WeakAuras.loaded = {};
local loaded = WeakAuras.loaded;
WeakAuras.specificBosses = {};
local specificBosses = WeakAuras.specificBosses;
WeakAuras.specificUnits = {};
local specificUnits = WeakAuras.specificUnits;
WeakAuras.clones = {};
local clones = WeakAuras.clones;
WeakAuras.clonePool = {};
local clonePool = WeakAuras.clonePool;
WeakAuras.regionTypes = {};
local regionTypes = WeakAuras.regionTypes;
WeakAuras.regionOptions = {};
local regionOptions = WeakAuras.regionOptions;
WeakAuras.forceable_events = {};
local from_files = {};
local timers = {};
local loaded_events = {};
WeakAuras.loaded_events = loaded_events;
local loaded_auras = {};
WeakAuras.loaded_auras = loaded_auras;
WeakAuras.animations = {};
local animations = WeakAuras.animations;
WeakAuras.pending_controls = {};
local pending_controls = WeakAuras.pending_controls;
WeakAuras.frames = {};
WeakAuras.raidUnits = {};
WeakAuras.partyUnits = {};
do
for i=1,40 do
WeakAuras.raidUnits[i] = "raid"..i
end
for i=1,4 do
WeakAuras.partyUnits[i] = "party"..i
end
end
WeakAuras.me = GetUnitName("player",true)
WeakAuras.myGUID = nil
local playerLevel = UnitLevel("player");
local function_strings = WeakAuras.function_strings;
local anim_function_strings = WeakAuras.anim_function_strings;
local anim_presets = WeakAuras.anim_presets;
local load_prototype = WeakAuras.load_prototype;
local event_prototypes = WeakAuras.event_prototypes;
local levelColors = {
[0] = "|cFFFFFFFF",
[1] = "|cFF40FF40",
[2] = "|cFF6060FF",
[3] = "|cFFFF4040"
};
function WeakAuras.debug(msg, level)
if(db.debug) then
level = (level and levelColors[level] and level) or 2;
msg = (type(msg) == "string" and msg) or (msg and "Invalid debug message of type "..type(msg)) or "Debug message not specified";
ChatFrame3:AddMessage(levelColors[level]..msg);
end
end
local debug = WeakAuras.debug;
function WeakAuras.split(input)
input = input or "";
local ret = {};
local split, element = true;
split = input:find("[,%s]");
while(split) do
element, input = input:sub(1, split-1), input:sub(split+1);
if(element ~= "") then
tinsert(ret, element);
end
split = input:find("[,%s]");
end
if(input ~= "") then
tinsert(ret, input);
end
return ret;
end
function WeakAuras.validate(input, default)
for field, defaultValue in pairs(default) do
if(type(defaultValue) == "table" and type(input[field]) ~= "table") then
input[field] = {};
elseif(input[field] == nil) then
input[field] = defaultValue;
elseif(type(input[field]) ~= type(defaultValue)) then
input[field] = defaultValue;
end
if(type(input[field]) == "table") then
WeakAuras.validate(input[field], defaultValue);
end
end
end
function WeakAuras.RegisterRegionType(name, createFunction, modifyFunction, default)
if not(name) then
error("Improper arguments to WeakAuras.RegisterRegionType - name is not defined");
elseif(type(name) ~= "string") then
error("Improper arguments to WeakAuras.RegisterRegionType - name is not a string");
elseif not(createFunction) then
error("Improper arguments to WeakAuras.RegisterRegionType - creation function is not defined");
elseif(type(createFunction) ~= "function") then
error("Improper arguments to WeakAuras.RegisterRegionType - creation function is not a function");
elseif not(modifyFunction) then
error("Improper arguments to WeakAuras.RegisterRegionType - modification function is not defined");
elseif(type(modifyFunction) ~= "function") then
error("Improper arguments to WeakAuras.RegisterRegionType - modification function is not a function")
elseif not(default) then
error("Improper arguments to WeakAuras.RegisterRegionType - default options are not defined");
elseif(type(default) ~= "table") then
error("Improper arguments to WeakAuras.RegisterRegionType - default options are not a table");
elseif(regionTypes[name]) then
error("Improper arguments to WeakAuras.RegisterRegionType - region type \""..name.."\" already defined");
else
regionTypes[name] = {
create = createFunction,
modify = modifyFunction,
default = default
};
end
end
function WeakAuras.RegisterRegionOptions(name, createFunction, icon, displayName, createThumbnail, modifyThumbnail, description)
if not(name) then
error("Improper arguments to WeakAuras.RegisterRegionOptions - name is not defined");
elseif(type(name) ~= "string") then
error("Improper arguments to WeakAuras.RegisterRegionOptions - name is not a string");
elseif not(createFunction) then
error("Improper arguments to WeakAuras.RegisterRegionOptions - creation function is not defined");
elseif(type(createFunction) ~= "function") then
error("Improper arguments to WeakAuras.RegisterRegionOptions - creation function is not a function");
elseif not(icon) then
error("Improper arguments to WeakAuras.RegisterRegionOptions - icon is not defined");
elseif not(type(icon) == "string" or type(icon) == "function") then
error("Improper arguments to WeakAuras.RegisterRegionOptions - icon is not a string or a function")
elseif not(displayName) then
error("Improper arguments to WeakAuras.RegisterRegionOptions - display name is not defined".." "..name);
elseif(type(displayName) ~= "string") then
error("Improper arguments to WeakAuras.RegisterRegionOptions - display name is not a string");
elseif(regionOptions[name]) then
error("Improper arguments to WeakAuras.RegisterRegionOptions - region type \""..name.."\" already defined");
else
regionOptions[name] = {
create = createFunction,
icon = icon,
displayName = displayName,
createThumbnail = createThumbnail,
modifyThumbnail = modifyThumbnail,
description = description
};
end
end
-- This function is replaced in WeakAurasOptions.lua
function WeakAuras.IsOptionsOpen()
return false;
end
WeakAuras.unusedOverlayGlows = {}
WeakAuras.numOverlayGlows = 0
local function OverlayGlowAnimOutFinished(animGroup)
local overlay = animGroup:GetParent()
local frame = overlay:GetParent()
overlay:Hide()
tinsert(WeakAuras.unusedOverlayGlows, overlay)
frame.overlay = nil
end
local function OverlayGlow_OnHide(self)
if self.animOut:IsPlaying() then
self.animOut:Stop()
OverlayGlowAnimOutFinished(self.animOut)
end
end
local function GetOverlayGlow()
local overlay = tremove(WeakAuras.unusedOverlayGlows)
if not overlay then
WeakAuras.numOverlayGlows = WeakAuras.numOverlayGlows + 1
overlay = CreateFrame("Frame", "WeakAurasGlowOverlay"..WeakAuras.numOverlayGlows, UIParent, "ActionBarButtonSpellActivationAlert")
overlay.animOut:SetScript("OnFinished", OverlayGlowAnimOutFinished)
overlay:SetScript("OnHide", OverlayGlow_OnHide)
end
return overlay
end
local function WeakAuras_ShowOverlayGlow(frame)
if frame.overlay then
if frame.overlay.animOut:IsPlaying() then
frame.overlay.animOut:Stop()
frame.overlay.animIn:Play()
end
else
frame.overlay = GetOverlayGlow()
local frameWidth, frameHeight = frame:GetSize()
frame.overlay:SetParent(frame)
frame.overlay:ClearAllPoints()
--Make the height/width available before the next frame:
frame.overlay:SetSize(frameWidth * 1.4, frameHeight * 1.4)
frame.overlay:SetPoint("TOPLEFT", frame, "TOPLEFT", -frameWidth * 0.2, frameHeight * 0.2)
frame.overlay:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", frameWidth * 0.2, -frameHeight * 0.2)
frame.overlay.animIn:Play()
end
end
local function WeakAuras_HideOverlayGlow(frame)
if frame.overlay then
if frame.overlay.animIn:IsPlaying() then
frame.overlay.animIn:Stop()
end
if frame:IsVisible() then
frame.overlay.animOut:Play()
else
OverlayGlowAnimOutFinished(frame.overlay.animOut)
end
end
end
local function forbidden()
print("|cffffff00A WeakAura that you are using just tried to use a forbidden function but has been blocked from doing so. Please check your auras!|r")
end
local blockedFunctions = {
-- Lua functions that may allow breaking out of the environment
getfenv = true,
setfenv = true,
loadstring = true,
pcall = true,
-- blocked WoW API
SendMail = true,
SetTradeMoney = true,
}
local overrideFunctions = {
ActionButton_ShowOverlayGlow = WeakAuras_ShowOverlayGlow,
ActionButton_HideOverlayGlow = WeakAuras_HideOverlayGlow,
}
local exec_env = setmetatable({}, { __index =
function(t, k)
if k == "_G" then
return t
elseif blockedFunctions[k] then
return forbidden
elseif overrideFunctions[k] then
return overrideFunctions[k]
else
return _G[k]
end
end
})
local function_cache = {};
function WeakAuras.LoadFunction(string)
if function_cache[string] then
return function_cache[string]
else
local loadedFunction, errorString = loadstring(string)
if errorString then
print(errorString)
else
setfenv(loadedFunction, exec_env)
local success, func = pcall(assert(loadedFunction))
if success then
function_cache[string] = func
return func
end
end
end
end
local aura_cache = {};
do
aura_cache.max = 0;
aura_cache.watched = {};
aura_cache.players = {};
-- Test if aura_cache data is consistent with trigger settings, eg. OwnOnly, RemainingTime, StackCount, ect.
-- Extra check needed, because aura_cache can potentially contain data of two different triggers with different settings!
local function TestNonUniformSettings(acEntry, data)
if(data.remFunc) then
if not(data.remFunc(acEntry.expirationTime - acEntry.duration)) then
return false
end
end
-- Test OwnOnly
if (
data.ownOnly == true and WeakAuras.myGUID ~= acEntry.unitCaster or
data.ownOnly == false and WeakAuras.myGUID == acEntry.unitCaster
) then
return false;
end
-- Test StackCount
if (data.count and not data.count(acEntry.count)) then
return false;
end
-- Success
return true;
end
function aura_cache.Reloading()
aura_cache.reloading = true;
end
function aura_cache:DoneReloading()
aura_cache.reloading = nil;
end
function aura_cache.ForceUpdate()
if not(paused) then
WeakAuras.ScanAurasGroup()
end
end
function aura_cache.Watch(self, auraname)
self.watched[auraname] = self.watched[auraname] or {};
self.watched[auraname].players = self.watched[auraname].players or {};
self.watched[auraname].recentChanges = self.watched[auraname].recentChanges or {};
self:ForceUpdate()
end
function aura_cache.Unwatch(self, auraname)
self.watched[auraname] = nil;
end
function aura_cache.GetMaxNumber(self)
return self.max;
end
function aura_cache.GetNumber(self, names, data)
local num = 0;
local active;
for guid, _ in pairs(self.players) do
active = false;
for index, auraname in pairs(names) do
-- Need to check if cached data conforms to trigger
if(self.watched[auraname].players[guid] and TestNonUniformSettings(self.watched[auraname].players[guid], data)) then
active = true;
break;
end
end
if(active) then
num = num + 1;
end
end
return num;
end
function aura_cache.GetDynamicInfo(self, names, data)
local bestDuration, bestExpirationTime, bestName, bestIcon, bestCount, bestSpellId = 0, math.huge, "", "", 0, 0;
for _, auraname in pairs(names) do
if(self.watched[auraname]) then
for guid, durationInfo in pairs(self.watched[auraname].players) do
-- Need to check if cached data conforms to trigger
if(durationInfo.expirationTime < bestExpirationTime and TestNonUniformSettings(durationInfo, data)) then
bestDuration = durationInfo.duration;
bestExpirationTime = durationInfo.expirationTime;
bestName = durationInfo.name;
bestIcon = durationInfo.icon;
bestCount = durationInfo.count;
bestSpellId = durationInfo.spellId;
end
end
end
end
return bestDuration, bestExpirationTime, bestName, bestIcon, bestCount, bestSpellId;
end
function aura_cache.GetPlayerDynamicInfo(self, names, guid, data)
local bestDuration, bestExpirationTime, bestName, bestIcon, bestCount, bestSpellId = 0, math.huge, "", "", 0, 0;
for _, auraname in pairs(names) do
if(self.watched[auraname]) then
local durationInfo = self.watched[auraname].players[guid]
if(durationInfo) then
-- Need to check if cached data conforms to trigger
if(durationInfo.expirationTime < bestExpirationTime and TestNonUniformSettings(durationInfo, data)) then
bestDuration = durationInfo.duration;
bestExpirationTime = durationInfo.expirationTime;
bestName = durationInfo.name;
bestIcon = durationInfo.icon;
bestCount = durationInfo.count;
bestSpellId = durationInfo.spellId;
end
end
end
end
return bestDuration, bestExpirationTime, bestName, bestIcon, bestCount;
end
function aura_cache.GetAffected(self, names, data)
local affected = {};
for _, auraname in pairs(names) do
if(self.watched[auraname]) then
for guid, acEntry in pairs(self.watched[auraname].players) do
-- Need to check if cached data conforms to trigger
if (TestNonUniformSettings(acEntry, data)) then
affected[self.players[guid]] = true;
end
end
end
end
return affected;
end
function aura_cache.GetUnaffected(self, names, data)
local affected = self:GetAffected(names, data);
local ret = {};
for guid, name in pairs(self.players) do
if not(affected[name]) then
ret[name] = true;
end
end
return ret;
end
function aura_cache.AssertAura(self, auraname, guid, duration, expirationTime, name, icon, count, unitCaster, spellId)
-- Don't watch aura on non watching players
if not self.players[guid] then return end
if not(self.watched[auraname].players[guid]) then
self.watched[auraname].players[guid] = {
duration = duration,
expirationTime = expirationTime,
name = name,
icon = icon,
count = count,
unitCaster = unitCaster,
spellId = spellId
};
self.watched[auraname].recentChanges[guid] = true;
return true;
else
local auradata = self.watched[auraname].players[guid];
if(expirationTime ~= auradata.expirationTime) then
auradata.duration = duration;
auradata.expirationTime = expirationTime;
auradata.name = name;
auradata.icon = icon;
auradata.count = count;
auradata.unitCaster = unitCaster;
auradata.spellId = spellId;
self.watched[auraname].recentChanges[guid] = true;
return true;
else
return self.reloading or self.watched[auraname].recentChanges[guid];
end
end
end
function aura_cache.DeassertAura(self, auraname, guid)
if(self.watched[auraname] and self.watched[auraname].players[guid]) then
self.watched[auraname].players[guid] = nil;
self.watched[auraname].recentChanges[guid] = true;
return true;
else
return self.reloading or self.watched[auraname].recentChanges[guid];
end
end
function aura_cache.ClearRecentChanges(self)
for auraname, t in pairs(self.watched) do
wipe(t.recentChanges);
end
end
function aura_cache.AssertMember(self, guid, name, forceupdate)
if not(self.players[guid]) then
self.players[guid] = name;
self.max = self.max + 1;
end
if(forceupdate) then
self:ForceUpdate();
end
end
function aura_cache.DeassertMember(self, guid)
if(self.players[guid]) then
self.players[guid] = nil;
for auraname, _ in pairs(self.watched) do
self:DeassertAura(auraname, guid);
end
self.max = self.max - 1;
end
end
function aura_cache.AssertMemberList(self, guids)
local toAdd = {};
local toDelete = {};
for guid, name in pairs(guids) do
if not(self.players[guid]) then
toAdd[guid] = name;
end
end
for guid, _ in pairs(self.players) do
if not(guids[guid]) then
toDelete[guid] = true;
end
end
for guid, _ in pairs(toDelete) do
self:DeassertMember(guid);
end
for guid, name in pairs(toAdd) do
self:AssertMember(guid, name);
end
self:ForceUpdate();
end
end
WeakAuras.aura_cache = aura_cache;
local groupFrame = CreateFrame("FRAME");
WeakAuras.frames["Group Makeup Handler"] = groupFrame;
groupFrame:RegisterEvent("GROUP_ROSTER_UPDATE");
groupFrame:RegisterEvent("PLAYER_ENTERING_WORLD");
groupFrame:SetScript("OnEvent", function(self, event)
local groupMembers,playerName,uid,guid = {};
if IsInRaid() then
for i=1, GetNumGroupMembers() do
uid = WeakAuras.raidUnits[i];
playerName = GetUnitName(uid,true);
playerName = playerName:gsub("-", " - ");
guid = UnitGUID(uid);
if (guid) then
groupMembers[guid] = playerName;
end
end
elseif IsInGroup() then
for i=1, GetNumSubgroupMembers() do
uid = WeakAuras.partyUnits[i];
guid = UnitGUID(uid);
if (guid) then
groupMembers[guid] = GetUnitName(uid,true);
end
end
if (WeakAuras.myGUID) then
groupMembers[WeakAuras.myGUID] = WeakAuras.me;
else
WeakAuras.myGUID = UnitGUID("player")
end
end
aura_cache:AssertMemberList(groupMembers);
end);
do
local mh = GetInventorySlotInfo("MainHandSlot")
local oh = GetInventorySlotInfo("SecondaryHandSlot")
local swingTimerFrame;
local lastSwingMain, lastSwingOff, lastSwingRange;
local swingDurationMain, swingDurationOff, swingDurationRange;
local mainTimer, offTimer, rangeTimer;
function WeakAuras.GetSwingTimerInfo(hand)
if(hand == "main") then
local itemId = GetInventoryItemID("player", mh);
local name, _, _, _, _, _, _, _, _, icon = GetItemInfo(itemId or 0);
if(lastSwingMain) then
return swingDurationMain, lastSwingMain + swingDurationMain, name, icon;
elseif (lastSwingRange) then
return swingDurationRange, lastSwingRange + swingDurationRange, name, icon;
else
return 0, math.huge, name, icon;
end
elseif(hand == "off") then
local itemId = GetInventoryItemID("player", oh);
local name, _, _, _, _, _, _, _, _, icon = GetItemInfo(itemId or 0);
if(lastSwingOff) then
return swingDurationOff, lastSwingOff + swingDurationOff, name, icon;
else
return 0, math.huge, name, icon;
end
end
return 0, math.huge;
end
local function swingEnd(hand)
if(hand == "main") then
lastSwingMain, swingDurationMain = nil, nil;
elseif(hand == "off") then
lastSwingOff, swingDurationOff = nil, nil;
elseif(hand == "range") then
lastSwingRange, swingDurationRange = nil, nil;
end
WeakAuras.ScanEvents("SWING_TIMER_END");
end
local function swingTimerCheck(frame, event, _, message, _, _, source)
if(UnitIsUnit(source or "", "player")) then
if(message == "SWING_DAMAGE" or message == "SWING_MISSED") then
local event;
local currentTime = GetTime();
local mainSpeed, offSpeed = UnitAttackSpeed("player");
offSpeed = offSpeed or 0;
if not(lastSwingMain) then
lastSwingMain = currentTime;
swingDurationMain = mainSpeed;
event = "SWING_TIMER_START";
mainTimer = timer:ScheduleTimer(swingEnd, mainSpeed, "main");
elseif(OffhandHasWeapon() and not lastSwingOff) then
lastSwingOff = currentTime;
swingDurationOff = offSpeed;
event = "SWING_TIMER_START";
offTimer = timer:ScheduleTimer(swingEnd, offSpeed, "off");
else
-- A swing occurred while both weapons are supposed to be on cooldown
-- Simply refresh the timer of the weapon swing which would have ended sooner
local mainRem, offRem = (lastSwingMain or math.huge) + mainSpeed - currentTime, (lastSwingOff or math.huge) + offSpeed - currentTime;
if(mainRem < offRem or not OffhandHasWeapon()) then
timer:CancelTimer(mainTimer, true);
lastSwingMain = currentTime;
swingDurationMain = mainSpeed;
event = "SWING_TIMER_CHANGE";
mainTimer = timer:ScheduleTimer(swingEnd, mainSpeed, "main");
else
timer:CancelTimer(mainTimer, true);
lastSwingOff = currentTime;
swingDurationOff = offSpeed;
event = "SWING_TIMER_CHANGE";
offTimer = timer:ScheduleTimer(swingEnd, offSpeed, "off");
end
end
WeakAuras.ScanEvents(event);
elseif(message == "RANGE_DAMAGE" or message == "RANGE_MISSED") then
local event;
local currentTime = GetTime();
local speed = UnitRangedDamage("player");
if(lastSwingRange) then
timer:CancelTimer(rangeTimer, true);
event = "SWING_TIMER_CHANGE";
else
event = "SWING_TIMER_START";
end
lastSwingRange = currentTime;
swingDurationRange = speed;
rangeTimer = timer:ScheduleTimer(swingEnd, speed, "range");
WeakAuras.ScanEvents(event);
end
end
end
function WeakAuras.InitSwingTimer()
if not(swingTimerFrame) then
swingTimerFrame = CreateFrame("frame");
swingTimerFrame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
swingTimerFrame:SetScript("OnEvent", swingTimerCheck);
end
end
end
do
local cdReadyFrame;
WeakAuras.frames["Cooldown Trigger Handler"] = cdReadyFrame
local spells = {};
local spellCdDurs = {};
local spellCdExps = {};
local spellCharges = {};
local spellCdHandles = {};
local items = {};
local itemCdDurs = {};
local itemCdExps = {};
local itemCdHandles = {};
local runes = {};
local runeCdDurs = {};
local runeCdExps = {};
local runeCdHandles = {};
local gcdReference;
local gcdStart;
local gcdDuration;
local gcdSpellName;
local gcdSpellIcon;
local gcdEndCheck;
function WeakAuras.InitCooldownReady()
cdReadyFrame = CreateFrame("FRAME");
cdReadyFrame:RegisterEvent("SPELL_UPDATE_COOLDOWN");
cdReadyFrame:RegisterEvent("RUNE_POWER_UPDATE");
cdReadyFrame:RegisterEvent("RUNE_TYPE_UPDATE");
cdReadyFrame:SetScript("OnEvent", function(self, event, ...)
if(event == "SPELL_UPDATE_COOLDOWN" or event == "RUNE_POWER_UPDATE" or event == "RUNE_TYPE_UPDATE") then
WeakAuras.CheckCooldownReady();
elseif(event == "UNIT_SPELLCAST_SENT") then
local unit, name = ...;
if(unit == "player") then
if(gcdSpellName ~= name) then
local icon = GetSpellTexture(name);
gcdSpellName = name;
gcdSpellIcon = icon;
end
end
end
end);
end
function WeakAuras.GetRuneCooldown(id)
if(runes[id] and runeCdExps[id] and runeCdDurs[id]) then
return runeCdExps[id] - runeCdDurs[id], runeCdDurs[id];
else
return 0, 0;
end
end
function WeakAuras.GetSpellCooldown(id)
if(spells[id] and spellCdExps[id] and spellCdDurs[id]) then
return spellCdExps[id] - spellCdDurs[id], spellCdDurs[id];
else
return 0, 0;
end
end
function WeakAuras.GetSpellCharges(id)
return spellCharges[id];
end
function WeakAuras.GetItemCooldown(id)
if(items[id] and itemCdExps[id] and itemCdDurs[id]) then
return itemCdExps[id] - itemCdDurs[id], itemCdDurs[id];
else
return 0, 0;
end
end
function WeakAuras.GetGCDInfo()
if(gcdStart) then
return gcdDuration, gcdStart + gcdDuration, gcdSpellName or "Invalid", gcdSpellIcon or "Interface\\Icons\\INV_Misc_QuestionMark";
else
return 0, math.huge, gcdSpellName or "Invalid", gcdSpellIcon or "Interface\\Icons\\INV_Misc_QuestionMark";
end
end
local function RuneCooldownFinished(id)
runeCdHandles[id] = nil;
runeCdDurs[id] = nil;
runeCdExps[id] = nil;
WeakAuras.ScanEvents("RUNE_COOLDOWN_READY", id);
end
local function SpellCooldownFinished(id)
spellCdHandles[id] = nil;
spellCdDurs[id] = nil;
spellCdExps[id] = nil;
spellCharges[id] = select(2, GetSpellCharges(id));
WeakAuras.ScanEvents("SPELL_COOLDOWN_READY", id, nil);
end
local function ItemCooldownFinished(id)
itemCdHandles[id] = nil;
itemCdDurs[id] = nil;
itemCdExps[id] = nil;
WeakAuras.ScanEvents("ITEM_COOLDOWN_READY", id);
end
local function CheckGCD()
local event;
local startTime, duration = GetSpellCooldown(gcdReference);
if(duration and duration > 0) then
if not(gcdStart) then
event = "GCD_START";
elseif(gcdStart ~= startTime) then
event = "GCD_CHANGE";
end
gcdStart, gcdDuration = startTime, duration;
local endCheck = startTime + duration + 0.1;
if(gcdEndCheck ~= endCheck) then
gcdEndCheck = endCheck;
timer:ScheduleTimer(CheckGCD, duration + 0.1);
end
else
if(gcdStart) then
event = "GCD_END"
end
gcdStart, gcdDuration = nil, nil;
gcdEndCheck = 0;
end
if(event) then
WeakAuras.ScanEvents(event);
end
end
function WeakAuras.CheckCooldownReady()
if(gcdReference) then
CheckGCD();
end
for id, _ in pairs(runes) do
local startTime, duration = GetRuneCooldown(id);
startTime = startTime or 0;
duration = duration or 0;
local time = GetTime();
if(not startTime or startTime == 0) then
startTime = 0
duration = 0
end
if(startTime > 0 and duration > 1.51) then
-- On non-GCD cooldown
local endTime = startTime + duration;
if not(runeCdExps[id]) then
-- New cooldown
runeCdDurs[id] = duration;
runeCdExps[id] = endTime;
runeCdHandles[id] = timer:ScheduleTimer(RuneCooldownFinished, endTime - time, id);
WeakAuras.ScanEvents("RUNE_COOLDOWN_STARTED", id);
elseif(runeCdExps[id] ~= endTime) then
-- Cooldown is now different
if(runeCdHandles[id]) then
timer:CancelTimer(runeCdHandles[id]);
end
runeCdDurs[id] = duration;
runeCdExps[id] = endTime;
runeCdHandles[id] = timer:ScheduleTimer(RuneCooldownFinished, endTime - time, id);
WeakAuras.ScanEvents("RUNE_COOLDOWN_CHANGED", id);
end
elseif(startTime > 0 and duration > 0) then
-- GCD
-- Do nothing
else
if(runeCdExps[id]) then
-- Somehow CheckCooldownReady caught the rune cooldown before the timer callback
-- This shouldn't happen, but if it doesn, no problem
if(runeCdHandles[id]) then
timer:CancelTimer(runeCdHandles[id]);
end
RuneCooldownFinished(id);
end
end
end
for id, _ in pairs(spells) do
local charges, maxCharges, startTime, duration = GetSpellCharges(id);
if (charges == nil) then -- charges is nil iff the spell has no charges
startTime, duration = GetSpellCooldown(id);
elseif (charges == maxCharges) then
startTime, duration = 0, 0;
end
startTime = startTime or 0;
duration = duration or 0;
local time = GetTime();
local remaining = startTime + duration - time;
if(duration > 1.51) then
-- On non-GCD cooldown
local endTime = startTime + duration;
if not(spellCdExps[id]) then
local match = nil
if (select(2, UnitClass("player")) == "DEATHKNIGHT") then
match = false
for runeId = 1,6 do
local runeStart, runeDuration = GetRuneCooldown(runeId)
local runeRemaining = runeStart + runeDuration - time;
if math.abs(remaining - runeRemaining) < 0.03 then
match = true;
break;
end
end
end
-- New cooldown
spellCdDurs[id] = duration;
spellCdExps[id] = endTime;
spellCharges[id] = charges;
spellCdHandles[id] = timer:ScheduleTimer(SpellCooldownFinished, endTime - time, id);
WeakAuras.ScanEvents("SPELL_COOLDOWN_STARTED", id, match);
elseif(spellCdExps[id] ~= endTime or spellCharges[id] ~= charges) then
local match = nil
if (select(2, UnitClass("player")) == "DEATHKNIGHT") then
match = false
for runeId = 1,6 do
local runeStart, runeDuration = GetRuneCooldown(runeId)
local runeRemaining = runeStart + runeDuration - time;
if math.abs(remaining - runeRemaining) < 0.03 then
match = true;
break;
end
end
end
-- Cooldown is now different
if(spellCdHandles[id]) then
timer:CancelTimer(spellCdHandles[id]);
end
spellCdDurs[id] = duration;
spellCdExps[id] = endTime;
spellCharges[id] = charges;
if (maxCharges == nil or charges + 1 == maxCharges) then
spellCdHandles[id] = timer:ScheduleTimer(SpellCooldownFinished, endTime - time, id);
end
WeakAuras.ScanEvents("SPELL_COOLDOWN_CHANGED", id, match);
end
elseif(duration > 0 and not (spellCdExps[id] and spellCdExps[id] - time > 1.51)) then
-- GCD
-- Do nothing
else
if(spellCdExps[id]) then
-- Somehow CheckCooldownReady caught the spell cooldown before the timer callback
-- This shouldn't happen, but if it does, no problem
if(spellCdHandles[id]) then
timer:CancelTimer(spellCdHandles[id]);
end
SpellCooldownFinished(id);