Skip to content

Commit

Permalink
v0.4.4
Browse files Browse the repository at this point in the history
* Improved tests to handle more cases
* Simplified generator assert generation
* When a function fails on the server it will now output the error message
* Event On now returns a disconnect function
* Various fixes
  • Loading branch information
1Axen committed Jan 19, 2024
1 parent 561e5f5 commit 2447400
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 38 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,4 @@
/release
/Network
/Blink
test/Client.luau
test/Server.luau
test/Sources/Game.txt
42 changes: 35 additions & 7 deletions src/Generator/Prefabs.luau
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type AssertGenerator = ((Variable: string, Min: number, Max: number) -> AssertPr

local SEND_BUFFER = "SendBuffer"
local RECIEVE_BUFFER = "RecieveBuffer"
local SEND_POSITION = "SendOffset"

local BYTE = 1
local SHORT = 2
Expand Down Expand Up @@ -178,7 +179,10 @@ for Index, Size in Numbers do
return `{Variable} = buffer.read{Index}({RECIEVE_BUFFER}, Read({Size}))`
end,
Write = function(Value: string): string
return `buffer.write{Index}({SEND_BUFFER}, Allocate({Size}), {Value})`
local Write = Builder.new()
Write.Push(`Allocate({Size})`)
Write.Push(`buffer.write{Index}({SEND_BUFFER}, {SEND_POSITION}, {Value})`, 0)
return Write.Dump()
end
}
Primitives[Index] = {
Expand Down Expand Up @@ -243,7 +247,10 @@ do
return `{Variable} = (buffer.readu8({RECIEVE_BUFFER}, Read({BYTE})) == 1)`
end,
Write = function(Value: string): string
return `buffer.writeu8({SEND_BUFFER}, Allocate({BYTE}), {Value} and 1 or 0)`
local Write = Builder.new()
Write.Push(`Allocate({BYTE})`)
Write.Push(`buffer.writeu8({SEND_BUFFER}, {SEND_POSITION}, {Value} and 1 or 0)`, 0)
return Write.Dump()
end
}

Expand All @@ -265,11 +272,19 @@ do
return `{Types.u16.Read("local Length")}\n{Variable} = buffer.readstring({RECIEVE_BUFFER}, Read(Length), Length)`
end,
Write = function(Value: string, Range: NumberRange?): string
local Write = Builder.new()

if Range and Range.Min == Range.Max then
return `buffer.writestring({SEND_BUFFER}, Allocate({Range.Min}), {Value}, {Range.Min})`
Write.Push(`Allocate({Range.Min})`)
Write.Push(`buffer.writestring({SEND_BUFFER}, {SEND_POSITION}, {Value}, {Range.Min})`, 0)
else
Write.Push(`local Length = #{Value}`)
Write.Push(Types.u16.Write("Length"))
Write.Push(`Allocate(Length)`)
Write.Push(`buffer.writestring({SEND_BUFFER}, {SEND_POSITION}, {Value}, Length)`, 0)
end

return `local Length = #{Value}\n{Types.u16.Write("Length")}\nbuffer.writestring({SEND_BUFFER}, Allocate(Length), {Value}, Length)`
return Write.Dump()
end
}

