From cd76d58c4d06b6d858f906e94873e623439f9251 Mon Sep 17 00:00:00 2001 From: khtsly Date: Sat, 14 Feb 2026 00:02:17 +0700 Subject: [PATCH] rewrite(phase4): fix ids arent ordered --- docs/guide/example.md | 6 +++-- src/Util/Buffer/init.luau | 9 +++----- src/Util/Identifier.luau | 48 ++++++++++++++++++++++++--------------- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/docs/guide/example.md b/docs/guide/example.md index ca309df..2415d55 100644 --- a/docs/guide/example.md +++ b/docs/guide/example.md @@ -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 diff --git a/src/Util/Buffer/init.luau b/src/Util/Buffer/init.luau index eaefbe3..ed4d619 100644 --- a/src/Util/Buffer/init.luau +++ b/src/Util/Buffer/init.luau @@ -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 diff --git a/src/Util/Identifier.luau b/src/Util/Identifier.luau index f9a1ade..7bb0137 100644 --- a/src/Util/Identifier.luau +++ b/src/Util/Identifier.luau @@ -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