-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImprovedTalentLoadouts.lua
4039 lines (3609 loc) · 148 KB
/
ImprovedTalentLoadouts.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
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
local addonName, TalentLoadouts = ...
local isTWW = select(4, GetBuildInfo()) >= 110000
local talentUI = isTWW and "Blizzard_PlayerSpells" or "Blizzard_ClassTalentUI"
local LibDD = LibStub:GetLibrary("LibUIDropDownMenu-4.0")
local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")
local internalVersion = 7
local NUM_ACTIONBAR_BUTTONS = 15 * 12
local ITL_LOADOUT_NAME = "[ITL] Temp"
local LDB = LibStub:GetLibrary("LibDataBroker-1.1")
local dataObjText = "ITL: %s, %s"
local dataObj = LDB:NewDataObject(addonName, {type = "data source", text = "ITL: Spec, Loadout", OnClick = function(self) TalentLoadouts.easyMenuAnchor = self LibDD:ToggleDropDownMenu(1, nil, TalentLoadouts.easyMenu, self, 0, 0); end})
local dropdownFont = CreateFont("ITL_DropdownFont")
local iterateLoadouts, iterateCategories
local delayed = {}
local talentFrame
--- Create an iterator for a hash table.
-- @param t:table The table to create the iterator for.
-- @param order:function A sort function for the keys.
-- @return function The iterator usable in a loop.
local function spairs(t, order, categoryKey)
local keys = {}
for k in pairs(t) do
keys[#keys + 1] = k
end
if order then
table.sort(
keys,
function(a, b)
return order(t, a, b, categoryKey)
end
)
else
table.sort(keys)
end
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]], keys[i + 1]
end
end
end
local function sortByName(t, a, b)
if t[a] and t[b] and t[a].name and t[b].name then
return t[a].name < t[b].name
end
end
local function sortByValue(t, a, b)
if t[a] and t[b] then
return t[a] < t[b]
end
end
local function sortByOrderAndName(t, a, b, categoryKey)
if t[a] and t[b] then
if categoryKey then
local orderA = t[a].categoryCustomOrder and t[a].categoryCustomOrder[categoryKey] or 1000
local orderB = t[b].categoryCustomOrder and t[b].categoryCustomOrder[categoryKey] or 1000
return (orderA < orderB) or (orderA == orderB and (t[a].name < t[b].name))
else
local orderA = t[a].customOrder or 1000
local orderB = t[b].customOrder or 1000
return (orderA < orderB) or (orderA == orderB and (t[a].name < t[b].name))
end
end
end
local function sortByOrder(t, a, b, categoryKey)
if categoryKey then
local orderA = t[a].categoryCustomOrder and t[a].categoryCustomOrder[categoryKey] or 1000
local orderB = t[b].categoryCustomOrder and t[b].categoryCustomOrder[categoryKey] or 1000
return (orderA < orderB) or (orderA == orderB and (a < b))
else
local orderA = t[a].customOrder or 1000
local orderB = t[b].customOrder or 1000
return (orderA < orderB) or (orderA == orderB and (a < b))
end
end
local function sortByKey(t, a, b)
if a and b then
return a < b
end
end
local defaultDB = {
loadouts = {
globalLoadouts = {},
characterLoadouts = {},
},
actionbars = {
macros = {
global = {},
char = {},
}
}
}
local hooks = {}
local function HookFunction(namespace, key, func)
hooksecurefunc(namespace, key, function()
if hooks[key] then
func()
end
end)
hooks[key] = true
end
local function UnhookFunction(key)
hooks[key] = nil
end
local RegisterEvent, UnregisterEvent
do
local eventFrame = CreateFrame("Frame")
function RegisterEvent(event)
eventFrame:RegisterEvent(event)
end
function UnregisterEvent(event)
eventFrame:UnregisterEvent(event)
end
RegisterEvent("ADDON_LOADED")
RegisterEvent("PLAYER_ENTERING_WORLD")
RegisterEvent("TRAIT_CONFIG_UPDATED")
RegisterEvent("TRAIT_CONFIG_CREATED")
RegisterEvent("PLAYER_REGEN_ENABLED")
RegisterEvent("PLAYER_REGEN_DISABLED")
RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
RegisterEvent("UPDATE_MACROS")
RegisterEvent("ACTIVE_PLAYER_SPECIALIZATION_CHANGED")
RegisterEvent("EQUIPMENT_SWAP_FINISHED")
RegisterEvent("VOID_STORAGE_UPDATE")
RegisterEvent("MODIFIER_STATE_CHANGED")
eventFrame:SetScript("OnEvent", function(self, event, arg1, arg2, arg3)
if event == "ADDON_LOADED" then
if arg1 == addonName then
TalentLoadouts:Initialize()
TalentLoadouts:InitializeEasyMenu()
elseif arg1 == talentUI then
talentFrame = isTWW and PlayerSpellsFrame.TalentsFrame or ClassTalentFrame.TalentsTab
self:UnregisterEvent("ADDON_LOADED")
TalentLoadouts:UpdateSpecID()
TalentLoadouts:InitializeTalentLoadouts()
TalentLoadouts:InitializeDropdown()
TalentLoadouts:InitializeButtons()
TalentLoadouts:InitializeHooks()
end
elseif event == "PLAYER_ENTERING_WORLD" and (arg1 or arg2) then
TalentLoadouts:InitializeCharacterDB()
TalentLoadouts:SaveCurrentLoadouts()
TalentLoadouts:UpdateDataObj(ITLAPI:GetCurrentLoadout())
TalentLoadouts:DeleteTempLoadouts()
TalentLoadouts:UpdateKnownFlyouts()
elseif event == "VOID_STORAGE_UPDATE" then
TalentLoadouts:UpdateKnownFlyouts()
elseif event == "PLAYER_REGEN_ENABLED" then
RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
for _, func in pairs(delayed) do
func()
end
delayed = {}
elseif event == "PLAYER_REGEN_DISABLED" then
UnregisterEvent("UNIT_SPELLCAST_SUCCEEDED")
elseif event == "TRAIT_CONFIG_UPDATED" then
TalentLoadouts:UpdateConfig(arg1)
if not TalentLoadouts.blizzImported then
TalentLoadouts.pendingLoadout = nil
end
elseif event == "TRAIT_CONFIG_CREATED" and TalentLoadouts.blizzImported then
if arg1.type ~= Enum.TraitConfigType.Combat then return end
TalentLoadouts:LoadAsBlizzardLoadout(arg1)
elseif event == "UPDATE_MACROS" then
TalentLoadouts:UpdateMacros()
elseif event == "ACTIVE_PLAYER_SPECIALIZATION_CHANGED" then
TalentLoadouts.charDB.lastCategory = nil
TalentLoadouts:UpdateSpecID(true)
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateSpecButtons()
TalentLoadouts:UpdateDataObj()
TalentLoadouts:UpdateLoadoutIterator()
TalentLoadouts:UpdateCategoryIterator()
if not InCombatLockdown() then
TalentLoadouts:UpdateKnownFlyouts()
TalentLoadouts:UpdateActionBar()
else
delayed.UpdateActionBar = delayed.UpdateActionBar or GenerateClosure(TalentLoadouts.UpdateActionBar, TalentLoadouts)
delayed.UpdateKnownFlyouts = delayed.UpdateKnownFlyouts or GenerateClosure(TalentLoadouts.UpdateKnownFlyouts, TalentLoadouts)
end
elseif event == "CONFIG_COMMIT_FAILED" then
C_Timer.After(0.1, function()
if (TalentLoadouts.pendingLoadout and not UnitCastingInfo("player")) or TalentLoadouts.lastUpdated then
TalentLoadouts:OnLoadoutFail(event)
end
end)
elseif event == "UNIT_SPELLCAST_SUCCEEDED" and arg1 == "player" and arg3 == Constants.TraitConsts.COMMIT_COMBAT_TRAIT_CONFIG_CHANGES_SPELL_ID then
if TalentLoadouts.pendingLoadout then
TalentLoadouts:OnLoadoutSuccess()
else
TalentLoadouts:OnUnknownLoadoutSuccess()
end
elseif event == "EQUIPMENT_SWAP_FINISHED" and not arg1 then
local name = C_EquipmentSet.GetEquipmentSetInfo(arg2)
TalentLoadouts:Print("Equipment swap failed:", name)
elseif event == "MODIFIER_STATE_CHANGED" and TalentLoadouts.saveButton and TalentLoadouts.saveButton:IsShown() then
if IsShiftKeyDown() or IsControlKeyDown() then
TalentLoadouts.saveButton:Enable()
if GetMouseFocus and GetMouseFocus() == TalentLoadouts.saveButton then
TalentLoadouts.saveButton:SetButtonState("NORMAL")
elseif GetMouseFoci and tContains(GetMouseFoci(), TalentLoadouts.saveButton) then
TalentLoadouts.saveButton:SetButtonState("NORMAL")
end
else
TalentLoadouts.saveButton:Disable()
end
end
end)
end
local function GetPlayerName()
local name, realm = UnitName("player"), GetNormalizedRealmName()
return name .. "-" .. realm
end
function TalentLoadouts:Initialize()
ImprovedTalentLoadoutsDB = ImprovedTalentLoadoutsDB or TalentLoadoutProfilesDB or defaultDB
if not ImprovedTalentLoadoutsDB.classesInitialized then
self:InitializeClassDBs()
ImprovedTalentLoadoutsDB.classesInitialized = true
end
self:CheckForDBUpdates()
dropdownFont:SetFont(GameFontNormal:GetFont(), ImprovedTalentLoadoutsDB.options.fontSize, "")
end
function TalentLoadouts:InitializeClassDBs()
local classes = {"HUNTER", "WARLOCK", "PRIEST", "PALADIN", "MAGE", "ROGUE", "DRUID", "SHAMAN", "WARRIOR", "DEATHKNIGHT", "MONK", "DEMONHUNTER", "EVOKER"}
for i, className in ipairs(classes) do
ImprovedTalentLoadoutsDB.loadouts.globalLoadouts[className] = ImprovedTalentLoadoutsDB.loadouts.globalLoadouts[className] or {configIDs = {}, categories = {}}
end
end
function TalentLoadouts:InitializeCharacterDB()
local playerName = GetPlayerName()
if not ImprovedTalentLoadoutsDB.loadouts.characterLoadouts[playerName] then
ImprovedTalentLoadoutsDB.loadouts.characterLoadouts[playerName] = {}
end
if not ImprovedTalentLoadoutsDB.actionbars.macros.char[playerName] then
ImprovedTalentLoadoutsDB.actionbars.macros.char[playerName] = {}
end
ImprovedTalentLoadoutsDB.actionbars.macros.global = ImprovedTalentLoadoutsDB.actionbars.macros.global or {}
self.charDB = ImprovedTalentLoadoutsDB.loadouts.characterLoadouts[playerName]
self.globalDB = ImprovedTalentLoadoutsDB.loadouts.globalLoadouts[UnitClassBase("player")]
self.charMacros = ImprovedTalentLoadoutsDB.actionbars.macros.char[playerName]
self.globalMacros = ImprovedTalentLoadoutsDB.actionbars.macros.global
self.flyouts = {}
self.specID = PlayerUtil.GetCurrentSpecID()
self:CheckDBIntegrity()
self:CheckForVersionUpdates()
self.initialized = true
self:UpdateMacros()
end
-- Not sure how that can happen but apparently it was a problem for someone. Maybe there is another AddOn that overwrites my db?
function TalentLoadouts:CheckDBIntegrity()
ImprovedTalentLoadoutsDB.loadouts = ImprovedTalentLoadoutsDB.loadouts or {globalLoadouts = {}, characterLoadouts = {}}
ImprovedTalentLoadoutsDB.loadouts.globalLoadouts = ImprovedTalentLoadoutsDB.loadouts.globalLoadouts or {}
ImprovedTalentLoadoutsDB.loadouts.characterLoadouts = ImprovedTalentLoadoutsDB.loadouts.characterLoadouts or {}
if not self.globalDB then
ImprovedTalentLoadoutsDB.classesInitialized = nil
self:Initialize()
self.globalDB = ImprovedTalentLoadoutsDB.loadouts.globalLoadouts[UnitClassBase("player")]
end
if not self.charDB then
local playerName = GetPlayerName()
self.charDB = ImprovedTalentLoadoutsDB.loadouts.characterLoadouts[playerName]
end
self.charDB.cachedActionbars = self.charDB.cachedActionbars or {}
end
function TalentLoadouts:CheckForDBUpdates()
ImprovedTalentLoadoutsDB.options = ImprovedTalentLoadoutsDB.options or {
fontSize = ImprovedTalentLoadoutsDB.fontSize or 10,
simc = ImprovedTalentLoadoutsDB.simc == nil and true or ImprovedTalentLoadoutsDB.simc,
applyLoadout = ImprovedTalentLoadoutsDB.applyLoadout == nil and true or ImprovedTalentLoadoutsDB.applyLoadout,
showSpecButtons = ImprovedTalentLoadoutsDB.showSpecButtons == nil and true or ImprovedTalentLoadoutsDB.showSpecButtons,
specButtonType = "text",
}
local options = ImprovedTalentLoadoutsDB.options
local optionKeys = {
["loadActionbars"] = true,
["clearEmptySlots"] = false,
["findMacroByName"] = false,
["loadBlizzard"] = false,
["showCategoryName"] = false,
["sortLoadoutsByName"] = false,
["sortCategoriesByName"] = false,
["sortSubcategoriesByName"] = false,
["loadActionbarsSpec"] = false,
["loadAsBlizzard"] = true,
["useAddOnLoadoutFallback"] = false,
["findMatchingLoadout"] = true,
["showCustomOrder"] = true,
}
for key, defaultValue in pairs(optionKeys) do
if options[key] == nil then
options[key] = defaultValue
end
end
end
function TalentLoadouts:CheckForVersionUpdates()
local currentVersion = ImprovedTalentLoadoutsDB.version
if (currentVersion or 0) < 7 then
ImprovedTalentLoadoutsDB.options.loadAsBlizzard = true
end
ImprovedTalentLoadoutsDB.version = internalVersion
end
function TalentLoadouts:GetTreeID(configInfo)
return (configInfo and configInfo.treeIDs[1]) or (ClassTalentFrame and talentFrame:GetTalentTreeID()) or (C_ClassTalents.GetActiveConfigID() and C_Traits.GetConfigInfo(C_ClassTalents.GetActiveConfigID()).treeIDs[1])
end
function TalentLoadouts:UpdateSpecID(isRespec)
--if not self.loaded then
-- UIParentLoadAddOn("Blizzard_ClassTalentUI")
-- self.loaded = true
--end
self.specID = PlayerUtil.GetCurrentSpecID()
self.treeID = self:GetTreeID() or self.treeID
if self.charDB then
self:UpdateTempLoadout()
end
if isRespec then
StaticPopup_Hide("TALENTLOADOUTS_LOADOUT_DELETE_ALL")
self.charDB.lastLoadout = nil
end
end
function TalentLoadouts:UpdateTempLoadout()
local configIDs = C_ClassTalents.GetConfigIDsBySpecID(self.specID)
self.charDB.tempLoadout = nil
for _, configID in ipairs(configIDs) do
local configInfo = C_Traits.GetConfigInfo(configID)
if configInfo.name == ITL_LOADOUT_NAME then
self.charDB.tempLoadout = configID
end
end
self.charDB.activeConfigIDs = self.charDB.activeConfigIDs or {}
self.charDB.activeConfigIDs[C_ClassTalents.GetActiveConfigID()] = true
end
function TalentLoadouts:UpdateActionBar()
if not ImprovedTalentLoadoutsDB.options.loadActionbarsSpec then return end
local currentSpecID = self.specID
local configInfo = self.globalDB.configIDs[currentSpecID][self.charDB[currentSpecID]]
if configInfo and configInfo.actionBars then
-- Players are reporting that it sometimes doesn't work after changing the specialization. As I can't reproduce I will add a small delay for now, maybe that fixes it.
C_Timer.After(0.1, function()
self:LoadActionBar(configInfo.actionBars, configInfo.name)
end)
elseif not configInfo then
self:Print("Couldn't find the last loadout of the spec. Make sure that the dropdown doesn't say \"Unknown\". This means that you've changed the tree without updating a loadout.")
end
end
function TalentLoadouts:UpdateLoadoutIterator(categoryKey)
local currentSpecID = self.specID
if ImprovedTalentLoadoutsDB.options.sortLoadoutsByName then
iterateLoadouts = GenerateClosure(spairs, TalentLoadouts.globalDB.configIDs[currentSpecID] or {}, sortByOrderAndName, categoryKey)
else
iterateLoadouts = GenerateClosure(spairs, TalentLoadouts.globalDB.configIDs[currentSpecID] or {}, sortByOrder, categoryKey)
end
end
function TalentLoadouts:UpdateCategoryIterator()
local currentSpecID = self.specID
if ImprovedTalentLoadoutsDB.options.sortCategoriesByName then
iterateCategories = GenerateClosure(spairs, TalentLoadouts.globalDB.categories[currentSpecID] or {}, sortByName)
else
iterateCategories = GenerateClosure(pairs, TalentLoadouts.globalDB.categories[currentSpecID] or {})
end
end
-- WIP, Wasn't able to reproduce the issue of empty loadout info
local function CreateEntryInfoFromString(configID, exportString, treeID, repeating)
configID = C_Traits.GetConfigInfo(configID) and configID or C_ClassTalents.GetActiveConfigID()
local treeID = TalentLoadouts:GetTreeID()
local importStream = ExportUtil.MakeImportDataStream(exportString)
local loadoutContent, success, loadoutEntryInfo
if ClassTalentFrame then
local _ = securecallfunction(talentFrame.ReadLoadoutHeader, talentFrame, importStream)
loadoutContent = securecallfunction(talentFrame.ReadLoadoutContent, talentFrame, importStream, treeID)
success, loadoutEntryInfo = pcall(talentFrame.ConvertToImportLoadoutEntryInfo, talentFrame, configID, treeID, loadoutContent)
elseif PlayerSpellsFrame then
local _ = securecallfunction(PlayerSpellsFrame.TalentsFrame.ReadLoadoutHeader, PlayerSpellsFrame.TalentsFrame, importStream)
loadoutContent = securecallfunction(PlayerSpellsFrame.TalentsFrame.ReadLoadoutContent, PlayerSpellsFrame.TalentsFrame, importStream, treeID)
success, loadoutEntryInfo = pcall(PlayerSpellsFrame.TalentsFrame.ConvertToImportLoadoutEntryInfo, PlayerSpellsFrame.TalentsFrame, configID, treeID, loadoutContent)
end
-- TalentLoadouts:Print(success, loadoutEntryInfo and #loadoutEntryInfo)
if success and #loadoutEntryInfo > 0 then
return loadoutEntryInfo
elseif not repeating then
return CreateEntryInfoFromString(configID, exportString, treeID, true)
else
TalentLoadouts:Print("Wasn't able to import the loadout. Try reloading or restarting your game.")
end
end
local function CreateExportString(configInfo, configID, specID, skipEntryInfo)
local treeID = TalentLoadouts:GetTreeID(configInfo)
local treeHash = treeID and C_Traits.GetTreeHash(treeID);
if treeID and treeID == TalentLoadouts.treeID and C_Traits.GetTreeNodes(treeID) then
local serializationVersion = C_Traits.GetLoadoutSerializationVersion()
local dataStream = ExportUtil.MakeExportDataStream()
if ClassTalentFrame then
talentFrame:WriteLoadoutHeader(dataStream, serializationVersion, specID, treeHash)
talentFrame:WriteLoadoutContent(dataStream , configID, treeID)
elseif PlayerSpellsFrame then
PlayerSpellsFrame.TalentsFrame:WriteLoadoutHeader(dataStream, serializationVersion, specID, treeHash)
PlayerSpellsFrame.TalentsFrame:WriteLoadoutContent(dataStream , configID, treeID)
end
local exportString = dataStream:GetExportString()
local loadoutEntryInfo
if not skipEntryInfo then
loadoutEntryInfo = CreateEntryInfoFromString(configID, exportString, treeID)
end
return exportString, loadoutEntryInfo, treeHash
end
end
function TalentLoadouts:InitializeTalentLoadouts()
if not self.globalDB or not self.charDB then
self:CheckDBIntegrity()
end
local specConfigIDs = self.globalDB.configIDs
local currentSpecID = self.specID
if specConfigIDs[currentSpecID] then
for configID, configInfo in pairs(specConfigIDs[currentSpecID]) do
if C_Traits.GetConfigInfo(configID) and (not configInfo.exportString or not configInfo.entryInfo or not configInfo.treeHash) then
configInfo.exportString, configInfo.entryInfo, configInfo.treeHash = CreateExportString(configInfo, configID, currentSpecID)
end
end
else
self:UpdateSpecID()
self:SaveCurrentLoadouts()
end
self.loaded = true
end
function TalentLoadouts:InitializeTalentLoadout(newConfigID)
local specConfigIDs = self.globalDB.configIDs
local currentSpecID = self.specID
if not specConfigIDs[currentSpecID] then
self:UpdateSpecID()
currentSpecID = self.specID
specConfigIDs[currentSpecID] = {}
end
local configID = C_Traits.GetConfigInfo(newConfigID) and newConfigID or C_ClassTalents.GetActiveConfigID()
local configInfo = specConfigIDs[currentSpecID][newConfigID]
if configInfo and (not configInfo.exportString or not configInfo.entryInfo or not configInfo.treeHash) then
configInfo.exportString, configInfo.entryInfo, configInfo.treeHash = CreateExportString(configInfo, configID, currentSpecID)
end
end
function TalentLoadouts:UpdateConfig(configID)
if not self.loaded then
UIParentLoadAddOn(talentUI)
self.loaded = true
self:UpdateConfig(configID)
return
end
local currentSpecID = self.specID
local configInfo = self.globalDB.configIDs[currentSpecID][configID]
if configInfo then
local newConfigInfo = C_Traits.GetConfigInfo(configID)
configInfo.exportString, configInfo.entryInfo = CreateExportString(configInfo, configID, currentSpecID)
configInfo.name = newConfigInfo and newConfigInfo.name or configInfo.name
else
self:SaveLoadout(configID, currentSpecID)
end
end
function TalentLoadouts:SaveLoadout(configID, currentSpecID)
local specLoadouts = self.globalDB.configIDs[currentSpecID]
local configInfo = C_Traits.GetConfigInfo(configID)
if configInfo.type == 1 and configInfo.name ~= ITL_LOADOUT_NAME and (not self.charDB.activeConfigIDs or not self.charDB.activeConfigIDs[configInfo.ID]) then
configInfo.default = configID == C_ClassTalents.GetActiveConfigID() or nil
specLoadouts[configID] = configInfo
self:InitializeTalentLoadout(configID)
end
end
-- Delete duplicate Temp Loadouts
function TalentLoadouts:DeleteTempLoadouts()
if InCombatLockdown() then return end
local specID = self.specID
local configIDs = C_ClassTalents.GetConfigIDsBySpecID(specID)
local counter = 0
self.charDB.tempLoadout = nil
for _, configID in ipairs(configIDs) do
local configInfo = C_Traits.GetConfigInfo(configID)
if configInfo.name == ITL_LOADOUT_NAME then
if counter > 1 then
C_ClassTalents.DeleteConfig(configID)
else
self.charDB.tempLoadout = configID
end
counter = counter + 1
end
end
if ClassTalentFrame and ClassTalentFrame:IsShown() then
C_ClassTalents.UpdateLastSelectedSavedConfigID(specID, nil)
securecall(talentFrame.RefreshLoadoutOptions, talentFrame)
securecall(talentFrame.LoadoutDropDown.ClearSelection, talentFrame.LoadoutDropDown)
elseif PlayerSpellsFrame and talentFrame:IsShown() then
C_ClassTalents.UpdateLastSelectedSavedConfigID(specID, nil)
securecall(talentFrame.RefreshLoadoutOptions, talentFrame)
securecall(talentFrame.LoadoutDropDown.ClearSelection, talentFrame.LoadoutDropDown)
end
self.pendingDeletion = nil
end
function TalentLoadouts:GetExportStringForTree(skipEntryInfo)
return CreateExportString(nil, C_ClassTalents.GetActiveConfigID(), self.specID, skipEntryInfo)
end
function TalentLoadouts:SaveCurrentLoadouts()
if not self.globalDB or not self.charDB then
self:CheckDBIntegrity()
end
for specIndex=1, GetNumSpecializations() do
local specID = GetSpecializationInfo(specIndex)
self.globalDB.configIDs[specID] = self.globalDB.configIDs[specID] or {}
local specLoadouts = self.globalDB.configIDs[specID]
local configIDs = C_ClassTalents.GetConfigIDsBySpecID(specID)
for _, configID in ipairs(configIDs) do
if not specLoadouts[configID] then
local configInfo = C_Traits.GetConfigInfo(configID)
if not (configInfo.name == ITL_LOADOUT_NAME) then
specLoadouts[configID] = configInfo
end
end
end
end
local currentSpecID = self.specID
local activeConfigID = C_ClassTalents.GetActiveConfigID()
if activeConfigID then
self.globalDB.configIDs[currentSpecID][activeConfigID] = self.globalDB.configIDs[currentSpecID][activeConfigID] or C_Traits.GetConfigInfo(activeConfigID)
self.globalDB.configIDs[currentSpecID][activeConfigID].default = true
end
end
function TalentLoadouts:UpdateDataObj(configInfo)
local _, name = GetSpecializationInfoByID(TalentLoadouts.specID)
dataObj.text = dataObjText:format(name, configInfo and configInfo.name or "Unknown")
end
function TalentLoadouts:LoadGearAndLayout(configInfo)
if not InCombatLockdown() then
if configInfo.gearset then
-- Without delay it may fail.
C_Timer.After(0, function() EquipmentManager_EquipSet(configInfo.gearset) end)
end
if configInfo.layout then
C_EditMode.SetActiveLayout(configInfo.layout)
end
else
self:Print("Can't change gear or layouts in combat")
end
end
local function ResetTree(treeID, treeType)
local activeConfigID = C_ClassTalents.GetActiveConfigID()
if C_Traits.ConfigHasStagedChanges(activeConfigID) then
C_Traits.RollbackConfig(activeConfigID)
end
if not treeType or treeType == 1 then
C_Traits.ResetTree(activeConfigID, treeID)
else
local currencyInfo = C_Traits.GetTreeCurrencyInfo(activeConfigID, treeID, false)
if treeType == 2 then
C_Traits.ResetTreeByCurrency(activeConfigID, treeID, currencyInfo[1].traitCurrencyID)
elseif treeType == 3 then
C_Traits.ResetTreeByCurrency(activeConfigID, treeID, currencyInfo[2].traitCurrencyID)
end
end
end
local function CommitLoadout()
local configInfo = TalentLoadouts.pendingLoadout
if configInfo then
local activeConfigID = C_ClassTalents.GetActiveConfigID()
if not configInfo.entryInfo and configInfo.exportString then
configInfo.entryInfo = CreateEntryInfoFromString(configInfo.ID, configInfo.exportString)
end
local entryInfo = configInfo.entryInfo
if not entryInfo then return end
table.sort(entryInfo, function(a, b)
local nodeA = C_Traits.GetNodeInfo(activeConfigID, a.nodeID)
local nodeB = C_Traits.GetNodeInfo(activeConfigID, b.nodeID)
return nodeA.posY < nodeB.posY or (nodeA.posY == nodeB.posY and nodeA.posX < nodeB.posX)
end)
for i=1, #entryInfo do
local entry = entryInfo[i]
local nodeInfo = C_Traits.GetNodeInfo(activeConfigID, entry.nodeID)
if nodeInfo.isAvailable and nodeInfo.isVisible and nodeInfo.meetsEdgeRequirements then
if nodeInfo.type == Enum.TraitNodeType.Selection or nodeInfo.type == Enum.TraitNodeType.SubTreeSelection then
C_Traits.SetSelection(activeConfigID, entry.nodeID, entry.selectionEntryID)
end
if C_Traits.CanPurchaseRank(activeConfigID, entry.nodeID, entry.selectionEntryID) then
for rank=1, entry.ranksPurchased do
C_Traits.PurchaseRank(activeConfigID, entry.nodeID)
end
end
end
end
if ImprovedTalentLoadoutsDB.options.applyLoadout then
local canChange, _, changeError = C_ClassTalents.CanChangeTalents()
if not canChange then
if changeError == ERR_TALENT_FAILED_UNSPENT_TALENT_POINTS then
configInfo.error = true
end
TalentLoadouts:OnLoadoutFail()
TalentLoadouts:Print("|cffff0000Can't load Loadout.|r", changeError)
return
end
if pcall(C_Traits.GetConfigInfo, TalentLoadouts.charDB.tempLoadout) then
--securecallfunction(talentFrame.UpdateTreeCurrencyInfo, talentFrame)
RunNextFrame(GenerateClosure(C_ClassTalents.CommitConfig, TalentLoadouts.charDB.tempLoadout))
RunNextFrame(GenerateClosure(C_ClassTalents.UpdateLastSelectedSavedConfigID, TalentLoadouts.specID, TalentLoadouts.charDB.tempLoadout))
else
--securecallfunction(talentFrame.UpdateTreeCurrencyInfo, talentFrame)
RunNextFrame(GenerateClosure(C_ClassTalents.CommitConfig, activeConfigID))
end
RegisterEvent("CONFIG_COMMIT_FAILED")
else
HookFunction(C_Traits, "RollbackConfig", TalentLoadouts.OnLoadoutFail)
end
if not C_Traits.ConfigHasStagedChanges(activeConfigID) and TalentLoadouts.pendingLoadout then
TalentLoadouts:OnLoadoutSuccess()
end
end
end
function TalentLoadouts:LoadAsBlizzardLoadout(newConfigInfo)
if newConfigInfo.name == ITL_LOADOUT_NAME then
if not self.charDB.tempLoadout or self.charDB.tempLoadout ~= newConfigInfo.ID then
self.charDB.tempLoadout = newConfigInfo.ID
--C_ClassTalents.SetUsesSharedActionBars(newConfigInfo.ID, true)
end
ResetTree(newConfigInfo.treeIDs[1], newConfigInfo.type)
RunNextFrame(CommitLoadout)
end
end
local function LoadBlizzardLoadout(configID, currentSpecID, configInfo, categoryInfo)
C_ClassTalents.LoadConfig(configID, true)
C_ClassTalents.UpdateLastSelectedSavedConfigID(currentSpecID, nil)
TalentLoadouts.pendingLoadout = configInfo
TalentLoadouts.pendingCategory = categoryInfo
TalentLoadouts.lastUpdated = nil
TalentLoadouts.lastUpdatedCategory = nil
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj(configInfo)
TalentLoadouts:LoadGearAndLayout(configInfo)
end
local function LoadLoadout(self, configInfo, categoryInfo, forceBlizzardDisable)
if not configInfo then return end
local currentSpecID = TalentLoadouts.specID
local configID = configInfo.ID
if ImprovedTalentLoadoutsDB.options.loadBlizzard and C_Traits.GetConfigInfo(configID) then
LoadBlizzardLoadout(configID, currentSpecID, configInfo, categoryInfo)
return
end
if not forceBlizzardDisable and ImprovedTalentLoadoutsDB.options.loadAsBlizzard then
local tempLoadoutInfo = TalentLoadouts.charDB.tempLoadout and C_Traits.GetConfigInfo(TalentLoadouts.charDB.tempLoadout)
local canCreate = C_ClassTalents.CanCreateNewConfig()
if (tempLoadoutInfo or canCreate) and TalentLoadouts.charDB.lastLoadout ~= configInfo.ID then
TalentLoadouts:CacheActionBars()
TalentLoadouts.pendingLoadout = configInfo
TalentLoadouts.pendingCategory = categoryInfo
TalentLoadouts.lastUpdated = nil
TalentLoadouts.lastUpdatedCategory = nil
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj()
TalentLoadouts.blizzImported = true
if not TalentLoadouts.charDB.tempLoadout or not tempLoadoutInfo then
C_ClassTalents.RequestNewConfig(ITL_LOADOUT_NAME)
elseif tempLoadoutInfo then
C_ClassTalents.UpdateLastSelectedSavedConfigID(currentSpecID, TalentLoadouts.charDB.tempLoadout)
ResetTree(configInfo.treeIDs[1], configInfo.type)
RunNextFrame(CommitLoadout)
end
elseif not tempLoadoutInfo or not canCreate then
if not ImprovedTalentLoadoutsDB.options.useBlizzardFallback then
TalentLoadouts:Print("|cFFFF0000Can't load the loadouts as a Blizzard one because you have 10 Blizzard loadouts|r. |cFFFFFF00Delete one of them or disable \"Options -> Loadouts -> Load As Blizzard Loadout\"|r. You can also enable the \"Fall back to AddOn Loadout \" option to fall back to the old way of loading loadouts if the AddOn can't create/find the " .. ITL_LOADOUT_NAME .. " loadout.")
else
LoadLoadout(self, configInfo, categoryInfo, true)
end
end
elseif configInfo.entryInfo then
TalentLoadouts:CacheActionBars()
TalentLoadouts.pendingLoadout = configInfo
TalentLoadouts.pendingCategory = categoryInfo
TalentLoadouts.lastUpdated = nil
TalentLoadouts.lastUpdatedCategory = nil
ResetTree(configInfo.treeIDs[1], configInfo.type)
RunNextFrame(CommitLoadout)
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj()
elseif C_Traits.GetConfigInfo(configID) then
LoadBlizzardLoadout(configID, currentSpecID, configInfo, categoryInfo)
end
configInfo.error = nil
LibDD:CloseDropDownMenus()
end
function TalentLoadouts:LoadLoadoutByConfigID(configID, categoryInfo)
LoadLoadout(nil, TalentLoadouts.globalDB.configIDs[TalentLoadouts.specID][configID], categoryInfo)
end
function TalentLoadouts:OnLoadoutSuccess()
local configInfo = TalentLoadouts.pendingLoadout
local categoryInfo = TalentLoadouts.pendingCategory
TalentLoadouts:LoadGearAndLayout(configInfo)
TalentLoadouts.charDB.lastLoadout = configInfo.ID
TalentLoadouts.charDB[self.specID] = configInfo.ID
TalentLoadouts.charDB.lastCategory = categoryInfo
TalentLoadouts.pendingLoadout = nil
TalentLoadouts.pendingCategory = nil
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj(configInfo)
UnhookFunction("RollbackConfig")
UnregisterEvent("CONFIG_COMMIT_FAILED")
if TalentLoadouts.blizzImported then
TalentLoadouts.blizzImported = nil
TalentLoadouts.pendingDeletion = true
end
if ImprovedTalentLoadoutsDB.options.loadActionbars and configInfo.actionBars then
C_Timer.After(0.25, function()
TalentLoadouts:LoadActionBar(configInfo.actionBars, configInfo.name)
end)
end
C_Timer.After(0.25, function()
if C_AddOns.IsAddOnLoaded(talentUI) then
TalentLoadouts:UpdateCurrentExportString()
end
end)
C_Timer.After(0.35, function()
TalentLoadouts:CacheActionBars()
end)
end
function TalentLoadouts:OnUnknownLoadoutSuccess()
local known = false
if TalentLoadouts.lastUpdated then
local configInfo = TalentLoadouts.lastUpdated
local exportString = CreateExportString(configInfo, C_ClassTalents.GetActiveConfigID(), self.specID, true)
if exportString == configInfo.exportString then
TalentLoadouts.charDB.lastLoadout = configInfo.ID
TalentLoadouts.charDB[self.specID] = configInfo.ID
TalentLoadouts.charDB.lastCategory = TalentLoadouts.lastUpdatedCategory
known = true
end
end
if not known then
local exportString = CreateExportString(nil, C_ClassTalents.GetActiveConfigID(), self.specID, true)
local configID = nil
if ImprovedTalentLoadoutsDB.options.findMatchingLoadout then
for _, configInfo in pairs(self.globalDB.configIDs[self.specID]) do
if configInfo and configInfo.exportString and configInfo.exportString == exportString then
configID = configInfo.ID
break
end
end
end
TalentLoadouts.charDB.lastLoadout = configID
TalentLoadouts.charDB[self.specID] = configID
TalentLoadouts.charDB.lastCategory = nil
if not configID then
C_ClassTalents.UpdateLastSelectedSavedConfigID(self.specID, nil)
end
end
UnregisterEvent("CONFIG_COMMIT_FAILED")
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj()
TalentLoadouts.pendingDeletion = true
end
function TalentLoadouts:OnLoadoutFail()
TalentLoadouts.pendingLoadout = nil
TalentLoadouts.pendingCategory = nil
TalentLoadouts.lastUpdated = nil
TalentLoadouts.lastUpdatedCategory = nil
UnhookFunction("RollbackConfig")
UnregisterEvent("CONFIG_COMMIT_FAILED")
TalentLoadouts:UpdateDropdownText()
TalentLoadouts:UpdateDataObj()
end
function TalentLoadouts:CacheActionBars()
local cachedActionbars = self.charDB.cachedActionbars
cachedActionbars[self.specID] = TalentLoadouts:GetCurrentActionBarsCompressed(cachedActionbars[self.specID]) or cachedActionbars[self.specID]
end
function TalentLoadouts:UpdateCurrentExportString()
local configID = TalentLoadouts.charDB[self.specID]
local configInfo = configID and self.globalDB.configIDs[self.specID][configID]
if configInfo then
local exportString, entryInfo
if self.charDB.tempLoadout and ImprovedTalentLoadoutsDB.options.loadAsBlizzard then
exportString = C_Traits.GenerateImportString(self.charDB.tempLoadout)
entryInfo = CreateEntryInfoFromString(self.charDB.tempLoadout, exportString)
else
if not configInfo.entryInfo then
exportString, entryInfo = CreateExportString(nil, C_ClassTalents.GetActiveConfigID(), self.specID)
else
exportString = CreateExportString(nil, C_ClassTalents.GetActiveConfigID(), self.specID, true)
end
end
configInfo.exportString = exportString
configInfo.entryInfo = configInfo.entryInfo or entryInfo
end
end
function TalentLoadouts:LoadLoadoutByName(name)
local configIDs = self.globalDB.configIDs[self.specID]
for _, configInfo in pairs(configIDs) do
if configInfo.name == name then
LoadLoadout(nil, configInfo)
break
end
end
end
local function FindFreeConfigID()
local freeIndex = 1
for _ in ipairs(TalentLoadouts.globalDB.configIDs[TalentLoadouts.specID]) do
freeIndex = freeIndex + 1
end
return freeIndex
end
StaticPopupDialogs["TALENTLOADOUTS_CATEGORY_CREATE"] = {
text = "Category Name",
button1 = "Create",
button2 = "Cancel",
OnAccept = function(self)
local categoryName = self.editBox:GetText()
TalentLoadouts:CreateCategory(categoryName)
end,
timeout = 0,
EditBoxOnEnterPressed = function(self)
if ( self:GetParent().button1:IsEnabled() ) then
self:GetParent().button1:Click();
end
end,
hasEditBox = true,
whileDead = true,
hideOnEscape = true,
}
local function CreateCategory()
StaticPopup_Show("TALENTLOADOUTS_CATEGORY_CREATE")
end
function TalentLoadouts:CreateCategory(categoryName)
local key = categoryName:lower()
local currentSpecID = self.specID
self.globalDB.categories[currentSpecID] = self.globalDB.categories[currentSpecID] or {}
if self.globalDB.categories[currentSpecID][key] then
self:Print("A category with this name already exists.")
return
end
self.globalDB.categories[currentSpecID][key] = {
name = categoryName,
key = key,
loadouts = {},
}
end
StaticPopupDialogs["TALENTLOADOUTS_CATEGORY_IMPORT"] = {
text = "Category Import String",
button1 = "Import",
button2 = "Cancel",
OnAccept = function(self)
local importString = self.editBox:GetText()
TalentLoadouts:ProcessCategoryImport(importString)
end,
timeout = 0,
EditBoxOnEnterPressed = function(self)
if ( self:GetParent().button1:IsEnabled() ) then
self:GetParent().button1:Click();
end
end,
hasEditBox = true,