-
Notifications
You must be signed in to change notification settings - Fork 24
/
OutfitterInventory.lua
1410 lines (1110 loc) · 38.1 KB
/
OutfitterInventory.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
----------------------------------------
-- General
----------------------------------------
function Outfitter:FindNextCooldownItem(pItemCodes, pIgnoreSwapCooldown)
local vInventoryCache = self:GetInventoryCache()
local vBestItem, vBestTime
for _, vItemCode in ipairs(pItemCodes) do
if type(vItemCode) == "string" then
local vItemName, vItemLink = GetItemInfo(vItemCode)
if vItemLink then
vItemCode = self:ParseItemLink2(vItemLink)[1]
end
end
local vItems = vItemCode and vInventoryCache.ItemsByCode[vItemCode]
if vItems then
for _, vItemInfo in ipairs(vItems) do
local vStart, vDuration, vEnabled
if vItemInfo.Location.BagIndex then
vStart, vDuration, vEnabled = C_Container.GetContainerItemCooldown(vItemInfo.Location.BagIndex,
vItemInfo.Location.BagSlotIndex)
elseif vItemInfo.Location.SlotID then
vStart, vDuration, vEnabled = GetInventoryItemCooldown("player", vItemInfo.Location.SlotID)
end
local vRemainingTime
if vEnabled == 1 and vStart ~= 0 then
local vElapsed = GetTime() - vStart
vRemainingTime = vDuration - vElapsed
elseif self:ItemHasUseFeature(vItemInfo.Link) then
vRemainingTime = 0
else
vRemainingTime = 30 -- Items without a /use are just considered to be 30 secs so that they have lowest priority
end
-- If it's in the bag then the minimum is 30 secs
if vItemInfo.Location.BagIndex
and not pIgnoreSwapCooldown
and vRemainingTime < 30 then
vRemainingTime = 30
end
-- Compare it to the current result
if not vBestTime or vRemainingTime < vBestTime then
vBestItem = vItemInfo
vBestTime = vRemainingTime
end
end
end
end
return vBestItem, vBestTime
end
function Outfitter:InventorySlotIsEmpty(pInventorySlot)
return GetInventoryItemTexture("player", self.cSlotIDs[pInventorySlot]) == nil
end
function Outfitter:GetBagItemInfo(bagIndex, slotIndex)
local itemLink = C_Container.GetContainerItemLink(bagIndex, slotIndex)
if not itemLink then
return
end
local itemInfo = self:GetItemInfoFromLink(itemLink)
if not itemInfo then
return
end
local location = ItemLocation:CreateFromBagAndSlot(bagIndex, slotIndex)
local containerItemInfo = C_Container.GetContainerItemInfo(bagIndex, slotIndex)
itemInfo.Texture = containerItemInfo.iconFileID
local itemLinkInfo = self:ParseItemLink(itemLink)
_, _, itemInfo.Gem1, itemInfo.Gem2, itemInfo.Gem3, itemInfo.Gem4 = unpack(self:ParseItemLink(itemLink))
if itemInfo.Gem1 ~= nil then
itemInfo.Gem1Link = select(2, GetItemInfo(itemInfo.Gem1))
end
if itemInfo.Gem2 ~= nil then
itemInfo.Gem2Link = select(2, GetItemInfo(itemInfo.Gem2))
end
if itemInfo.Gem3 ~= nil then
itemInfo.Gem3Link = select(2, GetItemInfo(itemInfo.Gem3))
end
if itemInfo.Gem4 ~= nil then
itemInfo.Gem4Link = select(2, GetItemInfo(itemInfo.Gem4))
end
itemInfo.Location = {BagIndex = bagIndex, BagSlotIndex = slotIndex}
return itemInfo
end
function Outfitter:GetBagItemLinkInfo(bagIndex, slotIndex)
local itemLink = C_Container.GetContainerItemLink(bagIndex, slotIndex)
if not itemLink then
return
end
return self:ParseItemLink2(itemLink)
end
function Outfitter:GetBagItemInvType(bagIndex, slotIndex)
local itemLink = C_Container.GetContainerItemLink(bagIndex, slotIndex)
if not itemLink then
return
end
local _, _, _, _, _, _, _, _, itemInvType = GetItemInfo(itemLink)
return itemInvType
end
function Outfitter:GetItemLocationLink(pItemLocation)
if not pItemLocation then
return
end
if pItemLocation.BagIndex then
return C_Container.GetContainerItemLink(pItemLocation.BagIndex, pItemLocation.BagSlotIndex)
elseif pItemLocation.SlotName then
return self:GetInventorySlotIDLink(pSlotID)
else
self:ErrorMessage("Unknown location in GetItemLocationLink")
return
end
end
function Outfitter:GetItemLocationBagType(pItemLocation)
if not pItemLocation then
return
end
if pItemLocation.BagIndex then
return self:GetBagItemBagType(pItemLocation.BagIndex, pItemLocation.BagSlotIndex)
elseif pItemLocation.SlotName then
return self:GetSlotIDItemBagType(self.cSlotIDs[pItemLocation.SlotName])
else
self:ErrorMessage("Unknown location in GetItemLocationBagType")
return
end
end
function Outfitter:GetBagItemBagType(pBagIndex, pSlotIndex)
local vItemCodes = self:GetBagItemLinkInfo(pBagIndex, pSlotIndex)
if not vItemCodes then
return
end
return GetItemFamily(vItemCodes[1])
end
function Outfitter:GetSlotIDLinkInfo(pSlotID)
return self:ParseItemLink2(self:GetInventorySlotIDLink(pSlotID))
end
function Outfitter:GetSlotIDItemBagType(pSlotID)
local vItemCodes = self:GetSlotIDLinkInfo(pSlotID)
if not vItemCodes then
return
end
return GetItemFamily(vItemCodes[1])
end
function Outfitter:ParseItemLink(pItemLink)
if not pItemLink then
return
end
local payload = {}
local index = 1;
for info in string.gmatch(pItemLink, "|([^\|]*)") do
if string.match(info, "^H") then
for field in string.gmatch(info, ":([^\:]*)") do
payload[index] = field
index = index + 1
end
end
end
return payload
end
function Outfitter:ParseItemLink2(pItemLink)
if not pItemLink then
return
end
local _, _, vLinkType = pItemLink:find("|H([^:]+):")
if vLinkType ~= "item" then
return
end
local vStartIndex, vEndIndex, vCodeStrings, vName = pItemLink:find("|Hitem:([^|]*)|h%[([^%]]*)%]|h")
-- self:DebugMessage("start %s, end %s, codes %s, name %s", tostring(vStartIndex), tostring(vEndIndex), tostring(vCodeStrings), tostring(vName))
if not vCodeStrings then
return
end
local vCodes = {}
for vCodeString in string.gmatch(vCodeStrings..":", "%d*:") do
local vCode = tonumber(strmatch(vCodeString, "(%d+):")) or 0
vCodes[#vCodes + 1] = vCode
end
return vCodes, vName
end
function Outfitter:GetItemInfoFromLink(itemLink)
-- Return nothing if no link is given
if not itemLink then
return
end
-- Create the object and have it populate itself
local itemInfo = Outfitter:New(Outfitter._ItemInfoMetaTable)
itemInfo:GetItemInfoFromLink(itemLink)
-- Done
return itemInfo
end
function Outfitter:GetItemInfoFromCode(itemCode)
local itemInfo = Outfitter:New(Outfitter._ItemInfoMetaTable)
itemInfo:GetItemInfoFromCode(itemCode)
return itemInfo
end
function Outfitter:GetBagSlotItemName(bag, slot)
local tooltip = self.TooltipLib:SharedTooltip()
tooltip:ClearLines()
tooltip:SetBagItem(bag, slot)
if not tooltip:IsShown() then
return
end
-- Find the first left text line
local tooltipName = tooltip:GetName()
local textLeft1Name = tooltipName.."TextLeft1"
local textLeft1 = _G[textLeft1Name]
-- Return the item name
local itemName = textLeft1:GetText()
return itemName
end
function Outfitter:IsBankBagIndex(pBagIndex)
return pBagIndex and (pBagIndex > NUM_BAG_SLOTS or pBagIndex < 0)
end
----------------------------------------
Outfitter._ItemInfo = {}
setmetatable(Outfitter._ItemInfo, Outfitter.ObjectMetaTable)
Outfitter._ItemInfoMetaTable = {__index = Outfitter._ItemInfo}
----------------------------------------
function Outfitter._ItemInfo:Construct()
end
function Outfitter._ItemInfo:GetItemInfoFromCode(itemCode)
local itemFamilyName,
itemLink,
itemQuality,
itemLevel,
itemMinLevel,
itemType,
itemSubType,
itemCount,
itemInvType = GetItemInfo(itemCode)
--
self.Name = itemFamilyName
self.Link = ""
self.Code = itemCode
self.SubCode = 0
self.Quality = itemQuality
self.Level = itemLevel
self.MinLevel = itemMinLevel
self.Type = itemType
self.SubType = itemSubType
self.Count = itemCount
self.InvType = itemInvType
self.EnchantCode = 0
self.JewelCode1 = 0
self.JewelCode2 = 0
self.JewelCode3 = 0
self.JewelCode4 = 0
self.UniqueID = 0
self.UpgradeTypeID = 0
self.InstanceDifficultyID = 0
self.BonusIDs = "::"
self.UpgradeID = 0
-- Return if there's no inventory type
if not itemInvType
or itemInvType == "" then
return
end
-- If it's a known inventory type add that knowledge to the item info
local invTypeInfo = Outfitter.cInvTypeToSlotName[itemInvType]
if invTypeInfo then
-- Get the slot name
if not invTypeInfo.SlotName then
Outfitter:ErrorMessage("Unknown slot name for inventory type "..itemInvType)
return
end
-- Save the slot info
self.ItemSlotName = invTypeInfo.SlotName
self.MetaSlotName = invTypeInfo.MetaSlotName
else
-- This function can be used to query non-equippable items, so it's not an error for
-- the inventory type to be unknown. Should Blizzard ever add a new type though, this
-- debug message may be useful in figuring out its characteristics
-- Outfitter:ErrorMessage("Unknown slot type "..itemInvType.." for item "..itemName)
end
end
function Outfitter._ItemInfo:GetItemInfoFromLink(itemLink)
-- Do nothing if invalid
if not itemLink then
return
end
-- Do nothing if the codes can't be extracted
local itemCodes, itemName = Outfitter:ParseItemLink2(itemLink)
if not itemCodes then
return
end
-- Start with the base code info
self:GetItemInfoFromCode(itemCodes[1])
-- The provided itemLink may have Quality, ItemLevel etc. that differ from the base item.
-- supersede info from ItemCode with info from the provided itemLink:
local _, _,
itemQuality,
itemLevel,
itemMinLevel = GetItemInfo(itemLink)
self.Name = itemName
self.Link = itemLink
self.Quality = itemQuality
self.Level = itemLevel
self.MinLevel = itemMinLevel
-- Fill in the rest from itemCodes
self.EnchantCode = itemCodes[2] or 0
self.JewelCode1 = itemCodes[3] or 0
self.JewelCode2 = itemCodes[4] or 0
self.JewelCode3 = itemCodes[5] or 0
self.JewelCode4 = itemCodes[6] or 0
self.SubCode = itemCodes[7] or 0
self.UniqueID = itemCodes[8] or 0
-- self.LinkLevel = itemCodes[9] -- LinkLevel is the level of the player who generated the link, which isn't interesting to us
-- self.SpecializationID = itemCodes[10] -- DON'T capture this
self.UpgradeTypeID = itemCodes[11] or 0
self.InstanceDifficultyID = itemCodes[12] or 0
local numBonusIDsString = itemCodes[13]
local index = 14
local numBonusIDs = tonumber(numBonusIDsString)
local bonusID1, bonusID2
if numBonusIDs > 0 then
bonusID1 = itemCodes[index]
index = index + 1
end
if numBonusIDs > 1 then
bonusID2 = itemCodes[index]
index = index + 1
end
self.BonusIDs = (numBonusIDsString ~= 0 and numBonusIDsString or "")..":"..(bonusID1 ~= 0 and bonusID1 or "")..":"..(bonusID2 ~= 0 and bonusID2 or "")
self.UpgradeID = itemCodes[index] or 0
end
function Outfitter._ItemInfo:ParseTooltip()
-- Grab a tooltip
local tooltip = Outfitter.TooltipLib:SharedTooltip()
tooltip:ClearLines()
if self.Location and self.Location.BagIndex then
tooltip:SetBagItem(self.Location.BagIndex, self.Location.BagSlotIndex)
elseif self.Location and self.Location.SlotID then
tooltip:SetInventoryItem("player", self.Location.SlotID)
elseif self.Link then
tooltip:SetHyperlink(self.Link)
else
assert(false, "can't find item for tooltip")
return
end
-- Return if something went wrong
if not tooltip:IsShown() then
return
end
-- Iterate the lines of the tooltip
for line in Outfitter.TooltipLib:TooltipLines(tooltip) do
self:ParseTooltipLine(line.leftText, line.leftColor)
end
self.Gem1UniqueType, self.Gem1UniqueCount = self:ParseGemLinkForUniqueEquips(self.Gem1Link)
self.Gem2UniqueType, self.Gem2UniqueCount = self:ParseGemLinkForUniqueEquips(self.Gem2Link)
self.Gem3UniqueType, self.Gem3UniqueCount = self:ParseGemLinkForUniqueEquips(self.Gem3Link)
self.Gem4UniqueType, self.Gem4UniqueCount = self:ParseGemLinkForUniqueEquips(self.Gem4Link)
-- Done
self.didParseTooltip = true
end
function Outfitter._ItemInfo:ParseGemLinkForUniqueEquips(link)
-- Item didnt have that Gem1/Gem2/etc
if link == nil then
return nil, nil
end
local tooltip = Outfitter.TooltipLib:SharedTooltip()
tooltip:ClearLines()
tooltip:SetHyperlink(link)
-- Return if something went wrong
if not tooltip:IsShown() then
return nil, nil
end
for line in Outfitter.TooltipLib:TooltipLines(tooltip) do
-- Check for Unique-Equipped
local type, count = line.leftText:match(Outfitter.cUniqueEquippedSearchPattern)
if type then
return type, tonumber(count)
end
end
return nil, nil
end
function Outfitter._ItemInfo:ParseTooltipLine(text, color)
-- Check for Binds on Equip
if text == ITEM_BIND_ON_EQUIP then
self.BoE = true
return
end
-- Check for Unique-Equipped
local type, count = text:match(Outfitter.cUniqueEquippedSearchPattern)
if type then
self.UniqueType = type
self.UniqueCount = tonumber(count)
return
end
-- Check for Requires
if text:match(Outfitter.cRequiresPrefix) then
local hsvColor = Outfitter:RGBToHSV(color)
self.FailedRequirements = hsvColor.s > 0.2 and hsvColor.v > 0.2 and (hsvColor.h < 50 or hsvColor.h > 310)
return
end
end
function Outfitter._ItemInfo:GetBoE()
-- Get the info from the tooltip if necessary
if not self.didParseTooltip then
self:ParseTooltip()
end
-- Return the value
return self.BoE
end
function Outfitter._ItemInfo:GetMeetsRequirements()
-- Get the info from the tooltip if necessary
if not self.didParseTooltip then
self:ParseTooltip()
end
-- Return the value
return not self.FailedRequirements
end
function Outfitter._ItemInfo:GetUniqueEquipTypes()
-- Get the info from the tooltip if necessary
if not self.didParseTooltip then
self:ParseTooltip()
end
local uniqueTypesTable = {}
if self.UniqueType then
uniqueTypesTable[self.UniqueType] = self.UniqueCount
end
if self.Gem1UniqueType then
uniqueTypesTable[self.Gem1UniqueType] = self.Gem1UniqueCount
end
if self.Gem2UniqueType then
uniqueTypesTable[self.Gem2UniqueType] = self.Gem2UniqueCount
end
if self.Gem3UniqueType then
uniqueTypesTable[self.Gem3UniqueType] = self.Gem3UniqueCount
end
if self.Gem4UniqueType then
uniqueTypesTable[self.Gem4UniqueType] = self.Gem4UniqueCount
end
return uniqueTypesTable
end
----------------------------------------
--
----------------------------------------
function Outfitter:GetInventoryItemInfo(pInventorySlot)
local vSlotID = self.cSlotIDs[pInventorySlot]
if not vSlotID then
return
end
local vItemInfo = self:GetSlotIDItemInfo(vSlotID)
if not vItemInfo then
return
end
vItemInfo.Location.SlotName = pInventorySlot
return vItemInfo
end
function Outfitter:GetSlotIDItemInfo(slotID)
local itemLink = self:GetInventorySlotIDLink(slotID)
if not itemLink then
return
end
local itemInfo = self:GetItemInfoFromLink(itemLink)
if not itemInfo then
return
end
local location = ItemLocation:CreateFromEquipmentSlot(slotID)
itemInfo.Quality = GetInventoryItemQuality("player", slotID)
itemInfo.Texture = GetInventoryItemTexture("player", slotID)
if slotID then
_, _, itemInfo.Gem1, itemInfo.Gem2, itemInfo.Gem3, itemInfo.Gem4 = unpack(self:ParseItemLink(itemLink))
if itemInfo.Gem1 ~= nil then
itemInfo.Gem1Link = select(2, GetItemInfo(itemInfo.Gem1))
end
if itemInfo.Gem2 ~= nil then
itemInfo.Gem2Link = select(2, GetItemInfo(itemInfo.Gem2))
end
if itemInfo.Gem3 ~= nil then
itemInfo.Gem3Link = select(2, GetItemInfo(itemInfo.Gem3))
end
if itemInfo.Gem4 ~= nil then
itemInfo.Gem4Link = select(2, GetItemInfo(itemInfo.Gem4))
end
end
itemInfo.Location = {SlotID = slotID}
local location = ItemLocation:CreateFromEquipmentSlot(slotID)
return itemInfo
end
function Outfitter:GetAzeriteCodesForLocation(location)
return
end
function Outfitter:GetNumBags()
if self.BankFrameIsOpen then
return NUM_BAG_SLOTS + NUM_BANKBAGSLOTS, -1
else
return NUM_BAG_SLOTS, 0
end
end
function Outfitter:GetBagList()
local vBagList = {}
if self.BankFrameIsOpen then
for vIndex = -1, NUM_BAG_SLOTS + NUM_BANKBAGSLOTS do
vBagList[vIndex] = true
end
else
for vIndex = 0, NUM_BAG_SLOTS do
vBagList[vIndex] = true
end
end
--[[
if self.VoidStorageIsOpen then
vBagList["VOID_DEPOSIT"] = true
vBagList["VOID_WITHDRAW"] = true
vBagList["VOID_STORAGE"] = true
end
]]
return vBagList
end
function Outfitter:GetInventorySlotIDLink(slotID)
return GetInventoryItemLink("player", slotID)
end
Outfitter.LinkCache =
{
Inventory = {},
FirstBagIndex = 0,
NumBags = 0,
Bags = {},
}
function Outfitter:ScheduleSynch()
self.SchedulerLib:RescheduleTask(0.01, self.Synchronize, self)
end
function Outfitter:Synchronize()
local vBagsChanged, vInventoryChanged = false, false
if self.Debug.InventoryCache then
self:DebugMessage("Synchronize()")
end
-- Synchronize bag links
local vNumBags, vFirstBagIndex = self:GetNumBags()
if self.LinkCache.FirstBagIndex ~= vFirstBagIndex
or self.LinkCache.NumBags ~= vNumBags then
self.LinkCache.FirstBagIndex = vFirstBagIndex
self.LinkCache.NumBags = vNumBags
vBagsChanged = true
end
for vBagIndex = vFirstBagIndex, vNumBags do
local vBag = self.LinkCache.Bags[vBagIndex]
local vBagChanged = false
if not vBag then
vBag = {}
self.LinkCache.Bags[vBagIndex] = vBag
end
local vNumBagSlots = C_Container.GetContainerNumSlots(vBagIndex)
if #vBag ~= vNumBagSlots then
wipe(vBag)
vBagChanged = true
end
for vSlotIndex = 1, vNumBagSlots do
local vItemLink = C_Container.GetContainerItemLink(vBagIndex, vSlotIndex) or ""
if vBag[vSlotIndex] ~= vItemLink then
vBag[vSlotIndex] = vItemLink
vBagChanged = true
end
end
if vBagChanged then
if self.InventoryCache then
self.InventoryCache:FlushBag(vBagIndex)
end
vBagsChanged = true
end
end
-- Synchronize inventory links
for _, vInventorySlot in ipairs(self.cSlotNames) do
local vItemLink
vItemLink = GetInventoryItemLink("player", self.cSlotIDs[vInventorySlot])
if self.Debug.InventoryCache then
self:DebugMessage("Synchronize: Slot %s contains %s", tostring(vInventorySlot), tostring(vItemLink))
end
if self.LinkCache.Inventory[vInventorySlot] ~= vItemLink then
self.LinkCache.Inventory[vInventorySlot] = vItemLink
vInventoryChanged = true
end
end
if vInventoryChanged then
if self.Debug.InventoryCache then
self:DebugMessage("Synchronize: InventoryChanged")
end
if self.InventoryCache then
self.InventoryCache:FlushInventory()
end
self.EventLib:DispatchEvent("OUTFITTER_INVENTORY_CHANGED")
end
if vBagsChanged then
if self.Debug.InventoryCache then
self:DebugMessage("Synchronize: Bags changed")
end
self.EventLib:DispatchEvent("OUTFITTER_BAGS_CHANGED")
end
-- Done
if vBagsChanged or vInventoryChanged then
self.DisplayIsDirty = true
self:Update(false)
end
--
self:RunThreads()
return vBagsChanged or vInventoryChanged, vInventoryChanged, vBagsChanged
end
----------------------------------------
-- InventoryCache
----------------------------------------
function Outfitter:GetInventoryCache()
if not self.InventoryCache then
self.InventoryCache = self:New(self._InventoryCache)
if self.Debug.InventoryCache then
self:DebugTable(self.InventoryCache, "InventoryCache")
end
end
self.InventoryCache:Synchronize()
return self.InventoryCache
end
function Outfitter:FlushInventoryCache()
if self.InventoryCache then
self.InventoryCache:Flush()
end
end
----------------------------------------
Outfitter._InventoryCache = {}
----------------------------------------
function Outfitter._InventoryCache:Construct()
self.ItemsByCode = {}
self.ItemsBySlot = {}
self.InventoryItems = nil
self.BagItems = {}
self.NeedsUpdate = true
self.FirstBagIndex = 0
self.NumBags = 0
Outfitter.EventLib:RegisterEvent("GET_ITEM_INFO_RECEIVED", self.Flush, self)
end
function Outfitter._InventoryCache:Flush()
wipe(self.ItemsByCode)
wipe(self.ItemsBySlot)
self.InventoryItems = nil
wipe(self.BagItems)
self.NeedsUpdate = true
self.FirstBagIndex = 0
self.NumBags = 0
end
function Outfitter._InventoryCache:Synchronize()
-- Check for a change in the number of bags
local vNumBags, vFirstBagIndex = Outfitter:GetNumBags()
if self.FirstBagIndex ~= vFirstBagIndex
or self.NumBags ~= vNumBags then
for vBagIndex = self.FirstBagIndex, vFirstBagIndex - 1 do
self:FlushBag(vBagIndex)
end
for vBagIndex = vNumBags + 1, self.NumBags do
self:FlushBag(vBagIndex)
end
self.NeedsUpdate = true
end
-- If there's a cached copy just clear the IgnoreItem flags and return it
if not self.NeedsUpdate then
return
end
if not self.InventoryItems then
self.InventoryItems = {}
for _, vInventorySlot in ipairs(Outfitter.cSlotNames) do
local vItemInfo = Outfitter:GetInventoryItemInfo(vInventorySlot)
if vItemInfo
and vItemInfo.ItemSlotName
and vItemInfo.Code ~= 0 then
vItemInfo.SlotName = vInventorySlot
self:AddItem(vItemInfo)
end
end
end
for vBagIndex = vFirstBagIndex, vNumBags do
local vBagItems = self.BagItems[vBagIndex]
if not vBagItems then
self.BagItems[vBagIndex] = {}
local vNumBagSlots = C_Container.GetContainerNumSlots(vBagIndex)
if vNumBagSlots > 0 then
for vBagSlotIndex = 1, vNumBagSlots do
local vItemInfo = Outfitter:GetBagItemInfo(vBagIndex, vBagSlotIndex)
if vItemInfo
and vItemInfo.Code ~= 0
and vItemInfo.ItemSlotName
and Outfitter:CanEquipBagItem(vBagIndex, vBagSlotIndex)
and not vItemInfo:GetBoE() then
self:AddItem(vItemInfo)
end
end -- for vBagSlotIndex
end -- if vNumBagSlots > 0
end -- if not BagItems
end -- for vBagIndex
self.FirstBagIndex = vFirstBagIndex
self.NumBags = vNumBags
self.NeedsUpdate = false
end
function Outfitter._InventoryCache:AddItem(pItem)
-- Add the item to the code list
local vItemFamily = self.ItemsByCode[pItem.Code]
if not vItemFamily then
vItemFamily = {}
self.ItemsByCode[pItem.Code] = vItemFamily
end
table.insert(vItemFamily, pItem)
-- Add the item to the slot list
local vItemSlot = self.ItemsBySlot[pItem.ItemSlotName]
if not vItemSlot then
vItemSlot = {}
self.ItemsBySlot[pItem.ItemSlotName] = vItemSlot
end
table.insert(vItemSlot, pItem)
-- Add the item to the bags
if pItem.Location.BagIndex then
local vBagItems = self.BagItems[pItem.Location.BagIndex]
if not vBagItems then
vBagItems = {}
self.BagItems[pItem.Location.BagIndex] = vBagItems
end
vBagItems[pItem.Location.BagSlotIndex] = pItem
-- Add the item to the inventory
elseif pItem.Location.SlotName then
self.InventoryItems[pItem.Location.SlotName] = pItem
end
end
function Outfitter._InventoryCache:RemoveItem(pItem)
-- Remove the item from the code list
local vItems = self.ItemsByCode[pItem.Code]
for vIndex, vItem in ipairs(vItems) do
if vItem == pItem then
table.remove(vItems, vIndex)
break
end
end
-- Remove the item from the slot list
local vItemSlot = self.ItemsBySlot[pItem.ItemSlotName]
if vItemSlot then
for vIndex, vItem in ipairs(vItemSlot) do
if vItem == pItem then
table.remove(vItemSlot, vIndex)
break
end
end
end
-- Remove the item from the bags list
if pItem.Location.BagIndex then
local vBagItems = self.BagItems[pItem.Location.BagIndex]
if vBagItems then
vBagItems[pItem.Location.BagSlotIndex] = nil
end
-- Remove the item from the inventory list
elseif pItem.Location.SlotName then
self.InventoryItems[pItem.Location.SlotName] = nil
end
end
function Outfitter._InventoryCache:SwapLocations(pLocation1, pLocation2)
-- if pLocation1.BagIndex then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocations: Swapping bag "..pLocation1.BagIndex..", "..pLocation1.BagSlotIndex)
-- elseif pLocation1.SlotName then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocations: Swapping slot "..pLocation1.SlotName)
-- end
-- if pLocation2.BagIndex then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocations: with bag "..pLocation2.BagIndex..", "..pLocation2.BagSlotIndex)
-- elseif pLocation2.SlotName then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocations: with slot "..pLocation2.SlotName)
-- end
end
function Outfitter._InventoryCache:SwapLocationWithInventorySlot(pLocation, pSlotName)
-- if pLocation.BagIndex then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocationWithInventorySlot: Swapping bag "..pLocation.BagIndex..", "..pLocation.BagSlotIndex.." with slot "..pSlotName)
-- elseif pLocation.SlotName then
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapLocationWithInventorySlot: Swapping slot "..pLocation.SlotName.." with slot "..pSlotName)
-- end
end
function Outfitter._InventoryCache:SwapBagSlotWithInventorySlot(pBagIndex, pBagSlotIndex, pSlotName)
-- Outfitter:TestMessage("Outfitter._InventoryCache:SwapBagSlotWithInventorySlot: Swapping bag "..pBagIndex..", "..pBagSlotIndex.." with slot "..pSlotName)
end
function Outfitter._InventoryCache:FindItemInfoByCode(pItemInfo)
local vItems = self.ItemsByCode[pItemInfo.Code]
for _, vItemInfo in ipairs(vItems) do
if pItemInfo == vItemInfo then
return true
end
end
return false
end
function Outfitter._InventoryCache:FindItemInfoBySlot(pItemInfo)
local vItems = self.ItemsBySlot[pItemInfo.ItemSlotName]
for _, vItemInfo in ipairs(vItems) do
if pItemInfo == vItemInfo then
return true
end
end
return false
end