fix(events): forward leading-nil event payloads to callbacks

EventRegistry callbacks (RegisterOnceFrameEventAndCallback, etc.) dropped any
payload whose first arg was nil — e.g. this client's reload PLAYER_ENTERING_WORLD
(arg1=nil, arg2=1) delivered nil to every callback parameter. Root cause is a
WoW Lua 5.0 quirk: unpacking a table whose hole lands right after a present
element into a `func(first, ...)` collapses the receiving vararg count to 0, so
the payload after `owner` was silently lost.

Fix both build-then-unpack sites in CallbackRegistry to pass the leading arg
explicitly and the payload as the unpacked tail:
- TriggerEvent Function branch: func(owner, unpack(arg, 1, arg.n))
- GenerateClosure: func(bound[1], unpack(rest, 1, restN))

Verified in-game: RegisterOnceFrameEventAndCallback("PLAYER_ENTERING_WORLD", cb)
now delivers arg2=1 to the callback's 2nd parameter on reload.
This commit is contained in:
Brues
2026-08-03 14:23:12 -05:00
parent e0e4fbcf84
commit a0d8d6d723
+19 -9
View File
@@ -26,10 +26,17 @@ local function GenerateClosure(func, ...)
for i = 1, boundN do bound[i] = arg[i] end
return function(...)
local callN = arg.n
local merged = {}
for i = 1, boundN do merged[i] = bound[i] end
for i = 1, callN do merged[boundN + i] = arg[i] end
return func(unpack(merged, 1, boundN + callN))
-- Pass the first bound arg (the owner, always bound[1] here) explicitly
-- and everything else as the unpacked tail. Merging bound + call into
-- one table and unpacking THAT collapses the vararg count on this Lua
-- 5.0 build when a hole lands right after a present element — same trap
-- fixed in TriggerEvent's Function branch. Keeping owner out of the
-- unpacked table keeps a leading-nil payload inside the vararg tail.
local rest = {}
local restN = 0
for i = 2, boundN do restN = restN + 1; rest[restN] = bound[i] end
for i = 1, callN do restN = restN + 1; rest[restN] = arg[i] end
return func(bound[1], unpack(rest, 1, restN))
end
end
@@ -145,11 +152,14 @@ function CallbackRegistryMixin:TriggerEvent(event, ...)
if funcs then
for owner, func in pairs(funcs) do
-- Stock 3.3.5 passes the owner as the first arg so callers can
-- distinguish multiple registrations of the same function.
local n = arg.n
local merged = { owner }
for i = 1, n do merged[i + 1] = arg[i] end
func(unpack(merged, 1, n + 1))
-- distinguish multiple registrations of the same function. Pass it
-- explicitly with the payload as the unpacked tail. Do NOT build a
-- { owner, ... } table and unpack THAT: on this Lua 5.0 build,
-- unpacking a table whose hole lands right after a present element
-- (owner) into a `func(first, ...)` collapses the vararg count to 0,
-- silently dropping a leading-nil event payload (verified in-game
-- with reload's PLAYER_ENTERING_WORLD, arg1=nil arg2=1).
func(owner, unpack(arg, 1, arg.n))
end
end
end