fixes to v1.0.13

This commit is contained in:
EternityDev 2024-11-20 10:25:00 +07:00
parent 22996c9357
commit 43c4a1594f
9 changed files with 98 additions and 176 deletions

BIN
Warp.rbxm

Binary file not shown.

View file

@ -33,14 +33,12 @@ function side() {
text: 'Feature', text: 'Feature',
items: [ items: [
{ text: 'Rate Limit', link: '/api/1.0/ratelimit' }, { text: 'Rate Limit', link: '/api/1.0/ratelimit' },
{ text: 'Middleware', link: '/api/1.0/middleware' },
] ]
}, },
{ {
text: 'Utilities', text: 'Utilities',
items: [ items: [
{ text: 'Signal', link: '/api/1.0/signal' }, { text: 'Signal', link: '/api/1.0/signal' },
{ text: 'Buffer', link: '/api/1.0/buffer' },
] ]
}, },
] ]

View file

@ -1,3 +0,0 @@
# Buffer <Badge type="tip" text="utilities" />
A Additional buffer.

View file

@ -1,64 +0,0 @@
# Middleware <Badge type="tip" text="feature" />
::: code-group
```lua [Server]
local Event1 = Warp.Server("Remote1")
local storeC = Event1:Connect(function(player: Player, arg1: string, arg2: number, arg3: boolean)
print(player, arg1, arg2, arg3)
end):middleware(function(player: Player, arg1: string, arg2: number, arg3: boolean)
assert(type(player) == "userdata" and player:IsA("Player"), "player must be a Player.")
assert(typeof(arg1) == "string", "arg1 must be a string.")
assert(typeof(arg2) == "number", "arg2 must be a number.")
assert(typeof(arg3) == "boolean", "arg3 must be a boolean.")
end)
print(storeC:key())
task.delay(15, function()
Event1:Disconnect(storeC:key())
end)
for _=1,5 do
print("send incorrect values")
Event1:Fires(true, 1e9, "hello world!")
task.wait(0.5)
end
for _=1,5 do
print("send correct values")
Event1:Fires(true, "hello world!", 1e9)
task.wait(0.5)
end
```
```lua [Client]
local Event1 = Warp.Client("Remote1")
local storeC = Event1:Connect(function(arg1: boolean, arg2: string, arg3: number)
print(arg1, arg2, arg3)
end):middleware(function(arg1: boolean, arg2: string, arg3: number)
assert(typeof(arg1) == "boolean", "arg1 must be a boolean.")
assert(typeof(arg2) == "string", "arg2 must be a string.")
assert(typeof(arg3) == "number", "arg3 must be a number.")
end)
print(storeC:key())
task.delay(15, function()
Event1:Disconnect(storeC:key())
end)
for _=1,5 do
print("send incorrect values")
Event1:Fires("hello world!", false, 1e9)
task.wait(0.5)
end
for _=1,5 do
print("send correct values")
Event1:Fires("hello world!", 1e9, false)
task.wait(0.5)
end
```
:::

View file

@ -8,7 +8,7 @@
::: code-group ::: code-group
```toml [wally.toml] ```toml [wally.toml]
[dependencies] [dependencies]
warp = "imezx/warp@1.0.12" warp = "imezx/warp@1.0.13"
``` ```
3. Run `wally install` in command. 3. Run `wally install` in command.

View file

@ -55,7 +55,7 @@ local function initializeEachPlayer(player: Player)
queueOut[player] = {} queueOut[player] = {}
end end
for _, Identifier: string in registeredIdentifier do for Identifier: string in registeredIdentifier do
if not player then break end if not player then break end
if not queueOut[player][Identifier] then if not queueOut[player][Identifier] then
queueOut[player][Identifier] = {} queueOut[player][Identifier] = {}

View file

