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]
|
||||
local Players = game:GetService("Players")
|
||||
local Warp = require(path.to.warp).Client()
|
||||
local Schemas = require(path.to.schemas)
|
||||
local Warp = require(game.ReplicatedStorage.WarpNew).Client()
|
||||
local Schemas = require(game.ReplicatedStorage.Schemas)
|
||||
|
||||
-- Use schemas
|
||||
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!
|
||||
Warp.Fire("Ping", true, "ping!") -- we send through reliable event
|
||||
|
||||
task.wait(1) -- lets wait for a second!
|
||||
|
||||
-- Disconnect All the events
|
||||
connection1:Disconnect()
|
||||
-- or just disconnect spesific connection
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ export type Writer = {
|
|||
|
||||
local DEFAULT_CAPACITY: number = 64
|
||||
|
||||
local F16_SUBNORMAL_MULT = 2 ^ (-14)
|
||||
local F16_SUBNORMAL_SCALE = 2 ^ 24
|
||||
|
||||
-- 0x00-0x7F: + fixint (0-127) - single byte
|
||||
-- 0x80-0x9F: - fixint (-32 to -1) - single byte
|
||||
local T_NIL = 0xA0
|
||||
|
|
@ -1650,7 +1647,7 @@ local function writeEvents(w: Writer, events: { { any } }, schemas: { [number]:
|
|||
for _, event in events do
|
||||
local id = event[1]
|
||||
local args = event[2]
|
||||
wU16(w, id)
|
||||
wByte(w, id)
|
||||
local schema = schemas[id]
|
||||
if schema then
|
||||
packStrict(w, schema, args[1])
|
||||
|
|
@ -1665,8 +1662,8 @@ local function readEvents(buf: buffer, refs: { any }?, schemas: { [number]: Sche
|
|||
count, pos = readVarUInt(buf, pos)
|
||||
local events = table.create(count)
|
||||
for i = 1, count do
|
||||
local id: number = buffer.readu16(buf, pos)
|
||||
pos += 2
|
||||
local id: number = buffer.readu8(buf, pos)
|
||||
pos += 1
|
||||
local args: any
|
||||
local schema = schemas[id]
|
||||
if schema then
|
||||
|
|
|
|||
|
|
@ -1,25 +1,37 @@
|
|||
--!optimize 2
|
||||
--!native
|
||||
--@EternityDev
|
||||
local BITS: number = 8
|
||||
local MAX_VALUE: number = bit32.lshift(1, BITS) - 1
|
||||
|
||||
if not shared.__identifier_registry then
|
||||
shared.__identifier_registry = {
|
||||
cache = {} :: { [string]: number },
|
||||
counter = 0,
|
||||
}
|
||||
if not shared.__identifier_cache then
|
||||
shared.__identifier_cache = {
|
||||
cache = {} :: { [string]: number },
|
||||
used = {} :: { [number]: string },
|
||||
}
|
||||
end
|
||||
|
||||
local registry = shared.__identifier_registry
|
||||
local registry = shared.__identifier_cache
|
||||
local cache = registry.cache
|
||||
local used = registry.used
|
||||
|
||||
return function(name: string): number
|
||||
local cached = registry.cache[name]
|
||||
if cached then
|
||||
return cached
|
||||
end
|
||||
local cached = cache[name]
|
||||
if cached then
|
||||
return cached
|
||||
end
|
||||
|
||||
local id = registry.counter + 1
|
||||
assert(id <= MAX_VALUE, `Identifier overflow: exceeded {MAX_VALUE + 1} unique names.`)
|
||||
registry.counter = id + 1
|
||||
registry.cache[name] = id
|
||||
return id
|
||||
local h: number = 2166136261
|
||||
for i = 1, #name do
|
||||
h = bit32.bxor(h, string.byte(name, i))
|
||||
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
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue