Files
WoW-Hybrid-gateway/ops/native_loot_roll_cache.lua

78 lines
3.3 KiB
Lua

-- Loaded only as part of the original native GroupLootFrame Lua child.
-- An addon or an insecure callback must never redispatch into managed layout.
if not issecure or not issecure() then return end
-- Native GroupLootFrame_OnShow removes a frame when GetLootRollItemInfo has
-- no name yet. Recover only those real START_LOOT_ROLL events, within their
-- original deadline. No synthetic rolls, votes, corpse bindings or item data.
if not GetLootRollItemInfo or not GetLootRollTimeLeft or not C_Timer then return end
local pending, pendingCount = {}, 0
local scheduled, loading = false, false
local frame = CreateFrame("Frame")
local function Forget(id)
if pending[id] then pending[id] = nil; pendingCount = pendingCount - 1 end
end
local function OwnedByNative(id)
local container = GroupLootContainer
if not container then return false end
for _, roll in pairs(container.waitingRolls or {}) do
if roll.rollID == id then return true end
end
for _, roll in pairs(container.rollFrames or {}) do
if roll.rollID == id and roll:IsShown() then return true end
end
return false
end
local Retry
local function Schedule()
if not scheduled and pendingCount > 0 then
scheduled = true
C_Timer.After(0.25, function() scheduled = false; Retry() end)
end
end
Retry = function()
for id, deadline in pairs(pending) do
local left = math.min((deadline - GetTime()) * 1000, GetLootRollTimeLeft(id) or 0)
local _, name = GetLootRollItemInfo(id)
local nativeOwned = OwnedByNative(id)
if left <= 0 or (nativeOwned and name) then
Forget(id)
elseif not nativeOwned and not loading and GroupLootContainer and GroupLootContainer_AddRoll then
if name then
-- Remove first: native show/cancel callbacks may run synchronously.
Forget(id)
if issecure() then GroupLootContainer_AddRoll(id, left) end
end
end
end
Schedule()
end
frame:SetScript("OnEvent", function(_, event, id, duration)
if event == "START_LOOT_ROLL" then
local _, name = GetLootRollItemInfo(id)
if not name and type(duration) == "number" and duration > 0 and duration <= 60000
and not pending[id] and pendingCount < 128 then
pending[id] = GetTime() + duration / 1000
pendingCount = pendingCount + 1
Schedule()
end
elseif event == "CANCEL_LOOT_ROLL" then
Forget(id)
elseif event == "CANCEL_ALL_LOOT_ROLLS" or event == "PLAYER_LEAVING_WORLD" then
pending, pendingCount = {}, 0
elseif event == "LOADING_SCREEN_ENABLED" then
loading = true
elseif event == "LOADING_SCREEN_DISABLED" or event == "PLAYER_ENTERING_WORLD" then
loading = false
Schedule()
else
Schedule() -- Item-data callbacks coalesce into the same bounded retry.
end
end)
for _, event in ipairs({"START_LOOT_ROLL", "CANCEL_LOOT_ROLL", "CANCEL_ALL_LOOT_ROLLS",
"PLAYER_LEAVING_WORLD", "PLAYER_ENTERING_WORLD", "LOADING_SCREEN_ENABLED",
"LOADING_SCREEN_DISABLED", "GET_ITEM_INFO_RECEIVED", "ITEM_DATA_LOAD_RESULT"}) do
frame:RegisterEvent(event)
end
-- A legitimate vote may be issued by another addon before our pending item loads.
if hooksecurefunc and RollOnLoot then hooksecurefunc("RollOnLoot", Forget) end