@ -19,8 +19,8 @@ local writestring = buffer.writestring
local default: { [string]: number } = { local default: { [string]: number } = {
point = 0, point = 0,
next = 0, next = 0,
size = 32, size = 128,
bufferSize = 32, bufferSize = 128,
} }
function DedicatedBuffer.copy(self: any, offset: number, b: buffer?, src: buffer?, srcOffset: number?, count: number?) function DedicatedBuffer.copy(self: any, offset: number, b: buffer?, src: buffer?, srcOffset: number?, count: number?)
@ -65,51 +65,51 @@ function DedicatedBuffer.buildAndRemove(self: any): buffer
return build return build
end end
function DedicatedBuffer.wi8(self: any, val: number) function DedicatedBuffer.wi8(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(1) self:alloc(alloc or 1)
writei8(self.buffer, self.point, val) writei8(self.buffer, self.point, val)
end end
function DedicatedBuffer.wi16(self: any, val: number) function DedicatedBuffer.wi16(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(2) self:alloc(alloc or 2)
writei16(self.buffer, self.point, val) writei16(self.buffer, self.point, val)
end end
function DedicatedBuffer.wi32(self: any, val: number) function DedicatedBuffer.wi32(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(4) self:alloc(alloc or 4)
writei32(self.buffer, self.point, val) writei32(self.buffer, self.point, val)
end end
function DedicatedBuffer.wu8(self: any, val: number) function DedicatedBuffer.wu8(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(1) self:alloc(alloc or 1)
writeu8(self.buffer, self.point, val) writeu8(self.buffer, self.point, val)
end end
function DedicatedBuffer.wu16(self: any, val: number) function DedicatedBuffer.wu16(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(2) self:alloc(alloc or 2)
writeu16(self.buffer, self.point, val) writeu16(self.buffer, self.point, val)
end end
function DedicatedBuffer.wu32(self: any, val: number) function DedicatedBuffer.wu32(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(4) self:alloc(alloc or 4)
writeu32(self.buffer, self.point, val) writeu32(self.buffer, self.point, val)
end end
function DedicatedBuffer.wf32(self: any, val: number) function DedicatedBuffer.wf32(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(4) self:alloc(alloc or 4)
writef32(self.buffer, self.point, val) writef32(self.buffer, self.point, val)
end end
function DedicatedBuffer.wf64(self: any, val: number) function DedicatedBuffer.wf64(self: any, val: number, alloc: number?)
if not val then return end if not val then return end
self:alloc(8) self:alloc(alloc or 8)
writef64(self.buffer, self.point, val) writef64(self.buffer, self.point, val)
end end
@ -119,14 +119,15 @@ function DedicatedBuffer.wstring(self: any, val: string)
writestring(self.buffer, self.point, val) writestring(self.buffer, self.point, val)
end end
function DedicatedBuffer.wType(self: any, typeByte: number) function DedicatedBuffer.wType(self: any, ref: number)
self:alloc(1) writeu8(self.buffer, self.point, ref)
writeu8(self.buffer, self.point, typeByte)
self.point += 1 self.point += 1
end end
function DedicatedBuffer.pack(self: any, data: {any}) function DedicatedBuffer.pack(self: any, data: {any})
if typeof(data) == "number" then if typeof(data) == "nil" then
self:wi8(0)
elseif typeof(data) == "number" then
if math.floor(data) == data then -- Integer if math.floor(data) == data then -- Integer
if data >= 0 and data <= 255 then if data >= 0 and data <= 255 then
self:wi8(1) -- u8 marker self:wi8(1) -- u8 marker
@ -149,26 +150,37 @@ function DedicatedBuffer.pack(self: any, data: {any})
self:wi8(5) -- boolean marker self:wi8(5) -- boolean marker
self:wu8(data and 1 or 0) self:wu8(data and 1 or 0)
elseif typeof(data) == "string" then elseif typeof(data) == "string" then
self:wi8(6) -- string marker
local length = #data local length = #data
if length < 255 then if length <= 255 then
--self:wi8(7) -- small string marker self:wi8(6)
self:wu8(length) -- use 1 byte for length if small self:wu8(length)
elseif length <= 65535 then
self:wi8(7)
self:wu16(length)
else else
--self:wi8(8) -- large string marker self:wi8(8)
self:wi32(length) -- 32-bit for larger strings self:wi32(length)
end end
self:wstring(data) self:wstring(data)
elseif typeof(data) == "Vector3" then elseif typeof(data) == "Vector3" then
self:wi8(7) -- Vector3 marker self:wi8(9) -- Vector3 marker
self:wf32(data.X) self:wf32(data.X)
self:wf32(data.Y) self:wf32(data.Y)
self:wf32(data.Z) self:wf32(data.Z)
elseif typeof(data) == "Vector2" then
self:wi8(10) -- Vector2 marker
self:wf32(data.X)
self:wf32(data.Y)
elseif typeof(data) == "CFrame" then elseif typeof(data) == "CFrame" then
self:wi8(8) -- CFrame marker self:wi8(11) -- CFrame marker
for _, v in {data:GetComponents()} do for _, v in {data:GetComponents()} do
self:wf32(v) self:wf32(v)
end end
elseif typeof(data) == "Color3" then
self:wi8(12) -- Color3 marker
self:wu8(data.R * 255)
self:wu8(data.G * 255)
self:wu8(data.B * 255)
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
@ -180,21 +192,26 @@ function DedicatedBuffer.pack(self: any, data: {any})
end end
end end
if isArray then if isArray then
self:wi8(9) -- array marker self:wi8(13) -- array marker
self:wi32(count) -- use 32-bit length self:wu16(count) -- use 32-bit length
for _, v in data do for _, v in data do
self:pack(v) self:pack(v)
end end
else else
self:wi8(10) -- dictionary marker self:wi8(14) -- dictionary marker
self:wi32(count) -- number of key-value pairs self:wu16(count) -- number of key-value pairs
for k, v in data do for k, v in data do
self:pack(k) -- pack the key self:pack(k) -- pack the key
self:pack(v) -- pack the value self:pack(v) -- pack the value
end end
end end
elseif typeof(data) == "Instance" then
self:wi8(15) -- Instance marker
self:pack(data.ClassName) -- Serialize ClassName
self:pack(data.Name) -- Serialize Name
self:pack(data:GetAttributes())
else else
error("Unsupported data type: " .. typeof(data)) warn(`Unsupported data type: {typeof(data)} value: {data}`)
end end
end end

View file

@ -21,13 +21,15 @@ local len = buffer.len
local function readValue(b: buffer, position: number): (any, number) local function readValue(b: buffer, position: number): (any, number)
local typeByte = readu8(b, position) local typeByte = readu8(b, position)
position += 1 position += 1
if typeByte == 1 then -- u8 if typeByte == 0 then -- nil
return nil, position
elseif typeByte == 1 then -- int u8
local value = readu8(b, position) local value = readu8(b, position)
return value, position + 1 return value, position + 1
elseif typeByte == 2 then -- i16 elseif typeByte == 2 then -- int i16
local value = readi16(b, position) local value = readi16(b, position)
return value, position + 2 return value, position + 2
elseif typeByte == 3 then -- i32 elseif typeByte == 3 then -- int i32
local value = readi32(b, position) local value = readi32(b, position)
return value, position + 4 return value, position + 4
elseif typeByte == 4 then -- f64 elseif typeByte == 4 then -- f64
@ -36,33 +38,41 @@ local function readValue(b: buffer, position: number): (any, number)
elseif typeByte == 5 then -- boolean elseif typeByte == 5 then -- boolean
local value = readu8(b, position) == 1 local value = readu8(b, position) == 1
return value, position + 1 return value, position + 1
elseif typeByte == 6 then -- string elseif typeByte == 6 then -- string u8
local lengthByte = readu8(b, position) local length = readu8(b, position)
position += 1 local value = readstring(b, position + 1, length)
local value return value, position + length + 1
if lengthByte < 255 then elseif typeByte == 7 then -- string u16
value = readstring(b, position, lengthByte) local length = readu16(b, position)
return value, position + lengthByte local value = readstring(b, position + 2, length)
else return value, position + length + 2
elseif typeByte == 8 then -- string i32
local length = readi32(b, position) local length = readi32(b, position)
position += 4 local value = readstring(b, position + 4, length)
value = readstring(b, position, length) return value, position + length + 4
return value, position + length elseif typeByte == 9 then -- Vector3
end
elseif typeByte == 7 then -- Vector3
local x = readf32(b, position) local x = readf32(b, position)
local y = readf32(b, position + 4) local y = readf32(b, position + 4)
local z = readf32(b, position + 8) local z = readf32(b, position + 8)
return Vector3.new(x, y, z), position + 12 return Vector3.new(x, y, z), position + 12
elseif typeByte == 8 then -- CFrame elseif typeByte == 10 then -- Vector2
local x = readf32(b, position)
local y = readf32(b, position + 8)
return Vector2.new(x, y), position + 8
elseif typeByte == 11 then -- CFrame
local components = {} local components = {}
for i = 1, 12 do for i = 1, 12 do
table.insert(components, readf32(b, position + (i - 1) * 4)) table.insert(components, readf32(b, position + (i - 1) * 4))
end end
return CFrame.new(unpack(components)), position + 48 return CFrame.new(unpack(components)), position + 48
elseif typeByte == 9 then -- array elseif typeByte == 12 then -- Vector2
local length = readi32(b, position) local r = readu8(b, position)
position += 4 local g = readu8(b, position + 1)
local b = readu8(b, position + 2)
return Color3.fromRGB(r, g, b), position + 3
elseif typeByte == 13 then -- array
local length = readu16(b, position)
position += 2
local array = {} local array = {}
for _ = 1, length do for _ = 1, length do
local value local value
@ -70,9 +80,9 @@ local function readValue(b: buffer, position: number): (any, number)
table.insert(array, value) table.insert(array, value)
end end
return array, position return array, position
elseif typeByte == 10 then -- dictionary elseif typeByte == 14 then -- dictionary
local length = readi32(b, position) local length = readu16(b, position)
position += 4 position += 2
local dict = {} local dict = {}
for _ = 1, length do for _ = 1, length do
local key, value local key, value
@ -81,6 +91,16 @@ local function readValue(b: buffer, position: number): (any, number)
dict[key] = value dict[key] = value
end end
return dict, position return dict, position
elseif typeByte == 15 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)
end
return instance, position3
end end
error(`Unsupported type marker: {typeByte}`) error(`Unsupported type marker: {typeByte}`)
end end

View file

@ -1,46 +0,0 @@
--!strict
--!native
--!optimize 2
local Middleware = {}
Middleware.__index = Middleware
local function wrap(middleware: (...any) -> (...any)): (...any) -> boolean
return function(...): boolean
local obj: any = { ... }
local s, r = pcall(function()
return middleware(table.unpack(obj))
end)
if not s and r then
warn(r)
r = nil
table.clear(obj)
obj = nil
end
return s
end
end
function Middleware.new(key: string)
return setmetatable({
root = key,
bridge = function(...: any?): any?
return true
end,
}, Middleware)
end
function Middleware:middleware(middleware: (...any) -> (...any))
self.bridge = wrap(middleware)
return self
end
function Middleware:key(): string
return self.root
end
function Middleware:destroy()
table.clear(self)
setmetatable(self, nil)
end
return Middleware.new :: typeof(Middleware.new)