Compare commits

..

No commits in common. "master" and "1.0.13" have entirely different histories.

10 changed files with 55 additions and 128 deletions

BIN
Warp.rbxm

Binary file not shown.

View file

@ -230,16 +230,18 @@ function ClientProcess.start()
end end
end end
end) end)
local function onClientNetworkReceive(Identifier: buffer | string, data: buffer, ref: { any }?) local function onClientNetworkReceive(Identifier: buffer | string, data: buffer)
if not Identifier or typeof(Identifier) ~= "buffer" or not data or typeof(data) ~= "buffer" then return end if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier) Identifier = Buffer.convert(Identifier :: buffer)
if not registeredIdentifier[Identifier :: string] then return end local read = Buffer.read(data)
local read = Buffer.read(data, ref)
if not read then return end if not read then return end
local callback = clientCallback[Identifier :: string] for idx: string in registeredIdentifier do
if not callback then return end if idx ~= Identifier then continue end
for _, fn: any in callback do local callback = clientCallback[idx] or nil
Spawn(fn, table.unpack(read)) if not callback then continue end
for _, fn: any in callback do
Spawn(fn, table.unpack(read))
end
end end
end end
ReliableEvent.OnClientEvent:Connect(onClientNetworkReceive) ReliableEvent.OnClientEvent:Connect(onClientNetworkReceive)

View file

@ -49,13 +49,12 @@ local ReliableEvent = Event.Reliable
local UnreliableEvent = Event.Unreliable local UnreliableEvent = Event.Unreliable
local RequestEvent = Event.Request local RequestEvent = Event.Request
RateLimit.Protect()
local function initializeEachPlayer(player: Player) local function initializeEachPlayer(player: Player)
if not player then return end if not player then return end
if not queueOut[player] then if not queueOut[player] then
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
@ -85,29 +84,6 @@ local function initializeEachPlayer(player: Player)
end end
Players.PlayerAdded:Connect(initializeEachPlayer) Players.PlayerAdded:Connect(initializeEachPlayer)
Players.PlayerRemoving:Connect(function(player: Player)
if not player then return end
if queueOut[player] then
queueOut[player] = nil
end
for _, map in { serverQueue, unreliableServerQueue, serverRequestQueue } do
for Identifier: string in map do
map[Identifier][player] = nil
end
end
for i=1,2 do
for Identifier: string in queueInRequest[i] do
if queueInRequest[i][Identifier][player] then
queueInRequest[i][Identifier][player] = nil
end
end
for Identifier: string in queueOutRequest[i] do
if queueOutRequest[i][Identifier][player] then
queueOutRequest[i][Identifier][player] = nil
end
end
end
end)
function ServerProcess.insertQueue(Identifier: string, reliable: boolean, player: Player, ...: any) function ServerProcess.insertQueue(Identifier: string, reliable: boolean, player: Player, ...: any)
if not reliable then if not reliable then
@ -327,16 +303,18 @@ function ServerProcess.start()
end end
end end
end) end)
local function onServerNetworkReceive(player: Player, Identifier: buffer | string, data: buffer, ref: { any }?) local function onServerNetworkReceive(player: Player, Identifier: buffer | string, data: buffer)
if not Identifier or typeof(Identifier) ~= "buffer" or not data or typeof(data) ~= "buffer" then return end if not Identifier or not data then return end
Identifier = Buffer.convert(Identifier :: buffer) Identifier = Buffer.convert(Identifier :: buffer)
if not registeredIdentifier[Identifier :: string] then return end local read = Buffer.read(data)
local read = Buffer.read(data, ref)
if not read then return end if not read then return end
local callback = serverCallback[Identifier :: string] for idx: string in registeredIdentifier do
if not callback then return end if idx ~= Identifier then continue end
for _, fn: any in callback do local callback = serverCallback[idx] or nil
Spawn(fn, player, table.unpack(read)) if not callback then continue end
for _, fn: any in callback do
Spawn(fn, player, table.unpack(read))
end
end end
end end
ReliableEvent.OnServerEvent:Connect(onServerNetworkReceive) ReliableEvent.OnServerEvent:Connect(onServerNetworkReceive)

