mirror of
https://github.com/imezx/Warp.git
synced 2026-03-18 00:44:16 +00:00
80 lines
2.4 KiB
Text
80 lines
2.4 KiB
Text
--!optimize 2
|
|
--!strict
|
|
local Replication = {}
|
|
|
|
local RunService = game:GetService("RunService")
|
|
local _repl: RemoteEvent = script.Parent:WaitForChild("_repl")
|
|
|
|
local Buffer = require("./Util/Buffer")
|
|
local Identifier = require("./Util/Identifier")
|
|
|
|
local identifiers_schema = Buffer.Schema.string
|
|
local writer: Buffer.Writer = Buffer.createWriter()
|
|
local warp_identifier_registry = shared.__warp_identifier_registry
|
|
|
|
if RunService:IsServer() then
|
|
local replication_ready: { Player } = {}
|
|
local replication_id: number = Identifier.get_id("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?)
|
|
if #replication_ready == 0 then return end
|
|
local to_repl: any = type(content) == "string" and { [content] = id } or content
|
|
Buffer.writeRepl(writer, to_repl, 1, identifiers_schema)
|
|
do
|
|
local buf = Buffer.build(writer)
|
|
Buffer.reset(writer)
|
|
for _, player: Player in replication_ready do
|
|
_repl:FireClient(player, buf)
|
|
end
|
|
end
|
|
end
|
|
|
|
local function replicateTo(player: Player)
|
|
Buffer.writeRepl(writer, warp_identifier_registry.cache, warp_identifier_registry.counter, identifiers_schema)
|
|
do
|
|
local buf = Buffer.build(writer)
|
|
Buffer.reset(writer)
|
|
_repl:FireClient(player, buf)
|
|
end
|
|
end
|
|
|
|
Identifier.on_added(replicateToAll)
|
|
|
|
_repl.OnServerEvent:Connect(function(player: Player)
|
|
if table.find(replication_ready, player) then return end
|
|
table.insert(replication_ready, player)
|
|
replicateTo(player)
|
|
end)
|
|
|
|
Replication.remove = function(player: Player)
|
|
table.remove(replication_ready, table.find(replication_ready, player))
|
|
end
|
|
|
|
elseif RunService:IsClient() then
|
|
_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
|
|
local id, remote = content[1], content[2]
|
|
warp_identifier_registry.cache[remote] = id
|
|
warp_identifier_registry.name[id] = remote
|
|
end
|
|
end)
|
|
_repl:FireServer()
|
|
|
|
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)
|