mirror of
https://github.com/imezx/Warp.git
synced 2026-03-18 00:44:16 +00:00
rewrite(phase4): fix ids arent ordered
This commit is contained in:
parent
4d983a0756
commit
cd76d58c4d
3 changed files with 37 additions and 26 deletions
|
|
@ -37,8 +37,8 @@ end)
|
||||||
|
|
||||||
```luau [Client]
|
```luau [Client]
|
||||||
local Players = game:GetService("Players")
|
local Players = game:GetService("Players")
|
||||||
local Warp = require(path.to.warp).Client()
|
local Warp = require(game.ReplicatedStorage.WarpNew).Client()
|
||||||
local Schemas = require(path.to.schemas)
|
local Schemas = require(game.ReplicatedStorage.Schemas)
|
||||||
|
|
||||||
-- Use schemas
|
-- Use schemas
|
||||||
for eventName, schema in Schemas do
|
for eventName, schema in Schemas do
|
||||||
|
|
@ -65,6 +65,8 @@ print(Warp.Invoke("Example", 1, { "Hello!", `this is from: @{Players.LocalPlayer
|
||||||
-- Do a ping & pong to server!
|
-- Do a ping & pong to server!
|
||||||
Warp.Fire("Ping", true, "ping!") -- we send through reliable event
|
Warp.Fire("Ping", true, "ping!") -- we send through reliable event
|
||||||
|
|
||||||
|
task.wait(1) -- lets wait for a second!
|
||||||
|
|
||||||
-- Disconnect All the events
|
-- Disconnect All the events
|
||||||
connection1:Disconnect()
|
connection1:Disconnect()
|
||||||
-- or just disconnect spesific connection
|
-- or just disconnect spesific connection
|
||||||
|
|
|
||||||
|
|
@ -10,9 +10,6 @@ export type Writer = {
|
||||||
|
|
||||||
local DEFAULT_CAPACITY: number = 64
|
local DEFAULT_CAPACITY: number = 64
|
||||||
|
|
||||||
local F16_SUBNORMAL_MULT = 2 ^ (-14)
|
|
||||||
local F16_SUBNORMAL_SCALE = 2 ^ 24
|
|
||||||
|
|
||||||
-- 0x00-0x7F: + fixint (0-127) - single byte
|
-- 0x00-0x7F: + fixint (0-127) - single byte
|
||||||
-- 0x80-0x9F: - fixint (-32 to -1) - single byte
|
-- 0x80-0x9F: - fixint (-32 to -1) - single byte
|
||||||
local T_NIL = 0xA0
|
local T_NIL = 0xA0
|
||||||
|
|
@ -1650,7 +1647,7 @@ local function writeEvents(w: Writer, events: { { any } }, schemas: { [number]:
|
||||||
for _, event in events do
|
for _, event in events do
|
||||||
local id = event[1]
|
local id = event[1]
|
||||||
local args = event[2]
|
local args = event[2]
|
||||||
wU16(w, id)
|
wByte(w, id)
|
||||||
local schema = schemas[id]
|
local schema = schemas[id]
|
||||||
if schema then
|
if schema then
|
||||||
packStrict(w, schema, args[1])
|
packStrict(w, schema, args[1])
|
||||||
|
|
@ -1665,8 +1662,8 @@ local function readEvents(buf: buffer, refs: { any }?, schemas: { [number]: Sche
|
||||||
count, pos = readVarUInt(buf, pos)
|
count, pos = readVarUInt(buf, pos)
|
||||||
local events = table.create(count)
|
local events = table.create(count)
|
||||||
for i = 1, count do
|
for i = 1, count do
|
||||||
local id: number = buffer.readu16(buf, pos)
|
local id: number = buffer.readu8(buf, pos)
|
||||||
pos += 2
|
pos += 1
|
||||||
local args: any
|
local args: any
|
||||||
local schema = schemas[id]
|
local schema = schemas[id]
|
||||||
if schema then
|
if schema then
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,37 @@
|
||||||
--!optimize 2
|
--!optimize 2
|
||||||
|
--!native
|
||||||
--@EternityDev
|
--@EternityDev
|
||||||
local BITS: number = 8
|
if not shared.__identifier_cache then
|
||||||
local MAX_VALUE: number = bit32.lshift(1, BITS) - 1
|
shared.__identifier_cache = {
|
||||||
|
|
||||||
if not shared.__identifier_registry then
|
|
||||||
shared.__identifier_registry = {
|
|
||||||
cache = {} :: { [string]: number },
|
cache = {} :: { [string]: number },
|
||||||
counter = 0,
|
used = {} :: { [number]: string },
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
local registry = shared.__identifier_registry
|
local registry = shared.__identifier_cache
|
||||||
|
local cache = registry.cache
|
||||||
|
local used = registry.used
|
||||||
|
|
||||||
return function(name: string): number
|
return function(name: string): number
|
||||||
local cached = registry.cache[name]
|
local cached = cache[name]
|
||||||
if cached then
|
if cached then
|
||||||
return cached
|
return cached
|
||||||
end
|
end
|
||||||
|
|
||||||
local id = registry.counter + 1
|
local h: number = 2166136261
|
||||||
assert(id <= MAX_VALUE, `Identifier overflow: exceeded {MAX_VALUE + 1} unique names.`)
|
for i = 1, #name do
|
||||||
registry.counter = id + 1
|
h = bit32.bxor(h, string.byte(name, i))
|
||||||
registry.cache[name] = id
|
h = bit32.band(h * 16777619, 0xFFFFFFFF)
|
||||||
|
end
|
||||||
|
|
||||||
|
local id = h % 254 + 2
|
||||||
|
|
||||||
|
local existing = used[id]
|
||||||
|
if existing and existing ~= name then
|
||||||
|
warn(`[Warp] Hash collision: "{name}" & "{existing}" both map to ID {id}`)
|
||||||
|
end
|
||||||
|
|
||||||
|
cache[name] = id
|
||||||
|
used[id] = name
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue