refactor: consolidate small compatibility layers
This commit is contained in:
@@ -445,3 +445,93 @@ function InventoryIndex:Initialize()
|
||||
|
||||
addon:Debug("ClassicAPI inventory count index initialized")
|
||||
end
|
||||
|
||||
--=====================================================
|
||||
-- Consolidated from Core/InventoryIndexSafety.lua
|
||||
--=====================================================
|
||||
-- Guda ClassicAPI InventoryIndex mutation hooks
|
||||
-- Ensures the O(1) tooltip index never stays stale after SavedVariables or
|
||||
-- cross-character data changes that do not necessarily emit a fresh bag event.
|
||||
-- Lua 5.0 compatible.
|
||||
|
||||
local addon = Guda
|
||||
local Index = addon.Modules.InventoryIndex
|
||||
local DB = addon.Modules.DB
|
||||
if not Index or not DB then return end
|
||||
|
||||
local oldSaveBags = DB.SaveBags
|
||||
local oldSaveBank = DB.SaveBank
|
||||
local oldSaveEquipment = DB.SaveEquipment
|
||||
local oldSaveMailbox = DB.SaveMailbox
|
||||
local oldAddMailToCharacter = DB.AddMailToCharacter
|
||||
local oldToggleGoldBlacklist = DB.ToggleGoldBlacklist
|
||||
local oldRemoveCharacter = DB.RemoveCharacter
|
||||
local oldCleanupOldCharacters = DB.CleanupOldCharacters
|
||||
|
||||
if oldSaveBags then
|
||||
function DB:SaveBags(bagData)
|
||||
oldSaveBags(self, bagData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveBank then
|
||||
function DB:SaveBank(bankData)
|
||||
oldSaveBank(self, bankData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveEquipment then
|
||||
function DB:SaveEquipment(equipmentData)
|
||||
oldSaveEquipment(self, equipmentData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveMailbox then
|
||||
function DB:SaveMailbox(mailboxData)
|
||||
oldSaveMailbox(self, mailboxData)
|
||||
-- MAIL_INBOX_UPDATE can fire before MailboxScanner's debounced save.
|
||||
-- Re-invalidating here guarantees the next tooltip sees the persisted
|
||||
-- post-update mailbox snapshot rather than the pre-save one.
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldAddMailToCharacter then
|
||||
function DB:AddMailToCharacter(name, realm, mailRow)
|
||||
local added = oldAddMailToCharacter(self, name, realm, mailRow)
|
||||
if added then
|
||||
-- This can mutate a different local character's mailbox.
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
return added
|
||||
end
|
||||
end
|
||||
|
||||
if oldToggleGoldBlacklist then
|
||||
function DB:ToggleGoldBlacklist(fullName)
|
||||
oldToggleGoldBlacklist(self, fullName)
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
end
|
||||
|
||||
if oldRemoveCharacter then
|
||||
function DB:RemoveCharacter(fullName)
|
||||
local removed = oldRemoveCharacter(self, fullName)
|
||||
if removed then
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
return removed
|
||||
end
|
||||
end
|
||||
|
||||
if oldCleanupOldCharacters then
|
||||
function DB:CleanupOldCharacters()
|
||||
oldCleanupOldCharacters(self)
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("ClassicAPI inventory index mutation safety enabled")
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
-- Guda ClassicAPI InventoryIndex mutation hooks
|
||||
-- Ensures the O(1) tooltip index never stays stale after SavedVariables or
|
||||
-- cross-character data changes that do not necessarily emit a fresh bag event.
|
||||
-- Lua 5.0 compatible.
|
||||
|
||||
local addon = Guda
|
||||
local Index = addon.Modules.InventoryIndex
|
||||
local DB = addon.Modules.DB
|
||||
if not Index or not DB then return end
|
||||
|
||||
local oldSaveBags = DB.SaveBags
|
||||
local oldSaveBank = DB.SaveBank
|
||||
local oldSaveEquipment = DB.SaveEquipment
|
||||
local oldSaveMailbox = DB.SaveMailbox
|
||||
local oldAddMailToCharacter = DB.AddMailToCharacter
|
||||
local oldToggleGoldBlacklist = DB.ToggleGoldBlacklist
|
||||
local oldRemoveCharacter = DB.RemoveCharacter
|
||||
local oldCleanupOldCharacters = DB.CleanupOldCharacters
|
||||
|
||||
if oldSaveBags then
|
||||
function DB:SaveBags(bagData)
|
||||
oldSaveBags(self, bagData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveBank then
|
||||
function DB:SaveBank(bankData)
|
||||
oldSaveBank(self, bankData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveEquipment then
|
||||
function DB:SaveEquipment(equipmentData)
|
||||
oldSaveEquipment(self, equipmentData)
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldSaveMailbox then
|
||||
function DB:SaveMailbox(mailboxData)
|
||||
oldSaveMailbox(self, mailboxData)
|
||||
-- MAIL_INBOX_UPDATE can fire before MailboxScanner's debounced save.
|
||||
-- Re-invalidating here guarantees the next tooltip sees the persisted
|
||||
-- post-update mailbox snapshot rather than the pre-save one.
|
||||
Index:InvalidateCurrent()
|
||||
end
|
||||
end
|
||||
|
||||
if oldAddMailToCharacter then
|
||||
function DB:AddMailToCharacter(name, realm, mailRow)
|
||||
local added = oldAddMailToCharacter(self, name, realm, mailRow)
|
||||
if added then
|
||||
-- This can mutate a different local character's mailbox.
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
return added
|
||||
end
|
||||
end
|
||||
|
||||
if oldToggleGoldBlacklist then
|
||||
function DB:ToggleGoldBlacklist(fullName)
|
||||
oldToggleGoldBlacklist(self, fullName)
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
end
|
||||
|
||||
if oldRemoveCharacter then
|
||||
function DB:RemoveCharacter(fullName)
|
||||
local removed = oldRemoveCharacter(self, fullName)
|
||||
if removed then
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
return removed
|
||||
end
|
||||
end
|
||||
|
||||
if oldCleanupOldCharacters then
|
||||
function DB:CleanupOldCharacters()
|
||||
oldCleanupOldCharacters(self)
|
||||
Index:InvalidateAll()
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("ClassicAPI inventory index mutation safety enabled")
|
||||
@@ -1,77 +0,0 @@
|
||||
-- Preserve Guda's original charge overlay behavior.
|
||||
-- ClassicAPI's container charge value is not reliable for distinguishing
|
||||
-- ordinary stack counts from explicit item charges on the target client, so
|
||||
-- only show the yellow "xN" overlay when the item tooltip actually contains a
|
||||
-- Charges line (matching upstream Guda behavior).
|
||||
|
||||
local addon = Guda
|
||||
local ItemDetection = addon.Modules.ItemDetection
|
||||
if not ItemDetection then return end
|
||||
|
||||
local chargesCache = {}
|
||||
|
||||
local function SafeSetHyperlink(tooltip, link)
|
||||
if not link then return false end
|
||||
local _, _, bare = string.find(link, "|H(item:[^|]+)|h")
|
||||
if not bare and string.find(link, "^item:") then bare = link end
|
||||
if not bare then return false end
|
||||
return pcall(tooltip.SetHyperlink, tooltip, bare)
|
||||
end
|
||||
|
||||
local function GetExplicitTooltipCharges(itemData, bagID, slotID)
|
||||
local tooltip, tooltipName = addon.Modules.Utils:GetScanTooltip()
|
||||
tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
|
||||
tooltip:ClearLines()
|
||||
|
||||
local ok = false
|
||||
if bagID and slotID and bagID ~= -1 then
|
||||
ok = pcall(tooltip.SetBagItem, tooltip, bagID, slotID)
|
||||
end
|
||||
if not ok then
|
||||
ok = SafeSetHyperlink(tooltip, itemData and itemData.link)
|
||||
end
|
||||
if not ok then return nil, false end
|
||||
|
||||
local numLines = tooltip:NumLines() or 0
|
||||
for i = 1, numLines do
|
||||
local line = getglobal(tooltipName .. "TextLeft" .. i)
|
||||
local text = line and line:GetText()
|
||||
if text then
|
||||
local _, _, num = string.find(string.lower(text), "^(%d+) charges?$")
|
||||
if num then return tonumber(num), numLines >= 2 end
|
||||
end
|
||||
end
|
||||
return nil, numLines >= 2
|
||||
end
|
||||
|
||||
function ItemDetection:GetCharges(itemData, bagID, slotID)
|
||||
if not bagID or not slotID then return nil end
|
||||
|
||||
local slotKey = bagID .. ":" .. slotID
|
||||
local cached = chargesCache[slotKey]
|
||||
if cached ~= nil then
|
||||
if cached == false then return nil end
|
||||
return cached
|
||||
end
|
||||
|
||||
local charges, complete = GetExplicitTooltipCharges(itemData, bagID, slotID)
|
||||
if complete then
|
||||
chargesCache[slotKey] = charges or false
|
||||
end
|
||||
return charges
|
||||
end
|
||||
|
||||
function ItemDetection:InvalidateCharges(bagID)
|
||||
if bagID then
|
||||
local prefix = bagID .. ":"
|
||||
for key in pairs(chargesCache) do
|
||||
if string.find(key, "^" .. prefix) then
|
||||
chargesCache[key] = nil
|
||||
end
|
||||
end
|
||||
else
|
||||
chargesCache = {}
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("Upstream-compatible charge display enabled")
|
||||
@@ -1,25 +0,0 @@
|
||||
-- Guda ClassicAPI ItemDetection cache safety
|
||||
-- Keep the optimized detection cache and the legacy per-slot charge cache in
|
||||
-- sync when ClassicAPI charge access is unavailable.
|
||||
-- Lua 5.0 compatible.
|
||||
|
||||
local addon = Guda
|
||||
local ItemDetection = addon.Modules.ItemDetection
|
||||
if not ItemDetection then return end
|
||||
|
||||
local FastClearCache = ItemDetection.ClearCache
|
||||
|
||||
function ItemDetection:ClearCache()
|
||||
if FastClearCache then
|
||||
FastClearCache(self)
|
||||
end
|
||||
|
||||
-- ItemDetectionClassicAPI keeps the original Vanilla charge-cache
|
||||
-- invalidator behind this public method. With ClassicAPI charges present
|
||||
-- it is a no-op; without them it clears the legacy bagID:slotID cache.
|
||||
if self.InvalidateCharges then
|
||||
self:InvalidateCharges(nil)
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("ClassicAPI ItemDetection cache safety enabled")
|
||||
@@ -364,3 +364,113 @@ function ItemDetection:InvalidateCharges(bagID)
|
||||
end
|
||||
|
||||
addon:Debug("ClassicAPI single-pass ItemDetection enabled")
|
||||
|
||||
--=====================================================
|
||||
-- Consolidated from Core/ItemChargesClassicAPI.lua
|
||||
--=====================================================
|
||||
-- Preserve Guda's original charge overlay behavior.
|
||||
-- ClassicAPI's container charge value is not reliable for distinguishing
|
||||
-- ordinary stack counts from explicit item charges on the target client, so
|
||||
-- only show the yellow "xN" overlay when the item tooltip actually contains a
|
||||
-- Charges line (matching upstream Guda behavior).
|
||||
|
||||
local addon = Guda
|
||||
local ItemDetection = addon.Modules.ItemDetection
|
||||
if not ItemDetection then return end
|
||||
|
||||
local chargesCache = {}
|
||||
|
||||
local function SafeSetHyperlink(tooltip, link)
|
||||
if not link then return false end
|
||||
local _, _, bare = string.find(link, "|H(item:[^|]+)|h")
|
||||
if not bare and string.find(link, "^item:") then bare = link end
|
||||
if not bare then return false end
|
||||
return pcall(tooltip.SetHyperlink, tooltip, bare)
|
||||
end
|
||||
|
||||
local function GetExplicitTooltipCharges(itemData, bagID, slotID)
|
||||
local tooltip, tooltipName = addon.Modules.Utils:GetScanTooltip()
|
||||
tooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
|
||||
tooltip:ClearLines()
|
||||
|
||||
local ok = false
|
||||
if bagID and slotID and bagID ~= -1 then
|
||||
ok = pcall(tooltip.SetBagItem, tooltip, bagID, slotID)
|
||||
end
|
||||
if not ok then
|
||||
ok = SafeSetHyperlink(tooltip, itemData and itemData.link)
|
||||
end
|
||||
if not ok then return nil, false end
|
||||
|
||||
local numLines = tooltip:NumLines() or 0
|
||||
for i = 1, numLines do
|
||||
local line = getglobal(tooltipName .. "TextLeft" .. i)
|
||||
local text = line and line:GetText()
|
||||
if text then
|
||||
local _, _, num = string.find(string.lower(text), "^(%d+) charges?$")
|
||||
if num then return tonumber(num), numLines >= 2 end
|
||||
end
|
||||
end
|
||||
return nil, numLines >= 2
|
||||
end
|
||||
|
||||
function ItemDetection:GetCharges(itemData, bagID, slotID)
|
||||
if not bagID or not slotID then return nil end
|
||||
|
||||
local slotKey = bagID .. ":" .. slotID
|
||||
local cached = chargesCache[slotKey]
|
||||
if cached ~= nil then
|
||||
if cached == false then return nil end
|
||||
return cached
|
||||
end
|
||||
|
||||
local charges, complete = GetExplicitTooltipCharges(itemData, bagID, slotID)
|
||||
if complete then
|
||||
chargesCache[slotKey] = charges or false
|
||||
end
|
||||
return charges
|
||||
end
|
||||
|
||||
function ItemDetection:InvalidateCharges(bagID)
|
||||
if bagID then
|
||||
local prefix = bagID .. ":"
|
||||
for key in pairs(chargesCache) do
|
||||
if string.find(key, "^" .. prefix) then
|
||||
chargesCache[key] = nil
|
||||
end
|
||||
end
|
||||
else
|
||||
chargesCache = {}
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("Upstream-compatible charge display enabled")
|
||||
|
||||
--=====================================================
|
||||
-- Consolidated from Core/ItemDetectionCacheSafety.lua
|
||||
--=====================================================
|
||||
-- Guda ClassicAPI ItemDetection cache safety
|
||||
-- Keep the optimized detection cache and the legacy per-slot charge cache in
|
||||
-- sync when ClassicAPI charge access is unavailable.
|
||||
-- Lua 5.0 compatible.
|
||||
|
||||
local addon = Guda
|
||||
local ItemDetection = addon.Modules.ItemDetection
|
||||
if not ItemDetection then return end
|
||||
|
||||
local FastClearCache = ItemDetection.ClearCache
|
||||
|
||||
function ItemDetection:ClearCache()
|
||||
if FastClearCache then
|
||||
FastClearCache(self)
|
||||
end
|
||||
|
||||
-- ItemDetectionClassicAPI keeps the original Vanilla charge-cache
|
||||
-- invalidator behind this public method. With ClassicAPI charges present
|
||||
-- it is a no-op; without them it clears the legacy bagID:slotID cache.
|
||||
if self.InvalidateCharges then
|
||||
self:InvalidateCharges(nil)
|
||||
end
|
||||
end
|
||||
|
||||
addon:Debug("ClassicAPI ItemDetection cache safety enabled")
|
||||
|
||||
Reference in New Issue
Block a user