mirror of
https://github.com/imezx/Warp.git
synced 2026-03-18 00:44:16 +00:00
rewrite(phase4): didnt think if global exists lol
This commit is contained in:
parent
4af26f4d41
commit
81d3c540c1
1 changed files with 20 additions and 25 deletions
|
|
@ -1,30 +1,25 @@
|
|||
--!optimize 2
|
||||
--!native
|
||||
--@EternityDev
|
||||
local BITS: number = 16
|
||||
local BITS: number = 8
|
||||
local MAX_VALUE: number = bit32.lshift(1, BITS) - 1
|
||||
|
||||
local cache: { [string]: number } = {}
|
||||
local hash: number = game.PlaceVersion + 5381
|
||||
local count: number = 0
|
||||
|
||||
return function(name: string): number
|
||||
local cached = cache[name]
|
||||
if cached then
|
||||
return cached
|
||||
end
|
||||
if count >= 255 then -- 255 is safer from collision
|
||||
warn("ran out of ids :(")
|
||||
return 0
|
||||
end
|
||||
|
||||
local b: number = hash
|
||||
for i = 1, #name do
|
||||
b = bit32.bxor(bit32.lshift(b, 5) + b, string.byte(name, i))
|
||||
end
|
||||
|
||||
local reduced = bit32.band(b, MAX_VALUE)
|
||||
cache[name] = reduced
|
||||
count += 1
|
||||
return reduced
|
||||
if not shared.__identifier_registry then
|
||||
shared.__identifier_registry = {
|
||||
cache = {} :: { [string]: number },
|
||||
counter = 0,
|
||||
}
|
||||
end
|
||||
|
||||
local registry = shared.__identifier_registry
|
||||
return function(name: string): number
|
||||
local cached = registry.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
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue