From 58b32e76696d63fa7cb55a63fd5fc3478a4bcc26 Mon Sep 17 00:00:00 2001 From: ffrostfall <80861876+ffrostflame@users.noreply.github.com> Date: Wed, 6 Mar 2024 20:40:57 -0500 Subject: [PATCH] Fixes & type fixes --- CHANGELOG.md | 9 ++++++++- dev/server/serverTests.server.luau | 8 +++----- dev/shared/testPackets.luau | 7 ++----- sourcemap.json | 2 +- src/dataTypes/inst.luau | 9 ++++++--- src/dataTypes/unknown.luau | 9 ++++++--- src/process/bufferWriter.luau | 1 + src/process/client.luau | 20 ++++++++++++-------- src/process/read.luau | 5 ++++- src/process/readRefs.luau | 13 +++++++++++++ src/process/server.luau | 18 +++++++++++------- src/types.luau | 5 ++++- 12 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 src/process/readRefs.luau diff --git a/CHANGELOG.md b/CHANGELOG.md index 0000dbd..126e71e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,14 @@ ByteNet uses [semantic versioning](https://semver.org/spec/v2.0.0.html). ### Improvements -- +- Arrays are now forced to have number indexes + +### Fixes + +- Added all of the new data types to the ByteNet type. +- Fixed the client not clearing instance references +- Fixed the client sending a buffer every frame +- Fixed instances/unknowns not being sendable in any special type --- diff --git a/dev/server/serverTests.server.luau b/dev/server/serverTests.server.luau index bd5c435..ebeb835 100644 --- a/dev/server/serverTests.server.luau +++ b/dev/server/serverTests.server.luau @@ -33,19 +33,17 @@ RunService.Heartbeat:Connect(function() ReplicatedStorage.RemoteEvent:FireAllClients(data) end]] - debug.profilebegin("send") + --[[debug.profilebegin("send") for _ = 1, 100 do --ReplicatedStorage.RemoteEvent:FireAllClients(out) testPackets.myPacket.sendToAll(out) end - debug.profileend() + debug.profileend()]] end) Players.PlayerAdded:Connect(function() task.wait(1) - testPackets.myPacket.sendToAll({ - myMessage = "", - }) + testPackets.myPacket.sendToAll() end) testPackets.myPacket.listen(function(a) diff --git a/dev/shared/testPackets.luau b/dev/shared/testPackets.luau index 2486a5b..68e8c8e 100644 --- a/dev/shared/testPackets.luau +++ b/dev/shared/testPackets.luau @@ -3,13 +3,10 @@ local ReplicatedStorage = game:GetService("ReplicatedStorage") local ByteNet = require(ReplicatedStorage.Packages.ByteNet) -- return ByteNet.defineNamespace("game", function() - local myStruct = ByteNet.struct({ - a = ByteNet.inst, - }) - return { myPacket = ByteNet.definePacket({ - value = ByteNet.array(myStruct), + value = ByteNet.nothing, + reliabilityType = "reliable", }), } end) diff --git a/sourcemap.json b/sourcemap.json index 394ae2f..20fa228 100644 --- a/sourcemap.json +++ b/sourcemap.json @@ -1 +1 @@ -{"name":"bytenet-dev","className":"DataModel","filePaths":["dev.project.json"],"children":[{"name":"ReplicatedStorage","className":"ReplicatedStorage","children":[{"name":"Packages","className":"Folder","children":[{"name":"ByteNet","className":"ModuleScript","filePaths":["src\\init.luau"],"children":[{"name":"dataTypes","className":"Folder","children":[{"name":"array","className":"ModuleScript","filePaths":["src\\dataTypes\\array.luau"]},{"name":"bool","className":"ModuleScript","filePaths":["src\\dataTypes\\bool.luau"]},{"name":"buff","className":"ModuleScript","filePaths":["src\\dataTypes\\buff.luau"]},{"name":"cframe","className":"ModuleScript","filePaths":["src\\dataTypes\\cframe.luau"]},{"name":"float32","className":"ModuleScript","filePaths":["src\\dataTypes\\float32.luau"]},{"name":"float64","className":"ModuleScript","filePaths":["src\\dataTypes\\float64.luau"]},{"name":"inst","className":"ModuleScript","filePaths":["src\\dataTypes\\inst.luau"]},{"name":"int16","className":"ModuleScript","filePaths":["src\\dataTypes\\int16.luau"]},{"name":"int32","className":"ModuleScript","filePaths":["src\\dataTypes\\int32.luau"]},{"name":"int8","className":"ModuleScript","filePaths":["src\\dataTypes\\int8.luau"]},{"name":"map","className":"ModuleScript","filePaths":["src\\dataTypes\\map.luau"]},{"name":"nothing","className":"ModuleScript","filePaths":["src\\dataTypes\\nothing.luau"]},{"name":"optional","className":"ModuleScript","filePaths":["src\\dataTypes\\optional.luau"]},{"name":"string","className":"ModuleScript","filePaths":["src\\dataTypes\\string.luau"]},{"name":"struct","className":"ModuleScript","filePaths":["src\\dataTypes\\struct.luau"]},{"name":"uint16","className":"ModuleScript","filePaths":["src\\dataTypes\\uint16.luau"]},{"name":"uint32","className":"ModuleScript","filePaths":["src\\dataTypes\\uint32.luau"]},{"name":"uint8","className":"ModuleScript","filePaths":["src\\dataTypes\\uint8.luau"]},{"name":"unknown","className":"ModuleScript","filePaths":["src\\dataTypes\\unknown.luau"]},{"name":"vec2","className":"ModuleScript","filePaths":["src\\dataTypes\\vec2.luau"]},{"name":"vec3","className":"ModuleScript","filePaths":["src\\dataTypes\\vec3.luau"]}]},{"name":"namespaces","className":"Folder","children":[{"name":"namespace","className":"ModuleScript","filePaths":["src\\namespaces\\namespace.luau"]},{"name":"namespacesDependencies","className":"ModuleScript","filePaths":["src\\namespaces\\namespacesDependencies.luau"]},{"name":"packetIDs","className":"ModuleScript","filePaths":["src\\namespaces\\packetIDs.luau"]}]},{"name":"packets","className":"Folder","children":[{"name":"definePacket","className":"ModuleScript","filePaths":["src\\packets\\definePacket.luau"]},{"name":"packet","className":"ModuleScript","filePaths":["src\\packets\\packet.luau"]}]},{"name":"process","className":"Folder","children":[{"name":"bufferWriter","className":"ModuleScript","filePaths":["src\\process\\bufferWriter.luau"]},{"name":"client","className":"ModuleScript","filePaths":["src\\process\\client.luau"]},{"name":"read","className":"ModuleScript","filePaths":["src\\process\\read.luau"]},{"name":"server","className":"ModuleScript","filePaths":["src\\process\\server.luau"]}]},{"name":"replicated","className":"Folder","children":[{"name":"replicatedValue","className":"ModuleScript","filePaths":["src\\replicated\\replicatedValue.luau"]},{"name":"values","className":"ModuleScript","filePaths":["src\\replicated\\values.luau"]}]},{"name":"types","className":"ModuleScript","filePaths":["src\\types.luau"]}]}]},{"name":"shared","className":"Folder","children":[{"name":"testPackets","className":"ModuleScript","filePaths":["dev/shared\\testPackets.luau"]}]}]},{"name":"ServerScriptService","className":"ServerScriptService","children":[{"name":"server","className":"Folder","children":[{"name":"serverTests","className":"Script","filePaths":["dev/server\\serverTests.server.luau"]}]}]},{"name":"StarterPlayer","className":"StarterPlayer","children":[{"name":"StarterPlayerScripts","className":"StarterPlayerScripts","children":[{"name":"clientTests","className":"LocalScript","filePaths":["dev/client\\clientTests.client.luau"]}]}]}]} \ No newline at end of file +{"name":"bytenet-dev","className":"DataModel","filePaths":["dev.project.json"],"children":[{"name":"ReplicatedStorage","className":"ReplicatedStorage","children":[{"name":"Packages","className":"Folder","children":[{"name":"ByteNet","className":"ModuleScript","filePaths":["src\\init.luau"],"children":[{"name":"dataTypes","className":"Folder","children":[{"name":"array","className":"ModuleScript","filePaths":["src\\dataTypes\\array.luau"]},{"name":"bool","className":"ModuleScript","filePaths":["src\\dataTypes\\bool.luau"]},{"name":"buff","className":"ModuleScript","filePaths":["src\\dataTypes\\buff.luau"]},{"name":"cframe","className":"ModuleScript","filePaths":["src\\dataTypes\\cframe.luau"]},{"name":"float32","className":"ModuleScript","filePaths":["src\\dataTypes\\float32.luau"]},{"name":"float64","className":"ModuleScript","filePaths":["src\\dataTypes\\float64.luau"]},{"name":"inst","className":"ModuleScript","filePaths":["src\\dataTypes\\inst.luau"]},{"name":"int16","className":"ModuleScript","filePaths":["src\\dataTypes\\int16.luau"]},{"name":"int32","className":"ModuleScript","filePaths":["src\\dataTypes\\int32.luau"]},{"name":"int8","className":"ModuleScript","filePaths":["src\\dataTypes\\int8.luau"]},{"name":"map","className":"ModuleScript","filePaths":["src\\dataTypes\\map.luau"]},{"name":"nothing","className":"ModuleScript","filePaths":["src\\dataTypes\\nothing.luau"]},{"name":"optional","className":"ModuleScript","filePaths":["src\\dataTypes\\optional.luau"]},{"name":"string","className":"ModuleScript","filePaths":["src\\dataTypes\\string.luau"]},{"name":"struct","className":"ModuleScript","filePaths":["src\\dataTypes\\struct.luau"]},{"name":"uint16","className":"ModuleScript","filePaths":["src\\dataTypes\\uint16.luau"]},{"name":"uint32","className":"ModuleScript","filePaths":["src\\dataTypes\\uint32.luau"]},{"name":"uint8","className":"ModuleScript","filePaths":["src\\dataTypes\\uint8.luau"]},{"name":"unknown","className":"ModuleScript","filePaths":["src\\dataTypes\\unknown.luau"]},{"name":"vec2","className":"ModuleScript","filePaths":["src\\dataTypes\\vec2.luau"]},{"name":"vec3","className":"ModuleScript","filePaths":["src\\dataTypes\\vec3.luau"]}]},{"name":"namespaces","className":"Folder","children":[{"name":"namespace","className":"ModuleScript","filePaths":["src\\namespaces\\namespace.luau"]},{"name":"namespacesDependencies","className":"ModuleScript","filePaths":["src\\namespaces\\namespacesDependencies.luau"]},{"name":"packetIDs","className":"ModuleScript","filePaths":["src\\namespaces\\packetIDs.luau"]}]},{"name":"packets","className":"Folder","children":[{"name":"definePacket","className":"ModuleScript","filePaths":["src\\packets\\definePacket.luau"]},{"name":"packet","className":"ModuleScript","filePaths":["src\\packets\\packet.luau"]}]},{"name":"process","className":"Folder","children":[{"name":"bufferWriter","className":"ModuleScript","filePaths":["src\\process\\bufferWriter.luau"]},{"name":"client","className":"ModuleScript","filePaths":["src\\process\\client.luau"]},{"name":"read","className":"ModuleScript","filePaths":["src\\process\\read.luau"]},{"name":"readRefs","className":"ModuleScript","filePaths":["src\\process\\readRefs.luau"]},{"name":"server","className":"ModuleScript","filePaths":["src\\process\\server.luau"]}]},{"name":"replicated","className":"Folder","children":[{"name":"replicatedValue","className":"ModuleScript","filePaths":["src\\replicated\\replicatedValue.luau"]},{"name":"values","className":"ModuleScript","filePaths":["src\\replicated\\values.luau"]}]},{"name":"types","className":"ModuleScript","filePaths":["src\\types.luau"]}]}]},{"name":"shared","className":"Folder","children":[{"name":"testPackets","className":"ModuleScript","filePaths":["dev/shared\\testPackets.luau"]}]}]},{"name":"ServerScriptService","className":"ServerScriptService","children":[{"name":"server","className":"Folder","children":[{"name":"serverTests","className":"Script","filePaths":["dev/server\\serverTests.server.luau"]}]}]},{"name":"StarterPlayer","className":"StarterPlayer","children":[{"name":"StarterPlayerScripts","className":"StarterPlayerScripts","children":[{"name":"clientTests","className":"LocalScript","filePaths":["dev/client\\clientTests.client.luau"]}]}]}]} \ No newline at end of file diff --git a/src/dataTypes/inst.luau b/src/dataTypes/inst.luau index 895e6aa..c536d59 100644 --- a/src/dataTypes/inst.luau +++ b/src/dataTypes/inst.luau @@ -1,4 +1,5 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter) +local readRefs = require(script.Parent.Parent.process.readRefs) local types = require(script.Parent.Parent.types) local reference = bufferWriter.reference @@ -11,12 +12,14 @@ return function(): types.dataTypeInterface reference(value) end, - read = function(b: buffer, cursor: number, references) - if not references then + read = function(b: buffer, cursor: number) + local refs = readRefs.get() + + if not refs then return nil, 1 end - local ref = references[buffer.readu8(b, cursor)] + local ref = refs[buffer.readu8(b, cursor)] if typeof(ref) == "Instance" then return ref, 1 diff --git a/src/dataTypes/unknown.luau b/src/dataTypes/unknown.luau index fb87fc9..9842fb2 100644 --- a/src/dataTypes/unknown.luau +++ b/src/dataTypes/unknown.luau @@ -1,4 +1,5 @@ local bufferWriter = require(script.Parent.Parent.process.bufferWriter) +local readRefs = require(script.Parent.Parent.process.readRefs) local types = require(script.Parent.Parent.types) local reference = bufferWriter.reference @@ -11,12 +12,14 @@ return function(): types.dataTypeInterface reference(value) end, - read = function(b: buffer, cursor: number, references) - if not references then + read = function(b: buffer, cursor: number) + local refs = readRefs.get() + + if not refs then return nil, 1 end - return references[buffer.readu8(b, cursor)], 1 + return refs[buffer.readu8(b, cursor)], 1 end, } end diff --git a/src/process/bufferWriter.luau b/src/process/bufferWriter.luau index 3fca382..e5a715c 100644 --- a/src/process/bufferWriter.luau +++ b/src/process/bufferWriter.luau @@ -70,6 +70,7 @@ function bufferWriter.reference(value: any) buffer.writeu8(buff, cursor, index) cursor += 1 + print(references) end function bufferWriter.u16(value: number) diff --git a/src/process/client.luau b/src/process/client.luau index 91544ab..5292686 100644 --- a/src/process/client.luau +++ b/src/process/client.luau @@ -9,8 +9,8 @@ local alloc = bufferWriter.alloc local u8 = bufferWriter.u8 local load = bufferWriter.load -local function onClientEvent(receivedBuffer) - read(receivedBuffer) +local function onClientEvent(receivedBuffer, ref) + read(receivedBuffer, ref) end -- Shared with: src/process/server.luau (Infeasible to split this into another file) @@ -23,13 +23,13 @@ local function create() } end -local function dump(channel: types.channelData): buffer +local function dump(channel: types.channelData): (buffer, { unknown }?) local cursor = channel.cursor local dumpBuffer = buffer.create(cursor) buffer.copy(dumpBuffer, 0, channel.buff, 0, cursor) - return dumpBuffer, channel.references + return dumpBuffer, if #channel.references > 0 then channel.references else nil end -- No longer shared @@ -67,17 +67,21 @@ function clientProcess.start() RunService.Heartbeat:Connect(function() -- Again, checking if there's anything in the channel before we send it. - if reliable ~= nil then - reliableRemote:FireServer(dump(reliable)) + if reliable.cursor > 0 then + local b, r = dump(reliable) + reliableRemote:FireServer(b, r) -- effectively clears the channel reliable.cursor = 0 + table.clear(reliable.references) end - if unreliable ~= nil then - unreliableRemote:FireServer(dump(unreliable)) + if unreliable.cursor > 0 then + local b, r = dump(unreliable) + unreliableRemote:FireServer(b, r) unreliable.cursor = 0 + table.clear(unreliable) end end) end diff --git a/src/process/read.luau b/src/process/read.luau index de98435..ce31da1 100644 --- a/src/process/read.luau +++ b/src/process/read.luau @@ -1,4 +1,5 @@ local packetIDs = require(script.Parent.Parent.namespaces.packetIDs) +local readRefs = require(script.Parent.readRefs) local ref = packetIDs.ref() local freeThread: thread? @@ -29,11 +30,13 @@ return function(incomingBuffer: buffer, references: { [number]: unknown }?, play local length = buffer.len(incomingBuffer) local readCursor = 0 + readRefs.set(references) + while readCursor < length do local packet = ref[buffer.readu8(incomingBuffer, readCursor)] readCursor += 1 - local value, valueLength = packet.reader(incomingBuffer, readCursor, references) + local value, valueLength = packet.reader(incomingBuffer, readCursor) readCursor += valueLength diff --git a/src/process/readRefs.luau b/src/process/readRefs.luau new file mode 100644 index 0000000..745b5d6 --- /dev/null +++ b/src/process/readRefs.luau @@ -0,0 +1,13 @@ +local current + +local readRefs = {} + +function readRefs.set(refTable) + current = refTable +end + +function readRefs.get() + return current +end + +return readRefs diff --git a/src/process/server.luau b/src/process/server.luau index a48175f..a4e909f 100644 --- a/src/process/server.luau +++ b/src/process/server.luau @@ -26,13 +26,13 @@ local function create() } end -local function dump(channel: types.channelData): (buffer, { unknown }) +local function dump(channel: types.channelData): (buffer, { unknown }?) local cursor = channel.cursor local dumpBuffer = buffer.create(cursor) - buffer.copy(dumpBuffer, 0, channel.buff, 0, cursor) + buffer.copy(dumpBuffer, 0, channel.buff, 0, cursor) - return dumpBuffer, channel.references + return dumpBuffer, if #channel.references > 0 then channel.references else nil end -- No longer shared @@ -120,14 +120,16 @@ function serverProcess.start() RunService.Heartbeat:Connect(function() -- Check if the channel has anything before trying to send it if globalReliable.cursor > 0 then - reliableRemote:FireAllClients(dump(globalReliable)) + local b, r = dump(globalReliable) + reliableRemote:FireAllClients(b, r) globalReliable.cursor = 0 table.clear(globalReliable.references) end if globalUnreliable.cursor > 0 then - unreliableRemote:FireAllClients(dump(globalUnreliable)) + local b, r = dump(globalUnreliable) + unreliableRemote:FireAllClients(b, r) globalUnreliable.cursor = 0 table.clear(globalUnreliable.references) @@ -135,14 +137,16 @@ function serverProcess.start() for _, player in Players:GetPlayers() do if perPlayerReliable[player].cursor > 0 then - reliableRemote:FireClient(player, dump(perPlayerReliable[player])) + local b, r = dump(perPlayerReliable[player]) + reliableRemote:FireClient(player, b, r) perPlayerReliable[player].cursor = 0 table.clear(perPlayerReliable[player].references) end if perPlayerUnreliable[player].cursor > 0 then - unreliableRemote:FireClient(player, dump(perPlayerUnreliable[player])) + local b, r = dump(perPlayerUnreliable[player]) + unreliableRemote:FireClient(player, b, r) perPlayerUnreliable[player].cursor = 0 table.clear(perPlayerUnreliable[player].references) diff --git a/src/types.luau b/src/types.luau index b29cd71..ce59a33 100644 --- a/src/types.luau +++ b/src/types.luau @@ -51,8 +51,11 @@ export type ByteNet = { struct: (format: T) -> T, bool: boolean, - array: (value: T) -> { T }, + array: (value: T) -> { [number]: T }, optional: (value: T) -> T?, + inst: Instance, + nothing: nil, + unknown: unknown, uint8: number, uint16: number, uint32: number,