forked from ATTWoWAddon/AllTheThings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCache.lua
1288 lines (1243 loc) · 41.8 KB
/
Cache.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
do
local _, app = ...;
-- Global locals
local ipairs, tinsert, pairs, rawset, type, wipe, setmetatable, rawget, math_floor
= ipairs, tinsert, pairs, rawset, type, wipe, setmetatable, rawget, math.floor
local C_Map_GetAreaInfo, C_Map_GetMapInfo = C_Map.GetAreaInfo, C_Map.GetMapInfo;
-- App locals
local contains, classIndex, raceIndex, factionID =
app.contains, app.ClassIndex, app.RaceIndex, app.FactionID;
-- Module locals
local AllCaches, AllGamePatches, postscripts, runners, QuestTriggers = {}, {}, {}, {}, {};
local containerMeta = {
__index = function(t, id)
if id then
local container = {};
t[id] = container;
return container;
end
end,
};
local fieldMeta = {
__index = function(t, field)
if field then
local container = setmetatable({}, containerMeta);
t[field] = container;
return container;
end
end,
__newindex = function(t, field, value)
if field then
local container = setmetatable(value, containerMeta);
rawset(t, field, container);
return container;
end
end,
};
local currentCache, CacheFields;
-- Cache a given group into the current cache for the provided field and value
local function CacheField(group, field, value)
-- comment this in for testing if some caching issue happens
-- if not (field and value) then print("Attempting to cache invalid data", field, value, group.text); end
local c = currentCache[field][value]
c[#c + 1] = group
end
-- Returns: An object which can be used for holding cached data by various keys allowing for quick updates of data states.
local CreateDataCache = function(name, skipMapCaching)
local cache = { name = name };
AllCaches[name] = cache;
cache.CacheField = function(group, field, value)
local c = cache[field][value]
c[#c + 1] = group
end
cache.CacheFields = function(groups)
local oldCache = currentCache;
currentCache = cache;
CacheFields(groups, skipMapCaching);
currentCache = oldCache;
end
setmetatable(cache, fieldMeta);
cache.npcID = cache.creatureID; -- identical cache as creatureID (probably deprecate npcID use eventually)
--cache.requireSkill = cache.professionID; -- identical cache as professionID (in Retail)
return cache;
end
currentCache = CreateDataCache("default");
local currentMapGroup, allowMapCaching = setmetatable({}, { __index = function() return end }), true
local cacheAchievementID = function(group, value)
CacheField(group, "achievementID", value);
end
local cacheCreatureID = function(group, creatureID)
if creatureID > 0 then
CacheField(group, "creatureID", creatureID);
end
end
local cacheFactionID = function(group, id)
CacheField(group, "factionID", id);
end
local cacheHeaderID = function(group, headerID)
CacheField(group, "headerID", headerID);
end
local function allowCacheMapID(group, mapID)
-- already are within a caching group for this mapID or not allowing map caching, don't cache
if currentMapGroup[mapID] or not allowMapCaching then return end
-- this group should not be map-cached into its coord map
-- if the group has no sub-groups, then we allow caching for this new mapID, but don't capture it as a mapgroup
if not rawget(group, "g") then return true end
-- otherwise capture this group as the containing cache group of this mapID
-- app.PrintDebug(">>>map:",mapID,group.key,group[group.key])
currentMapGroup[mapID] = group
return true
end
local cacheMapID = function(group, mapID)
if not allowCacheMapID(group, mapID) then return end
CacheField(group, "mapID", mapID);
return true
end
local cacheObjectID = function(group, objectID)
CacheField(group, "objectID", objectID);
end;
local cacheQuestID = function(group, questID)
CacheField(group, "questID", questID);
end
if app.Debugging and app.Version == "[Git]" then
local L = app.L;
local referenceCounter = {};
app.ReferenceCounter = referenceCounter;
app.CheckReferenceCounters = function()
local CUSTOM_HEADERS = {};
for id,count in pairs(referenceCounter) do
if type(id) == "number" and tonumber(id) < 1 and tonumber(id) > -100000 then
tinsert(CUSTOM_HEADERS, { id, count });
end
end
for id,_ in pairs(L.HEADER_NAMES) do
if not referenceCounter[id] then
referenceCounter[id] = 1;
tinsert(CUSTOM_HEADERS, { id, 0 });
end
end
for id,_ in pairs(L.HEADER_DESCRIPTIONS) do
if not referenceCounter[id] then
tinsert(CUSTOM_HEADERS, { id, 0, " and only exists as a description..." });
end
end
for id,_ in pairs(L.HEADER_ICONS) do
if not referenceCounter[id] then
tinsert(CUSTOM_HEADERS, { id, 0, " and only exists as an icon..." });
end
end
app.Sort(CUSTOM_HEADERS, function(a, b)
return (a[1] or 0) < (b[1] or 0);
end);
for _,data in ipairs(CUSTOM_HEADERS) do
local id = data[1];
local header = {};
if L.HEADER_NAMES[id] then header.name = L.HEADER_NAMES[id]; end
if L.HEADER_ICONS[id] then header.icon = L.HEADER_ICONS[id]; end
if L.HEADER_DESCRIPTIONS[id] then header.description = L.HEADER_DESCRIPTIONS[id]; end
print("Header " .. id .. " has " .. data[2] .. " references" .. (data[3] or "."), header.name);
tinsert(data, header);
end
app.SetDataMember("CUSTOM_HEADERS", CUSTOM_HEADERS);
end
cacheCreatureID = function(group, creatureID)
if creatureID > 0 then
CacheField(group, "creatureID", creatureID);
else
referenceCounter[creatureID] = (referenceCounter[creatureID] or 0) + 1;
end
end
cacheHeaderID = function(group, headerID)
if not group.type and not L.HEADER_NAMES[headerID] then
print("Header Missing Name ", headerID);
L.HEADER_NAMES[headerID] = "Header #" .. headerID;
end
referenceCounter[headerID] = (referenceCounter[headerID] or 0) + 1;
CacheField(group, "headerID", headerID);
end
cacheObjectID = function(group, objectID)
if not app.ObjectNames[objectID] then
print("Object Missing Name ", objectID);
app.ObjectNames[objectID] = "Object #" .. objectID;
end
CacheField(group, "objectID", objectID);
end
end
-- Cost & Providers Helper
local providerTypeConverters = {
["n"] = cacheCreatureID,
["o"] = cacheObjectID,
["c"] = function(group, providerID)
CacheField(group, "currencyIDAsCost", providerID);
CacheField(group, "currencyID", providerID);
end,
["i"] = function(group, providerID)
CacheField(group, "itemIDAsCost", providerID);
CacheField(group, "itemID", providerID);
end,
["g"] = function(group, providerID)
-- Do nothing, nothing to cache.
end
};
local cacheProviderOrCost = function(group, provider)
providerTypeConverters[provider[1]](group, provider[2]);
end
local uncacheMap = function(group, mapID)
-- remove this group's mapID if it is the current map group for this mapID
if currentMapGroup[mapID] == group then
-- app.PrintDebug("<<<map:",mapID,group.key,group[group.key])
currentMapGroup[mapID] = nil
end
end;
local mapKeyUncachers = {
["mapID"] = uncacheMap,
["maps"] = function(group, maps)
for _,mapID in ipairs(maps) do
uncacheMap(group, mapID);
end
end,
["coord"] = function(group, coord)
uncacheMap(group, coord[3]);
end,
["coords"] = function(group, coords)
for i,coord in ipairs(coords) do
uncacheMap(group, coord[3]);
end
end,
};
-- Map Remapping
-- This feature takes the original mapID and allows modifications on it to
-- change the displayed name and the content of the mini list.
local MapRemapping = {};
app.MapRemapping = MapRemapping;
local nextCustomMapID = -2;
local function assignZoneAreaIDs(originalMapID, mapID, ids)
if originalMapID then
local remap = MapRemapping[originalMapID];
if not remap then
remap = {};
MapRemapping[originalMapID] = remap;
end
local areaIDs = remap.areaIDs;
if not areaIDs then
areaIDs = {};
remap.areaIDs = areaIDs;
end
for j,areaID in ipairs(ids) do
areaIDs[areaID] = mapID;
end
end
end
local function zoneArtIDRunner(group, value)
-- If this group uses a normal map, we want to rip out the cache for it.
-- Doing it after the cache is finished allows us to still prevent the coordinates
-- on relative objects and npcs from getting cached to the parent mapID.
local originalMaps = group.maps;
local originalMapID = group.mapID or (originalMaps and originalMaps[1]);
if originalMapID then
-- Generate a new unique mapID (negative)
local mapID = nextCustomMapID;
nextCustomMapID = nextCustomMapID - 1;
if originalMaps then
if group.mapID then
tinsert(originalMaps, mapID);
else
group.mapID = nil;
end
else
group.maps = {mapID};
end
-- Manually assign the name of this map since it is not a real mapID.
CacheField(group, "mapID", mapID);
app.L.MAP_ID_TO_ZONE_TEXT[mapID] = group.text;
-- Remap the original mapID to the new mapID when it encounters any of these artIDs.
local remap = MapRemapping[originalMapID];
if not remap then
remap = {};
MapRemapping[originalMapID] = remap;
end
local artIDs = remap.artIDs;
if not artIDs then
artIDs = {};
remap.artIDs = artIDs;
end
for j,artID in ipairs(value) do
artIDs[artID] = mapID;
end
-- Uncache the original mapID
local mapIDCache = currentCache.mapID;
mapIDCache = mapIDCache[originalMapID];
for i,o in ipairs(mapIDCache) do
if o == group then
table.remove(mapIDCache, i);
break;
end
end
--local info = C_Map_GetMapInfo(originalMapID);
--print("MapRemapping (artIDs): ", originalMapID, info and info.name, mapID, group.text);
--else
--print("Invalid MapRemapping (artIDs):", group.hash);
end
end
local function zoneTextAreasRunner(group, value)
local mapID = group.mapID;
if not mapID then
-- Generate a new unique mapID (negative)
mapID = nextCustomMapID;
nextCustomMapID = nextCustomMapID - 1;
if group.maps then
tinsert(group.maps, mapID)
else
group.maps = {mapID};
end
-- Manually assign the name of this map since it is not a real mapID.
CacheField(group, "mapID", mapID);
end
-- Use the localizer to force the minilist to display this as if it was a map file.
local name = C_Map_GetAreaInfo(value[1]);
if name then app.L.MAP_ID_TO_ZONE_TEXT[mapID] = name; end
-- Remap the original mapID to the new mapID when it encounters any of these artIDs.
local mapIDs, parentMapID, info = {};
if group.coords then
parentMapID = group.coords[1][3];
if parentMapID then
mapIDs[parentMapID] = 1;
info = C_Map_GetMapInfo(parentMapID);
if info and info.parentMapID then
mapIDs[info.parentMapID] = 1;
end
end
else
parentMapID = app.GetRelativeValue(group.parent, "mapID");
if parentMapID then
mapIDs[parentMapID] = 1;
info = C_Map_GetMapInfo(parentMapID);
if info and info.parentMapID then
mapIDs[info.parentMapID] = 1;
end
end
end
if group.maps then
for i,parentMapID in ipairs(group.maps) do
mapIDs[parentMapID] = 1;
info = C_Map_GetMapInfo(parentMapID);
if info and info.parentMapID then
mapIDs[info.parentMapID] = 1;
end
end
end
for parentMapID,_ in pairs(mapIDs) do
assignZoneAreaIDs(parentMapID, mapID, value);
end
end
local function zoneTextContinentRunner(group, value)
local mapID = group.mapID;
if mapID then
local remap = MapRemapping[mapID];
if not remap then
remap = {};
MapRemapping[mapID] = remap;
end
remap.isContinent = true;
--local info = C_Map_GetMapInfo(mapID);
--print("MapRemapping (continent): ", mapID, info and info.name);
end
end
local function zoneTextNamesRunner(group, value)
--if true then return; end
-- Remap the original mapID to the new mapID when it encounters any of these artIDs.
local originalMapID = (group.coords and group.coords[1][3]) or app.GetRelativeValue(group.parent, "mapID");
if originalMapID then
local mapID = group.mapID;
if not mapID then
-- Generate a new unique mapID (negative)
mapID = nextCustomMapID;
nextCustomMapID = nextCustomMapID - 1;
if group.maps then
tinsert(group.maps, mapID)
else
group.maps = {mapID};
end
-- Manually assign the name of this map since it is not a real mapID.
CacheField(group, "mapID", mapID);
end
local remap = MapRemapping[originalMapID];
if not remap then
remap = {};
MapRemapping[originalMapID] = remap;
end
local names = remap.names;
if not names then
names = {};
remap.names = names;
end
for j,name in ipairs(value) do
names[name] = mapID;
end
--local info = C_Map_GetMapInfo(originalMapID);
--print("MapRemapping (name): ", originalMapID, info and info.name, mapID);
--else
--print("Invalid MapRemapping (name):", group.hash);
end
end
local fieldConverters = {
-- Simple Converters
["achievementID"] = cacheAchievementID,
["achievementCategoryID"] = function(group, value)
CacheField(group, "achievementCategoryID", value);
end,
["achID"] = cacheAchievementID,
["altAchID"] = cacheAchievementID,
["artifactID"] = function(group, value)
CacheField(group, "artifactID", value);
end,
["azeriteEssenceID"] = function(group, value)
CacheField(group, "azeriteEssenceID", value);
end,
["creatureID"] = cacheCreatureID,
["currencyID"] = function(group, value)
CacheField(group, "currencyID", value);
end,
["encounterID"] = function(group, value)
CacheField(group, "encounterID", value);
end,
["expansionID"] = function(group, value)
CacheField(group, "expansionID", value);
end,
["explorationID"] = function(group, value)
CacheField(group, "explorationID", value);
end,
["factionID"] = cacheFactionID,
["flightPathID"] = function(group, value)
CacheField(group, "flightPathID", value);
end,
["followerID"] = function(group, value)
CacheField(group, "followerID", value);
end,
["garrisonBuildingID"] = function(group, value)
CacheField(group, "garrisonBuildingID", value);
end,
["headerID"] = cacheHeaderID,
["heirloomUnlockID"] = function(group, value)
CacheField(group, "heirloomUnlockID", value);
end,
["illusionID"] = function(group, value)
CacheField(group, "illusionID", value);
end,
["instanceID"] = function(group, value)
CacheField(group, "instanceID", value);
end,
["itemID"] = function(group, value)
CacheField(group, "itemID", value);
end,
["otherItemID"] = function(group, value)
CacheField(group, "itemID", value);
end,
["drakewatcherManuscriptID"] = function(group, value)
CacheField(group, "itemID", value);
end,
["heirloomID"] = function(group, value)
CacheField(group, "itemID", value);
end,
["mapID"] = cacheMapID,
["mountID"] = function(group, value)
CacheField(group, "spellID", value);
end,
["npcID"] = cacheCreatureID,
["objectID"] = cacheObjectID,
["professionID"] = function(group, value)
CacheField(group, "professionID", value);
end,
["questID"] = cacheQuestID,
["questIDA"] = cacheQuestID,
["questIDH"] = cacheQuestID,
["requireSkill"] = function(group, value)
CacheField(group, "requireSkill", value); -- NOTE: professionID in Retail, investigate why
end,
["runeforgePowerID"] = function(group, value)
CacheField(group, "runeforgePowerID", value);
end,
["rwp"] = function(group, value)
CacheField(group, "rwp", value);
end,
["sourceID"] = function(group, value)
CacheField(group, "sourceID", value);
end,
["speciesID"] = function(group, value)
CacheField(group, "speciesID", value);
end,
["spellID"] = function(group, value)
CacheField(group, "spellID", value);
end,
["titleID"] = function(group, value)
CacheField(group, "titleID", value);
end,
["toyID"] = function(group, value)
CacheField(group, "toyID", value);
CacheField(group, "itemID", value);
end,
-- Complex Converters
["altQuests"] = function(group, value)
for i=1,#value,1 do
CacheField(group, "questID", value[i]);
end
end,
["crs"] = function(group, value)
for i=1,#value,1 do
cacheCreatureID(group, value[i]);
end
end,
["qgs"] = function(group, value)
for i=1,#value,1 do
cacheCreatureID(group, value[i]);
end
end,
["maps"] = function(group, value)
for i=1,#value,1 do
cacheMapID(group, value[i]);
end
return true;
end,
["coord"] = function(group, coord)
-- Retail used this commented out section instead, see which one is better
-- don't cache mapID from coord for anything which is itself an actual instance or a map
-- if currentInstance ~= group and not rawget(group, "mapID") and not rawget(group, "difficultyID") then
if not (group.instanceID or group.mapID or group.objectiveID or group.difficultyID or (group.headerID and group.parent and group.parent.instanceID)) then
return cacheMapID(group, coord[3]);
end
end,
["coords"] = function(group, coords)
-- Retail used this commented out section instead, see which one is better
-- don't cache mapID from coord for anything which is itself an actual instance or a map
-- if currentInstance ~= group and not rawget(group, "mapID") and not rawget(group, "difficultyID") then
if not (group.instanceID or group.mapID or group.objectiveID or group.difficultyID or (group.headerID and group.parent and group.parent.instanceID)) then
for i,coord in ipairs(coords) do
cacheMapID(group, coord[3]);
end
return true;
end
end,
["cost"] = function(group, value)
if type(value) == "table" then
for i=1,#value,1 do
cacheProviderOrCost(group, value[i]);
end
end
end,
["provider"] = cacheProviderOrCost,
["providers"] = function(group, value)
for i=1,#value,1 do
cacheProviderOrCost(group, value[i]);
end
end,
["maxReputation"] = function(group, value)
cacheFactionID(group, value[1]);
end,
["minReputation"] = function(group, value)
cacheFactionID(group, value[1]);
end,
["c"] = function(group, value)
if not contains(value, classIndex) then
group.nmc = true; -- "Not My Class"
end
end,
["r"] = function(group, value)
if value ~= factionID then
group.nmr = true; -- "Not My Race"
end
end,
["races"] = function(group, value)
if not contains(value, raceIndex) then
group.nmr = true; -- "Not My Race"
end
end,
["nextQuests"] = function(group, value)
for _,questID in ipairs(value) do
CacheField(group, "nextQuests", questID);
end
end,
["sourceQuests"] = function(group, value)
for i=1,#value,1 do
CacheField(group, "sourceQuestID", value[i]);
end
end,
-- Localization Helpers
["zone-artIDs"] = function(group, value)
tinsert(runners, function()
zoneArtIDRunner(group, value);
end);
end,
["zone-text-areaID"] = function(group, value)
tinsert(runners, function()
zoneTextAreasRunner(group, { value });
end);
end,
["zone-text-areas"] = function(group, value)
tinsert(runners, function()
zoneTextAreasRunner(group, value);
end);
end,
["zone-text-continent"] = function(group, value)
tinsert(runners, function()
zoneTextContinentRunner(group, value);
end);
end,
["zone-text-names"] = function(group, value)
tinsert(runners, function()
zoneTextNamesRunner(group, value);
end);
end,
-- Patch Helpers
["awp"] = function(group, value)
if value then AllGamePatches[value] = true; end
end,
};
local _converter;
local function _CacheFields(group)
local n = 0;
local clone, mapKeys, key, value, hasG = {};
for key,value in pairs(group) do
if key == "g" then
hasG = true;
else
n = n + 1
clone[n] = key;
end
end
for i=1,n,1 do
key = clone[i];
_converter = fieldConverters[key];
if _converter then
value = group[key];
if _converter(group, value) then
if not mapKeys then mapKeys = {}; end
mapKeys[key] = value;
end
end
end
if hasG then
for _,subgroup in ipairs(group.g) do
_CacheFields(subgroup);
end
end
if mapKeys then
for key,value in pairs(mapKeys) do
mapKeyUncachers[key](group, value);
end
end
end
---- Retail Differences ----
if app.IsRetail then
-- 'altQuests' in Retail pretending to be the same quest as a different quest actually causes problems for searches
-- and it makes more sense to not pretend they're the same than to hamper existing logic with more conditionals to
-- make sure we actually get the data that we search for
fieldConverters.altQuests = nil;
-- 'awp' isn't needed for caching into 'AllGamePatches' currently... I don't really see a future where we 'pre-add' future Retail content in public releases
fieldConverters.awp = nil;
-- Base Class provides auto-fields for these and they do no actual caching
fieldConverters.c = nil
fieldConverters.r = nil
fieldConverters.races = nil
-- Retail doesn't need to double cache the object attached to currencies/items because it uses the cost
-- caches for the same information
providerTypeConverters.c = function(group, providerID)
CacheField(group, "currencyIDAsCost", providerID);
end
providerTypeConverters.i = function(group, providerID)
CacheField(group, "itemIDAsCost", providerID);
end
-- use single iteration of each group by way of not performing any group field additions while the cache process is running
_CacheFields = function(group)
local mapKeys
local hasG = rawget(group, "g")
for key,value in pairs(group) do
_converter = fieldConverters[key];
if _converter then
if _converter(group, value) then
if mapKeys then mapKeys[key] = value
else mapKeys = { [key] = value }; end
end
end
end
if hasG then
for _,subgroup in ipairs(hasG) do
_CacheFields(subgroup);
end
end
if mapKeys then
for key,value in pairs(mapKeys) do
mapKeyUncachers[key](group, value);
end
end
end
-- Retail has this required modItemID field that complicates everything, so put that here instead.
local cacheGroupForModItemID = {}
fieldConverters.itemID = function(group, value)
CacheField(group, "itemID", value);
cacheGroupForModItemID[#cacheGroupForModItemID + 1] = group
end
fieldConverters.drakewatcherManuscriptID = fieldConverters.itemID;
fieldConverters.heirloomID = fieldConverters.itemID;
tinsert(postscripts, function()
local modItemID
-- app.PrintDebug("caching for modItemID",#cacheGroupForModItemID)
for _,group in ipairs(cacheGroupForModItemID) do
modItemID = group.modItemID
if modItemID then
CacheField(group, "modItemID", modItemID)
if modItemID ~= group.itemID then
CacheField(group, "itemID", modItemID)
end
end
end
wipe(cacheGroupForModItemID)
-- app.PrintDebug("caching for modItemID done")
end)
-- Retail doesn't have objectives so don't bother checking for it
fieldConverters.coord = function(group, coord)
-- don't cache mapID from coord for anything which is itself an actual instance or a map
if rawget(group, "instanceID") or rawget(group, "mapID") or rawget(group, "difficultyID") or (group.headerID and group.parent and group.parent.instanceID) then return end
return cacheMapID(group, coord[3]);
end
fieldConverters.coords = function(group, coords)
-- don't cache mapID from coord for anything which is itself an actual instance or a map
if rawget(group, "instanceID") or rawget(group, "mapID") or rawget(group, "difficultyID") or (group.headerID and group.parent and group.parent.instanceID) then return end
local any
for i,coord in ipairs(coords) do
any = cacheMapID(group, coord[3]) or any
end
return any;
end
fieldConverters.conduitID = function(group, id)
CacheField(group, "conduitID", id);
end
fieldConverters.up = function(group, up)
CacheField(group, "up", up);
end
end
CacheFields = function(group, skipMapCaching)
allowMapCaching = not skipMapCaching
_CacheFields(group);
for i,runner in ipairs(runners) do
runner();
end
for i,postscript in ipairs(postscripts) do
postscript();
end
wipe(runners);
return group;
end
-- This data type requires additional processing.
fieldConverters.otherQuestData = function(group, value)
_CacheFields(value);
end
-- Performance Tracking for Caching
if app.__perf then
app.__perf.CaptureTable(fieldConverters, "CacheFields");
end
-- Cache-Based Searching
local function GetRawField(field, id)
-- Returns: A table containing all groups which contain the provided id for a given field.
-- NOTE: Can be nil for simplicity in use
local container = rawget(currentCache, field)
if not container then return end
return rawget(container, id);
end
local function GetRawFieldContainer(field)
-- Returns: The actual table containing all groups which contain a given field
-- NOTE: Can be nil for simplicity in use
return rawget(currentCache, field);
end
local function SearchForField(field, id)
-- Returns: A table containing all groups which contain the provided id for a given field.
return currentCache[field][id];
end
local function SearchForFieldContainer(field)
-- Returns: A table containing all groups which contain a given field.
return currentCache[field];
end
-- Recursive Searching
local function SearchForFieldRecursively(group, field, value)
-- Returns: A table containing all subgroups which contain a given value of field relative to the group or nil.
if group.g then
-- Go through the sub groups and determine if any of them have a response.
local first = nil;
for i, subgroup in ipairs(group.g) do
local g = SearchForFieldRecursively(subgroup, field, value);
if g then
if first then
-- Merge!
for j,data in ipairs(g) do
tinsert(first, data);
end
else
-- Cool! (This should be the most common occurance)
first = g;
end
end
end
if group[field] == value then
-- OH BOY, WE FOUND IT!
if first then
return tinsert(first, group);
else
return { group };
end
end
return first;
elseif group[field] == value then
-- OH BOY, WE FOUND IT!
return { group };
end
end
local function SearchForRelativeItems(group, listing)
-- Search a group for all items relative to the given group. (excluding the group passed in)
if group and group.g then
for i,subgroup in ipairs(group.g) do
SearchForRelativeItems(subgroup, listing);
if subgroup.itemID then
tinsert(listing, subgroup);
end
end
end
end
-- Search for a thing that matches some requirements
local function SearchForObject(field, id, require, allowMultiple)
-- This method performs the SearchForField logic, but then may verifies that ONLY a specific matching, filtered-priority object is returned
-- require - Determine the required level of matching found objects:
-- * "key" - only accept objects whose key is also the field with value
-- * "field" - only accept objects which contain the exact field with value
-- * none - accept any object which is cached against the specific field value
-- allowMultiple - Whether to return multiple matching objects as an array (within the 'require' restriction)
local fcache, count
-- Items are cached by base ItemID and ModItemID, so when searching by ItemID, use ModItemID for
-- match requirement accuracy
if field == "itemID" then
-- try searching by modItemID cache, any results are the EXACT id searched for
fcache = GetRawField("modItemID", id)
if fcache and #fcache > 0 then
-- use modItemID as the field for 'require' since it returned results
field = "modItemID"
else
local idBase = math_floor(id)
-- if we're NOT searching for a plain itemID and found no results, we can revert to the plain itemID
if idBase ~= id and (not fcache or #fcache == 0) then
id = idBase
fcache = nil
end
end
end
fcache = fcache or GetRawField(field, id)
count = fcache and #fcache;
if not count or count == 0 then
-- app.PrintDebug("SFO",field,id,require,"0~")
return allowMultiple and app.EmptyTable or nil
end
local fcacheObj;
require = (require == "key" and 2) or (require == "field" and 1) or 0;
-- quick escape for single cache results! hooray!
if count == 1 then
fcacheObj = fcache[1];
if (require == 0) or
(require == 1 and fcacheObj[field] == id) or
(require == 2 and fcacheObj.key == field and fcacheObj[field] == id)
then
-- app.PrintDebug("SFO",field,id,require,"1=",fcacheObj.hash)
return allowMultiple and {fcacheObj} or fcacheObj
end
-- one result, but doesn't meet the 'require'
-- app.PrintDebug("SFO",field,id,require,"1~",fcacheObj.hash)
return allowMultiple and app.EmptyTable or nil
end
local keyMatch, fieldMatch, match;
local Filter = app.RecursiveCharacterRequirementsFilter;
-- split logic based on allowMultiple to reduce conditionals within loop
if allowMultiple then
local filterMatch
-- split logic based on require to reduce conditionals within loop
if require == 2 then
-- Key require
for i=1,count,1 do
fcacheObj = fcache[i];
-- field matching id
if fcacheObj[field] == id then
if fcacheObj.key == field then
-- with keyed-field matching key & current filters
if Filter(fcacheObj) then
-- app.PrintDebug("SFO",field,id,require,"F>",fcacheObj.hash)
if filterMatch then filterMatch[#filterMatch + 1] = fcacheObj
else filterMatch = {fcacheObj} end
end
if keyMatch then keyMatch[#keyMatch + 1] = fcacheObj
else keyMatch = {fcacheObj} end
end
end
end
elseif require == 1 then
-- Field require
for i=1,count,1 do
fcacheObj = fcache[i];
-- field matching id
if fcacheObj[field] == id then
if fcacheObj.key == field then
-- with keyed-field matching key & current filters
if Filter(fcacheObj) then
-- app.PrintDebug("SFO",field,id,require,"F>",fcacheObj.hash)
if filterMatch then filterMatch[#filterMatch + 1] = fcacheObj
else filterMatch = {fcacheObj} end
end
if keyMatch then keyMatch[#keyMatch + 1] = fcacheObj
else keyMatch = {fcacheObj} end
else
-- with field matching id
if fieldMatch then fieldMatch[#fieldMatch + 1] = fcacheObj
else fieldMatch = {fcacheObj} end
end
end
end
else
-- No require
for i=1,count,1 do
fcacheObj = fcache[i];
-- field matching id
if fcacheObj[field] == id then
if fcacheObj.key == field then
-- with keyed-field matching key & current filters
if Filter(fcacheObj) then
-- app.PrintDebug("SFO",field,id,require,"F>",fcacheObj.hash)
if filterMatch then filterMatch[#filterMatch + 1] = fcacheObj
else filterMatch = {fcacheObj} end
end
if keyMatch then keyMatch[#keyMatch + 1] = fcacheObj
else keyMatch = {fcacheObj} end
else
-- with field matching id
if fieldMatch then fieldMatch[#fieldMatch + 1] = fcacheObj
else fieldMatch = {fcacheObj} end
end
-- basic group related to search
else
if match then match[#match + 1] = fcacheObj
else match = {fcacheObj} end
end
end
end
-- app.PrintDebug("SFO",field,id,require,"?>",filterMatch and #filterMatch,keyMatch and #keyMatch,fieldMatch and #fieldMatch,match and #match)
return filterMatch or keyMatch or fieldMatch or match or app.EmptyTable
else -- single returnd object
-- split logic based on require to reduce conditionals within loop
if require == 2 then
-- Key require
for i=1,count,1 do
fcacheObj = fcache[i];
-- field matching id
if fcacheObj[field] == id then
if fcacheObj.key == field then
-- with keyed-field matching key & current filters
if Filter(fcacheObj) then
-- app.PrintDebug("SFO",field,id,require,"F>",fcacheObj.hash)
return fcacheObj;
end
keyMatch = keyMatch or fcacheObj;
end
end
end
elseif require == 1 then
-- Field require
for i=1,count,1 do
fcacheObj = fcache[i];
-- field matching id
if fcacheObj[field] == id then
if fcacheObj.key == field then
-- with keyed-field matching key & current filters
if Filter(fcacheObj) then
-- app.PrintDebug("SFO",field,id,require,"F>",fcacheObj.hash)
return fcacheObj;
end
keyMatch = keyMatch or fcacheObj;
else
-- with field matching id
fieldMatch = fieldMatch or fcacheObj;
end
end