Expand All @@ -286,7 +301,13 @@ do
return `{Variable} = Vector3.new(buffer.readf32({RECIEVE_BUFFER}, Read({FLOAT})), buffer.readf32({RECIEVE_BUFFER}, Read({FLOAT})), buffer.readf32({RECIEVE_BUFFER}, Read({FLOAT})))`
end,
Write = function(Value: string)
return `local Vector = {Value}\nbuffer.writef32({SEND_BUFFER}, Allocate({FLOAT}), Vector.X)\nbuffer.writef32({SEND_BUFFER}, Allocate({FLOAT}), Vector.Y)\nbuffer.writef32({SEND_BUFFER}, Allocate({FLOAT}), Vector.Z)`
local Write = Builder.new()
Write.Push(`Allocate({FLOAT * 3})`)
Write.Push(`local Vector = {Value}`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION}, Vector.X)`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION} + {FLOAT}, Vector.Y)`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION} + {FLOAT * 2}, Vector.Z)`, 0)
return Write.Dump()
end
}

Expand Down Expand Up @@ -322,7 +343,8 @@ do
Write.Push(`{Types.u16.Write("Length")}`, 1, 1)
end

Write.Push(`buffer.copy({SEND_BUFFER}, Allocate(Length), {Value}, 0, Length)`, 0, 1)
Write.Push(`Allocate(Length)`, 0, 1)
Write.Push(`buffer.copy({SEND_BUFFER}, {SEND_POSITION}, {Value}, 0, Length)`, 0, 1)

return Write.Dump()
end
Expand Down Expand Up @@ -370,7 +392,13 @@ do
return `{Variable} = Color3.new(buffer.readu8({RECIEVE_BUFFER}, Read({BYTE})), buffer.readu8({RECIEVE_BUFFER}, Read({BYTE})), buffer.readu8({RECIEVE_BUFFER}, Read({BYTE})))`
end,
Write = function(Value: string)
return `local Color = {Value}\nbuffer.writeu8({SEND_BUFFER}, Allocate({BYTE}), Color.R * 8)\nbuffer.writeu8({SEND_BUFFER}, Allocate({BYTE}), Color.G * 8)\nbuffer.writeu8({SEND_BUFFER}, Allocate({BYTE}), Color.B * 8)`
local Write = Builder.new()
Write.Push(`Allocate({FLOAT * 3})`)
Write.Push(`local Color = {Value}`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION}, Color.R * 8)`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION} + {FLOAT}, Color.G * 8)`)
Write.Push(`buffer.writef32({SEND_BUFFER}, {SEND_POSITION} + {FLOAT * 2}, Color.B * 8)`, 0)
return Write.Dump()
end
}

Expand Down
21 changes: 11 additions & 10 deletions src/Generator/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ local SAVE_BODY = {
Header = "local Buffer, Size, Instances = SendBuffer, SendCursor, SendInstances",
Body = {
"Load(PlayersMap[Player])",
"buffer.copy(SendBuffer, Allocate(Size), Buffer, 0, Size)",
"local Position = Allocate(Size)",
"buffer.copy(SendBuffer, Position, Buffer, 0, Size)",
"table.move(Instances, 1, #Instances, #SendInstances + 1, SendInstances)",
"PlayersMap[Player] = Save()"
}
Expand Down Expand Up @@ -139,7 +140,7 @@ function Writers.Optional(ReadBody: string, WriteBody: string, Variable: string,
Read.PushMultiline(ReadBody, 0, 1)
Read.Push("end", 1, 1)

Write.Push(Prefabs.Types.u8.Write(`({Variable} ~= nil) and 1 or 0`), 1, 1)
Write.PushMultiline(Prefabs.Types.u8.Write(`({Variable} ~= nil) and 1 or 0`), 0, 1)
Write.Push(`if {Variable} ~= nil then`, 1, 1)
Write.PushMultiline(WriteBody, 0, 1)
Write.Push("end", 1, 1)
Expand Down Expand Up @@ -339,13 +340,13 @@ local function WriteBody(Identifier: string, Data: Parser.Declaration, Index: nu

local Type, Reference
local DataValue = Data.Value
Write.Push(TypePrefabs.u8.Write(Index), 1, 1)
Write.PushMultiline(TypePrefabs.u8.Write(Index), 0, 1)

if Invoking ~= nil then
Write.Push(TypePrefabs.u8.Write("InvocationIdentifier"), 1, 1)
Write.PushMultiline(TypePrefabs.u8.Write("InvocationIdentifier"), 0, 1)

if not Invoking then
Write.Push(TypePrefabs.u8.Write("1"), 1, 1)
Write.PushMultiline(TypePrefabs.u8.Write("1"), 0, 1)

Read.Push(`local {TypePrefabs.u8.Read("Success")} == 1`, 1, 1)
Read.Push(`if not Success then`, 1, 1)
Expand Down Expand Up @@ -586,9 +587,9 @@ function Writers.FunctionDeclaration(

Function.Push(`Load(PlayersMap[Player])`, 1, 4)
Function.Push(`if not Success then`, 1, 4)
Function.Push(`buffer.writeu8(SendBuffer, Allocate(1), {FunctionIndex})`, 1, 5)
Function.Push(`buffer.writeu8(SendBuffer, Allocate(1), InvocationIdentifier)`, 1, 5)
Function.Push(`buffer.writeu8(SendBuffer, Allocate(1), 0)`, 1, 5)
Function.PushMultiline(TypePrefabs.u8.Write(FunctionIndex), 0, 5)
Function.PushMultiline(TypePrefabs.u8.Write("InvocationIdentifier"), 0, 5)
Function.PushMultiline(TypePrefabs.u8.Write(0), 0, 5)
Function.Push(`warn(\`"{Value.Identifier}" encountered an error, \{Return\}\`)`, 1, 5)
Function.Push(`else`, 1, 4)
Function.Push(`{GetTypesPath(ReturnIdentifier, Scope, true)}(Return, InvocationIdentifier)`, 1, 5)
Expand All @@ -607,7 +608,7 @@ function Writers.FunctionDeclaration(
Function.Push(`end`, 1, 2)

ReliableEvents.Push(`{Channel.Listening and "elseif" or "if"} Index == {FunctionIndex} then`, 1, 2)
ReliableEvents.Push(`local {TypePrefabs.u8.Read("InvocationIdentifier")}`, 1, 3)
ReliableEvents.PushMultiline(`local {TypePrefabs.u8.Read("InvocationIdentifier")}`, 0, 3)
ReliableEvents.Push(`local Value = {GetTypesPath(DataIdentifier, Scope, false)}()`, 1, 3)
ReliableEvents.Push(`if {Events} then`, 1, 3)
ReliableEvents.Push(`{Events}(Player, Value, InvocationIdentifier)`, 1, 4)
Expand Down Expand Up @@ -641,7 +642,7 @@ function Writers.FunctionDeclaration(
Function.Push(`end`, 1, 2)

ReliableEvents.Push(`{Channel.Listening and "elseif" or "if"} Index == {FunctionIndex} then`, 1, 2)
ReliableEvents.Push(`local {TypePrefabs.u8.Read("InvocationIdentifier")}`, 1, 3)
ReliableEvents.PushMultiline(`local {TypePrefabs.u8.Read("InvocationIdentifier")}`, 0, 3)
ReliableEvents.Push(`if Calls[InvocationIdentifier] then`, 1, 3)
ReliableEvents.Push(`local Success, Value = pcall(function() return {GetTypesPath(ReturnIdentifier, Scope, false)}() end)`, 1, 4)
ReliableEvents.Push(`task.spawn(Calls[InvocationIdentifier], Success, Value)`, 1, 4)
Expand Down
7 changes: 5 additions & 2 deletions src/Templates/Base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Invocations = 0

local SendOffset = 0
local SendCursor = 0
local SendBuffer = buffer.create(64)
local SendInstances = {}
Expand Down Expand Up @@ -29,12 +30,14 @@ end
local function Load(Save: BufferSave?)
if Save then
SendCursor = Save.Cursor
SendOffset = Save.Cursor
SendBuffer = Save.Buffer
SendInstances = Save.Instances
return
end

SendCursor = 0
SendOffset = 0
SendBuffer = buffer.create(64)
SendInstances = {}
end
Expand Down Expand Up @@ -66,10 +69,10 @@ local function Allocate(Bytes: number)
SendBuffer = Buffer
end

local Offset = SendCursor
SendOffset = SendCursor
SendCursor += Bytes

return Offset
return SendOffset
end

local Types = {}
Expand Down
1 change: 1 addition & 0 deletions src/Templates/Client.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local function StepReplication()
Reliable:FireServer(Buffer, SendInstances)

SendCursor = 0
SendOffset = 0
buffer.fill(SendBuffer, 0, 0)
table.clear(SendInstances)
end
46 changes: 46 additions & 0 deletions test/Client.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local Shared = require("Shared")

local Signal = Shared.Signal
local ClientEnviornment = Shared.GetEnviornment()

local function Clone(Source: buffer): buffer
local Size = buffer.len(Source)
local Target = buffer.create(Size)
buffer.copy(Target, 0, Source, 0, Size)
return Target
end

local function Fire(Reliable: boolean, ...)
local Arguments = {...}
local Buffer = #Arguments == 2 and Arguments[1] or Arguments[2]
local Instances = Arguments[#Arguments]

Buffer = Clone(Buffer)
Instances = table.clone(Instances)

Shared.Bridge:Fire("Client", Reliable and "BLINK_RELIABLE_REMOTE" or "BLINK_UNRELIABLE_REMOTE", Buffer, Instances)
end

ClientEnviornment.Instances.BLINK_RELIABLE_REMOTE = {
FireServer = function(self, ...)
Fire(true, ...)
end,
OnClientEvent = Signal.new()
}

ClientEnviornment.Instances.BLINK_UNRELIABLE_REMOTE = {
FireServer = function(self, ...)
Fire(false, ...)
end,
OnClientEvent = Signal.new()
}

Shared.Bridge:Connect(function(From: string, Remote: string, Buffer: buffer, Instances: {Instance})
if From == "Client" then
return
end

ClientEnviornment.Instances[Remote].OnClientEvent:Fire(Buffer, Instances)
end)

return ClientEnviornment
52 changes: 52 additions & 0 deletions test/Server.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local Shared = require("Shared")

local Signal = Shared.Signal
local ServerEnviornment = Shared.GetEnviornment()

local function Clone(Source: buffer): buffer
local Size = buffer.len(Source)
local Target = buffer.create(Size)
buffer.copy(Target, 0, Source, 0, Size)
return Target
end

local function Fire(Reliable: boolean, ...)
local Arguments = {...}
local Buffer = #Arguments == 2 and Arguments[1] or Arguments[2]
local Instances = Arguments[#Arguments]

Buffer = Clone(Buffer)
Instances = table.clone(Instances)

Shared.Bridge:Fire("Server", Reliable and "BLINK_RELIABLE_REMOTE" or "BLINK_UNRELIABLE_REMOTE", Buffer, Instances)
end

ServerEnviornment.Instances.BLINK_RELIABLE_REMOTE = {
FireClient = function(self, Player, ...)
Fire(true, ...)
end,
FireAllClients = function(self, ...)
Fire(true, ...)
end,
OnServerEvent = Signal.new()
}

ServerEnviornment.Instances.BLINK_UNRELIABLE_REMOTE = {
FireClient = function(Player, ...)
Fire(false, ...)
end,
FireAllClients = function(self, ...)
Fire(false, ...)
end,
OnServerEvent = Signal.new()
}

Shared.Bridge:Connect(function(From: string, Remote: string, Buffer: buffer, Instances: {Instance})
if From == "Server" then
return
end

ServerEnviornment.Instances[Remote].OnServerEvent:Fire(Shared.Player, Buffer, Instances)
end)

return ServerEnviornment
12 changes: 6 additions & 6 deletions test/Sources/Test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,42 @@ event UnreliableClient = {

event InstanceAny = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Instance
}

event InstanceOfType = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Instance(Sound)
}

event InstanceOptional = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Instance?
}

event Reference = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Example?
}

event ReferenceArray = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Example[8]
}

event ReferenceOptional = {
From = Server,
Type = Unreliable,
Type = Reliable,
Call = SingleSync,
Data = Example?
}
Expand Down
Loading

0 comments on commit 2447400

Please sign in to comment.