Skip to content

Commit

Permalink
Fixes & type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ffrostfall committed Mar 7, 2024
1 parent 1da2b8d commit 58b32e7
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 35 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down
8 changes: 3 additions & 5 deletions dev/server/serverTests.server.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions dev/shared/testPackets.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion sourcemap.json
Original file line number Diff line number Diff line change
@@ -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"]}]}]}]}
{"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"]}]}]}]}
9 changes: 6 additions & 3 deletions src/dataTypes/inst.luau
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,12 +12,14 @@ return function(): types.dataTypeInterface<Instance?>
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
Expand Down
9 changes: 6 additions & 3 deletions src/dataTypes/unknown.luau
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,12 +12,14 @@ return function(): types.dataTypeInterface<unknown>
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
1 change: 1 addition & 0 deletions src/process/bufferWriter.luau
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function bufferWriter.reference(value: any)

buffer.writeu8(buff, cursor, index)
cursor += 1
print(references)
end

function bufferWriter.u16(value: number)
Expand Down
20 changes: 12 additions & 8 deletions src/process/client.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion src/process/read.luau
Original file line number Diff line number Diff line change
@@ -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?
Expand Down Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/process/readRefs.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
local current

local readRefs = {}

function readRefs.set(refTable)
current = refTable
end

function readRefs.get()
return current
end

return readRefs
18 changes: 11 additions & 7 deletions src/process/server.luau
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -120,29 +120,33 @@ 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)
end

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)
Expand Down
5 changes: 4 additions & 1 deletion src/types.luau
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ export type ByteNet = {

struct: <T>(format: T) -> T,
bool: boolean,
array: <T>(value: T) -> { T },
array: <T>(value: T) -> { [number]: T },
optional: <T>(value: T) -> T?,
inst: Instance,
nothing: nil,
unknown: unknown,
uint8: number,
uint16: number,
uint32: number,
Expand Down

0 comments on commit 58b32e7

Please sign in to comment.