-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
2646 lines (2516 loc) · 127 KB
/
Copy pathCore.lua
File metadata and controls
2646 lines (2516 loc) · 127 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
local ADDON_NAME, BindCards = ...
_G.BindCards = BindCards
BindCards.ADDON_NAME = ADDON_NAME
BindCards.SCHEMA_VERSION = 3
BindCards.MAX_PROFILES = 64
BindCards.MAX_PROFILE_SETS = 1024
BindCards.MAX_CARDS = 512
BindCards.MAX_TILES = 4096
BindCards.MIN_COLUMNS = 3
BindCards.MAX_COLUMNS = 12
BindCards.GENERAL_BINDER_ID = "general"
BindCards.VALID_CLASSES = {
DEATHKNIGHT = true, DEMONHUNTER = true, DRUID = true, EVOKER = true,
HUNTER = true, MAGE = true, MONK = true, PALADIN = true, PRIEST = true,
ROGUE = true, SHAMAN = true, WARLOCK = true, WARRIOR = true,
}
-- WoW can return an unlearned action-bar override while dragging a spell
-- from the spellbook. Keep the stable base spell in saved tiles.
BindCards.SPELL_ID_ALIASES = {
[406971] = 372048, -- Oppressing Roar override -> base spell
}
BindCards.callbacks = BindCards.callbacks or {}
local DB_DEFAULTS = {
schemaVersion = BindCards.SCHEMA_VERSION,
nextId = 1,
profiles = {},
selections = {
defaults = { generalProfileId = nil, classProfileIds = {} },
characters = {},
},
settings = {
uiScale = 1.0,
iconSize = 36,
workspaceMode = "binder",
selectedBinderKey = "class",
theme = "eui",
defaultProfileId = nil,
windowWidth = 580,
windowHeight = 520,
windowManualSize = false,
windowX = nil,
windowY = nil,
binderWidth = 580,
binderHeight = 300,
binderManualSize = false,
gatherLooseSheets = true,
suspendBindPad = false,
showAbilityNames = true,
experimentalBinderStrings = false,
experimentalColorPass = false,
experimentalCardSizing = false,
legacyBindingsRestored = false,
bindingRestore = {},
},
}
local function trim(value)
if type(value) ~= "string" then return "" end
return value:match("^%s*(.-)%s*$")
end
local function numberOr(value, fallback)
local number = tonumber(value)
if not number or number ~= number or number == math.huge or number == -math.huge then return fallback end
return number
end
local function copy(value, seen)
if type(value) ~= "table" then return value end
seen = seen or {}
if seen[value] then return seen[value] end
local result = {}
seen[value] = result
for key, child in pairs(value) do
result[copy(key, seen)] = copy(child, seen)
end
return result
end
BindCards.CopyTable = copy
function BindCards:IsValidTemplateKey(templateKey, condition)
if templateKey == "default:general" then
return not condition or (not condition.role and not condition.class and not condition.specID and
not condition.form and not condition.activity)
end
local role = type(templateKey) == "string" and templateKey:match("^default:role:([A-Z]+)$")
if role == "TANK" or role == "HEALER" or role == "DPS" then
local expected = role == "DPS" and "DAMAGER" or role
return type(condition) == "table" and condition.role == expected and not condition.class and
not condition.specID and not condition.form and not condition.activity
end
local class = type(templateKey) == "string" and templateKey:match("^default:class:([A-Z]+)$")
if class then
return self.VALID_CLASSES[class] == true and type(condition) == "table" and
condition.class == class and not condition.specID and not condition.form and not condition.activity
end
local specID = tonumber(type(templateKey) == "string" and templateKey:match("^default:spec:(%d+)$") or nil)
return specID ~= nil and specID > 0 and specID % 1 == 0 and type(condition) == "table" and
tonumber(condition.specID) == specID and not condition.form and not condition.activity
end
function BindCards:RefreshClassCatalog()
local count = C_ClassInfo and C_ClassInfo.GetNumClasses and C_ClassInfo.GetNumClasses() or
(GetNumClasses and GetNumClasses() or 0)
if issecretvalue and issecretvalue(count) then count = 0 end
count = math.max(0, math.min(64, math.floor(tonumber(count) or 0)))
for classID = 1, count do
local info = C_ClassInfo and C_ClassInfo.GetClassInfo and C_ClassInfo.GetClassInfo(classID)
local classFile = info and info.classFile
if not classFile and GetClassInfo then classFile = select(2, GetClassInfo(classID)) end
if type(classFile) == "string" and classFile:match("^[A-Z]+$") then self.VALID_CLASSES[classFile] = true end
end
end
function BindCards:NewID(kind)
local rawRealm = GetRealmName and GetRealmName() or "realm"
if issecretvalue and issecretvalue(rawRealm) then rawRealm = "realm" end
local realm = tostring(rawRealm):gsub("[^%w]", "")
local guid = UnitGUID and UnitGUID("player") or "player"
if issecretvalue and issecretvalue(guid) then guid = "player" end
local tail = tostring(guid):match("([^%-]+)$") or "player"
self.usedIds = self.usedIds or {}
local id
repeat
local sequence = math.max(1, math.floor(numberOr(self.db.nextId, 1)))
self.db.nextId = sequence + 1
id = string.format("bc-%s-%s-%s-%x", kind or "id", realm, tail, sequence)
until not self.usedIds[id]
self.usedIds[id] = true
return id
end
function BindCards:CountProfiles(binderId)
local count = 0
for _, profile in pairs(self.db and self.db.profiles or {}) do
if not binderId or profile.binderId == binderId then count = count + 1 end
end
return count
end
function BindCards:CountTiles(profile)
local count = 0
for _, card in ipairs(profile and profile.cards or {}) do count = count + #(card.tiles or {}) end
return count
end
function BindCards:UniqueProfileName(name, exceptId, binderId)
name = trim(name)
if name == "" then name = "New profile" end
name = name:sub(1, 64)
local used = {}
for id, profile in pairs(self.db.profiles or {}) do
if id ~= exceptId and (not binderId or profile.binderId == binderId) and
type(profile.name) == "string" then
used[profile.name:lower()] = true
end
end
if not used[name:lower()] then return name end
local base, suffix = name:sub(1, 57), 2
local candidate
repeat
candidate = string.format("%s (%d)", base, suffix):sub(1, 64)
suffix = suffix + 1
until not used[candidate:lower()]
return candidate
end
function BindCards:RegisterCallback(event, func, owner)
if type(event) ~= "string" or type(func) ~= "function" then return false end
self.callbacks[event] = self.callbacks[event] or {}
self.callbacks[event][#self.callbacks[event] + 1] = { func = func, owner = owner }
return true
end
function BindCards:UnregisterCallbacks(owner)
for event, entries in pairs(self.callbacks) do
for index = #entries, 1, -1 do
if entries[index].owner == owner then table.remove(entries, index) end
end
if #entries == 0 then self.callbacks[event] = nil end
end
end
function BindCards:Fire(event, ...)
local entries = self.callbacks[event]
if not entries then return end
for index = 1, #entries do
local entry = entries[index]
local ok, message = pcall(entry.func, entry.owner, ...)
if not ok then geterrorhandler()(message) end
end
end
function BindCards:Notify(message)
local text = tostring(message or "")
if text == "" then return end
print("|cff55b8d6BindCards:|r " .. text)
self:Fire("NOTIFICATION", text)
end
function BindCards:GetCurrentClassFile()
local classFile = UnitClass and select(2, UnitClass("player")) or nil
if issecretvalue and issecretvalue(classFile) then return nil end
return type(classFile) == "string" and self.VALID_CLASSES[classFile] and classFile or nil
end
function BindCards:GetCurrentCharacterKey()
local guid = UnitGUID and UnitGUID("player") or nil
if issecretvalue and issecretvalue(guid) then return nil end
return type(guid) == "string" and guid ~= "" and guid or nil
end
function BindCards:GetProfiles(binderId)
local profiles = self.db and self.db.profiles or {}
if not binderId then return profiles end
local filtered = {}
for id, profile in pairs(profiles) do
if profile.binderId == binderId then filtered[id] = profile end
end
return filtered
end
function BindCards:GetProfile(profileId)
if not self.db then return nil end
if profileId then return self.db.profiles[profileId] end
return self:GetActiveProfile()
end
function BindCards:GetSelectedProfileSet(binderId)
if not self.db or type(binderId) ~= "string" then return nil end
local selections = self.db.selections or {}
local defaults = selections.defaults or {}
local characterKey = self:GetCurrentCharacterKey()
local character = characterKey and selections.characters and selections.characters[characterKey] or nil
local classFile = binderId:match("^class:(.+)$")
local selectedId
if binderId == self.GENERAL_BINDER_ID then
selectedId = character and character.generalProfileId or defaults.generalProfileId
elseif classFile then
selectedId = character and character.classProfileIds and character.classProfileIds[classFile] or
(defaults.classProfileIds and defaults.classProfileIds[classFile])
end
local selected = selectedId and self.db.profiles[selectedId]
if selected and selected.binderId == binderId then return selected end
local candidates = {}
for _, profile in pairs(self.db.profiles) do
if profile.binderId == binderId then candidates[#candidates + 1] = profile end
end
table.sort(candidates, function(left, right)
if (left.createdAt or 0) ~= (right.createdAt or 0) then
return (left.createdAt or 0) < (right.createdAt or 0)
end
return left.id < right.id
end)
return candidates[1]
end
function BindCards:GetSelectedProfileSets(context)
context = context or (self.GetPlayerContext and self:GetPlayerContext()) or {}
local classFile = context.class or self:GetCurrentClassFile()
local general = self:GetSelectedProfileSet(self.GENERAL_BINDER_ID)
local class = classFile and self:GetSelectedProfileSet("class:" .. classFile) or nil
return general, class
end
function BindCards:GetActiveProfile()
local general, class = self:GetSelectedProfileSets()
if not general and not class then return nil end
local cards = {}
local function append(source)
for _, card in ipairs(source and source.cards or {}) do cards[#cards + 1] = card end
end
append(general)
append(class)
return {
id = "active-composite",
name = general and class and (general.name .. " / " .. class.name) or
((general or class) and (general or class).name or "Binding sets"),
cards = cards,
bindingsEnabled = true,
isComposite = true,
sourceProfiles = { general, class },
}
end
function BindCards:GetSelectedProfile()
return self:GetActiveProfile()
end
function BindCards:GetActiveProfileName()
local profile = self:GetActiveProfile()
return profile and profile.name
end
function BindCards:GetProfileNames()
local names = {}
for _, profile in pairs(self:GetProfiles() or {}) do names[#names + 1] = profile.name end
table.sort(names, function(left, right) return left:lower() < right:lower() end)
return names
end
function BindCards:SetProfile(nameOrId)
if self.db.profiles[nameOrId] then return self:SelectProfile(nameOrId) end
local wanted = trim(nameOrId):lower()
if wanted == "" then return false, "Enter a profile name." end
local focused = self.db.profiles[self.db.selectedProfileId]
local wantedBinder = focused and focused.binderId or self.GENERAL_BINDER_ID
for id, profile in pairs(self.db.profiles) do
if profile.binderId == wantedBinder and profile.name:lower() == wanted then
return self:SelectProfile(id)
end
end
return self:CreateProfile(nameOrId, nil, wantedBinder)
end
function BindCards:CreateOrSwitchProfile(name) return self:SetProfile(name) end
function BindCards:RenameProfile(profileId, name)
local profile = self:GetProfile(profileId)
name = trim(name)
if not profile then return false, "Profile not found." end
if name == "" then return false, "Enter a profile name." end
name = name:sub(1, 64)
for id, candidate in pairs(self.db.profiles) do
if id ~= profileId and candidate.binderId == profile.binderId and
candidate.name:lower() == name:lower() then
return false, "That profile name is already used."
end
end
profile.name = name
self:TouchProfile(profile)
self:Fire("PROFILES_CHANGED", profileId)
return true
end
function BindCards:GetCards(profileId)
local profile = self:GetProfile(profileId)
return profile and profile.cards or {}
end
function BindCards:GetCard(cardId, profileId)
local _, card = self:FindCardOwner(cardId, profileId)
return card
end
function BindCards:CreateProfile(name, source, binderId)
if source ~= nil then
if type(source) == "table" and source.id and self.db.profiles[source.id] == source then
return self:CopyProfile(source.id, name)
end
return nil, "Profile copies must use an installed source profile."
end
binderId = self:NormalizeBinderId(binderId) or self.GENERAL_BINDER_ID
local classFile = binderId:match("^class:(.+)$")
if binderId ~= self.GENERAL_BINDER_ID and not (classFile and self.VALID_CLASSES[classFile]) then
return nil, "Choose a valid General or class library."
end
if self:CountProfiles(binderId) >= self.MAX_PROFILES or self:CountProfiles() >= self.MAX_PROFILE_SETS then
return nil, "Binding set limit reached."
end
local previousSelectedProfileId = self.db.selectedProfileId
local originalNextId, originalUsedIds = self.db.nextId, copy(self.usedIds or {})
name = trim(name)
if name == "" then name = "New profile" end
local id = self:NewID("profile")
local profile = {
id = id,
name = self:UniqueProfileName(name, nil, binderId),
binderId = binderId,
cards = {},
bindingsEnabled = true,
}
profile.createdAt = time and time() or 0
profile.updatedAt = profile.createdAt
self.db.profiles[id] = profile
self.db.selectedProfileId = id
if type(self.EnsureProfileTemplates) == "function" then
local ensured, message = self:EnsureProfileTemplates(id)
if not ensured then
self.db.profiles[id] = nil
self.db.selectedProfileId = previousSelectedProfileId
self.db.nextId, self.usedIds = originalNextId, originalUsedIds
return nil, message
end
end
self:SelectProfile(id)
self:Fire("PROFILES_CHANGED", id)
return profile
end
function BindCards:CreateProfileSet(binderId, name, sourceId)
if sourceId then return self:CopyProfile(sourceId, name) end
return self:CreateProfile(name, nil, binderId)
end
function BindCards:CopyProfile(profileId, name)
local source = self:GetProfile(profileId)
if not source then return nil, "Profile not found." end
if self:CountProfiles(source.binderId) >= self.MAX_PROFILES or self:CountProfiles() >= self.MAX_PROFILE_SETS then
return nil, "Binding set limit reached."
end
local originalNextId, originalUsedIds = self.db.nextId, copy(self.usedIds or {})
local clone = copy(source)
clone.id = self:NewID("profile")
clone.name = self:UniqueProfileName(name or (source.name .. " Copy"), nil, source.binderId)
clone.createdAt = time and time() or 0
clone.updatedAt = clone.createdAt
local cardIds = {}
for index, card in ipairs(clone.cards) do
local oldId = card.id
card.id = self:NewID("card")
cardIds[oldId] = card.id
card.order = card.order or index
for tileIndex, tile in ipairs(card.tiles or {}) do
tile.id = self:NewID("tile")
tile.order = tile.order or tileIndex
end
end
for _, card in ipairs(clone.cards) do
card.parentId = card.parentId and cardIds[card.parentId] or nil
card.homeParentId = card.homeParentId and cardIds[card.homeParentId] or nil
local customRootId = type(card.binderId) == "string" and card.binderId:match("^custom:(.+)$")
if not card.parentId and customRootId and cardIds[customRootId] then
card.binderId = "custom:" .. cardIds[customRootId]
end
end
self.db.profiles[clone.id] = clone
local ensured, message = self:EnsureProfileTemplates(clone.id)
if not ensured then
self.db.profiles[clone.id] = nil
self.db.nextId, self.usedIds = originalNextId, originalUsedIds
return nil, message
end
self:TouchProfile(clone)
self:SelectProfile(clone.id)
self:Fire("PROFILES_CHANGED", clone.id)
return clone
end
function BindCards:DeleteProfile(profileId)
local removed = self.db.profiles[profileId]
if not removed then return false, "Binding set not found." end
if self:CountProfiles(removed.binderId) <= 1 then return false, "Keep at least one set in this library." end
self.db.profiles[profileId] = nil
local fallback = self:GetSelectedProfileSet(removed.binderId)
if not fallback then
for _, candidate in pairs(self.db.profiles) do
if candidate.binderId == removed.binderId then fallback = candidate; break end
end
end
local selections = self.db.selections or {}
local function Replace(record)
if type(record) ~= "table" then return end
if removed.binderId == self.GENERAL_BINDER_ID then
if record.generalProfileId == profileId then record.generalProfileId = fallback and fallback.id end
else
local classFile = removed.binderId:match("^class:(.+)$")
record.classProfileIds = record.classProfileIds or {}
if record.classProfileIds[classFile] == profileId then
record.classProfileIds[classFile] = fallback and fallback.id
end
end
end
Replace(selections.defaults)
for _, record in pairs(selections.characters or {}) do Replace(record) end
if self.db.selectedProfileId == profileId then self.db.selectedProfileId = fallback and fallback.id end
if self.db.settings.defaultProfileId == profileId then self.db.settings.defaultProfileId = fallback and fallback.id end
self:Fire("PROFILES_CHANGED", self.db.selectedProfileId)
self:Fire("PROFILE_SELECTED", self.db.selectedProfileId, removed.binderId)
self:Fire("ProfileChanged", self.db.selectedProfileId, removed.binderId)
self:RequestRecompile("profile-deleted")
return true
end
function BindCards:SelectProfile(profileId)
local profile = self.db.profiles[profileId]
if not profile then return false, "Binding set not found." end
local ensured, ensureMessage = self:EnsureProfileTemplates(profileId)
if not ensured then return false, ensureMessage end
self.db.selections = self.db.selections or { defaults = {}, characters = {} }
self.db.selections.defaults = self.db.selections.defaults or { classProfileIds = {} }
self.db.selections.defaults.classProfileIds = self.db.selections.defaults.classProfileIds or {}
self.db.selections.characters = self.db.selections.characters or {}
local classFile = profile.binderId and profile.binderId:match("^class:(.+)$")
local characterKey = self:GetCurrentCharacterKey()
if profile.binderId == self.GENERAL_BINDER_ID then
if not characterKey or not self.db.selections.defaults.generalProfileId then
self.db.selections.defaults.generalProfileId = profileId
end
elseif classFile then
if not characterKey or not self.db.selections.defaults.classProfileIds[classFile] then
self.db.selections.defaults.classProfileIds[classFile] = profileId
end
else
return false, "This binding set has no valid library."
end
local currentClass = self:GetCurrentClassFile()
if characterKey and (profile.binderId == self.GENERAL_BINDER_ID or classFile == currentClass) then
local record = self.db.selections.characters[characterKey]
if type(record) ~= "table" then
record = { classProfileIds = {} }
self.db.selections.characters[characterKey] = record
end
record.classProfileIds = record.classProfileIds or {}
if profile.binderId == self.GENERAL_BINDER_ID then record.generalProfileId = profileId
else record.classProfileIds[classFile] = profileId end
end
self.db.selectedProfileId = profileId
self:Fire("PROFILE_SELECTED", profileId, profile.binderId)
self:Fire("ProfileChanged", profileId, profile.binderId)
if profile.binderId == self.GENERAL_BINDER_ID or classFile == currentClass then
self:RequestRecompile("profile-selected")
end
return true
end
function BindCards:SelectProfileSet(profileId) return self:SelectProfile(profileId) end
function BindCards:SetProfileBindingsEnabled(profileId, enabled)
local profile = self:GetProfile(profileId)
if not profile then return false, "Profile not found." end
profile.bindingsEnabled = enabled == true
self:TouchProfile(profile)
self:RequestRecompile("profile-bindings-toggled")
return true
end
function BindCards:CreateUndoSnapshot(profileId)
local profile = self:GetProfile(profileId)
if not profile then return nil, "Undo data is unavailable." end
if profile.isComposite then
local profiles = {}
for _, source in pairs(profile.sourceProfiles or {}) do
if source and source.id then profiles[source.id] = copy(source) end
end
return {
profiles = profiles,
selectedProfileId = self.db.selectedProfileId,
defaultProfileId = self.db.settings and self.db.settings.defaultProfileId or nil,
}
end
return {
profileId = profile.id,
profile = copy(profile),
selectedProfileId = self.db.selectedProfileId,
defaultProfileId = self.db.settings and self.db.settings.defaultProfileId or nil,
}
end
function BindCards:RestoreUndoSnapshot(snapshot)
if type(snapshot) ~= "table" then return false, "Undo data is unavailable." end
if InCombatLockdown and InCombatLockdown() then return false, "Undo is unavailable during combat." end
-- Profile-scoped restores must not roll back unrelated profiles or settings.
if type(snapshot.profiles) == "table" then
self.usedIds = self.usedIds or {}
for profileId, savedProfile in pairs(snapshot.profiles) do
if type(savedProfile) == "table" then
local restored = copy(savedProfile)
restored.id = profileId
self.db.profiles[profileId] = restored
self.usedIds[profileId] = true
for _, card in ipairs(restored.cards or {}) do
if card.id then self.usedIds[card.id] = true end
for _, tile in ipairs(card.tiles or {}) do
if tile.id then self.usedIds[tile.id] = true end
end
end
end
end
if snapshot.selectedProfileId and self.db.profiles[snapshot.selectedProfileId] then
self.db.selectedProfileId = snapshot.selectedProfileId
end
if self.db.settings and (snapshot.defaultProfileId == nil or
self.db.profiles[snapshot.defaultProfileId]) then
self.db.settings.defaultProfileId = snapshot.defaultProfileId
end
BindCardsDB = self.db
elseif type(snapshot.profile) ~= "table" then
if type(snapshot.db) ~= "table" then return false, "Undo data is unavailable." end
self.db = copy(snapshot.db)
self.usedIds = copy(snapshot.usedIds or {})
BindCardsDB = self.db
else
local restored = copy(snapshot.profile)
local restoredProfileId = restored.id or snapshot.profileId
if type(restoredProfileId) ~= "string" or restoredProfileId == "" then
return false, "Undo data is unavailable."
end
restored.id = restoredProfileId
self.db.profiles[restoredProfileId] = restored
self.usedIds = self.usedIds or {}
self.usedIds[restoredProfileId] = true
for _, card in ipairs(restored.cards or {}) do
if card.id then self.usedIds[card.id] = true end
for _, tile in ipairs(card.tiles or {}) do
if tile.id then self.usedIds[tile.id] = true end
end
end
local selectedProfileId = snapshot.selectedProfileId
if selectedProfileId and self.db.profiles[selectedProfileId] then
self.db.selectedProfileId = selectedProfileId
elseif not self.db.profiles[self.db.selectedProfileId] then
self.db.selectedProfileId = restoredProfileId
end
if self.db.settings then
local defaultProfileId = snapshot.defaultProfileId
if defaultProfileId == nil or self.db.profiles[defaultProfileId] then
self.db.settings.defaultProfileId = defaultProfileId
end
end
BindCardsDB = self.db
end
self:Fire("PROFILES_CHANGED", self.db.selectedProfileId)
self:Fire("PROFILE_SELECTED", self.db.selectedProfileId)
self:Fire("ProfileChanged", self.db.selectedProfileId)
self:Fire("DATA_CHANGED", self.db.selectedProfileId)
self:Fire("DataChanged", self.db.selectedProfileId)
self:RequestRecompile("undo")
return true
end
function BindCards:TouchProfile(profile, changeKind)
profile = profile or self:GetSelectedProfile()
if profile then profile.updatedAt = time and time() or 0 end
if self.templateBatch then return end
self:Fire("DATA_CHANGED", profile and profile.id, changeKind)
self:Fire("DataChanged", profile and profile.id, changeKind)
end
function BindCards:FindCard(cardId, ...)
local supplied = select("#", ...) > 0
local profile = ...
if not supplied then profile = self:GetSelectedProfile() end
if not profile then return nil end
for index, card in ipairs(profile.cards) do
if card.id == cardId then return card, index end
end
end
-- Resolve globally unique card IDs to a real set when callers use the active composite.
function BindCards:FindCardOwner(cardId, profileId)
local explicit = type(profileId) == "table" and profileId or
(type(profileId) == "string" and self.db and self.db.profiles[profileId] or nil)
if explicit then
local card, index = self:FindCard(cardId, explicit)
return card and explicit or nil, card, index
end
if type(profileId) == "string" and profileId ~= "active-composite" then return nil end
local visited = {}
local general, class = self:GetSelectedProfileSets()
local function find(profile)
if not profile or visited[profile.id] then return nil end
visited[profile.id] = true
local card, index = self:FindCard(cardId, profile)
if card then return profile, card, index end
end
local profile, card, index = find(general)
if profile then return profile, card, index end
profile, card, index = find(class)
if profile then return profile, card, index end
for _, candidate in pairs(self.db and self.db.profiles or {}) do
profile, card, index = find(candidate)
if profile then return profile, card, index end
end
end
function BindCards:GetMutationProfile(profileId, binderId, parentId)
local explicit = type(profileId) == "table" and profileId or
(type(profileId) == "string" and self.db and self.db.profiles[profileId] or nil)
if explicit then return explicit end
if type(profileId) == "string" and profileId ~= "active-composite" then return nil end
if parentId then return self:FindCardOwner(parentId, profileId) end
binderId = self:NormalizeBinderId(binderId) or self.GENERAL_BINDER_ID
local classFile = binderId:match("^class:(.+)$")
if classFile then binderId = "class:" .. classFile
elseif binderId ~= self.GENERAL_BINDER_ID then binderId = self.GENERAL_BINDER_ID end
return self:GetSelectedProfileSet(binderId)
end
function BindCards:NormalizeBinderId(binderId)
if binderId == self.GENERAL_BINDER_ID then return binderId end
if type(binderId) ~= "string" or #binderId > 160 then return nil end
local class = binderId:match("^class:([A-Z]+)$")
if class and self.VALID_CLASSES[class] then return binderId end
if binderId:match("^custom:.+$") then return binderId end
end
local function inferredRootBinderId(addon, card)
local condition = card and card.condition or {}
local templateKey = card and card.templateKey
if type(templateKey) == "string" and templateKey:match("^default:class:") then
local binderId = "class:" .. templateKey:sub(15)
if addon:NormalizeBinderId(binderId) then return binderId end
end
if type(condition.class) == "string" and condition.class ~= "" then
return "class:" .. condition.class
end
if templateKey == "default:general" then return addon.GENERAL_BINDER_ID end
return card and ("custom:" .. card.id) or addon.GENERAL_BINDER_ID
end
function BindCards:GetCardBinderId(cardOrId, profile)
if type(profile) ~= "table" then profile = self:GetProfile(profile) end
local card = type(cardOrId) == "table" and cardOrId or self:FindCard(cardOrId, profile)
if not card then return nil end
local cardsById, cursor, visited = {}, card, {}
for _, candidate in ipairs(profile and profile.cards or {}) do cardsById[candidate.id] = candidate end
while cursor.parentId and cardsById[cursor.parentId] and not visited[cursor.id] do
visited[cursor.id] = true
cursor = cardsById[cursor.parentId]
end
return self:NormalizeBinderId(cursor.binderId) or inferredRootBinderId(self, cursor)
end
function BindCards:GetBinderFamily(cardOrId, profile)
local binderId = self:GetCardBinderId(cardOrId, profile)
if not binderId then return nil end
return binderId:match("^class:") and "class" or "general"
end
function BindCards:CreateCard(values, profileId)
if type(values) == "string" then
values, profileId = { name = values, parentId = profileId }, nil
end
values = type(values) == "table" and values or {}
if values.templateKey ~= nil and not self.templateBatch then return nil, "Generated cards are managed by BindCards." end
if values.homeParentId ~= nil then return nil, "Create the card in its parent before peeling it." end
values.parentId = values.parentId or values.parentID
local requestedBinderId = values.binderId
if not requestedBinderId and not values.parentId and type(values.condition) == "table" and
type(values.condition.class) == "string" then
requestedBinderId = "class:" .. values.condition.class
end
local profile = self:GetMutationProfile(profileId, requestedBinderId, values.parentId)
if not profile then return nil, "Profile not found." end
if not values.parentId and not requestedBinderId then requestedBinderId = profile.binderId end
if #profile.cards >= self.MAX_CARDS then return nil, "Card limit reached." end
if values.parentId and not self:FindCard(values.parentId, profile) then
return nil, "Parent card not found."
end
local columns = math.max(self.MIN_COLUMNS, math.min(self.MAX_COLUMNS, numberOr(values.columns, 6)))
local capacity = math.max(columns, math.min(192, math.floor(numberOr(values.capacity, columns * 2))))
local condition, conditionValid, conditionMessage = self:NormalizeCondition(values.condition or values.conditions or {}, true)
if not conditionValid then return nil, conditionMessage end
if condition.form and not values.parentId then return nil, "Form conditions belong on subcards." end
if condition.activity and not values.parentId then return nil, "Activity conditions belong on General subcards." end
if requestedBinderId ~= nil and not self:NormalizeBinderId(requestedBinderId) then
return nil, "Invalid binder."
end
if requestedBinderId ~= nil and values.parentId then return nil, "Only top-level cards own a binder." end
if profile.binderId and requestedBinderId and requestedBinderId:match("^custom:") == nil and
profile.binderId ~= requestedBinderId then
return nil, "Cards must stay in their General or class binding set."
end
local effectiveBinderId = requestedBinderId
if values.parentId then effectiveBinderId = self:GetCardBinderId(values.parentId, profile) end
local binderClass = effectiveBinderId and effectiveBinderId:match("^class:(.+)$")
if binderClass then
if condition.activity then return nil, "Activity cards must belong to the General binder." end
if condition.class and condition.class ~= binderClass then return nil, "Class binder and condition do not match." end
if condition.form and not self:IsFormValidForClass(condition.form, binderClass) then
return nil, "That form is not available in this class binder."
end
if not values.parentId then condition.class = binderClass end
elseif condition.form then
return nil, "Form cards must belong to a supported class binder."
elseif condition.activity and effectiveBinderId ~= self.GENERAL_BINDER_ID then
return nil, "Activity cards must belong to the General binder."
end
local overrideAllBindingsWhenActive = values.overrideAllBindingsWhenActive == true
if overrideAllBindingsWhenActive and condition.activity ~= "SKYRIDING" then
return nil, "Global override requires the Skyriding activity condition."
end
local cardId = self:NewID("card")
local card = {
id = cardId,
name = trim(values.name) ~= "" and trim(values.name):sub(1, 64) or "New card",
enabled = values.enabled ~= false,
collapsed = values.collapsed == true,
treeExpanded = values.treeExpanded == true,
parentId = values.parentId,
homeParentId = nil,
binderId = values.parentId and nil or requestedBinderId,
order = numberOr(values.order, #profile.cards + 1),
priority = numberOr(values.priority, 0),
columns = columns,
capacity = capacity,
layoutMode = values.layoutMode == "floating" and "floating" or "stacked",
floatX = values.floatX and math.max(-10000, math.min(10000, numberOr(values.floatX, 0))) or nil,
floatY = values.floatY and math.max(-10000, math.min(10000, numberOr(values.floatY, 0))) or nil,
floatWidth = values.floatWidth and math.max(240, math.min(1200, numberOr(values.floatWidth, 420))) or nil,
floatHeight = values.floatHeight and math.max(180, math.min(1000, numberOr(values.floatHeight, 360))) or nil,
paperWidth = values.paperWidth and math.max(300, math.min(720, numberOr(values.paperWidth, 430))) or nil,
paperHeight = values.paperHeight and math.max(120, math.min(1000, numberOr(values.paperHeight, 260))) or nil,
templateKey = type(values.templateKey) == "string" and values.templateKey:sub(1, 64) or nil,
condition = condition,
overrideAllBindingsWhenActive = overrideAllBindingsWhenActive and true or nil,
tiles = {},
}
if not card.parentId and not card.binderId then card.binderId = inferredRootBinderId(self, card) end
profile.cards[#profile.cards + 1] = card
if not self.templateBatch then
self:TouchProfile(profile)
self:RequestRecompile("card-created")
end
return card
end
local function wouldCreateCycle(addon, card, newParentId, profile)
local cursor = newParentId
local visited = {}
while cursor do
if cursor == card.id or visited[cursor] then return true end
visited[cursor] = true
local parent = addon:FindCard(cursor, profile)
cursor = parent and (parent.parentId or parent.homeParentId)
end
return false
end
local function subtreeClassConflict(addon, cardId, binderClass, profile)
local byId = {}
for _, candidate in ipairs(profile.cards or {}) do byId[candidate.id] = candidate end
for _, candidate in ipairs(profile.cards or {}) do
local cursor, seen = candidate, {}
while cursor and (cursor.parentId or cursor.homeParentId) and not seen[cursor.id] do
seen[cursor.id] = true
local ownerId = cursor.parentId or cursor.homeParentId
if ownerId == cardId then
local class = candidate.condition and candidate.condition.class
if class and class ~= binderClass then return true end
local form = candidate.condition and candidate.condition.form
if form and (not binderClass or not addon:IsFormValidForClass(form, binderClass)) then return true end
local activity = candidate.condition and candidate.condition.activity
if activity and binderClass then return true end
break
end
cursor = byId[ownerId]
end
end
return false
end
function BindCards:UpdateCard(cardId, values, profileId)
local profile, card = self:FindCardOwner(cardId, profileId)
if not card then return nil, "Card not found." end
values = type(values) == "table" and values or {}
if self:IsValidTemplateKey(card.templateKey, card.condition) and
(values.name ~= nil or values.enabled ~= nil) then
return nil, "Default card identity cannot be changed."
end
local pendingCondition
if values.condition ~= nil or values.conditions ~= nil then
local valid, message
pendingCondition, valid, message = self:NormalizeCondition(values.condition or values.conditions, true)
if not valid then return nil, message end
if card.templateKey and not self:IsValidTemplateKey(card.templateKey, pendingCondition) then
return nil, "Default card role, class, or specialization cannot be changed."
end
end
local inheritedBinderId = self:GetCardBinderId(card, profile)
if values.binderId ~= nil and not self:NormalizeBinderId(values.binderId) then
return nil, "Invalid binder."
end
local intendedParentId = card.parentId
if values.parentId ~= nil then intendedParentId = values.parentId ~= false and values.parentId or nil end
local intendedHomeParentId = card.homeParentId
if values.homeParentId ~= nil then
intendedHomeParentId = values.homeParentId ~= false and values.homeParentId or nil
if intendedHomeParentId and not self:FindCard(intendedHomeParentId, profile) then
return nil, "Home parent card not found."
end
if intendedHomeParentId and wouldCreateCycle(self, card, intendedHomeParentId, profile) then
return nil, "Cards cannot contain themselves."
end
end
if intendedParentId and intendedHomeParentId then
return nil, "A card cannot be nested and peeled at the same time."
end
if values.binderId ~= nil and intendedParentId then return nil, "Only top-level cards own a binder." end
if values.parentId ~= nil then
if intendedParentId and not self:FindCard(intendedParentId, profile) then
return nil, "Parent card not found."
end
if wouldCreateCycle(self, card, intendedParentId, profile) then return nil, "Cards cannot contain themselves." end
end
local intendedBinderId
if not intendedParentId then intendedBinderId = values.binderId or card.binderId or inheritedBinderId end
local effectiveBinderId = intendedBinderId
if intendedParentId then effectiveBinderId = self:GetCardBinderId(intendedParentId, profile) end
local intendedLayoutMode = values.layoutMode == nil and card.layoutMode or
(values.layoutMode == "floating" and "floating" or "stacked")
if intendedLayoutMode == "floating" and intendedParentId then
return nil, "Only top-level cards can be detached."
end
if intendedHomeParentId then
if intendedLayoutMode ~= "floating" then
return nil, "A peeled card must remain floating."
end
local homeBinderId = self:GetCardBinderId(intendedHomeParentId, profile)
if intendedBinderId ~= homeBinderId then
return nil, "A peeled card must remain in its parent binder."
end
end
local intendedCondition = pendingCondition or card.condition
if intendedCondition.form and not intendedParentId and not intendedHomeParentId then
return nil, "Form conditions belong on subcards."
end
if intendedCondition.activity and not intendedParentId and not intendedHomeParentId then
return nil, "Activity conditions belong on General subcards."
end
local binderClass = effectiveBinderId and effectiveBinderId:match("^class:(.+)$")
if binderClass then
if intendedCondition.activity then return nil, "Activity cards must belong to the General binder." end
if intendedCondition.class and intendedCondition.class ~= binderClass then
return nil, "Class binder and condition do not match."
end
if not intendedParentId and intendedCondition.class ~= binderClass then
intendedCondition = self.CopyTable(intendedCondition)
intendedCondition.class = binderClass
end
if intendedCondition.form and not self:IsFormValidForClass(intendedCondition.form, binderClass) then
return nil, "That form is not available in this class binder."
end
elseif intendedCondition.form then
return nil, "Form cards must belong to a supported class binder."
elseif intendedCondition.activity and effectiveBinderId ~= self.GENERAL_BINDER_ID then
return nil, "Activity cards must belong to the General binder."
end
local intendedOverride = card.overrideAllBindingsWhenActive == true
if values.overrideAllBindingsWhenActive ~= nil then
intendedOverride = values.overrideAllBindingsWhenActive == true
end
if intendedOverride and intendedCondition.activity ~= "SKYRIDING" then
return nil, "Global override requires the Skyriding activity condition."
end
if subtreeClassConflict(self, card.id, binderClass, profile) then
return nil, "A nested class condition conflicts with the destination binder."
end
if values.parentId ~= nil then
card.parentId = intendedParentId
card.binderId = intendedBinderId
if intendedParentId then card.homeParentId = nil end
elseif values.binderId ~= nil then
card.binderId = intendedBinderId
end
if values.homeParentId ~= nil then card.homeParentId = intendedHomeParentId end
if values.name ~= nil then card.name = (trim(values.name) ~= "" and trim(values.name) or "Card"):sub(1, 64) end
if values.enabled ~= nil then card.enabled = values.enabled == true end
if values.collapsed ~= nil then card.collapsed = values.collapsed == true end
if values.treeExpanded ~= nil then card.treeExpanded = values.treeExpanded == true end
if values.order ~= nil then card.order = numberOr(values.order, card.order) end
if values.priority ~= nil then card.priority = numberOr(values.priority, card.priority or 0) end
if values.columns ~= nil then
card.columns = math.max(self.MIN_COLUMNS, math.min(self.MAX_COLUMNS, numberOr(values.columns, card.columns)))
card.capacity = math.max(card.columns, card.capacity or card.columns * 2)
end
if values.capacity ~= nil then
card.capacity = math.max(card.columns or self.MIN_COLUMNS,
math.min(192, math.floor(numberOr(values.capacity, card.capacity or self.MIN_COLUMNS))))
end
if values.layoutMode ~= nil then
card.layoutMode = intendedLayoutMode
end
if values.floatX ~= nil then card.floatX = math.max(-10000, math.min(10000, numberOr(values.floatX, 0))) end
if values.floatY ~= nil then card.floatY = math.max(-10000, math.min(10000, numberOr(values.floatY, 0))) end
if values.floatWidth ~= nil then
card.floatWidth = math.max(240, math.min(1200, numberOr(values.floatWidth, card.floatWidth or 420)))
end
if values.floatHeight ~= nil then
card.floatHeight = math.max(180, math.min(1000, numberOr(values.floatHeight, card.floatHeight or 360)))
end
if values.paperWidth ~= nil then
card.paperWidth = math.max(300, math.min(720, numberOr(values.paperWidth, card.paperWidth or 430)))
end
if values.paperHeight ~= nil then
card.paperHeight = math.max(120, math.min(1000, numberOr(values.paperHeight, card.paperHeight or 260)))
end
if pendingCondition or intendedCondition ~= card.condition then card.condition = intendedCondition end
if values.overrideAllBindingsWhenActive ~= nil then
card.overrideAllBindingsWhenActive = values.overrideAllBindingsWhenActive == true and true or nil
end
self:TouchProfile(profile)
if values.enabled ~= nil or values.parentId ~= nil or values.homeParentId ~= nil or
values.binderId ~= nil or values.order ~= nil or values.priority ~= nil or
values.condition ~= nil or values.conditions ~= nil or values.overrideAllBindingsWhenActive ~= nil then
self:RequestRecompile("card-updated")
end
return card
end