-
Notifications
You must be signed in to change notification settings - Fork 4
/
validate.lua
715 lines (662 loc) · 25.6 KB
/
validate.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
local addon_name, addon = ...
local L = LibStub("AceLocale-3.0"):GetLocale(addon_name)
local function mark_for_remove(toremove, key)
if type(key) ~= "number" or key < 1 then
toremove.not_array = true
end
if not toremove.data then
toremove.data = {}
end
table.insert(toremove.data, key)
end
local function apply_remove(toremove, tbl, fix)
if fix and toremove.data then
if toremove.not_array then
for _, key in pairs(toremove.data) do
tbl[key] = nil
end
else
local offs = 0
for idx=1,#toremove.data do
table.remove(tbl, toremove.data[idx] - offs)
offs = offs + 1
end
end
end
addon.cleanArray(toremove)
end
local function validate_basic(prefix, data, template, fix)
local toremove = {}
for k,v in pairs(data) do
if template[k] == nil then
addon:warn("Extra Field %s:%s", prefix, tostring(k))
mark_for_remove(toremove, k)
elseif type(v) ~= type(template[k]) then
addon:warn("Incorrect type for %s:%s (was %s, expected %s)", prefix, tostring(k), type(v), type(template[k]))
mark_for_remove(toremove, k)
elseif type(v) == "table" then
if addon.tablelength(template[k]) > 0 and #template[k] == 0 then
validate_basic(prefix .. ":" .. k, v, template[k], fix)
end
end
end
apply_remove(toremove, data, fix)
end
local function validate_itemset_items(prefix, name, items, fix)
local toremove = {}
for idx, item in pairs(items) do
if type(item) ~= "string" and type(item) ~= "number" then
addon:warn("Invalid item in itemset %s:%s", prefix, name)
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, items, fix)
end
local function is_uuid(uuid)
return type(uuid) == "string" and uuid:match("(%x%x%x%x%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%-%x%x%x%x%x%x%x%x%x%x%x%x)") ~= nil
end
function addon:validate_itemset(prefix, name, itemset, fix)
if not is_uuid(name) then
addon:warn("Invalid ID for itemset %s:%s", prefix, name)
if fix then
addon.cleanArray(itemset)
return
end
end
local template = {
modified = false,
name = "test",
items = {}
}
validate_basic(prefix .. ":" .. name, itemset, template, fix)
validate_itemset_items(prefix, name, itemset.items, fix)
end
function addon:validate_effect(prefix, name, effect, fix)
if not is_uuid(name) then
addon:warn("Invalid ID for effect %s:%s", prefix, name)
if fix then
addon.cleanArray(effect)
return
end
end
if not effect["type"] then
addon:warn("No effect type for %s:%s", prefix, name)
if fix then
addon.cleanArray(effect)
end
return
end
local function get_effect_template(type)
if type == "texture" then
return {
texture = "some_texture"
}
elseif type == "pixel" then
return {
lines = 8,
frequency = 0.25,
length = 2,
thickness = 2
}
elseif type == "autocast" then
return {
particles = 4,
frequency = 0.25,
scale = 1.0
}
elseif type == "blizzard" then
return {
frequency = 0.125
}
elseif type == "dazzle" then
return {
texture = "some_texture",
frequency = 0.25,
sequence = {},
}
elseif type == "animate" then
return {
frequency = 0.25,
sequence = {},
}
elseif type == "pulse" then
return {
texture = "some_texture",
frequency = 0.25,
sequence = {},
}
elseif type == "custom" then
return {
frequency = 0.25,
sequence = {},
}
elseif type == "rotate" then
return {
texture = "some_texture",
frequency = 0.25,
steps = 4,
reverse = false
}
end
end
local template = get_effect_template(effect["type"])
template["type"] = "texture"
template["name"] = "some name"
validate_basic(prefix .. ":" .. name, effect, template, fix)
if effect["sequence"] then
local toremove = {}
for idx, seq in pairs(effect["sequence"]) do
if effect["type"] == "dazzle" then
if type(seq) == "table" then
local template = { r = 1.0, g = 1.0, b = 1.0, a = 1.0 }
validate_basic(prefix .. ":" .. name .. ":" .. idx, seq, template, fix)
else
addon:warn("Invalid type on %s effect sequence %s:%s:%d", effect["type"], prefix, name, idx)
mark_for_remove(toremove, idx)
end
elseif effect["type"] == "animate" then
if type(seq) ~= "string" then
addon:warn("Invalid type on %s effect sequence %s:%s:%d", effect["type"], prefix, name, idx)
mark_for_remove(toremove, idx)
end
elseif effect["type"] == "pulse" then
if type(seq) ~= "number" then
addon:warn("Invalid type on %s effect sequence %s:%s:%d", effect["type"], prefix, name, idx)
mark_for_remove(toremove, idx)
end
elseif effect["type"] == "custom" then
if type(seq) == "table" then
local template = {
texture = "some_texture",
angle = 0.0,
color = { r = 1.0, g = 1.0, b = 1.0, a = 1.0 },
magnification = 0.0,
}
validate_basic(prefix .. ":" .. name .. ":" .. idx, seq, template, fix)
else
addon:warn("Invalid type on %s effect sequence %s:%s:%d", effect["type"], prefix, name, idx)
mark_for_remove(toremove, idx)
end
end
end
apply_remove(toremove, effect["sequence"], fix)
end
end
function addon:validate_announce(prefix, idx, announce, fix)
if not announce["type"] then
addon:warn("No announce type for %s:%s", prefix, idx)
if fix then
addon.cleanArray(announce)
end
return
end
local template = {
type = "spell",
disabled = false,
announce = "party",
event = "SUCCEEDED",
id = "61b36ed1-fa31-4656-ad71-f3d50f193a85",
value = "Some Announce"
}
if announce["type"] == "spell" or announce["type"] == "petspell" then
template["spell"] = 12345
template["ranked"] = false
elseif announce["type"] ~= "item" then
addon:warn("Invalid type for announce %s:%s", prefix, tostring(announce["id"]))
if fix then
addon.cleanArray(announce)
return
end
end
validate_basic(prefix, announce, template, fix)
if not announce["id"] or not is_uuid(announce["id"]) then
addon:warn("Invalid ID for announce %s:%s", prefix, tostring(announce["id"]))
if fix then
addon.cleanArray(announce)
return
end
end
if announce["type"] == "item" and announce["item"] ~= nil then
if type(announce["item"]) == "table" then
validate_itemset_items(prefix, announce["id"], announce["item"], fix)
elseif type(announce["item"]) ~= "string" then
addon:warn("Invalid type for item set for announce %s:%s", prefix, tostring(announce["id"]))
if fix then
announce.item = nil
end
end
end
end
function addon:validate_condition_group(prefix, idx, group, fix)
local template = {
id = "61b36ed1-fa31-4656-ad71-f3d50f193a85",
name = "Some Group",
conditions = {}
}
validate_basic(prefix .. ":" .. idx, group, template, fix)
end
function addon:validate_custom_condition(prefix, name, custom_condition, fix)
validate_basic(prefix .. ":" .. name, custom_condition, addon.empty_condition, fix)
local function validate_custom_field_spec(parent, spec)
local toremove = {}
for k,v in pairs(spec) do
if type(v) == "string" then
if not (v == "string" or v == "number" or v == "boolean") then
addon:warn("Invalid type in custom funcion type spec %s:%s", parent, k)
mark_for_remove(toremove, k)
end
elseif type(v) == "table" then
validate_custom_field_spec(parent .. ":" .. k, v)
else
addon:warn("Invalid type in custom funcion type spec %s:%s", parent, k)
mark_for_remove(toremove, k)
end
end
apply_remove(toremove, spec, fix)
end
if custom_condition["fields"] then
validate_custom_field_spec(prefix .. ":" .. name, custom_condition["fields"])
end
end
local function validate_rotation_condition(prefix, condition, fix)
if addon.tablelength(condition) == 0 then
return
end
if type(condition["type"]) ~= "string" then
addon:warn("Invalid type for %s", prefix)
if fix then
addon.cleanArray(condition)
end
return
end
local full_id = prefix .. ":" .. condition["type"]
local fields = addon.deepcopy(select(4, addon:describeCondition(condition["type"])))
-- The condition might not exist if you're not playing a class where it is active.
if not fields then
return
end
fields["type"] = "string"
fields["disabled"] = "boolean"
local function handle_array_field(cond, field)
if type(cond) == "table" then
for i=1,#field do
if type(field[i]) == "table" then
local allfound = true
for j=1,#cond do
local subfound = handle_array_field(cond[j], field[i])
if not subfound then
allfound = false
break
end
end
if allfound then
return true
end
end
end
else
for i=1,#field do
if type(field[i]) ~= "table" and type(cond) == field[i] then
return true
end
end
end
return false
end
local function handle_fields(cond, f, subtree)
local keyremove = {}
for key, data in pairs(cond) do
local newsubtree = (subtree and (subtree .. ".") or "") .. key
local field = f[key]
if field == nil then
addon:warn("Extra Field %s.%s", full_id, newsubtree)
mark_for_remove(keyremove, key)
elseif type(field) == "table" then
-- The #field specifies this is an array, specifically.
if #field > 0 then
local found = handle_array_field(data, field)
if not found then
addon:warn("Condition %s.%s has data of the wrong type (got %s)", full_id, newsubtree, type(data))
if fix then
table.insert(toremove, key)
end
end
elseif type(data) == "table" then
handle_fields(data, field, newsubtree)
else
addon:warn("Condition %s.%s has data of the wrong type (got %s)", full_id, newsubtree, type(data))
mark_for_remove(keyremove, key)
end
elseif field == "condition" then
validate_rotation_condition(full_id, data, fix)
elseif type(field) == "string" and type(data) == "table" then
if addon.tablelength(data) ~= #data then
addon:warn("Condition %s.%s provides a table where an array is required", full_id, subtree)
mark_for_remove(keyremove, key)
elseif #data > 0 then
local basetype = field:match("^(.*)%[%]$")
if basetype == "condition" then
local toremove = {}
for i=1,#data do
if type(data[i]) == "table" then
validate_rotation_condition(full_id .. "[" .. i .. "]", data[i], fix)
else
addon:warn("Condition %s.%s[%d] has data of the wrong type (expected %s, got %s)", full_id, newsubtree, i, "table", type(data[i]))
mark_for_remove(toremove, i)
end
end
apply_remove(toremove, data, fix)
elseif basetype then
local toremove = {}
for i=1,#data do
if type(data[i]) ~= basetype then
addon:warn("Condition %s.%s[%d] has data of the wrong type (expected %s, got %s)", full_id, newsubtree, i, basetype, type(data[i]))
mark_for_remove(toremove, i)
end
end
apply_remove(toremove, data, fix)
else
addon:error("Unknown field type used in field spec for array on %s:%s", full_id, newsubtree)
end
end
elseif type(data) ~= field then
addon:warn("Condition %s.%s has data of the wrong type (expected %s, got %s)", full_id, newsubtree, field, type(data))
mark_for_remove(keyremove, key)
end
end
apply_remove(keyremove, cond, fix)
end
handle_fields(condition, fields)
end
function addon:validate_rotation(prefix, id, rotation, fix)
if id ~= DEFAULT and not is_uuid(id) then
addon:warn("Invalid ID for rotation %s:%s", prefix, id)
if fix then
addon.cleanArray(rotation)
return
end
end
local template = {
name = "Some Name",
disabled = false,
switch = {},
cooldowns = {},
rotation = {}
}
validate_basic(prefix .. ":" .. id, rotation, template, fix)
if id ~= DEFAULT and not rotation["name"] then
addon:warn("Unnamed non-default rotation %s:%s", prefix, id)
if fix then
addon.cleanArray(rotation)
return
end
end
local rotation_template = {
use_name = false,
name = "Some cool name",
id = "61b36ed1-fa31-4656-ad71-f3d50f193a85",
type = "spell",
disabled = false,
conditions = {},
}
local cooldown_template = {
use_name = false,
name = "Some cool name",
effect = "61b36ed1-fa31-4656-ad71-f3d50f193a85",
id = "61b36ed1-fa31-4656-ad71-f3d50f193a85",
type = "spell",
color = { r = 1.0, g = 1.0, b = 1.0, a = 1.0 },
magnification = 1.4,
setpoint = "CENTER",
xoffs = 0.0,
yoffs = 0.0,
announce = "None",
announce_sound = "LoudSound",
disabled = false,
conditions = {},
}
local function augment_template_type(template, rot)
if rot["type"] == "spell" then
template["action"] = 1234
template["ranked"] = false
elseif rot["type"] == "petspell" or template["type"] == "anyspell" then
template["action"] = 1234
elseif rot["type"] == "item" then
if type(rot["action"]) == "table" then
template["action"] = {}
else
template["action"] = "61b36ed1-fa31-4656-ad71-f3d50f193a85"
end
end
end
if rotation["cooldowns"] then
local toremove = {}
for idx, rot in pairs(rotation["cooldowns"]) do
local template = addon.deepcopy(cooldown_template)
augment_template_type(template, rot)
validate_basic(prefix .. ":" .. id .. ":cooldown:" .. idx, rot, template, fix)
if not is_uuid(rot["id"]) then
addon:warn("Invalid ID for cooldown %s:%d:%d", prefix, id, idx)
mark_for_remove(toremove, idx)
end
if rot["effect"] and not is_uuid(rot["effect"]) then
addon:warn("Invalid effect ID for cooldown %s:%d:%d", prefix, id, idx)
if fix then
rot["effect"] = nil
end
end
if rot["type"] == "item" then
if type(rot["action"]) == "table" then
validate_itemset_items(prefix .. ":" .. id .. ":cooldown", idx, rot["action"], fix)
elseif type(rot["action"]) ~= "string" then
addon:warn("Invalid type for item action in cooldown %s:%d:%d", prefix, id, idx)
if fix then
rot["action"] = nil
end
elseif not is_uuid(rot["action"]) then
addon:warn("Invalid itemset ID for cooldown %s:%d:%d", prefix, id, idx)
if fix then
rot["action"] = nil
end
end
end
if rot["conditions"] then
validate_rotation_condition( string.format("%s:%s:cooldowns:%d", prefix, id, idx),
rot["conditions"], fix)
end
if addon.tablelength(rot) == 0 then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, rotation["cooldowns"], fix)
end
if rotation["rotation"] then
local toremove = {}
for idx, rot in pairs(rotation["rotation"]) do
local template = addon.deepcopy(rotation_template)
augment_template_type(template, rot)
validate_basic(prefix .. ":" .. id .. ":rotation:" .. idx, rot, template, fix)
if not is_uuid(rot["id"]) then
addon:warn("Invalid ID for rotation step %s:%d:%d", prefix, id, idx)
mark_for_remove(toremove, idx)
end
if rot["type"] == "item" then
if type(rot["action"]) == "table" then
validate_itemset_items(prefix .. ":" .. id .. ":rotation", idx, rot["action"], fix)
elseif type(rot["action"]) ~= "string" then
addon:warn("Invalid type for item action in rotation %s:%d:%d", prefix, id, idx)
if fix then
rot["action"] = nil
end
elseif not is_uuid(rot["action"]) then
addon:warn("Invalid itemset ID for rotation %s:%d:%d", prefix, id, idx)
if fix then
rot["action"] = nil
end
end
end
if rot["conditions"] then
validate_rotation_condition( string.format("%s:%s:rotation:%d", prefix, id, idx),
rot["conditions"], fix)
end
if addon.tablelength(rot) == 0 then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, rotation["rotation"], fix)
end
if rotation["switch"] then
validate_rotation_condition( string.format("%s:%s:switch", prefix, id),
rotation["switch"], fix)
end
end
function addon:validate(template, fix)
local DB = _G[addon_name .. "DB"]
local glob_template = addon.deepcopy(template.global)
glob_template.itemsets = {}
glob_template.effects = {}
validate_basic("global", DB.global, glob_template, fix)
if DB.global.itemsets then
local toremove = {}
for name, itemset in pairs(DB.global.itemsets) do
addon:validate_itemset("global", name, itemset, fix)
if addon.tablelength(itemset) == 0 then
mark_for_remove(toremove, name)
end
end
apply_remove(toremove, DB.global.itemsets, fix)
end
if DB.global.effects then
local toremove = {}
for name, effect in pairs(DB.global.effects) do
addon:validate_effect("global", name, effect, fix)
if addon.tablelength(effect) == 0 then
mark_for_remove(toremove, name)
end
end
apply_remove(toremove, DB.global.effects, fix)
end
if DB.global.custom_conditions then
local toremove = {}
for name, custom_condition in pairs(DB.global.custom_conditions) do
addon:validate_custom_condition("global", name, custom_condition, fix)
if addon.tablelength(custom_condition) == 0 then
mark_for_remove(toremove, name)
end
end
apply_remove(toremove, DB.global.custom_conditions, fix)
end
for char, data in pairs(DB.char) do
validate_basic("char:" .. char, data, template.char, fix)
if data.bindings then
local toremove = {}
for id,buttons in pairs(data.bindings) do
if not is_uuid(id) then
addon:warn("Binding for invalid itemset %s", id)
if fix then
mark_for_remove(toremove, id)
end
end
local i = 1
while i <= #buttons do
if type(buttons[i]) ~= "number" then
addon:warn("Invalid button ID in button bindings %s:%s", char, id)
if fix then
table.remove(buttons, i)
else
i = i + 1
end
else
i = i + 1
end
end
if #buttons == 0 then
mark_for_remove(toremove, id)
end
end
apply_remove(toremove, data.bindings, fix)
end
end
for profile, data in pairs(DB.profiles) do
validate_basic("profile:" .. profile, data, template.profile, fix)
if data.itemsets then
local toremove = {}
for name, itemset in pairs(data.itemsets) do
addon:validate_itemset("profile:" .. profile, name, itemset, fix)
if addon.tablelength(itemset) == 0 then
mark_for_remove(toremove, name)
end
end
apply_remove(toremove, data.itemsets, fix)
end
if data.announces then
local toremove = {}
for idx, announce in pairs(data.announces) do
addon:validate_announce("profile:" .. profile, idx, announce, fix)
if addon.tablelength(announce) == 0 then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, data.announces, fix)
end
if data.rotations then
local specremove = {}
for spec, rotations in pairs(data.rotations) do
if type(spec) ~= "number" then
table.insert(specremove, spec)
end
local toremove = {}
for id, rotation in pairs(rotations) do
addon:validate_rotation("profile:" .. profile .. ":" .. spec, id, rotation, fix)
if addon.tablelength(rotation) == 0 then
mark_for_remove(toremove, id)
end
end
apply_remove(toremove, rotations, fix)
end
if fix then
for _, key in pairs(specremove) do
data.rotations[key] = nil
end
end
end
if data.condition_groups then
local toremove = {}
for idx,group in pairs(data.condition_groups) do
if type(idx) ~= "number" then
mark_for_remove(toremove, idx)
end
addon:validate_condition_group("profile:" .. profile, idx, group, fix)
if addon.tablelength(group) == 0 then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, data.condition_groups, fix)
end
local toremove = {}
if data.switch_conditions then
for idx,cond in pairs(data.switch_conditions) do
if type(idx) ~= "number" or type(cond) ~= "string" then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, data.switch_conditions, fix)
end
if data.other_conditions_order then
for idx,cond in pairs(data.other_conditions_order) do
if type(idx) ~= "number" or type(cond) ~= "string" then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, data.other_conditions_order, fix)
end
if data.disabled_conditions then
for idx,cond in pairs(data.disabled_conditions) do
if type(idx) ~= "number" or type(cond) ~= "string" then
mark_for_remove(toremove, idx)
end
end
apply_remove(toremove, data.disabled_conditions, fix)
end
end
end