Files

134 lines
7.2 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Display only: receipt metadata requires genuine Source delivery and owned
-- inventory linkage; roll metadata requires actual results and a bound terminal.
-- This file never rolls, grants or moves items.
local pending, seen, order, requested = {}, {}, {}, {}
local function public(value)
return type(issecretvalue) ~= "function" or not issecretvalue(value)
end
local function nonempty(value)
return public(value) and type(value) == "string" and value:find("%S") ~= nil
end
local function readyLink(value, item)
if not public(value) or type(value) ~= "string" or tonumber(value:match("|Hitem:(%d+)")) ~= item then return end
local label = value:match("|h(.-)|h")
if type(label) ~= "string" then return end
-- Original native links can contain the genuine item ID before its name
-- is available. Consuming that receipt would permanently print []. Keep
-- the original link, including instance/enchant/property fields, unchanged.
label = label:match("^%[(.*)%]$") or label
if nonempty(label) then return value end
end
local function display(row)
local link, pendingInstance
if row.kind == "R" and C_Container and type(C_Container.GetContainerItemLink) == "function" then
local candidate = C_Container.GetContainerItemLink(row.bag, row.slot)
if public(candidate) and type(candidate) == "string" and tonumber(candidate:match("|Hitem:(%d+)")) == row.item then
link = readyLink(candidate, row.item)
-- A base-item fallback must not replace a still-pending real
-- container instance with a different random-property context.
pendingInstance = not link
end
end
if not link and not pendingInstance then
local get = C_Item and C_Item.GetItemInfo or GetItemInfo
if type(get) == "function" then
local name, candidate = get(row.item)
if nonempty(name) then link = readyLink(candidate, row.item) end
end
end
local template
if row.kind == "N" then template = row.choice == 1 and LOOT_ROLL_ROLLED_NEED or LOOT_ROLL_ROLLED_GREED
elseif row.kind == "W" then template = row.isSelf and LOOT_ROLL_YOU_WON or LOOT_ROLL_WON
else template = row.count == 1 and LOOT_ITEM_SELF or LOOT_ITEM_SELF_MULTIPLE end
if type(link) ~= "string" or type(template) ~= "string" then
return false
end
-- Original70334 localized roll strings begin with a lootHistory link whose
-- hidden %d consumes a separate native history ID. Source outcomes have no
-- native history entry: retain its localized label without fabricating a link.
if row.kind == "N" or row.kind == "W" then
template = template:gsub("^|HlootHistory:%%d|h(.-)|h", "%1")
end
local message
if row.kind == "N" then message = string.format(template, row.number, link, row.player)
elseif row.kind == "W" then message = row.isSelf and string.format(template, link) or string.format(template, row.player, link)
else message = row.count == 1 and string.format(template, link) or string.format(template, link, row.count) end
-- Exact70334 original route: ContainsMessageGroup selects LOOT subscribers;
-- MessageEventHandler retains their filters, colors and native formatting.
for _, name in ipairs(CHAT_FRAMES or {"ChatFrame1"}) do
local frame = _G[name]
if frame and type(frame.ContainsMessageGroup) == "function" and frame:ContainsMessageGroup("LOOT") and type(frame.MessageEventHandler) == "function" then
frame:MessageEventHandler("CHAT_MSG_LOOT", message, "", "", "", "", "", 0, 0, "", 0, 0, "", 0, false, false, false, false, {})
end
end
return true
end
local function flush()
for _, key in ipairs(order) do local row = pending[key]; if row and display(row) then pending[key] = nil end end
end
local scheduled = false
local function revisitAfterEvent()
-- Native metadata callbacks may precede the last cache/link update in the
-- current event. Revisit once on the next frame, never poll or invent data.
if scheduled or not next(pending) or not C_Timer or type(C_Timer.After) ~= "function" then return end
scheduled = true
C_Timer.After(0, function() scheduled = false; flush() end)
end
local function receive(message)
if type(message) ~= "string" then return end
local key, item, count, bag, slot = message:match("^GWLOOT1 R (%d+:%d+) (%d+) (%d+) (%d+) (%d+)$")
local row
if key then
if #key > 48 then return end
item, count, bag, slot = tonumber(item), tonumber(count), tonumber(bag), tonumber(slot)
if not item or item < 1 or item > 4294967295 or count < 1 or count > 4294967295 or bag > 4 or slot < 1 or slot > 36 then return end
row = {kind = "R", item = item, count = count, bag = bag, slot = slot}; key = "R:" .. key
else
local kind, number, choice, isSelf, player
kind, key, item, number, choice, isSelf, player = message:match("^GWLOOT1 ([NW]) (%d+:%d+:%d+:%d+) (%d+) (%d+) (%d+) ([01]) (.+)$")
if not key or #key > 96 or #player > 48 or player:find("[%c|]") then return end
item, number, choice = tonumber(item), tonumber(number), tonumber(choice)
if not item or item < 1 or item > 4294967295 or number < 1 or number > 100 or (choice ~= 1 and choice ~= 2) then return end
row = {kind = kind, item = item, number = number, choice = choice, isSelf = isSelf == "1", player = player}; key = kind .. ":" .. key
end
if seen[key] then return end
seen[key] = true; order[#order + 1] = key
if #order > 128 then local old = table.remove(order, 1); seen[old] = nil; pending[old] = nil end
if display(row) then return end
pending[key] = row
if not requested[item] and C_Item and type(C_Item.RequestLoadItemDataByID) == "function" then
requested[item] = true
C_Item.RequestLoadItemDataByID(item)
end
end
local driver = CreateFrame("Frame")
driver:RegisterEvent("CHAT_MSG_SYSTEM")
driver:RegisterEvent("GET_ITEM_INFO_RECEIVED")
driver:RegisterEvent("ITEM_DATA_LOAD_RESULT")
driver:RegisterEvent("BAG_UPDATE_DELAYED")
driver:SetScript("OnEvent", function(_, event, message)
if event == "CHAT_MSG_SYSTEM" then
receive(message)
return
end
-- A fresh receipt may request metadata again after a failed load or cache
-- eviction. The completion event itself only revisits existing receipts.
if (event == "ITEM_DATA_LOAD_RESULT" or event == "GET_ITEM_INFO_RECEIVED") and public(message) and type(message) == "number" then
requested[message] = nil
end
flush()
revisitAfterEvent()
end)
local filter = ChatFrameUtil and ChatFrameUtil.AddMessageEventFilter or ChatFrame_AddMessageEventFilter
if type(filter) == "function" then
-- Guild controls receive their state on a separate event frame, including
-- before the load-on-demand guild UI has registered its own chat filter.
filter("CHAT_MSG_SYSTEM", function(_, _, message)
if not public(message) or type(message) ~= "string" then return false end
return message:match("^GWLOOT1 ") ~= nil or message:match("^GWTURTLE1 GUILD ") ~= nil
end)
end
return {receive = receive, flush = flush}