Skip to content

Commit

Permalink
Rework item cache to use links instead of IDs to better handle items …
Browse files Browse the repository at this point in the history
…with bonusIDs (like shadowlands legendaries)
  • Loading branch information
ascott18 committed Feb 13, 2021
1 parent 34e46ab commit 6481cf5
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 170 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes
* #1840 - Talents granted by Torghast powers are now correctly reflected by the Talent Learned condition.
* #1844 - The Totem icon type has been updated for Monks to better support the wide range of "totems" that Monks have.
* #1842 - Fixed handling of Shadowlands legendaries in item suggestion lists

## v9.0.3
### Bug Fixes
Expand Down
56 changes: 52 additions & 4 deletions Components/Core/Common/Item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ function ItemByID:GetName()
return name
end
end
function ItemByID:GetLink()
local _, itemLink = GetItemInfo(self.itemID)
return itemLink
end



Expand Down Expand Up @@ -271,7 +275,7 @@ function ItemByName:GetCooldown()
end

function ItemByName:GetID()
local _, itemLink = GetItemInfo(self.name)
local itemLink = self:GetLink()
if itemLink then
return tonumber(strmatch(itemLink, ":(%d+)"))
end
Expand All @@ -287,6 +291,10 @@ function ItemByName:GetName()
return self.name
end
end
function ItemByName:GetLink()
local _, itemLink = GetItemInfo(self.name)
return itemLink
end



Expand All @@ -297,6 +305,36 @@ end

local ItemBySlot = TMW:NewClass("ItemBySlot", Item)


local slotNames = {}
for _, slotName in pairs{
"BackSlot",
"ChestSlot",
"FeetSlot",
"Finger0Slot",
"Finger1Slot",
"HandsSlot",
"HeadSlot",
"LegsSlot",
"MainHandSlot",
"NeckSlot",
"SecondaryHandSlot",
"ShirtSlot",
"ShoulderSlot",
"TabardSlot",
"Trinket0Slot",
"Trinket1Slot",
"WaistSlot",
"WristSlot",
} do
local slotID = GetInventorySlotInfo(slotName)
if slotID then
slotNames[slotID] = slotName
else
TMW:Debug("Invalid slot name %s", slotName)
end
end

function ItemBySlot:OnNewInstance(itemSlot)
TMW:ValidateType("2 (itemSlot)", "ItemByID:New(itemSlot)", itemSlot, "number")

Expand All @@ -323,8 +361,12 @@ function ItemBySlot:GetID()
return GetInventoryItemID("player", self.slot)
end
function ItemBySlot:GetName()
local name = GetItemInfo(self:GetLink())
return name
local link = self:GetLink()
if link then
local name = GetItemInfo(link)
if name then return name end
end
return slotNames[self.slot] and _G[slotNames[self.slot]:upper()] or nil
end
function ItemBySlot:GetLink()
return GetInventoryItemLink("player", self.slot)
Expand All @@ -343,7 +385,7 @@ function ItemByLink:OnNewInstance(itemLink)
TMW:ValidateType("2 (itemLink)", "ItemByID:New(itemLink)", itemLink, "string")

self.link = itemLink
self.itemID = tonumber(strmatch(itemLink, ":(%d+)"))
self.itemID = tonumber(strmatch(itemLink, "item:(%d+)"))
self.name = GetItemInfo(itemLink)
end

Expand All @@ -367,9 +409,15 @@ end

