Shared change: codecs/compress/Deflate gains Deflate.peek(buf, off, len, n) (the first n inflated bytes of a gzip / zlib / raw stream, for sniffing gzip-wrapped NBT) through a backward-compatible prefix flag on Inflate.raw; gunzip's header parsing moved into a local helper both use. Co-Authored-By: Claude Opus 5.5 <[email protected]>
257 lines
7.9 KiB
Luau
257 lines
7.9 KiB
Luau
--!strict
|
|
-- codecs/text/Nbt/Value: the NBT value model shared by the binary and SNBT codecs. New code.
|
|
-- Plain Luau values with typed numbers, so a decode / encode round trip is exact:
|
|
-- int -> number string -> string byteArray -> buffer
|
|
-- compound -> Types.object (key order kept) list -> array (element type in Value.listType)
|
|
-- byte / short / long / float / double / intArray / longArray -> a tag { type = "<name>", value = ... } with a
|
|
-- metatable (tostring gives SNBT): long values are numbers when exact (|v| <= 2^53), else their exact decimal
|
|
-- text; intArray value = { numbers }; longArray value = a buffer of little-endian two's complement int64s.
|
|
-- Inputs are lenient: a plain number writes as int when integral and in int range, else double; true / false as
|
|
-- byte 1 / 0; a plain table with string keys as a compound.
|
|
local Types = require("@formats/Types")
|
|
|
|
local Value = {}
|
|
|
|
-- tag ids (the binary format) <-> names
|
|
Value.NAMES = {
|
|
[0] = "end",
|
|
"byte",
|
|
"short",
|
|
"int",
|
|
"long",
|
|
"float",
|
|
"double",
|
|
"byteArray",
|
|
"string",
|
|
"list",
|
|
"compound",
|
|
"intArray",
|
|
"longArray",
|
|
} :: { [number]: string }
|
|
Value.IDS = {} :: { [string]: number }
|
|
for id = 0, 12 do
|
|
Value.IDS[Value.NAMES[id]] = id
|
|
end
|
|
|
|
export type Tag = { type: string, value: any }
|
|
|
|
local TagMeta: { [string]: any } = {}
|
|
Value.TagMeta = TagMeta
|
|
-- the SNBT printer, filled in by codecs/text/Nbt (Value is below the printer)
|
|
Value.print = function(v: any): string
|
|
return tostring(v.value)
|
|
end
|
|
TagMeta.__tostring = function(v: any): string
|
|
return Value.print(v)
|
|
end
|
|
TagMeta.__eq = function(a: any, b: any): boolean
|
|
if a.type ~= b.type then
|
|
return false
|
|
end
|
|
local x, y = a.value, b.value
|
|
if type(x) == "buffer" and type(y) == "buffer" then
|
|
return buffer.tostring(x) == buffer.tostring(y)
|
|
elseif type(x) == "table" and type(y) == "table" then
|
|
if #x ~= #y then
|
|
return false
|
|
end
|
|
for i = 1, #x do
|
|
if x[i] ~= y[i] then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
return x == y
|
|
end
|
|
|
|
local listTypes: { [any]: string } = setmetatable({}, { __mode = "k" }) :: any
|
|
|
|
-- ------------------------------------------------------------------------------------------------ 64-bit integers
|
|
local TWO32 = 4294967296
|
|
|
|
-- fromHiLo(hi, lo) -> a long value: a number when exact, else the exact decimal text (hi, lo: u32 halves of the
|
|
-- two's complement int64)
|
|
function Value.fromHiLo(hi: number, lo: number): number | string
|
|
local s = if hi >= 0x80000000 then hi - TWO32 else hi
|
|
if s >= -0x200000 and s < 0x200000 then
|
|
return s * TWO32 + lo
|
|
end
|
|
local neg = s < 0
|
|
if neg then -- magnitude = 2^64 - v
|
|
lo = (TWO32 - lo) % TWO32
|
|
hi = (TWO32 - 1 - hi + (if lo == 0 then 1 else 0)) % TWO32
|
|
end
|
|
local parts = {}
|
|
while hi > 0 or lo > 0 do -- divide (hi, lo) by 10^4: r * 2^32 + lo < 10^4 * 2^32 stays exact, ql < 2^32
|
|
local qh = hi // 10000
|
|
local x = (hi - qh * 10000) * TWO32 + lo
|
|
local ql = x // 10000
|
|
local d = x - ql * 10000
|
|
hi, lo = qh, ql
|
|
table.insert(parts, 1, string.format(if hi > 0 or lo > 0 then "%04d" else "%d", d))
|
|
end
|
|
return (if neg then "-" else "") .. table.concat(parts)
|
|
end
|
|
|
|
-- toHiLo(v) -> hi, lo (u32 halves) | nil: v a whole number in int64 range or its decimal text
|
|
function Value.toHiLo(v: any): (number?, number?)
|
|
if type(v) == "number" then
|
|
if v % 1 ~= 0 or v < -9223372036854775808 or v >= 9223372036854775808 then
|
|
return nil, nil
|
|
end
|
|
local hi = v // TWO32
|
|
local lo = v - hi * TWO32
|
|
return hi % TWO32, lo
|
|
elseif type(v) == "string" then
|
|
local neg, digits = string.match(v, "^(%-?)(%d+)$")
|
|
if not digits or #digits > 19 then
|
|
return nil, nil
|
|
end
|
|
local hi, lo = 0, 0
|
|
for i = 1, #digits do
|
|
local x = lo * 10 + (string.byte(digits, i) - 48)
|
|
local carry = x // TWO32
|
|
lo = x - carry * TWO32
|
|
hi = hi * 10 + carry
|
|
end
|
|
if hi > 0x80000000 or (hi == 0x80000000 and (lo > 0 or neg == "")) then
|
|
return nil, nil
|
|
end
|
|
if neg == "-" and (hi > 0 or lo > 0) then
|
|
lo = (TWO32 - lo) % TWO32
|
|
hi = (TWO32 - 1 - hi + (if lo == 0 then 1 else 0)) % TWO32
|
|
end
|
|
return hi, lo
|
|
end
|
|
return nil, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ constructors
|
|
local function tag(name: string, v: any): Tag
|
|
return setmetatable({ type = name, value = v }, TagMeta) :: any
|
|
end
|
|
function Value.byte(n: number): Tag
|
|
return tag("byte", n)
|
|
end
|
|
function Value.short(n: number): Tag
|
|
return tag("short", n)
|
|
end
|
|
function Value.int(n: number): number
|
|
return n
|
|
end
|
|
-- long(n | "decimal") -> a long tag (decimal text within 2^53 becomes a number)
|
|
function Value.long(v: number | string): Tag
|
|
if type(v) == "string" then
|
|
local hi, lo = Value.toHiLo(v)
|
|
if hi then
|
|
v = Value.fromHiLo(hi, lo :: number)
|
|
end
|
|
end
|
|
return tag("long", v)
|
|
end
|
|
local f32 = buffer.create(4)
|
|
-- float(n) -> a float tag holding n rounded to single precision
|
|
function Value.float(n: number): Tag
|
|
buffer.writef32(f32, 0, n)
|
|
return tag("float", buffer.readf32(f32, 0))
|
|
end
|
|
function Value.double(n: number): Tag
|
|
return tag("double", n)
|
|
end
|
|
function Value.intArray(t: { number }?): Tag
|
|
return tag("intArray", t or {})
|
|
end
|
|
-- longArray(values | buffer) -> a longArray tag (values: numbers / decimal texts; a buffer: 8 bytes each, LE)
|
|
function Value.longArray(t: { any } | buffer | nil): Tag
|
|
if type(t) == "buffer" then
|
|
return tag("longArray", t)
|
|
end
|
|
local list: { any } = (t or {}) :: any
|
|
local b = buffer.create(#list * 8)
|
|
for i, x in list do
|
|
local hi, lo = Value.toHiLo(x)
|
|
buffer.writeu32(b, (i - 1) * 8, lo or 0)
|
|
buffer.writeu32(b, (i - 1) * 8 + 4, hi or 0)
|
|
end
|
|
return tag("longArray", b)
|
|
end
|
|
-- longs(longArray) -> { number | string }: the elements as long values
|
|
function Value.longs(a: Tag): { number | string }
|
|
local b: buffer = a.value
|
|
local out = table.create(buffer.len(b) // 8) :: { number | string }
|
|
for i = 0, buffer.len(b) // 8 - 1 do
|
|
out[i + 1] = Value.fromHiLo(buffer.readu32(b, i * 8 + 4), buffer.readu32(b, i * 8))
|
|
end
|
|
return out
|
|
end
|
|
-- list(typeName, items?) -> items marked as a list of that element type (an empty list keeps its type)
|
|
function Value.list(name: string, items: { any }?): { any }
|
|
local t = items or {}
|
|
listTypes[t] = name
|
|
return t
|
|
end
|
|
function Value.compound(t: { [string]: any }?, keys: { string }?): { [string]: any }
|
|
return Types.object(t, keys)
|
|
end
|
|
-- listType(t) -> the element type name recorded for a list (decoded, or made by Value.list)
|
|
function Value.listType(t: any): string?
|
|
return listTypes[t]
|
|
end
|
|
function Value.setListType(t: any, name: string)
|
|
listTypes[t] = name
|
|
end
|
|
function Value.isTag(v: any): boolean
|
|
return type(v) == "table" and getmetatable(v) == TagMeta
|
|
end
|
|
|
|
-- typeOf(v) -> the tag name v writes as, or nil + why
|
|
function Value.typeOf(v: any): (string?, string?)
|
|
local tv = type(v)
|
|
if tv == "number" then
|
|
return if v % 1 == 0 and v >= -2147483648 and v <= 2147483647 then "int" else "double", nil
|
|
elseif tv == "string" then
|
|
return "string", nil
|
|
elseif tv == "boolean" then
|
|
return "byte", nil
|
|
elseif tv == "buffer" then
|
|
return "byteArray", nil
|
|
elseif tv == "table" then
|
|
if getmetatable(v) == TagMeta then
|
|
local n = v.type
|
|
if Value.IDS[n] == nil or n == "end" or n == "list" or n == "compound" or n == "string" or n == "int" or n == "byteArray" then
|
|
if n == "int" or n == "string" or n == "byteArray" then
|
|
return n, nil
|
|
end
|
|
return nil, "bad tag type " .. tostring(n)
|
|
end
|
|
return n, nil
|
|
elseif Types.isObject(v) then
|
|
return "compound", nil
|
|
elseif listTypes[v] or v[1] ~= nil or next(v) == nil then
|
|
return "list", nil
|
|
end
|
|
return "compound", nil
|
|
end
|
|
return nil, "cannot write a " .. tv .. " as NBT"
|
|
end
|
|
|
|
-- number(v) -> the numeric value of a number or numeric tag (long texts through tonumber: approximate)
|
|
function Value.number(v: any): number?
|
|
if type(v) == "number" then
|
|
return v
|
|
elseif type(v) == "boolean" then
|
|
return if v then 1 else 0
|
|
elseif Value.isTag(v) then
|
|
local x = v.value
|
|
if type(x) == "number" then
|
|
return x
|
|
elseif type(x) == "string" then
|
|
return tonumber(x)
|
|
end
|
|
end
|
|
return nil
|
|
end
|
|
|
|
return Value
|