mirror of
https://github.com/imezx/Warp.git
synced 2026-03-18 00:44:16 +00:00
chore(identifier): more fixes
This commit is contained in:
parent
b6fa3cf2d7
commit
d2e362fd8d
5 changed files with 82 additions and 32 deletions
|
|
@ -1 +1 @@
|
|||
{"name":"warp","className":"ModuleScript","filePaths":["src/init.luau","default.project.json"],"children":[{"name":"Client","className":"ModuleScript","filePaths":["src/Client/init.luau"]},{"name":"Server","className":"ModuleScript","filePaths":["src/Server/init.luau"]},{"name":"Util","className":"Folder","children":[{"name":"Buffer","className":"ModuleScript","filePaths":["src/Util/Buffer/init.luau"]},{"name":"Identifier","className":"ModuleScript","filePaths":["src/Util/Identifier.luau"]},{"name":"Thread","className":"ModuleScript","filePaths":["src/Util/Thread.luau"]}]},{"name":"Replication","className":"ModuleScript","filePaths":["src/Replication/init.luau"]}]}
|
||||
{"name":"warp","className":"ModuleScript","filePaths":["src/init.luau","default.project.json"],"children":[{"name":"Client","className":"ModuleScript","filePaths":["src/Client/init.luau"]},{"name":"Replication","className":"ModuleScript","filePaths":["src/Replication/init.luau"]},{"name":"Server","className":"ModuleScript","filePaths":["src/Server/init.luau"]},{"name":"Util","className":"Folder","children":[{"name":"Buffer","className":"ModuleScript","filePaths":["src/Util/Buffer/init.luau"]},{"name":"Identifier","className":"ModuleScript","filePaths":["src/Util/Identifier.luau"]},{"name":"Thread","className":"ModuleScript","filePaths":["src/Util/Thread.luau"]}]}]}
|
||||
|
|
@ -30,12 +30,21 @@ local pendingInvokes: { [string]: thread } = {}
|
|||
local invokeId = 0
|
||||
|
||||
Client.useSchema = function(remoteName: string, schema: Buffer.SchemaType)
|
||||
eventSchemas[Replication.get_id(remoteName)] = schema
|
||||
local id = Replication.get_id(remoteName)
|
||||
if not id then
|
||||
warn(`[Warp]: ".useSchema"::"{remoteName}" does not exist, likely its not registered on the server yet.`)
|
||||
end
|
||||
eventSchemas[id] = schema
|
||||
end
|
||||
|
||||
Client.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection
|
||||
local id = Replication.get_id(remoteName)
|
||||
if not id then
|
||||
warn(`[Warp]: ".Connect"::"{remoteName}" does not exist, likely its not registered on the server yet.`)
|
||||
return { Connected = false, Disconnect = function() return end } :: Connection
|
||||
end
|
||||
local detail = {
|
||||
i = Replication.get_id(remoteName),
|
||||
i = id,
|
||||
c = fn,
|
||||
}
|
||||
table.insert(eventListeners, detail)
|
||||
|
|
@ -75,6 +84,9 @@ end
|
|||
|
||||
Client.DisconnectAll = function(remoteName: string)
|
||||
local id = Replication.get_id(remoteName)
|
||||
if not id then
|
||||
return
|
||||
end
|
||||
for idx = #eventListeners, 1, -1 do
|
||||
if eventListeners[idx].i == id then
|
||||
table.remove(eventListeners, idx)
|
||||
|
|
@ -84,14 +96,21 @@ end
|
|||
|
||||
Client.Destroy = Client.DisconnectAll
|
||||
|
||||
Client.Fire = function(remoteName: string, ...: any?)
|
||||
table.insert(queueEvent, {
|
||||
Replication.get_id(remoteName),
|
||||
{ ... } :: any,
|
||||
})
|
||||
Client.Fire = function(remoteName: string, reliable: boolean, ...: any?)
|
||||
local id = Replication.get_id(remoteName)
|
||||
if id then
|
||||
table.insert(reliable and queueEvent or queueUnreliableEvent, {
|
||||
Replication.get_id(remoteName),
|
||||
{ ... } :: any,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
Client.Invoke = function(remoteName: string, timeout: number?, ...: any?): ...any?
|
||||
local id = Replication.get_id(remoteName)
|
||||
if not id then
|
||||
return nil
|
||||
end
|
||||
invokeId += 1
|
||||
local reqid, thread = `{invokeId}`, coroutine.running()
|
||||
|
||||
|
|
@ -106,7 +125,7 @@ Client.Invoke = function(remoteName: string, timeout: number?, ...: any?): ...an
|
|||
end)
|
||||
table.insert(queueEvent, {
|
||||
0,
|
||||
{ Replication.get_id(remoteName), reqid :: any, { ... } :: any } :: any,
|
||||
{ id, reqid :: any, { ... } :: any } :: any,
|
||||
})
|
||||
return coroutine.yield()
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,10 +14,10 @@ local warp_identifier_registry = shared.__warp_identifier_registry
|
|||
|
||||
if RunService:IsServer() then
|
||||
local replication_ready: { Player } = {}
|
||||
local replication_id: number = Identifier.get("id_replication") or 1
|
||||
local replication_id: number = Identifier.get_id("id_replication") or 1
|
||||
|
||||
if not Identifier.has("id_replication") or not replication_id then
|
||||
replication_id = Identifier.get("id_replication") or 1
|
||||
if not Identifier.has_name("id_replication") or not replication_id then
|
||||
replication_id = Identifier.get_id("id_replication") or 1
|
||||
end
|
||||
|
||||
local function replicateToAll(content: any, id: number?)
|
||||
|
|
@ -28,7 +28,7 @@ if RunService:IsServer() then
|
|||
local buf = Buffer.build(writer)
|
||||
Buffer.reset(writer)
|
||||
for _, player: Player in replication_ready do
|
||||
_repl:FireClient(player, buf, replication_id)
|
||||
_repl:FireClient(player, buf)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -38,7 +38,7 @@ if RunService:IsServer() then
|
|||
do
|
||||
local buf = Buffer.build(writer)
|
||||
Buffer.reset(writer)
|
||||
_repl:FireClient(player, buf, replication_id)
|
||||
_repl:FireClient(player, buf)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -55,14 +55,16 @@ if RunService:IsServer() then
|
|||
end
|
||||
|
||||
elseif RunService:IsClient() then
|
||||
_repl.OnClientEvent:Connect(function(b: buffer, id: number)
|
||||
_repl.OnClientEvent:Connect(function(b: buffer)
|
||||
if type(b) ~= "buffer" then
|
||||
return
|
||||
end
|
||||
local contents = Buffer.readRepl(b, identifiers_schema)
|
||||
if #contents == 0 then return end
|
||||
for _, content in contents do
|
||||
warp_identifier_registry.cache[content[2]] = content[1]
|
||||
local id, remote = content[1], content[2]
|
||||
warp_identifier_registry.cache[remote] = id
|
||||
warp_identifier_registry.name[id] = remote
|
||||
end
|
||||
end)
|
||||
_repl:FireServer()
|
||||
|
|
@ -70,6 +72,9 @@ elseif RunService:IsClient() then
|
|||
Replication.get_id = function(name: string): number
|
||||
return warp_identifier_registry.cache[name]
|
||||
end
|
||||
Replication.get_name = function(name: string): number
|
||||
return warp_identifier_registry.name[name]
|
||||
end
|
||||
end
|
||||
|
||||
return Replication :: typeof(Replication)
|
||||
|
|
|
|||
|
|
@ -32,18 +32,24 @@ local queueUnreliableEvent: {
|
|||
} = {}
|
||||
local eventListeners: { Event } = {}
|
||||
local eventSchemas: { [number]: Buffer.SchemaType } = {}
|
||||
local players_ready: { Player } = {}
|
||||
local players_ready: { Player }, player_bytes: { [Player]: number } = {}, {}
|
||||
|
||||
local pendingInvokes: { [string]: thread } = {}
|
||||
local invokeId = 0
|
||||
|
||||
Server.reg_namespaces = function(namespaces: { string })
|
||||
for _, name: string in namespaces do
|
||||
Identifier.get_id(name)
|
||||
end
|
||||
end
|
||||
|
||||
Server.useSchema = function(remoteName: string, schema: Buffer.SchemaType)
|
||||
eventSchemas[Identifier.get(remoteName)] = schema
|
||||
eventSchemas[Identifier.get_id(remoteName)] = schema
|
||||
end
|
||||
|
||||
Server.Connect = function(remoteName: string, fn: (Player, ...any?) -> ...any?): Connection
|
||||
local detail = {
|
||||
i = Identifier.get(remoteName),
|
||||
i = Identifier.get_id(remoteName),
|
||||
c = fn,
|
||||
}
|
||||
table.insert(eventListeners, detail)
|
||||
|
|
@ -82,7 +88,7 @@ Server.Wait = function(remoteName: string): (number, ...any?)
|
|||
end
|
||||
|
||||
Server.DisconnectAll = function(remoteName: string)
|
||||
local id = Identifier.get(remoteName)
|
||||
local id = Identifier.get_id(remoteName)
|
||||
if not id then
|
||||
return
|
||||
end
|
||||
|
|
@ -95,26 +101,27 @@ end
|
|||
|
||||
Server.Destroy = Server.DisconnectAll
|
||||
|
||||
Server.Fire = function(remoteName: string, player: Player, ...: any?)
|
||||
if not queueEvent[player] then
|
||||
queueEvent[player] = {} :: any
|
||||
Server.Fire = function(remoteName: string, reliable: boolean, player: Player, ...: any?)
|
||||
local targetQueue = reliable and queueEvent or queueUnreliableEvent
|
||||
if not targetQueue[player] then
|
||||
targetQueue[player] = {} :: any
|
||||
end
|
||||
table.insert(queueEvent[player], {
|
||||
Identifier.get(remoteName),
|
||||
table.insert(targetQueue[player], {
|
||||
Identifier.get_id(remoteName),
|
||||
{ ... } :: any,
|
||||
})
|
||||
end
|
||||
|
||||
Server.Fires = function(remoteName: string, ...: any?)
|
||||
Server.Fires = function(remoteName: string, reliable: boolean, ...: any?)
|
||||
for _, player: Player in players_ready do
|
||||
Server.Fire(remoteName, player, ...)
|
||||
Server.Fire(remoteName, reliable, player, ...)
|
||||
end
|
||||
end
|
||||
|
||||
Server.FireExcept = function(remoteName: string, except: { Player }, ...: any?)
|
||||
Server.FireExcept = function(remoteName: string, reliable: boolean, except: { Player }, ...: any?)
|
||||
for _, player: Player in players_ready do
|
||||
if table.find(except, player) then continue end
|
||||
Server.Fire(remoteName, player, ...)
|
||||
Server.Fire(remoteName, reliable, player, ...)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -136,7 +143,7 @@ Server.Invoke = function(remoteName: string, player: Player, timeout: number?, .
|
|||
end
|
||||
table.insert(queueEvent[player], {
|
||||
0,
|
||||
{ Identifier.get(remoteName), reqId :: any, { ... } :: any } :: any,
|
||||
{ Identifier.get_id(remoteName), reqId :: any, { ... } :: any } :: any,
|
||||
})
|
||||
return coroutine.yield()
|
||||
end
|
||||
|
|
@ -146,6 +153,10 @@ if RunService:IsServer() then
|
|||
if type(b) ~= "buffer" then
|
||||
return
|
||||
end
|
||||
local bytes: number = (player_bytes[player] or 0) + math.max(buffer.len(b), 800)
|
||||
if bytes > 8e3 then return end
|
||||
player_bytes[player] = bytes
|
||||
|
||||
local contents = Buffer.readEvents(b, ref, eventSchemas)
|
||||
for _, content in contents do
|
||||
local remote = content[1]
|
||||
|
|
@ -228,6 +239,7 @@ if RunService:IsServer() then
|
|||
Event:FireClient(player, buf, ref)
|
||||
end
|
||||
end
|
||||
player_bytes[player] = 0
|
||||
table.clear(queueEvent[player])
|
||||
end
|
||||
-- unreliable
|
||||
|
|
@ -245,6 +257,7 @@ if RunService:IsServer() then
|
|||
UnreliableEvent:FireClient(player, buf, ref)
|
||||
end
|
||||
end
|
||||
table.clear(player_bytes)
|
||||
table.clear(queueUnreliableEvent[player])
|
||||
end
|
||||
end)
|
||||
|
|
@ -268,6 +281,9 @@ if RunService:IsServer() then
|
|||
table.clear(queueEvent[player])
|
||||
queueEvent[player] = nil
|
||||
end
|
||||
if player_bytes[player] then
|
||||
player_bytes[player] = nil
|
||||
end
|
||||
if queueUnreliableEvent[player] then
|
||||
table.clear(queueUnreliableEvent[player])
|
||||
queueUnreliableEvent[player] = nil
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ local hook_fn: (string, number) -> ()
|
|||
if not shared.__warp_identifier_registry then
|
||||
shared.__warp_identifier_registry = {
|
||||
cache = {} :: { [string]: number },
|
||||
name = {} :: { [number]: string },
|
||||
counter = 0,
|
||||
}
|
||||
end
|
||||
|
|
@ -26,11 +27,15 @@ Identifier.on_added = function(fn: (string, number) -> ())
|
|||
hook_fn = fn
|
||||
end
|
||||
|
||||
Identifier.has = function(name: string): boolean
|
||||
Identifier.has_name = function(name: string): boolean
|
||||
return registry.cache[name] ~= nil
|
||||
end
|
||||
|
||||
Identifier.get = function(name: string): number
|
||||
Identifier.has_id = function(id: number): boolean
|
||||
return registry.name[id] ~= nil
|
||||
end
|
||||
|
||||
Identifier.get_id = function(name: string): number
|
||||
local cached = registry.cache[name]
|
||||
if cached then
|
||||
return cached
|
||||
|
|
@ -40,10 +45,15 @@ Identifier.get = function(name: string): number
|
|||
fastAssert(id <= MAX_VALUE, `Identifier overflow: exceeded {MAX_VALUE + 1} unique names.`)
|
||||
registry.counter = id
|
||||
registry.cache[name] = id
|
||||
registry.name[id] = name
|
||||
if hook_fn then
|
||||
task.defer(hook_fn, name, id)
|
||||
end
|
||||
return id
|
||||
end
|
||||
|
||||
Identifier.get_name = function(id: number): string
|
||||
return registry.name[id]
|
||||
end
|
||||
|
||||
return Identifier :: typeof(Identifier)
|
||||
|
|
|
|||
Loading…
Reference in a new issue