View file

@ -36,7 +36,7 @@ function DedicatedBuffer.alloc(self: any, byte: number)
local b: buffer = self.buffer local b: buffer = self.buffer
while self.next + byte >= size do while self.next + byte >= size do
size = math.floor(size * 1.25) -- +25% increase size = math.floor(size * 1.2)
end end
local newBuffer: buffer = create(size) local newBuffer: buffer = create(size)
copy(newBuffer, 0, b) copy(newBuffer, 0, b)
@ -56,15 +56,13 @@ function DedicatedBuffer.build(self: any): buffer
return build return build
end end
function DedicatedBuffer.buildAndRemove(self: any): (buffer, (any)?) function DedicatedBuffer.buildAndRemove(self: any): buffer
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, ref return build
end end
function DedicatedBuffer.wi8(self: any, val: number, alloc: number?) function DedicatedBuffer.wi8(self: any, val: number, alloc: number?)
@ -126,21 +124,14 @@ function DedicatedBuffer.wType(self: any, ref: number)
self.point += 1 self.point += 1
end end
function DedicatedBuffer.wRef(self: any, value: any, alloc: number?)
if not value then return end
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:wRef(data) self:pack(data.ClassName) -- Serialize ClassName
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
@ -241,7 +232,6 @@ 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()
@ -249,8 +239,7 @@ 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,27 +18,28 @@ 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, ref: { any }?): (any, number) local function readValue(b: buffer, position: number): (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
if not ref or #ref == 0 then local className, position1 = readValue(b, position)
return nil, position + 1 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 end
local value = ref[readu8(b, position)] return instance, position3
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, ref) value, position = readValue(b, position)
table.insert(array, value) table.insert(array, value)
end end
return array, position return array, position
@ -48,8 +49,8 @@ local function readValue(b: buffer, position: number, ref: { any }?): (any, numb
local dict = {} local dict = {}
for _ = 1, length do for _ = 1, length do
local key, value local key, value
key, position = readValue(b, position, ref) key, position = readValue(b, position)
value, position = readValue(b, position, ref) value, position = readValue(b, position)
dict[key] = value dict[key] = value
end end
return dict, position return dict, position
@ -128,21 +129,20 @@ function Buffer.revert(s: string): buffer
return fromstring(s) return fromstring(s)
end end
function Buffer.write(data: { any }): (buffer, (any)?) function Buffer.write(data: { any }): buffer
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, ref: { any }?): any? function Buffer.read(b: buffer): 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, ref) value, position = readValue(b, position)
table.insert(result, value) table.insert(result, value)
end end
ref = nil
return table.unpack(result) return table.unpack(result)
end end

View file

