Files

444 lines
21 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Presentation only. Preserve original item identities, metadata, availability,
-- request handling and every original API function/return value unchanged.
-- Runtime texture handles are process-owned; never save them in DB2/ADB data.
local data = GWSourceItemIconData
if type(data) ~= "table" or data.version ~= 1 or type(data.items) ~= "table" then return end
local textures, missing = {}, {}
local combatRefresh = false
local stats = {version = 3, resolvedPaths = 0, unavailablePaths = 0, substitutions = 0, samples = {}}
GWSourceItemIcons = stats
local function public(value)
return type(issecretvalue) ~= "function" or not issecretvalue(value)
end
local function identity(value)
if not public(value) then return nil end
if type(value) == "number" then
if value > 0 and value <= 2147483647 and value == math.floor(value) then return value end
elseif type(value) == "string" then
return tonumber(value:match("^item:(%d+)") or value:match("|Hitem:(%d+):") or value:match("^(%d+)$"))
end
end
local function texture(value)
local id = identity(value)
local path = id and data.items[id]
if not path or missing[path] then return nil end
local result = textures[path]
if result then return result end
if type(GetFileIDFromPath) ~= "function" then return nil end
local ok, handle = pcall(GetFileIDFromPath, path)
if ok and public(handle) and type(handle) == "number" and handle < -1 and handle > -2147483648 and handle == math.floor(handle) then
textures[path] = handle; stats.resolvedPaths = stats.resolvedPaths + 1
return handle
end
missing[path] = true; stats.unavailablePaths = stats.unavailablePaths + 1
end
stats.TextureForItem = texture -- Read-only acceptance probe, not a state setter.
-- Keep the original icon, quantity, cooldown, and all methods on the original
-- button. Only the two anonymous regions created by this addon receive writes.
local regions = setmetatable({}, {__mode = "k"})
local dedicated = setmetatable({}, {__mode = "k"})
local sampleCount = 0
local function usable(button)
return public(button) and button and (not button.IsForbidden or not button:IsForbidden())
end
local function visible(frame)
if not usable(frame) then return false end
local shown = frame:IsVisible()
return public(shown) and shown
end
-- The substitute art must look like the native icon it covers: same crop and the same masks (round buttons such as
-- the profession output icon use a CircleMask; an unmasked overlay showed the square icon under the ring).
local function inherit(art, native)
if type(native.GetTexCoord) == "function" then
local ok, a, b, c, d, e, f, g, h = pcall(native.GetTexCoord, native)
if ok and public(a) and type(a) == "number" then art:SetTexCoord(a, b, c, d, e, f, g, h) end
end
if type(art.GetNumMaskTextures) == "function" then
for i = art:GetNumMaskTextures(), 1, -1 do art:RemoveMaskTexture(art:GetMaskTexture(i)) end
end
if type(native.GetNumMaskTextures) ~= "function" then return end
local ok, n = pcall(native.GetNumMaskTextures, native)
if not ok or not public(n) or type(n) ~= "number" then return end
for i = 1, n do
local mask = native:GetMaskTexture(i)
if mask then art:AddMaskTexture(mask) end
end
end
local function regionFor(button, native)
local current = regions[button]
if current or InCombatLockdown() then return current end
local layer, level = native:GetDrawLayer()
if not public(layer) or type(layer) ~= "string" or not public(level) or type(level) ~= "number" then return end
local art = button:CreateTexture(nil, layer, nil, math.min(level + 1, 7))
art:SetAllPoints(native)
inherit(art, native)
current = {art = art, anchor = native}
regions[button] = current
return current
end
local function hide(overlay)
if not overlay then return end
overlay.art:Hide()
if overlay.count then overlay.count:Hide() end
overlay.id = nil
end
local function paint(button, value, count, isDedicated)
if not usable(button) then return end
if isDedicated then dedicated[button] = true end
local id = identity(value)
local handle = id and texture(id)
local overlay = regions[button]
if not handle then
hide(overlay)
return
end
local native = button.Icon or button.icon or button.IconTexture
if not native and type(button.GetName) == "function" then
local name = button:GetName()
if public(name) and type(name) == "string" then native = _G[name .. "IconTexture"] end
end
if not public(native) or not native then hide(overlay); return end
if button ~= CharacterAmmoSlot and native.IsShown then
local shown = native:IsShown()
if not public(shown) or not shown then hide(overlay); return end
end
if overlay and overlay.anchor ~= native then
hide(overlay)
if InCombatLockdown() then return end
overlay.art:ClearAllPoints()
overlay.art:SetAllPoints(native)
inherit(overlay.art, native)
if overlay.count then
overlay.count:ClearAllPoints()
overlay.count:SetPoint("BOTTOMRIGHT", native, "BOTTOMRIGHT", -1, 1)
end
overlay.anchor = native
end
overlay = overlay or regionFor(button, native)
if not overlay then
if InCombatLockdown() then combatRefresh = true end
return
end
if overlay.id ~= id then
overlay.art:SetTexture(handle)
overlay.id = id
stats.substitutions = stats.substitutions + 1
local observed = overlay.art:GetTexture()
if public(observed) and type(observed) == "number" and (stats.samples[id] or sampleCount < 256) then
if not stats.samples[id] then sampleCount = sampleCount + 1 end
stats.samples[id] = {expected = handle, displayed = observed}
end
end
-- Mirror only public visual state from the stock icon. Cooldown sweeps,
-- stack counts, hotkeys, highlights and quality borders keep their layers.
local r,g,b,a = native:GetVertexColor()
local muted = native:IsDesaturated()
if not public(r) or not public(g) or not public(b) or not public(a) or not public(muted) or type(r) ~= "number" or type(g) ~= "number" or type(b) ~= "number" or type(a) ~= "number" then
hide(overlay)
return
end
overlay.art:SetVertexColor(r,g,b,a)
overlay.art:SetDesaturated(muted)
overlay.art:Show()
-- The stock quantity already draws when the native slot has an item GUID.
-- Our quantity fills the selected-ammo case where its ordinary count is blank.
local nativeCount = count and button.Count
if nativeCount and nativeCount.IsShown and nativeCount.GetText then
local on, text = nativeCount:IsShown(), nativeCount:GetText()
if not public(on) or not public(text) or (on and text and text ~= "") then count = nil end
end
if count and count > 1 then
if not overlay.count and not InCombatLockdown() then
overlay.count = button:CreateFontString(nil, "OVERLAY", "NumberFontNormal")
overlay.count:SetPoint("BOTTOMRIGHT", native, "BOTTOMRIGHT", -1, 1)
end
if overlay.count then overlay.count:SetText(count); overlay.count:Show() end
elseif overlay.count then overlay.count:Hide() end
end
local function inventoryID(slot)
if type(GetInventoryItemID) == "function" then return GetInventoryItemID("player", slot) end
end
local function ammo()
local button = CharacterAmmoSlot
if not usable(button) then return end
local id = inventoryID(0)
if public(id) and not identity(id) and C_TooltipInfo and C_TooltipInfo.GetInventoryItem
and C_Item and C_Item.GetItemInfoInstant then
local ok, info = pcall(C_TooltipInfo.GetInventoryItem, "player", 0)
if ok and public(info) and type(info) == "table" and public(info.type) and info.type == 0 then
local candidate = identity(info.id)
if candidate then
local read, found, _, _, location, _, class, subclass = pcall(C_Item.GetItemInfoInstant, candidate)
if read and public(found) and found == candidate and public(location) and location == "INVTYPE_AMMO"
and public(class) and class == 6 and public(subclass) and (subclass == 2 or subclass == 3) then id = candidate end
end
end
end
local count
if identity(id) and C_Item and C_Item.GetItemCount then
local ok, result = pcall(C_Item.GetItemCount, id, false, false, false)
if ok and public(result) and type(result) == "number" and result >= 0 and result <= 2147483647 and result == math.floor(result) then count = result end
end
paint(button, id, count, true)
end
local equipmentSlots = {"Head","Neck","Shoulder","Shirt","Chest","Waist","Legs","Feet","Wrist","Hands","Finger0","Finger1","Trinket0","Trinket1","Back","MainHand","SecondaryHand","Ranged","Tabard"}
local function equipment()
for _, name in ipairs(equipmentSlots) do
local button = _G["Character" .. name .. "Slot"]
if usable(button) then
local slot = button:GetID()
paint(button, public(slot) and type(slot) == "number" and slot > 0 and inventoryID(slot), nil, true)
end
end
ammo()
end
-- Never call Blizzard helpers that initialize state lazily: the first call from this addon creates
-- that state tainted. ContainerFrameUtil_EnumerateContainerFrames builds its frame list on first use
-- and GetBagSize writes frame.size; either one tainted the bank purchase (C_Bank.PurchaseBankTab
-- blocked, 2026-11-23). Read the container frames and their Items arrays directly instead.
local function containerFrames()
local list = {}
if public(ContainerFrameCombinedBags) and ContainerFrameCombinedBags then list[#list + 1] = ContainerFrameCombinedBags end
local holder = public(ContainerFrameContainer) and ContainerFrameContainer
local frames = holder and rawget(holder, "ContainerFrames")
if type(frames) == "table" then
for i = 1, (NUM_CONTAINER_FRAMES or 13) do
local frame = rawget(frames, i)
if frame then list[#list + 1] = frame end
end
end
return list
end
local function containers()
if C_Container then
for _, frame in ipairs(containerFrames()) do
local items = visible(frame) and rawget(frame, "Items")
if type(items) == "table" then
for _, button in ipairs(items) do
if usable(button) and visible(button) then
local bag, slot = button:GetBagID(), button:GetID()
local info
if public(bag) and type(bag) == "number" and public(slot) and type(slot) == "number" and slot > 0 then
info = C_Container.GetContainerItemInfo(bag, slot)
end
paint(button, public(info) and type(info) == "table" and info.itemID, nil, true)
end
end
end
end
end
for bag = 0, 3 do paint(_G["CharacterBag" .. bag .. "Slot"], inventoryID(20 + bag), nil, true) end
end
local bars = {"ActionButton", "MultiBarBottomLeftButton", "MultiBarBottomRightButton", "MultiBarLeftButton", "MultiBarRightButton", "MultiBar5Button", "MultiBar6Button", "MultiBar7Button"}
local function actions()
if not GetActionInfo then return end
for _, prefix in ipairs(bars) do
for slot = 1, 12 do
local button = _G[prefix .. slot]
if usable(button) then
local action = button.action
local id
if public(action) and type(action) == "number" then
local kind, item = GetActionInfo(action)
if public(kind) and kind == "item" then id = item end
end
paint(button,id,nil,true)
end
end
end
end
local function merchant()
if not visible(MerchantFrame) then return end
local tab, page = MerchantFrame.selectedTab, MerchantFrame.page
local sale = public(tab) and tab == 1
local buyback = public(tab) and tab == 2
local perPage = sale and MERCHANT_ITEMS_PER_PAGE or BUYBACK_ITEMS_PER_PAGE
if not public(perPage) or type(perPage) ~= "number" or perPage < 1 or perPage > 100 then return end
for index = 1, perPage do
local button = _G["MerchantItem" .. index .. "ItemButton"]
if usable(button) then
local item
if visible(button) then
if buyback and GetBuybackItemLink then item = GetBuybackItemLink(index)
elseif sale and public(page) and type(page) == "number" and page >= 1 and GetMerchantItemID then
item = GetMerchantItemID((page - 1) * perPage + index)
end
end
paint(button,item,nil,true)
end
end
end
local function bank()
local panel = BankFrame and BankFrame.BankPanel
if not visible(panel) or not panel.EnumerateValidItems then return end
for button in panel:EnumerateValidItems() do
if usable(button) then
-- Read the live slot: the button's cached itemInfo can still name a
-- just-withdrawn item when this refresh runs (ghost icon, 'needs a
-- second right-click').
local info
local tab = button.GetBankTabID and button:GetBankTabID()
local slot = button.GetContainerSlotID and button:GetContainerSlotID()
if public(tab) and type(tab) == "number" and public(slot) and type(slot) == "number" and slot > 0 then
info = C_Container.GetContainerItemInfo(tab, slot)
else
info = button.itemInfo
end
paint(button,public(info) and type(info) == "table" and info.itemID,nil,true)
end
end
end
local function schematic(form)
if not visible(form) or not form.OutputIcon then return end
local t = form.transaction
local item
if t and C_TradeSkillUI and C_TradeSkillUI.GetRecipeOutputItemData then
local recipe = t:GetRecipeID()
if public(recipe) and type(recipe) == "number" then
local ok, info = pcall(C_TradeSkillUI.GetRecipeOutputItemData, recipe, t:CreateCraftingReagentInfoTbl(), t:GetAllocationItemGUID())
if ok and public(info) and type(info) == "table" then item = info.hyperlink end
end
end
paint(form.OutputIcon,item,nil,true)
end
local function professions()
if not visible(ProfessionsFrame) then return end
local page = ProfessionsFrame.CraftingPage
schematic(page and page.SchematicForm)
local view = ProfessionsFrame.OrdersPage and ProfessionsFrame.OrdersPage.OrderView
local details = view and view.OrderDetails
if details then
schematic(details.SchematicForm)
local fulfillment = details.FulfillmentForm
if visible(fulfillment) then
local order = view.order
local item = public(order) and type(order) == "table" and public(order.isRecraft) and not order.isRecraft and order.outputItemHyperlink
paint(fulfillment.ItemIcon,item,nil,true)
end
end
end
local dirty, generic, scheduled = {}, {}, false
local knownGeneric = setmetatable({}, {__mode = "k"})
local refresh = {bags=containers, gear=equipment, action=actions, shop=merchant, bank=bank, profession=professions, ammo=ammo}
local order = {"bags","gear","ammo","action","shop","bank","profession"}
local function flush()
scheduled = false
local work, items = dirty, generic
dirty, generic = {}, {}
for _, key in ipairs(order) do if work[key] then refresh[key]() end end
for button in pairs(items) do
if usable(button) and not dedicated[button] and not button.IsBackpack and type(button.GetItemID) == "function" then
paint(button,button:GetItemID())
end
end
end
local function queue(group)
if group then dirty[group] = true end
if not scheduled then scheduled = true; C_Timer.After(0, flush) end
end
local function all()
for button in pairs(knownGeneric) do generic[button] = true end
for key in pairs(refresh) do if key ~= "ammo" then dirty[key] = true end end
queue()
end
-- These globals are ordinary completed stock updates. Generic item buttons
-- have no central iterator, and merchant tab/page changes can be local UI
-- operations without a MERCHANT_UPDATE. Observe only the global entry points.
local observed = {}
local function observe(name, callback)
if not observed[name] and type(_G[name]) == "function" then
hooksecurefunc(name, callback)
observed[name] = true
end
end
local function observeGlobals()
observe("SetItemButtonTexture",function(button)
if not usable(button) then return end
if dedicated[button] then
-- #68: the native bag/bank UpdateItems redraws every slot (pooled buttons change bag/slot when a bag
-- is swapped, resized or the combined window opens) without the bag events below; repaint the
-- dedicated surfaces after it, or Turtle icons stay on the slots they had before.
queue("bags"); queue("bank")
elseif not button.IsBackpack and type(button.GetItemID) == "function" then
knownGeneric[button], generic[button] = true, true
queue()
end
end)
if EventRegistry and not observed.containerOpen then
observed.containerOpen = true
EventRegistry:RegisterCallback("ContainerFrame.OpenBag", function() queue("bags") end, observed)
EventRegistry:RegisterCallback("ContainerFrame.OpenAllBags", function() queue("bags") end, observed)
end
observe("MerchantFrame_UpdateMerchantInfo",function() queue("shop") end)
observe("MerchantFrame_UpdateBuybackInfo",function() queue("shop") end)
end
local byEvent = {
BAG_OPEN="bags", BAG_CLOSED="bags", BAG_UPDATE_DELAYED="bags", ITEM_LOCK_CHANGED="bags",
BANKFRAME_OPENED="bank", PLAYERBANKSLOTS_CHANGED="bank", BANK_TABS_CHANGED="bank",
ACTIONBAR_SLOT_CHANGED="action", ACTIONBAR_PAGE_CHANGED="action", UPDATE_BONUS_ACTIONBAR="action",
UPDATE_OVERRIDE_ACTIONBAR="action", UPDATE_SHAPESHIFT_FORM="action", UPDATE_VEHICLE_ACTIONBAR="action",
ACTIONBAR_UPDATE_USABLE="action", ACTIONBAR_UPDATE_STATE="action",
MERCHANT_SHOW="shop", MERCHANT_UPDATE="shop",
TRADE_SKILL_SHOW="profession", TRADE_SKILL_DATA_SOURCE_CHANGED="profession",
TRADE_SKILL_LIST_UPDATE="profession", TRADE_SKILL_DETAILS_UPDATE="profession",
}
local frame = CreateFrame("Frame")
for event in pairs(byEvent) do frame:RegisterEvent(event) end
for _, event in ipairs({"PLAYER_LOGIN","PLAYER_ENTERING_WORLD","ADDON_LOADED","PLAYER_REGEN_ENABLED","PLAYER_EQUIPMENT_CHANGED","GET_ITEM_INFO_RECEIVED"}) do frame:RegisterEvent(event) end
frame:SetScript("OnEvent",function(_,event,value,success)
if event == "GET_ITEM_INFO_RECEIVED" then
if not public(success) or success == false or not public(value) or type(value) ~= "number" or not data.items[value] then return end
if visible(CharacterAmmoSlot) then queue("ammo") end
if visible(ProfessionsFrame) then queue("profession") end
elseif event == "PLAYER_EQUIPMENT_CHANGED" then
if not public(value) or type(value) ~= "number" then queue("gear"); queue("bags"); return end
if value == 0 then queue("ammo")
elseif value >= 20 and value <= 23 then queue("bags")
else queue("gear") end
elseif event == "ADDON_LOADED" then
-- Install observers when Blizzard UI globals appear, but don't scan
-- every item surface for every unrelated add-on loaded at startup.
observeGlobals()
elseif event == "PLAYER_REGEN_ENABLED" then
-- Only rescan after combat if creating addon-owned art was deferred.
if combatRefresh then
combatRefresh = false
all()
end
elseif event == "PLAYER_LOGIN" or event == "PLAYER_ENTERING_WORLD" then
all()
elseif byEvent[event] then
if (event == "BAG_UPDATE_DELAYED" or event == "ITEM_LOCK_CHANGED") and BankFrame and visible(BankFrame.BankPanel) then dirty.bank = true end
queue(byEvent[event])
else
return
end
end)
if EventRegistry then
for _, signal in ipairs({"BankPanelMixin.PageSelected","BankPanelMixin.PageSwitched","BankPanelMixin.PageInfoChanged"}) do
EventRegistry:RegisterCallback(signal,function() queue("bank") end,frame)
end
EventRegistry:RegisterCallback("ProfessionsRecipeListMixin.Event.OnRecipeSelected",function() queue("profession") end,frame)
end
observeGlobals()
all()