Warp/src/Index/Util/Serdes.luau

34 lines
1.2 KiB
Lua
Raw Normal View History

2024-01-05 12:14:38 +00:00
--!strict
local RunService = game:GetService("RunService")
local SerInt = 0
local Event = require(script.Parent.Parent.Event).Reliable
local Assert = require(script.Parent.Assert)
2024-04-02 06:10:21 +00:00
return function(Identifier: string, timeout: number?): number
2024-01-05 12:14:38 +00:00
Assert(typeof(Identifier) == "string", "Identifier must be a string type.")
Assert(SerInt < 255, "reached max 255 identifiers.")
if RunService:IsServer() then
if not Event:GetAttribute(Identifier) then
SerInt += 1
2024-03-13 01:01:27 +00:00
Event:SetAttribute(Identifier, SerInt)
2024-04-02 06:10:21 +00:00
--Event:SetAttribute(Identifier, string.pack("I1", SerInt)) -- I1 -> 255 max, I2 -> ~ 6.5e4 max. (SerInt), removed/disabled for buffer migration.
2024-01-05 12:14:38 +00:00
end
else
2024-05-04 05:49:50 +00:00
local yieldThread: thread = coroutine.running()
local cancel = task.delay(timeout or 10, function() -- yield cancelation (timerout)
task.spawn(yieldThread, nil)
2024-04-02 06:10:21 +00:00
error(`Serdes: {Identifier} is taking too long to retrieve, seems like not replicated on server.`, 2)
end)
2024-05-04 05:49:50 +00:00
while coroutine.status(cancel) ~= "dead" and task.wait(0.5) do -- let it loop for yields!
if (Event:GetAttribute(Identifier)) then
task.cancel(cancel)
task.spawn(yieldThread, Event:GetAttribute(Identifier))
break
end
2024-01-05 12:14:38 +00:00
end
2024-05-04 05:49:50 +00:00
return coroutine.yield() -- yield
2024-01-05 12:14:38 +00:00
end
return Event:GetAttribute(Identifier)
2024-05-04 05:49:50 +00:00
end