@ -4,51 +4,21 @@ local RateLimit = {}
local RunService = game:GetService("RunService") local RunService = game:GetService("RunService")
local Assert = require(script.Parent.Assert) local Assert = require(script.Parent.Assert)
local Events = require(script.Parent.Parent.Event) local Event = require(script.Parent.Parent.Event).Reliable
local Reliable, Unreliable, Request = Events.Reliable, Events.Unreliable, Events.Request
local Signal = require(script.Parent.Parent.Signal)("Warp_OnSpamSignal")
local map, activity, meta = {}, {}, {}
setmetatable(meta , {
__index = map,
__newindex = function(self, key, value)
if not activity[key] then
activity[key] = os.clock()
end
if (os.clock()-activity[key]) >= 1 then
activity[key] = os.clock()
map[key] = 1
return
end
if value >= 1e2 then -- 100
Signal:Fire(key)
return
end
map[key] = value
end,
})
local function onReceived(player: Player)
if not meta[player] then
meta[player] = 1
return
end
meta[player] += 1
end
function RateLimit.create(Identifier: string, entrance: number?, interval: number?) function RateLimit.create(Identifier: string, entrance: number?, interval: number?)
Assert(typeof(Identifier) == "string", "Identifier must a string type.") Assert(typeof(Identifier) == "string", "Identifier must a string type.")
if RunService:IsServer() then if RunService:IsServer() then
Assert(typeof(entrance) == "number", "entrance must a number type.") Assert(typeof(entrance) == "number", "entrance must a number type.")
Assert(entrance :: number > 0, "entrance must above 0.") Assert(entrance :: number > 0, "entrance must above 0.")
Reliable:SetAttribute(Identifier.."_ent", entrance) Event:SetAttribute(Identifier.."_ent", entrance)
Reliable:SetAttribute(Identifier.."_int", interval) Event:SetAttribute(Identifier.."_int", interval)
else else
while (not Reliable:GetAttribute(Identifier.."_ent")) or (not Reliable:GetAttribute(Identifier.."_int")) do while (not Event:GetAttribute(Identifier.."_ent")) or (not Event:GetAttribute(Identifier.."_int")) do
task.wait(0.1) task.wait(0.25)
end end
entrance = tonumber(Reliable:GetAttribute(Identifier.."_ent")) entrance = tonumber(Event:GetAttribute(Identifier.."_ent"))
interval = tonumber(Reliable:GetAttribute(Identifier.."_int")) interval = tonumber(Event:GetAttribute(Identifier.."_int"))
end end
local entrances: number = 0 local entrances: number = 0
return function(incoming: number?): boolean return function(incoming: number?): boolean
@ -62,14 +32,4 @@ function RateLimit.create(Identifier: string, entrance: number?, interval: numbe
end end
end end
function RateLimit.Protect()
if not RunService:IsServer() or Reliable:GetAttribute("Protected") or Unreliable:GetAttribute("Protected") or Request:GetAttribute("Protected") then return end
Reliable:SetAttribute("Protected", true)
Unreliable:SetAttribute("Protected", true)
Request:SetAttribute("Protected", true)
Reliable.OnServerEvent:Connect(onReceived)
Unreliable.OnServerEvent:Connect(onReceived)
Request.OnServerEvent:Connect(onReceived)
end
return RateLimit :: typeof(RateLimit) return RateLimit :: typeof(RateLimit)

View file

@ -24,7 +24,7 @@ function SerDes.increment(Identifier: string, timeout: number?): number
error(`Serdes: {Identifier} is taking too long to retrieve, seems like it's not replicated on server.`, 2) error(`Serdes: {Identifier} is taking too long to retrieve, seems like it's not replicated on server.`, 2)
end) end)
task.spawn(function() task.spawn(function()
while coroutine.status(cancel) ~= "dead" and task.wait(0.04) do -- let it loop for yields! 1/24 while coroutine.status(cancel) ~= "dead" and task.wait(0.25) do -- let it loop for yields!
if Event:GetAttribute(Identifier) then if Event:GetAttribute(Identifier) then
task.cancel(cancel) task.cancel(cancel)
task.spawn(yieldThread, Event:GetAttribute(Identifier)) task.spawn(yieldThread, Event:GetAttribute(Identifier))

View file

@ -77,4 +77,4 @@ function Index.buffer()
return Buffer.new() return Buffer.new()
end end
return table.freeze(Index) :: typeof(Index) return table.freeze(Index) :: typeof(Index)

View file

@ -1,5 +1,5 @@
-- Warp Library (@Eternity_Devs) -- Warp Library (@Eternity_Devs)
-- version 1.0.14 -- version 1.0.13
--!strict --!strict
--!native --!native
--!optimize 2 --!optimize 2
@ -11,8 +11,6 @@ return {
fromServerArray = Index.fromServerArray, fromServerArray = Index.fromServerArray,
fromClientArray = Index.fromClientArray, fromClientArray = Index.fromClientArray,
OnSpamSignal = Index.OnSpamSignal,
Signal = Index.Signal, Signal = Index.Signal,
fromSignalArray = Index.fromSignalArray, fromSignalArray = Index.fromSignalArray,

View file

@ -1,6 +1,6 @@
[package] [package]
name = "imezx/warp" name = "imezx/warp"
version = "1.0.14" version = "1.0.13"
registry = "https://github.com/UpliftGames/wally-index" registry = "https://github.com/UpliftGames/wally-index"
realm = "shared" realm = "shared"
license = "MIT" license = "MIT"