-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.lua
More file actions
1686 lines (1569 loc) · 70.1 KB
/
Copy pathBootstrap.lua
File metadata and controls
1686 lines (1569 loc) · 70.1 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, ns = ...
ns.ADDON_NAME = ADDON_NAME
-- One shared texture keeps the addon's header, minimap button, compartment
-- entry, and packaged metadata visually aligned.
ns.ICON_TEXTURE = "Interface\\AddOns\\Resonance\\Assets\\ResonanceIcon"
ns.GENERIC_ICON_TEXTURE = "Interface\\Icons\\INV_Misc_Rune_01"
local getMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
ns.VERSION = (type(getMetadata) == "function" and getMetadata("Resonance", "Version")) or "1.1.0"
ns.COLOR = "|cff9d7cff"
ns.GENERIC_SPEC_ID = 0
ns.SPEC_ORDER = {
ns.GENERIC_SPEC_ID,
71, 72, 73, -- Warrior
65, 66, 70, -- Paladin
253, 254, 255, -- Hunter
259, 260, 261, -- Rogue
256, 257, 258, -- Priest
250, 251, 252, -- Death Knight
262, 263, 264, -- Shaman
62, 63, 64, -- Mage
265, 266, 267, -- Warlock
268, 269, 270, -- Monk
102, 103, 104, 105, -- Druid
577, 581, 1480, -- Demon Hunter
1467, 1468, 1473, -- Evoker
}
ns.SUPPORTED_SPECS = {
[ns.GENERIC_SPEC_ID] = "Generic",
[71] = "Arms Warrior",
[72] = "Fury Warrior",
[73] = "Protection Warrior",
[65] = "Holy Paladin",
[66] = "Protection Paladin",
[70] = "Retribution Paladin",
[253] = "Beast Mastery Hunter",
[254] = "Marksmanship Hunter",
[255] = "Survival Hunter",
[259] = "Assassination Rogue",
[260] = "Outlaw Rogue",
[261] = "Subtlety Rogue",
[256] = "Discipline Priest",
[257] = "Holy Priest",
[258] = "Shadow Priest",
[250] = "Blood Death Knight",
[251] = "Frost Death Knight",
[252] = "Unholy Death Knight",
[262] = "Elemental Shaman",
[263] = "Enhancement Shaman",
[264] = "Restoration Shaman",
[62] = "Arcane Mage",
[63] = "Fire Mage",
[64] = "Frost Mage",
[265] = "Affliction Warlock",
[266] = "Demonology Warlock",
[267] = "Destruction Warlock",
[268] = "Brewmaster Monk",
[269] = "Windwalker Monk",
[270] = "Mistweaver Monk",
[102] = "Balance Druid",
[103] = "Feral Druid",
[104] = "Guardian Druid",
[105] = "Restoration Druid",
[577] = "Havoc Demon Hunter",
[581] = "Vengeance Demon Hunter",
[1480] = "Devourer Demon Hunter",
[1467] = "Devastation Evoker",
[1468] = "Preservation Evoker",
[1473] = "Augmentation Evoker",
}
-- Increment whenever curated built-in layers change. Existing untouched
-- built-ins then refresh on reload; personal and saved sets remain intact.
ns.BUILTIN_SET_VERSION = 10
ns.PROFILE_SCHEMA_VERSION = 1
ns.RULE_CATALOG_VERSION = 6
ns.SOUND_CATALOG_VERSION = 4
ns.CURATED_PRESETS = {
{ key = "subtle", name = "Resonance Subtle" },
{ key = "medium", name = "Resonance Medium" },
{ key = "expressive", name = "Resonance Expressive" },
}
local DEFAULTS = {
version = 8,
enabled = true,
palette = "subtle",
channel = "SFX",
specEnabled = {},
ruleOverrides = {},
ruleSettings = {},
specProfiles = {},
favorites = {},
soundSortDebug = false,
categoryDraft = {},
deleteDraft = {},
categoryExport = nil,
minimap = {
hide = false,
angle = 225,
},
debug = false,
soloMode = false,
tutorial = {
completedVersion = 0,
lastStep = 1,
},
}
for specID in pairs(ns.SUPPORTED_SPECS) do
DEFAULTS.specEnabled[specID] = true
end
local CHARACTER_DEFAULTS = {
version = 9,
specs = {},
}
ns.DEFAULTS = DEFAULTS
ns.Runtime = {
capabilities = { hasSpellID = {}, rankBySpellID = {} },
activeRules = {},
eventSpellRules = {},
lastRulePlay = {},
refreshQueued = false,
castingSoundGeneration = 0,
castingSoundHandles = {},
castingSoundTimers = {},
delayedSoundGeneration = 0,
delayedSoundTimers = {},
previewGeneration = 0,
previewTimers = {},
resolvedSoundCache = {},
}
-- Rule layers are edited in place, but profile loads/resets can replace the
-- whole working tree. Keep the derived sound descriptors and any queued
-- delayed playback in sync at the mutation boundary instead of waiting for
-- the next zero-delay refresh callback.
function ns:InvalidateRuntimeAudio(cancelDelayed)
self.Runtime.resolvedSoundCache = {}
if cancelDelayed and self.CancelDelayedSoundTimers then
self:CancelDelayedSoundTimers()
end
end
local function CopyDefaults(source)
local result = {}
for key, value in pairs(source) do
if type(value) == "table" then
result[key] = CopyDefaults(value)
else
result[key] = value
end
end
return result
end
local function FillDefaults(target, source)
for key, value in pairs(source) do
if target[key] == nil then
target[key] = type(value) == "table" and CopyDefaults(value) or value
elseif type(value) == "table" and type(target[key]) == "table" then
FillDefaults(target[key], value)
end
end
end
-- Compatibility registries are intentionally permanent. Never reuse a retired
-- rule ID for another spell; point it at its successor here instead. Removed
-- sounds should be remapped here or retained as hidden catalog tombstones.
local SOUND_ID_REMAP = {
[1884443] = 568175, -- encrypted Avenging Wrath revamp take -> verified legacy impact
}
local RULE_ID_ALIASES = {}
local CATEGORY_ID_ALIASES = {}
ns.SoundIDRemap = SOUND_ID_REMAP
ns.RuleIDAliases = RULE_ID_ALIASES
ns.CategoryIDAliases = CATEGORY_ID_ALIASES
local function ResolveAlias(mapping, value)
local seen = {}
while mapping[value] ~= nil and not seen[value] do
seen[value] = true
value = mapping[value]
end
return value
end
local function ValidateAliasMap(name, mapping)
for start in pairs(mapping) do
local seen, value = {}, start
while mapping[value] ~= nil do
if seen[value] then
error("Resonance " .. name .. " alias cycle at " .. tostring(value))
end
seen[value] = true
value = mapping[value]
end
end
end
local function ValidateCompatibilityRegistries(owner)
ValidateAliasMap("sound", SOUND_ID_REMAP)
ValidateAliasMap("rule", RULE_ID_ALIASES)
ValidateAliasMap("category", CATEGORY_ID_ALIASES)
local validCategories = {}
for _, category in ipairs(owner.SoundCategories or {}) do validCategories[category.id] = true end
for oldID in pairs(SOUND_ID_REMAP) do
local target = ResolveAlias(SOUND_ID_REMAP, oldID)
if not (owner.SoundByID and owner.SoundByID[target]) then
owner:Print("Compatibility warning: sound " .. oldID .. " maps to missing " .. tostring(target))
end
end
for oldID in pairs(RULE_ID_ALIASES) do
local target = ResolveAlias(RULE_ID_ALIASES, oldID)
if not (owner.RuleByID and owner.RuleByID[target]) then
owner:Print("Compatibility warning: rule " .. oldID .. " maps to missing " .. tostring(target))
end
end
for oldID in pairs(CATEGORY_ID_ALIASES) do
local target = ResolveAlias(CATEGORY_ID_ALIASES, oldID)
if not validCategories[target] then
owner:Print("Compatibility warning: category " .. oldID .. " maps to missing " .. tostring(target))
end
end
end
local function RemapNumericKeyedTable(source)
if type(source) ~= "table" then return {} end
local migrated = {}
-- Canonical keys win if an old and a new identifier coexist.
for key, value in pairs(source) do
local soundID = tonumber(key)
if not soundID or ResolveAlias(SOUND_ID_REMAP, soundID) == soundID then
migrated[soundID or key] = value
end
end
for key, value in pairs(source) do
local soundID = tonumber(key)
if soundID then
local resolved = ResolveAlias(SOUND_ID_REMAP, soundID)
if migrated[resolved] == nil then migrated[resolved] = value end
end
end
return migrated
end
local function NormalizeAccountSoundReferences(database)
database.favorites = RemapNumericKeyedTable(database.favorites)
database.categoryDraft = RemapNumericKeyedTable(database.categoryDraft)
database.deleteDraft = RemapNumericKeyedTable(database.deleteDraft)
if type(database.categoryExport) == "table" then
local export = database.categoryExport
local hadCompactMaps = type(export.moves) == "table" or type(export.deletions) == "table"
if not hadCompactMaps and type(export.sounds) == "table" then
export.moves, export.deletions = {}, {}
for _, entry in ipairs(export.sounds) do
if type(entry) == "table" and tonumber(entry.id) then
local soundID = ResolveAlias(SOUND_ID_REMAP, tonumber(entry.id))
local categoryID = ResolveAlias(CATEGORY_ID_ALIASES, entry.category)
local canonical = ns.SoundByID and ns.SoundByID[soundID]
if canonical and categoryID and categoryID ~= canonical.category then
export.moves[soundID] = categoryID
end
if entry.deleted then export.deletions[soundID] = true end
end
end
else
export.moves = RemapNumericKeyedTable(export.moves)
export.deletions = RemapNumericKeyedTable(export.deletions)
end
for soundID, categoryID in pairs(export.moves) do
export.moves[soundID] = ResolveAlias(CATEGORY_ID_ALIASES, categoryID)
end
if type(export.sounds) == "table" then
for _, sound in ipairs(export.sounds) do
if type(sound) == "table" and tonumber(sound.id) then
sound.id = ResolveAlias(SOUND_ID_REMAP, tonumber(sound.id))
sound.category = ResolveAlias(CATEGORY_ID_ALIASES, sound.category)
end
end
end
end
local validCategories = {}
for _, category in ipairs(ns.SoundCategories or {}) do validCategories[category.id] = true end
database.legacyCategoryDraft = type(database.legacyCategoryDraft) == "table"
and database.legacyCategoryDraft or {}
for soundID, categoryID in pairs(database.categoryDraft) do
local migratedCategory = ResolveAlias(CATEGORY_ID_ALIASES, categoryID)
if validCategories[migratedCategory] then
database.categoryDraft[soundID] = migratedCategory
else
-- Preserve the old value for a future alias while returning the
-- sound to its canonical category instead of hiding it.
database.legacyCategoryDraft[soundID] = categoryID
database.categoryDraft[soundID] = nil
end
end
-- Once a user draft has been canonized in the shipped catalog, retire its
-- staging records automatically. Removed sounds likewise leave no stale
-- red/deletion state behind after the next reload.
for soundID, categoryID in pairs(database.categoryDraft) do
local sound = ns.SoundByID and ns.SoundByID[tonumber(soundID)]
if not sound or categoryID == sound.category then database.categoryDraft[soundID] = nil end
end
for soundID in pairs(database.deleteDraft) do
if not (ns.SoundByID and ns.SoundByID[tonumber(soundID)]) then database.deleteDraft[soundID] = nil end
end
local export = database.categoryExport
if type(export) == "table" then
for soundID, categoryID in pairs(export.moves or {}) do
local sound = ns.SoundByID and ns.SoundByID[tonumber(soundID)]
if not sound or categoryID == sound.category then export.moves[soundID] = nil end
end
for soundID in pairs(export.deletions or {}) do
if not (ns.SoundByID and ns.SoundByID[tonumber(soundID)]) then export.deletions[soundID] = nil end
end
end
end
local ACCOUNT_MIGRATIONS = {
[6] = function() end, -- Introduced ordered migrations and compatibility metadata.
[7] = function(database) -- Interactive tutorial progress; no profile data changes.
if type(database.tutorial) ~= "table" then database.tutorial = {} end
end,
[8] = function(database) -- Debug sorting is always opt-in after this update.
database.soundSortDebug = false
end,
[9] = function() end, -- Individual hearthstone settings migrate per sound set.
}
local function RunOrderedMigrations(database, previousVersion, currentVersion, migrations)
previousVersion = math.max(0, math.floor(tonumber(previousVersion) or 0))
for version = previousVersion + 1, currentVersion do
local migrate = migrations[version]
if migrate then migrate(database) end
-- Advance one step at a time. If a later migration errors, WoW will not
-- falsely claim the database reached a schema it never completed.
database.version = version
end
end
local function MigrateAccountDatabase(database, previousVersion)
if previousVersion > DEFAULTS.version then return end
RunOrderedMigrations(database, previousVersion, DEFAULTS.version, ACCOUNT_MIGRATIONS)
-- Identifier aliases may grow without changing the surrounding table shape.
NormalizeAccountSoundReferences(database)
database.version = DEFAULTS.version
end
function ns:InitializeDatabase()
ValidateCompatibilityRegistries(self)
local previousAccountVersion
if type(ResonanceDB) ~= "table" then
ResonanceDB = CopyDefaults(DEFAULTS)
previousAccountVersion = DEFAULTS.version
else
previousAccountVersion = tonumber(ResonanceDB.version) or 0
MigrateAccountDatabase(ResonanceDB, previousAccountVersion)
if type(ResonanceDB.specEnabled) ~= "table" then ResonanceDB.specEnabled = {} end
if type(ResonanceDB.ruleOverrides) ~= "table" then ResonanceDB.ruleOverrides = {} end
if type(ResonanceDB.ruleSettings) ~= "table" then ResonanceDB.ruleSettings = {} end
if type(ResonanceDB.specProfiles) ~= "table" then ResonanceDB.specProfiles = {} end
if type(ResonanceDB.favorites) ~= "table" then ResonanceDB.favorites = {} end
if type(ResonanceDB.categoryDraft) ~= "table" then ResonanceDB.categoryDraft = {} end
if type(ResonanceDB.deleteDraft) ~= "table" then ResonanceDB.deleteDraft = {} end
if type(ResonanceDB.minimap) ~= "table" then ResonanceDB.minimap = {} end
FillDefaults(ResonanceDB, DEFAULTS)
end
if not self.SoundPalettes[ResonanceDB.palette] then ResonanceDB.palette = DEFAULTS.palette end
-- Density was a runtime filter before 1.1. Presets now own actual rule toggles.
ResonanceDB.density = nil
if ResonanceDB.channel ~= "SFX" and ResonanceDB.channel ~= "Dialog" and ResonanceDB.channel ~= "Master" then
ResonanceDB.channel = DEFAULTS.channel
end
if type(ResonanceDB.enabled) ~= "boolean" then ResonanceDB.enabled = DEFAULTS.enabled end
-- Debug controls are intentionally unavailable in this release. Always
-- fail closed here so an old saved toggle cannot continue spamming chat
-- after the UI that controlled it has been tucked away.
ResonanceDB.debug = false
if type(ResonanceDB.soloMode) ~= "boolean" then ResonanceDB.soloMode = false end
-- Keep category drafts themselves, but clear the dormant editor mode.
ResonanceDB.soundSortDebug = false
for specID, defaultValue in pairs(DEFAULTS.specEnabled) do
if type(ResonanceDB.specEnabled[specID]) ~= "boolean" then
ResonanceDB.specEnabled[specID] = defaultValue
end
end
for ruleID, enabled in pairs(ResonanceDB.ruleOverrides) do
if type(ruleID) ~= "string" or type(enabled) ~= "boolean" then
ResonanceDB.ruleOverrides[ruleID] = nil
end
end
self:InitializeProfiles(previousAccountVersion)
self:InitializeCharacterProfiles()
if type(ResonanceDB.minimap.hide) ~= "boolean" then ResonanceDB.minimap.hide = DEFAULTS.minimap.hide end
if type(ResonanceDB.minimap.angle) ~= "number" then ResonanceDB.minimap.angle = DEFAULTS.minimap.angle end
if (tonumber(ResonanceDB.version) or 0) < DEFAULTS.version then
ResonanceDB.version = DEFAULTS.version
end
self.DB = ResonanceDB
self.CharDB = ResonanceCharDB
end
function ns:InitializeProfiles(previousVersion)
local hadLegacy = next(ResonanceDB.ruleOverrides or {}) ~= nil or next(ResonanceDB.ruleSettings or {}) ~= nil
for specID in pairs(self.SUPPORTED_SPECS) do
local store = ResonanceDB.specProfiles[specID]
if type(store) ~= "table" then
store = { active = hadLegacy and "Migrated" or "Default", profiles = {} }
ResonanceDB.specProfiles[specID] = store
end
if type(store.profiles) ~= "table" then store.profiles = {} end
if type(store.active) ~= "string" or store.active == "" then store.active = "Default" end
if type(store.profiles[store.active]) ~= "table" then
store.profiles[store.active] = { rules = {} }
end
for name, profile in pairs(store.profiles) do
if type(name) ~= "string" or type(profile) ~= "table" then
store.profiles[name] = nil
elseif type(profile.rules) ~= "table" then
profile.rules = {}
end
end
end
if (tonumber(previousVersion) or 0) < 3 and hadLegacy then
for ruleID, rule in pairs(self.RuleByID or {}) do
local legacyEnabled = ResonanceDB.ruleOverrides[ruleID]
local legacy = ResonanceDB.ruleSettings[ruleID]
if legacyEnabled ~= nil or type(legacy) == "table" then
local profile = ResonanceDB.specProfiles[rule.spec].profiles.Migrated
profile.rules[ruleID] = profile.rules[ruleID] or {}
profile.rules[ruleID].enabled = legacyEnabled
if legacy and legacy.layers == 2 then profile.rules[ruleID].layerCount = 2 end
end
end
end
end
local function DeepCopy(source)
if type(source) ~= "table" then return source end
local copy = {}
for key, value in pairs(source) do copy[key] = DeepCopy(value) end
return copy
end
-- Grouped hearthstone cards became one stable card per toy/item. This is a
-- one-to-many profile migration, so it cannot use the normal ID alias map.
local function ExpandHearthstoneGroupRules(set)
if type(set) ~= "table" or type(set.rules) ~= "table" then return end
for legacyID, targetIDs in pairs(ns.HearthstoneGroupRuleExpansions or {}) do
local oldCast = set.rules[legacyID]
local oldCasting = set.rules[legacyID .. "_casting"]
if type(targetIDs) == "table" then
for _, targetID in ipairs(targetIDs) do
if type(oldCast) == "table" and set.rules[targetID] == nil then
set.rules[targetID] = DeepCopy(oldCast)
end
if type(oldCasting) == "table" and set.rules[targetID .. "_casting"] == nil then
set.rules[targetID .. "_casting"] = DeepCopy(oldCasting)
end
end
end
set.rules[legacyID] = nil
set.rules[legacyID .. "_casting"] = nil
end
end
-- Individual Hearthstone cards use the normal two editable slots unless the
-- player actually added another sound. Older family cards could carry an
-- unused third placeholder into every expanded card; remove only genuinely
-- empty optional slots, never a selected (even disabled) layer.
local function TrimEmptyHearthstoneLayers(set)
if type(set) ~= "table" or type(set.rules) ~= "table" then return end
for ruleID, config in pairs(set.rules) do
local rule = ns.RuleByID and ns.RuleByID[ruleID]
if rule and rule.hearthstoneItemID and type(config) == "table" and type(config.layers) == "table" then
local layerCount = math.max(2, math.floor(tonumber(config.layerCount) or 2))
while layerCount > 2 do
local layer = config.layers[layerCount]
if type(layer) == "table" and tonumber(layer.soundID) and tonumber(layer.soundID) > 0 then
break
end
config.layers[layerCount] = nil
layerCount = layerCount - 1
end
config.layerCount = layerCount
end
end
end
-- Crystal lift is a short pickup chime, not an Arcantina teleport texture.
-- Replace the bundled Casting accent with a longer power-source resonance and
-- its old completion accent with a compact spark impact. This preserves each
-- layer's enabled state and timing.
local function RefreshArcantinaCastingAccent(set)
local rules = type(set) == "table" and set.rules
if type(rules) ~= "table" then return end
local replacements = {
generic_arcantina = 2428623,
generic_arcantina_casting = 566646,
}
for ruleID, replacementID in pairs(replacements) do
local config = rules[ruleID]
if type(config) == "table" and type(config.layers) == "table" then
for _, layer in pairs(config.layers) do
if type(layer) == "table" and tonumber(layer.soundID) == 4580313 then
layer.soundID = replacementID
layer.soundKind = "file"
layer.soundLabel = nil
layer.missingSound = nil
end
end
end
end
end
local function RemapSetSounds(set)
if type(set) ~= "table" or type(set.rules) ~= "table" then return end
for _, config in pairs(set.rules) do
if type(config) == "table" and type(config.layers) == "table" then
for _, layer in pairs(config.layers) do
if type(layer) == "table" and tonumber(layer.soundID) then
layer.soundID = ResolveAlias(SOUND_ID_REMAP, tonumber(layer.soundID))
end
end
end
end
end
local function NormalizeProfileSet(set, allowNewRuleDefaults)
if type(set) ~= "table" then return end
if type(set.rules) ~= "table" then set.rules = {} end
ExpandHearthstoneGroupRules(set)
TrimEmptyHearthstoneLayers(set)
RefreshArcantinaCastingAccent(set)
local migratedRules = {}
-- Keep an already-current configuration if both old and new IDs exist.
for ruleID, config in pairs(set.rules) do
if ResolveAlias(RULE_ID_ALIASES, ruleID) == ruleID then
migratedRules[ruleID] = config
end
end
for ruleID, config in pairs(set.rules) do
local resolved = ResolveAlias(RULE_ID_ALIASES, ruleID)
if migratedRules[resolved] == nil then
migratedRules[resolved] = config
end
end
set.rules = migratedRules
for _, config in pairs(set.rules) do
if type(config) == "table" and type(config.layers) == "table" then
for _, layer in pairs(config.layers) do
if type(layer) == "table" and tonumber(layer.soundID) then
local soundID = tonumber(layer.soundID)
layer.soundID = ResolveAlias(SOUND_ID_REMAP, soundID)
local sound = ns.SoundByID and ns.SoundByID[layer.soundID]
if sound then
layer.soundKind = sound.kind or "file"
layer.soundLabel = sound.label
layer.missingSound = nil
else
-- Keep the original choice recoverable, but mark it so
-- runtime playback can fail closed until it is restored
-- or explicitly remapped in a future migration.
layer.soundKind = layer.soundKind or "file"
layer.soundLabel = layer.soundLabel or ("Sound " .. layer.soundID)
layer.missingSound = true
end
end
end
end
end
set.schemaVersion = math.max(tonumber(set.schemaVersion) or 0, ns.PROFILE_SCHEMA_VERSION)
set.ruleCatalogVersion = math.max(tonumber(set.ruleCatalogVersion) or 0, ns.RULE_CATALOG_VERSION)
set.soundCatalogVersion = math.max(tonumber(set.soundCatalogVersion) or 0, ns.SOUND_CATALOG_VERSION)
if set.ruleFallback == nil then
set.ruleFallback = allowNewRuleDefaults and "current-defaults" or "disabled"
end
end
local function FreezeProfileSet(set, specID, includeMissingRules)
if type(set) ~= "table" then return end
if type(set.rules) ~= "table" then set.rules = {} end
ExpandHearthstoneGroupRules(set)
for _, rule in ipairs(ns.RulesBySpec[specID] or {}) do
local existing = set.rules[rule.id]
if existing ~= nil or includeMissingRules then
local config = type(existing) == "table" and existing or {}
set.rules[rule.id] = config
if type(config.enabled) ~= "boolean" then
config.enabled = set.ruleFallback == "disabled" and false or rule.defaultOn == true
end
if type(config.layers) ~= "table" then config.layers = {} end
local layerCount = tonumber(config.layerCount)
if not layerCount then
layerCount = math.max(2, #(rule.defaultSounds or {}))
for index in pairs(config.layers) do
if type(index) == "number" and index > layerCount then layerCount = index end
end
end
layerCount = math.max(2, math.min(ns.MAX_RULE_LAYERS or 8, math.floor(layerCount)))
config.layerCount = layerCount
for index = 1, layerCount do
local layer = config.layers[index]
local defaultSound = rule.defaultSounds and rule.defaultSounds[index]
local defaultDelay = rule.defaultDelays and tonumber(rule.defaultDelays[index])
or (index == 1 and math.floor((rule.delay or 0) * 1000 + 0.5) or 0)
if type(layer) ~= "table" then
layer = {
enabled = defaultSound ~= nil,
soundID = defaultSound or false,
delayMs = defaultDelay,
}
config.layers[index] = layer
else
if type(layer.enabled) ~= "boolean" then layer.enabled = defaultSound ~= nil end
if layer.soundID == nil then layer.soundID = defaultSound or false end
if tonumber(layer.delayMs) == nil then layer.delayMs = defaultDelay end
end
end
for index in pairs(config.layers) do
if type(index) == "number" and index > layerCount then config.layers[index] = nil end
end
end
end
set.ruleFallback = "disabled"
end
local CHARACTER_STORE_MIGRATIONS = {
[4] = function(store, specID)
for _, set in pairs(store.savedSets or {}) do
if type(set) == "table" and set.builtin ~= true then
-- Generated sets already stored explicit toggles. Ambiguous
-- sparse legacy entries fail closed instead of inheriting a
-- possibly changed default from the new addon release.
set.ruleFallback = "disabled"
FreezeProfileSet(set, specID, false)
NormalizeProfileSet(set, false)
end
end
local loadedSet = store.loadedName and store.savedSets and store.savedSets[store.loadedName]
if type(store.working) == "table" and not (loadedSet and loadedSet.builtin == true) then
store.working.ruleFallback = "disabled"
FreezeProfileSet(store.working, specID, false)
NormalizeProfileSet(store.working, false)
end
end,
[5] = function(store)
-- Before v5, loadedName doubled as the dirty flag: every edit cleared
-- it. Preserve that meaning once, then keep the source set name and a
-- separate dirty bit from this version onward.
if type(store.dirty) ~= "boolean" then
store.dirty = store.loadedName == nil
end
end,
[6] = function(store)
-- The personal set name is created lazily on the first edit.
-- Keep older character/spec stores untouched until then.
if store.autoSaveName ~= nil and type(store.autoSaveName) ~= "string" then
store.autoSaveName = nil
end
end,
[7] = function(store)
-- v7 replaces rolling auto-saves with one first-edit personal set.
-- Existing automatic rows are reused and refreshed on that first edit.
store.personalSetInitialized = false
end,
[8] = function(store, specID)
-- The Generic tab is a normal per-character store shared by every
-- specialization. Its profile is created by the standard initializer.
if specID == ns.GENERIC_SPEC_ID and type(store.personalSetInitialized) ~= "boolean" then
store.personalSetInitialized = false
end
end,
}
local function RunCharacterStoreMigrations(store, specID, previousVersion)
previousVersion = math.max(0, math.floor(tonumber(previousVersion) or 0))
for version = previousVersion + 1, CHARACTER_DEFAULTS.version do
local migrate = CHARACTER_STORE_MIGRATIONS[version]
if migrate then migrate(store, specID) end
end
end
local PRESET_RANK = { subtle = 1, medium = 2, expressive = 3 }
local function BuildCuratedPreset(specID, presetKey)
local targetRank = PRESET_RANK[presetKey] or 2
local preset = {
rules = {}, builtin = true, builtinVersion = ns.BUILTIN_SET_VERSION, preset = presetKey,
schemaVersion = ns.PROFILE_SCHEMA_VERSION,
ruleCatalogVersion = ns.RULE_CATALOG_VERSION,
soundCatalogVersion = ns.SOUND_CATALOG_VERSION,
ruleFallback = "current-defaults",
}
for _, rule in ipairs(ns.RulesBySpec[specID] or {}) do
local curated = ns.CuratedRulePresets and ns.CuratedRulePresets[rule.id]
local curatedPreset = curated and curated[presetKey]
if type(curatedPreset) == "table" then
local config = { enabled = curatedPreset.enabled == true, layers = {} }
local layerCount = math.max(2, #(rule.defaultSounds or {}), #(curatedPreset.layers or {}))
for index = 1, math.min(ns.MAX_RULE_LAYERS or 8, layerCount) do
local source = curatedPreset.layers and curatedPreset.layers[index]
if type(source) == "table" and tonumber(source.soundID) and ns.SoundByID[tonumber(source.soundID)] then
config.layers[index] = {
enabled = source.enabled ~= false,
soundID = tonumber(source.soundID),
delayMs = math.max(0, math.min(5000, math.floor(tonumber(source.delayMs) or 0))),
}
else
config.layers[index] = { enabled = false, soundID = false, delayMs = 0 }
end
end
preset.rules[rule.id] = config
else
-- Compatibility fallback for rules awaiting a dedicated curation.
local ruleRank = PRESET_RANK[rule.preset]
local enabled = false
if rule.defaultOn and ruleRank and ruleRank <= targetRank then
enabled = true
elseif presetKey == "expressive" and rule.preset ~= "custom" then
enabled = true
end
preset.rules[rule.id] = { enabled = enabled }
end
end
return preset
end
function ns:InitializeCharacterProfiles()
local previousCharacterVersion
if type(ResonanceCharDB) ~= "table" then
ResonanceCharDB = CopyDefaults(CHARACTER_DEFAULTS)
previousCharacterVersion = CHARACTER_DEFAULTS.version
else
previousCharacterVersion = tonumber(ResonanceCharDB.version) or 0
end
if type(ResonanceCharDB.specs) ~= "table" then ResonanceCharDB.specs = {} end
for specID in pairs(self.SUPPORTED_SPECS) do
local store = ResonanceCharDB.specs[specID]
local createdStore = type(store) ~= "table"
local legacyStore = ResonanceDB.specProfiles and ResonanceDB.specProfiles[specID]
local legacyProfile = legacyStore and legacyStore.profiles and legacyStore.profiles[legacyStore.active]
local hasLegacyProfile = type(legacyProfile) == "table" and type(legacyProfile.rules) == "table"
and next(legacyProfile.rules) ~= nil
if type(store) ~= "table" then
store = { working = nil, savedSets = {}, loadedName = nil, dirty = false }
ResonanceCharDB.specs[specID] = store
end
if type(store.savedSets) ~= "table" then store.savedSets = {} end
if createdStore and type(store.dirty) ~= "boolean" then store.dirty = false end
if type(store.working) ~= "table" then
store.working = hasLegacyProfile and DeepCopy(legacyProfile) or { rules = {} }
end
if type(store.working.rules) ~= "table" then store.working.rules = {} end
RemapSetSounds(store.working)
for name, set in pairs(store.savedSets) do
if type(name) ~= "string" or type(set) ~= "table" then
store.savedSets[name] = nil
elseif type(set.rules) ~= "table" then
set.rules = {}
end
RemapSetSounds(set)
NormalizeProfileSet(set, set.builtin == true)
end
local oldBase = store.savedSets["Resonance Base"]
if type(oldBase) == "table" and oldBase.builtin == true then
store.savedSets["Resonance Base"] = nil
if store.loadedName == "Resonance Base" then store.loadedName = nil end
end
local refreshLoadedBuiltin
for _, definition in ipairs(self.CURATED_PRESETS) do
local existing = store.savedSets[definition.name]
if existing == nil or (type(existing) == "table" and existing.builtin == true
and (tonumber(existing.builtinVersion) or 0) < self.BUILTIN_SET_VERSION) then
store.savedSets[definition.name] = BuildCuratedPreset(specID, definition.key)
if store.loadedName == definition.name then refreshLoadedBuiltin = definition.name end
end
end
if createdStore then
if hasLegacyProfile then
store.savedSets.Migrated = DeepCopy(store.working)
store.loadedName = "Migrated"
else
store.working = DeepCopy(store.savedSets["Resonance Medium"])
store.loadedName = "Resonance Medium"
end
elseif refreshLoadedBuiltin and store.dirty ~= true then
-- Only refresh an untouched built-in preset. A working set with
-- edits keeps its current layers even when the bundled preset is
-- upgraded by a newer addon release.
store.working = DeepCopy(store.savedSets[refreshLoadedBuiltin])
store.loadedName = refreshLoadedBuiltin
end
local loadedSet = store.loadedName and store.savedSets[store.loadedName]
NormalizeProfileSet(store.working, loadedSet and loadedSet.builtin == true)
RunCharacterStoreMigrations(store, specID, previousCharacterVersion)
end
ResonanceCharDB.version = math.max(previousCharacterVersion, CHARACTER_DEFAULTS.version)
end
function ns:GetSpecProfileStore(specID)
specID = specID or self.Runtime.specID
return specID and self.CharDB and self.CharDB.specs[specID]
end
function ns:GetActiveProfile(specID)
local store = self:GetSpecProfileStore(specID)
return store and store.working, store and store.loadedName
end
local function GetPersonalSetBaseName()
local character, realm
if UnitFullName then character, realm = UnitFullName("player") end
character = type(character) == "string" and character ~= "" and character
or (UnitName and UnitName("player"))
realm = type(realm) == "string" and realm ~= "" and realm
or (GetRealmName and GetRealmName())
if type(character) == "string" and character ~= "" and type(realm) == "string" and realm ~= "" then
return character .. " — " .. realm
end
return type(character) == "string" and character ~= "" and character or "My Resonance Set"
end
local function PrepareSavedSnapshot(owner, source, specID, automatic)
local snapshot = DeepCopy(source)
NormalizeProfileSet(snapshot, false)
FreezeProfileSet(snapshot, specID, true)
NormalizeProfileSet(snapshot, false)
snapshot.builtin = nil
snapshot.builtinVersion = nil
snapshot.baseVersion = nil
snapshot.preset = nil
snapshot.ruleFallback = "disabled"
snapshot.schemaVersion = owner.PROFILE_SCHEMA_VERSION
snapshot.ruleCatalogVersion = owner.RULE_CATALOG_VERSION
snapshot.soundCatalogVersion = owner.SOUND_CATALOG_VERSION
snapshot.savedAt = GetServerTime and GetServerTime() or 0
snapshot.automatic = automatic == true or nil
return snapshot
end
local function GetAutomaticSetName(store)
local preferred = GetPersonalSetBaseName()
local name = type(store.autoSaveName) == "string" and store.autoSaveName or preferred
local existing = store.savedSets and store.savedSets[name]
if existing and existing.automatic ~= true then
name = preferred .. " (Auto)"
local suffix = 2
while store.savedSets[name] and store.savedSets[name].automatic ~= true do
name = preferred .. " (Auto " .. suffix .. ")"
suffix = suffix + 1
end
end
store.autoSaveName = name
return name
end
local function CreatePersonalSet(owner, store, specID, replaceSnapshot)
local name = GetAutomaticSetName(store)
if replaceSnapshot == true or type(store.savedSets[name]) ~= "table" then
store.savedSets[name] = PrepareSavedSnapshot(owner, store.working, specID, true)
end
store.personalSetInitialized = true
return name
end
function ns:MarkSpecProfileDirty(specID)
local store = self:GetSpecProfileStore(specID)
if not store or type(store.working) ~= "table" then return end
-- Sound descriptors are derived from the working profile. Invalidate the
-- small runtime cache and cancel delayed playback immediately so a
-- preview or gameplay timer fired before the queued refresh cannot use a
-- stale layer or delay.
self:InvalidateRuntimeAudio(true)
local loaded = store.loadedName and store.savedSets[store.loadedName]
-- Presets are sources, never editable destinations. The first change to a
-- bundled preset immediately moves the edited working copy under the
-- character's personal set, without overwriting that saved personal set.
if store.personalSetInitialized ~= true or not store.loadedName or (loaded and loaded.builtin == true) then
local personalName = CreatePersonalSet(self, store, specID, store.personalSetInitialized ~= true)
store.loadedName = personalName
end
-- Later edits are deliberately not written into the active set until the
-- player chooses Save changes.
store.dirty = true
local window = self.SoundSetWindow
if window and window:IsShown() and window.specID == specID and type(window.Refresh) == "function" then
window:Refresh()
end
end
function ns:HasUnsavedProfileChanges(specID)
local store = self:GetSpecProfileStore(specID)
return store and store.dirty == true or false
end
function ns:GetRuleConfig(ruleID, create)
local rule = self.RuleByID[ruleID]
if not rule then return nil end
local profile = self:GetActiveProfile(rule.spec)
if not profile then return nil end
local config = profile.rules[ruleID]
if not config and create then
config = {}
profile.rules[ruleID] = config
end
return config
end
local function CountProfileCompatibility(owner, profile, specID)
local report = { missingSounds = 0, retiredRules = 0, newRules = 0 }
if not profile or type(profile.rules) ~= "table" then return report end
for ruleID, config in pairs(profile.rules) do
if not owner.RuleByID[ruleID] then report.retiredRules = report.retiredRules + 1 end
if type(config) == "table" and type(config.layers) == "table" then
for _, layer in pairs(config.layers) do
if type(layer) == "table" and tonumber(layer.soundID)
and not owner.SoundByID[tonumber(layer.soundID)] then
report.missingSounds = report.missingSounds + 1
end
end
end
end
for _, rule in ipairs(owner.RulesBySpec[specID] or {}) do
if profile.rules[rule.id] == nil then report.newRules = report.newRules + 1 end
end
return report
end
function ns:GetProfileCompatibility(specID)
return CountProfileCompatibility(self, self:GetActiveProfile(specID), specID)
end
function ns:GetSavedSetsCompatibility(specID)
local store = self:GetSpecProfileStore(specID)
local total = { missingSounds = 0, retiredRules = 0, newRules = 0, affectedSets = 0 }
for _, set in pairs((store and store.savedSets) or {}) do
local report = CountProfileCompatibility(self, set, specID)
if report.missingSounds > 0 or report.retiredRules > 0 or report.newRules > 0 then
total.affectedSets = total.affectedSets + 1
total.missingSounds = total.missingSounds + report.missingSounds
total.retiredRules = total.retiredRules + report.retiredRules
total.newRules = total.newRules + report.newRules
end
end
return total
end
ns.MAX_RULE_LAYERS = 8
function ns:GetRuleLayerCount(rule)
local config = self:GetRuleConfig(rule.id, false)
if config and tonumber(config.layerCount) then
return math.max(2, math.min(self.MAX_RULE_LAYERS, math.floor(tonumber(config.layerCount))))
end
local count = math.max(2, #(rule.defaultSounds or {}))
if config and type(config.layers) == "table" then
for index in pairs(config.layers) do
if type(index) == "number" and index > count then count = index end
end
end
return math.min(self.MAX_RULE_LAYERS, count)
end
function ns:GetLayerConfig(rule, index)
local config = self:GetRuleConfig(rule.id, false)
local layer = config and config.layers and config.layers[index]
local default = rule.defaultSounds and rule.defaultSounds[index]
local soundID = default
if layer ~= nil then soundID = layer.soundID end
return {
enabled = layer ~= nil and layer.enabled ~= false or (layer == nil and default ~= nil),
soundID = soundID,
soundLabel = layer and layer.soundLabel,
missingSound = layer and layer.missingSound == true,
delayMs = layer and tonumber(layer.delayMs)
or (rule.defaultDelays and tonumber(rule.defaultDelays[index]))
or (index == 1 and math.floor((rule.delay or 0) * 1000 + 0.5) or 0),
}
end
function ns:AddRuleLayer(rule)
local count = self:GetRuleLayerCount(rule)
if count >= self.MAX_RULE_LAYERS then return false end