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 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 if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier :: buffer) Identifier = Buffer.convert(Identifier :: buffer)
local read = Buffer.read(data) local read = Buffer.read(data, ref)
if not read then return end if not read then return end
for idx: string in registeredIdentifier do for idx: string in registeredIdentifier do
if idx ~= Identifier then continue end if idx ~= Identifier then continue end

View file

@ -303,10 +303,10 @@ function ServerProcess.start()
end end
end 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 if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier :: buffer) Identifier = Buffer.convert(Identifier :: buffer)
local read = Buffer.read(data) local read = Buffer.read(data, ref)
if not read then return end if not read then return end
for idx: string in registeredIdentifier do for idx: string in registeredIdentifier do
if idx ~= Identifier then continue end if idx ~= Identifier then continue end

View file

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

View file

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