function ItemByLink:GetName()
local name = GetItemInfo(self.link)
if not name then
name = self.link:match("%[(.-)%]")
end
if name then
self.name = name
self.GetName = self.GetName_saved
return name
end
end
function ItemByLink:GetLink()
return self.link
end
2 changes: 2 additions & 0 deletions Components/Core/Spells/Equivalencies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ TMW.BE = {
288024, -- Diamond Barrier (Diamond-Laced Refracting Prism, Battle of Dazar'alor raid)
295271, -- Umbral Shell (Void Stone, Crucible of Storms raid)
295431, -- Ephemeral Vigor (Abyssal Speaker's Gauntlets, Crucible of Storms raid)
311444, -- Indomitable Deck (Shadowlands tank darkmoon deck)
322507, -- Celestial Brew (Monk, brewmaster)
324867, -- Fleshcraft (Necrolord)
},
ImmuneToMagicCC = {
Expand Down
68 changes: 41 additions & 27 deletions Components/Core/Spells/ItemCache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ TMW.IE:RegisterDatabaseDefaults{
},
}

TMW.IE:RegisterUpgrade(72310, {
TMW.IE:RegisterUpgrade(90403, {
locale = function(self, locale)
locale.ItemCache = nil
locale.XPac_ItemCache = nil
Expand All @@ -59,7 +59,7 @@ TMW.IE:RegisterUpgrade(62217, {

--[[ Returns the main cache table. Structure:
Cache = {
[itemID] = 1,
[link] = 1,
}
]]
function ItemCache:GetCache()
Expand All @@ -74,8 +74,8 @@ end

--[[ Returns a list of items that the player currently has. Structure:
Cache = {
[itemID] = name,
[name] = itemID,
[link] = 1,
[name] = link,
}
]]
function ItemCache:GetCurrentItems()
Expand Down Expand Up @@ -119,14 +119,14 @@ TMW:RegisterCallback("TMW_OPTIONS_LOADED", function()
-- Compile all items from all timesegments into a cohesive table for
-- fast lookups and easy iteration.
for timestamp, items in pairs(Cache) do
for id, name in pairs(items) do
CompiledCache[id] = name
for link in pairs(items) do
CompiledCache[link] = 1
end
end

--Start requests so that we can validate itemIDs.
for id in pairs(CompiledCache) do
GetItemInfo(id)
for link in pairs(CompiledCache) do
GetItemInfo(link)
end

ItemCache:RegisterEvent("BAG_UPDATE")
Expand All @@ -135,21 +135,36 @@ TMW:RegisterCallback("TMW_OPTIONS_LOADED", function()
ItemCache:CacheItems()
end)

local function cacheItem(itemID, name)
local function cacheItem(itemID, link)
-- Sanitize the link:
-- strip out all gems/enchants (not an important distinction for TMW)
-- LEAVE suffixID alone (makes sure that the name is correct)
-- strip out uniqueID (not an important distinction for TMW)
-- strip out linkLevel,specializationId
link = link:gsub("(item:%d+):%-?%d*:%-?%d*:%-?%d*:%-?%d*:%-?%d*:(%-?%d*):%-?%d*:%-?%d*:%-?%d*", "%1:::::%2::::")

if link:find("|h%[%]|h") then
-- Links that don't have the item name loaded
-- will just have brackets in the position of the name and nothing else.
return
end

CurrentItems[link] = 1

-- The item is not cached at all.
if not CompiledCache[itemID] then
Cache[currentTimestamp][itemID] = name
CompiledCache[itemID] = name
if not CompiledCache[link] then
Cache[currentTimestamp][link] = 1
CompiledCache[link] = 1

-- The item is in an old cache timesegment.
elseif not Cache[currentTimestamp][itemID] then
elseif not Cache[currentTimestamp][link] then
-- Remove the item from the old cache.
for timestamp, items in pairs(Cache) do
items[itemID] = nil
items[link] = nil
end

-- Add it to the current cache timesegment.
Cache[currentTimestamp][itemID] = name
Cache[currentTimestamp][link] = 1
end
end

Expand All @@ -169,30 +184,29 @@ function ItemCache:CacheItems(force)
for container = 0, NUM_BAG_SLOTS do
for slot = 1, GetContainerNumSlots(container) do
local id = GetContainerItemID(container, slot)
local link = GetContainerItemLink(container, slot)
if id then
local name = GetItemInfo(id)
name = name and strlower(name)

CurrentItems[id] = name
cacheItem(id, name)
cacheItem(id, link)
end
end
end

-- Cache equipped items
for slot = 1, 19 do
local id = GetInventoryItemID("player", slot)
local link = GetInventoryItemLink("player", slot)
if id then
local name = GetItemInfo(id)
name = name and strlower(name)

CurrentItems[id] = name
cacheItem(id, name)
cacheItem(id, link)
end
end

for id, name in pairs(CurrentItems) do
CurrentItems[name] = id
local reverseMap = {}
for link in pairs(CurrentItems) do
local name = link:match("%[(.-)%]")
reverseMap[strlower(name)] = link
end
for k, v in pairs(reverseMap) do
CurrentItems[k] = v
end

doUpdateCache = nil
Expand Down
Loading

0 comments on commit 6481cf5

Please sign in to comment.