rewrite buffer instance type serialization for fix issues

This commit is contained in:
EternityDev 2024-11-23 19:50:06 +07:00
parent 91862a65fe
commit 677d3fa675
4 changed files with 34 additions and 25 deletions

View file

@ -230,10 +230,10 @@ function ClientProcess.start()
end
end
end)
local function onClientNetworkReceive(Identifier: buffer | string, data: buffer)
local function onClientNetworkReceive(Identifier: buffer | string, data: buffer, ref: { any }?)
if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier :: buffer)
local read = Buffer.read(data)
local read = Buffer.read(data, ref)
if not read then return end
for idx: string in registeredIdentifier do
if idx ~= Identifier then continue end

View file

@ -303,10 +303,10 @@ function ServerProcess.start()
end
end
end)
local function onServerNetworkReceive(player: Player, Identifier: buffer | string, data: buffer)
local function onServerNetworkReceive(player: Player, Identifier: buffer | string, data: buffer, ref: { any }?)
if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier :: buffer)
local read = Buffer.read(data)
local read = Buffer.read(data, ref)
if not read then return end
for idx: string in registeredIdentifier do
if idx ~= Identifier then continue end

View file

@ -56,13 +56,15 @@ function DedicatedBuffer.build(self: any): buffer
return build
end
function DedicatedBuffer.buildAndRemove(self: any): buffer
function DedicatedBuffer.buildAndRemove(self: any): (buffer, (any)?)
local p: number = self.next > self.point and self.next or self.point
local build: buffer = create(p)
local ref = #self.ref > 0 and table.clone(self.ref) or nil
copy(build, 0, self.buffer, 0, p)
self:remove()
return build
return build, ref
end
function DedicatedBuffer.wi8(self: any, val: number, alloc: number?)
@ -124,14 +126,20 @@ function DedicatedBuffer.wType(self: any, ref: number)
self.point += 1
end
function DedicatedBuffer.wRef(self: any, value: any, alloc: number?)
self:alloc(alloc or 1)
table.insert(self.ref, value)
local index = #self.ref
writeu8(self.buffer, self.point, index)
self.point += 1
end
function DedicatedBuffer.pack(self: any, data: {any})
if typeof(data) == "nil" then
self:wi8(0)
elseif typeof(data) == "Instance" then
self:wi8(-1) -- Instance marker
self:pack(data.ClassName) -- Serialize ClassName
self:pack(data.Name) -- Serialize Name
self:pack(data:GetAttributes())
self:wRef(data)
elseif typeof(data) == "table" then
--local isArray = (next(data) ~= nil and #data > 0) and true or false
local isArray = true
@ -232,6 +240,7 @@ function DedicatedBuffer.flush(self: any)
self.next = default.next
self.size = default.size
self.buffer = create(default.bufferSize)
table.clear(self.ref)
end
function DedicatedBuffer.new()
@ -239,7 +248,8 @@ function DedicatedBuffer.new()
point = default.point,
next = default.next,
size = default.size,
buffer = create(default.bufferSize)
buffer = create(default.bufferSize),
ref = {},
}, DedicatedBuffer)
end

View file

@ -18,28 +18,27 @@ local readf64 = buffer.readf64
local readstring = buffer.readstring
local len = buffer.len
local function readValue(b: buffer, position: number): (any, number)
local function readValue(b: buffer, position: number, ref: { any }?): (any, number)
local typeByte = readi8(b, position)
position += 1
if typeByte == 0 then -- nil
return nil, position
elseif typeByte == -1 then -- Instance
local className, position1 = readValue(b, position)
local name, position2 = readValue(b, position1)
local properties, position3 = readValue(b, position2)
local instance: Instance = Instance.new(className)
instance.Name = name
for key, value in properties do
instance:SetAttribute(key, value)
if not ref or #ref == 0 then
return nil, position + 1
end
return instance, position3
local value = table.remove(ref, readu8(b, position))
if typeof(value) == "Instance" then
return value, position + 1
end
return nil, position + 1
elseif typeByte == -2 then -- array
local length = readu16(b, position)
position += 2
local array = {}
for _ = 1, length do
local value
value, position = readValue(b, position)
value, position = readValue(b, position, ref)
table.insert(array, value)
end
return array, position
@ -49,8 +48,8 @@ local function readValue(b: buffer, position: number): (any, number)
local dict = {}
for _ = 1, length do
local key, value
key, position = readValue(b, position)
value, position = readValue(b, position)
key, position = readValue(b, position, ref)
value, position = readValue(b, position, ref)
dict[key] = value
end
return dict, position
@ -129,18 +128,18 @@ function Buffer.revert(s: string): buffer
return fromstring(s)
end
function Buffer.write(data: { any }): buffer
function Buffer.write(data: { any }): (buffer, (any)?)
local newBuffer = Dedicated()
newBuffer:pack(data)
return newBuffer:buildAndRemove()
end
function Buffer.read(b: buffer): any?
function Buffer.read(b: buffer, ref: { any }?): any?
local position = 0
local result = {}
while position < len(b) do
local value
value, position = readValue(b, position)
value, position = readValue(b, position, ref)
table.insert(result, value)
end
return table.unpack(result)