--!strict --!native --!optimize 2 -- codecs/model/Rbx/Binary: the property columns of the Roblox binary format (rbx-dom docs/binary.md): one PROP chunk -- holds a property's values for every instance of a class, by type id, with the format's array transforms: byte -- interleaving (all first bytes, then all second bytes, ...), big-endian integers, zigzag signed integers, "rotated" -- floats (sign bit moved to bit 0), delta-coded referents, and CFrame rotation ids (codecs/model/Rbx). Never throws. -- -- local values, pEnd = Binary.decode(typeId, buf, p, limit, n, ctx) -- n plain values (codecs/model/Rbx forms) -- local err = Binary.encode(typeId, values, n, writer, ctx) -- nil, or a message -- Binary.readRefs(buf, p, limit, n) / Binary.writeRefs(writer, refs) -- referent arrays (INST, PRNT) -- -- ctx: { ref = function(referent) -> Instance? (decode), refOf = function(Instance?) -> referent (-1 = none), -- shared = { string } (SSTR entries, 1-based, decode), sharedIndex = function(string) -> 0-based index (encode) }. -- decode returns nil, message on bad data ("truncated", "corrupt: ...", "unknown" for a type id it does not know / -- a layout it cannot read, which the format keeps raw). local Rbx = require("@formats/codecs/model/Rbx") local Bytes = require("@formats/core/Bytes") local Binary = {} local readu8, readu16, readi16, readu32, readf32, readf64 = buffer.readu8, buffer.readu16, buffer.readi16, buffer.readu32, buffer.readf32, buffer.readf64 local writeu32, readstring = buffer.writeu32, buffer.readstring local writef32 = buffer.writef32 local floor = math.floor local scratch = buffer.create(8) local TWO32 = 4294967296 -- ------------------------------------------------------------------------------------------------ transforms -- the rotated float encoding: bits rotated left by one (sign in bit 0) local function unrotate(x: number): number writeu32(scratch, 0, x // 2 + (x % 2) * 2147483648) return readf32(scratch, 0) end local function rotate(v: number): number writef32(scratch, 0, v) local bits = readu32(scratch, 0) return (bits * 2) % TWO32 + bits // 2147483648 end local function unzigzag(x: number): number return if x % 2 == 0 then x // 2 else -(x + 1) // 2 end local function zigzag32(v: number): number v = floor(v) return (if v >= 0 then v * 2 else -v * 2 - 1) % TWO32 end -- n big-endian u32 values interleaved at p (4n bytes) local function u32s(b: buffer, p: number, n: number): { number } local out = table.create(n, 0) local n2, n3 = n * 2, n * 3 for i = 0, n - 1 do out[i + 1] = readu8(b, p + i) * 16777216 + readu8(b, p + n + i) * 65536 + readu8(b, p + n2 + i) * 256 + readu8(b, p + n3 + i) end return out end local function i32s(b: buffer, p: number, n: number): { number } local out = u32s(b, p, n) for i = 1, n do out[i] = unzigzag(out[i]) end return out end local function f32s(b: buffer, p: number, n: number): { number } local out = u32s(b, p, n) for i = 1, n do out[i] = unrotate(out[i]) end return out end -- n zigzag i64 values (8n interleaved bytes), as doubles local function i64s(b: buffer, p: number, n: number): { number } local out = table.create(n, 0) for i = 0, n - 1 do local hi = readu8(b, p + i) * 16777216 + readu8(b, p + n + i) * 65536 + readu8(b, p + 2 * n + i) * 256 + readu8(b, p + 3 * n + i) local lo = readu8(b, p + 4 * n + i) * 16777216 + readu8(b, p + 5 * n + i) * 65536 + readu8(b, p + 6 * n + i) * 256 + readu8(b, p + 7 * n + i) local half = hi * 2147483648 + lo // 2 out[i + 1] = if lo % 2 == 0 then half else -half - 1 end return out end -- writers: n values interleaved as big-endian u32 / zigzag i32 / rotated f32 / zigzag i64 local function putU32s(w: Bytes.Writer, vals: { number }) local n = #vals local p = w:reserve(4 * n) local b = w.buf for i = 0, n - 1 do local v = vals[i + 1] % TWO32 buffer.writeu8(b, p + i, v // 16777216) buffer.writeu8(b, p + n + i, (v // 65536) % 256) buffer.writeu8(b, p + 2 * n + i, (v // 256) % 256) buffer.writeu8(b, p + 3 * n + i, v % 256) end w.pos = p + 4 * n end local function putI32s(w: Bytes.Writer, vals: { number }) local t = table.create(#vals, 0) for i, v in vals do t[i] = zigzag32(v) end putU32s(w, t) end local function putF32s(w: Bytes.Writer, vals: { number }) local t = table.create(#vals, 0) for i, v in vals do t[i] = rotate(v) end putU32s(w, t) end local function putI64s(w: Bytes.Writer, vals: { number }) local n = #vals local p = w:reserve(8 * n) local b = w.buf for i = 0, n - 1 do local v = floor(vals[i + 1]) local z = if v >= 0 then v * 2 else -v * 2 - 1 local hi, lo = (z // TWO32) % TWO32, z % TWO32 buffer.writeu8(b, p + i, hi // 16777216) buffer.writeu8(b, p + n + i, (hi // 65536) % 256) buffer.writeu8(b, p + 2 * n + i, (hi // 256) % 256) buffer.writeu8(b, p + 3 * n + i, hi % 256) buffer.writeu8(b, p + 4 * n + i, lo // 16777216) buffer.writeu8(b, p + 5 * n + i, (lo // 65536) % 256) buffer.writeu8(b, p + 6 * n + i, (lo // 256) % 256) buffer.writeu8(b, p + 7 * n + i, lo % 256) end w.pos = p + 8 * n end -- referent arrays: zigzag i32, interleaved, each the difference from the previous one function Binary.readRefs(b: buffer, p: number, limit: number, n: number): ({ number }?, number) if n < 0 or p + 4 * n > limit then return nil, p end local out = i32s(b, p, n) local acc = 0 for i = 1, n do acc += out[i] out[i] = acc end return out, p + 4 * n end function Binary.writeRefs(w: Bytes.Writer, refs: { number }) local d = table.create(#refs, 0) local prev = 0 for i, r in refs do d[i] = r - prev prev = r end putI32s(w, d) end -- ------------------------------------------------------------------------------------------------ decode export type Ctx = { ref: ((number) -> any)?, refOf: ((any) -> number)?, shared: { string }?, sharedIndex: ((string) -> number)?, } -- a length-prefixed string at p: string, next p (nil when it runs past limit) local function str(b: buffer, p: number, limit: number): (string?, number) if p + 4 > limit then return nil, p end local len = readu32(b, p) if p + 4 + len > limit then return nil, p end return readstring(b, p + 4, len), p + 4 + len end local function cframes(b: buffer, p: number, limit: number, n: number): ({ any }?, number) local rots: { { number } } = table.create(n) :: any for i = 1, n do if p >= limit then return nil, p end local id = readu8(b, p) p += 1 if id == 0 then if p + 36 > limit then return nil, p end local m = table.create(9, 0) for k = 0, 8 do m[k + 1] = readf32(b, p + 4 * k) end rots[i] = m p += 36 else local m = Rbx.ROTATIONS[id] if not m then return nil, -1 end rots[i] = m end end if p + 12 * n > limit then return nil, p end local xs, ys, zs = f32s(b, p, n), f32s(b, p + 4 * n, n), f32s(b, p + 8 * n, n) local out = table.create(n) for i = 1, n do local m = rots[i] out[i] = { xs[i], ys[i], zs[i], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9] } end return out, p + 12 * n end local FIXED: { [number]: number } = { -- bytes per value of the fixed-size types [0x02] = 1, [0x03] = 4, [0x04] = 4, [0x05] = 8, [0x06] = 8, [0x07] = 16, [0x08] = 24, [0x09] = 1, [0x0A] = 1, [0x0B] = 4, [0x0C] = 12, [0x0D] = 8, [0x0E] = 12, [0x0F] = 4, [0x12] = 4, [0x13] = 4, [0x14] = 6, [0x17] = 8, [0x18] = 16, [0x1A] = 3, [0x1B] = 8, [0x1C] = 4, [0x1F] = 16, [0x21] = 8, } -- decode(typeId, b, p, limit, n, ctx) -> n values, the end offset | nil, message function Binary.decode(t: number, b: buffer, p: number, limit: number, n: number, ctx: Ctx): ({ any }?, any) if limit > buffer.len(b) or p < 0 or n < 0 then return nil, "corrupt: bad ranges" end local size = FIXED[t] if size and p + size * n > limit then return nil, "truncated property values" end if t == 0x01 or t == 0x1D then local out = table.create(n, "") for i = 1, n do local s, q = str(b, p, limit) if not s then return nil, "truncated string" end out[i] = s p = q end return out, p elseif t == 0x02 then local out = table.create(n, false) for i = 1, n do out[i] = readu8(b, p + i - 1) ~= 0 end return out, p + n elseif t == 0x03 then return i32s(b, p, n), p + 4 * n elseif t == 0x04 then return f32s(b, p, n), p + 4 * n elseif t == 0x05 then local out = table.create(n, 0) for i = 1, n do out[i] = readf64(b, p + 8 * (i - 1)) end return out, p + 8 * n elseif t == 0x06 then local s, o = f32s(b, p, n), i32s(b, p + 4 * n, n) local out = table.create(n) for i = 1, n do out[i] = { s[i], o[i] } end return out, p + 8 * n elseif t == 0x07 then local xs, ys, xo, yo = f32s(b, p, n), f32s(b, p + 4 * n, n), i32s(b, p + 8 * n, n), i32s(b, p + 12 * n, n) local out = table.create(n) for i = 1, n do out[i] = { xs[i], xo[i], ys[i], yo[i] } end return out, p + 16 * n elseif t == 0x08 then local out = table.create(n) for i = 1, n do local q = p + 24 * (i - 1) out[i] = { readf32(b, q), readf32(b, q + 4), readf32(b, q + 8), readf32(b, q + 12), readf32(b, q + 16), readf32(b, q + 20) } end return out, p + 24 * n elseif t == 0x09 or t == 0x0A then local out = table.create(n, 0) for i = 1, n do out[i] = readu8(b, p + i - 1) end return out, p + n elseif t == 0x0B or t == 0x12 then return u32s(b, p, n), p + 4 * n elseif t == 0x0C or t == 0x0E or t == 0x0D or t == 0x18 then local k = if t == 0x0D then 2 elseif t == 0x18 then 4 else 3 local cols: { { number } } = table.create(k) :: any for c = 1, k do cols[c] = f32s(b, p + 4 * n * (c - 1), n) end local out = table.create(n) for i = 1, n do local v = table.create(k, 0) for c = 1, k do v[c] = cols[c][i] end out[i] = v end return out, p + 4 * k * n elseif t == 0x0F or t == 0x14 then local k = if t == 0x0F then 2 else 3 local out = table.create(n) for i = 1, n do local q = p + 2 * k * (i - 1) out[i] = if k == 2 then { readi16(b, q), readi16(b, q + 2) } else { readi16(b, q), readi16(b, q + 2), readi16(b, q + 4) } end return out, p + 2 * k * n elseif t == 0x10 then local out, q = cframes(b, p, limit, n) if not out then return nil, if q == -1 then "corrupt: bad CFrame rotation id" else "truncated CFrame values" end return out, q elseif t == 0x13 then local refs, q = Binary.readRefs(b, p, limit, n) if not refs then return nil, "truncated referents" end local out = table.create(n) local ref = ctx.ref for i = 1, n do out[i] = if ref and refs[i] >= 0 then ref(refs[i]) else nil end return out, q elseif t == 0x15 or t == 0x16 then local k = if t == 0x15 then 3 else 5 local out = table.create(n) for i = 1, n do if p + 4 > limit then return nil, "truncated sequence" end local count = readu32(b, p) p += 4 if p + count * 4 * k > limit then return nil, "truncated sequence" end local seq = table.create(count) for j = 1, count do local key = table.create(k, 0) for c = 1, k do key[c] = readf32(b, p + 4 * (c - 1)) end seq[j] = key p += 4 * k end out[i] = seq end return out, p elseif t == 0x17 then local out = table.create(n) for i = 1, n do out[i] = { readf32(b, p + 8 * (i - 1)), readf32(b, p + 8 * (i - 1) + 4) } end return out, p + 8 * n elseif t == 0x19 then local out = table.create(n) for i = 1, n do if p >= limit then return nil, "truncated physical properties" end local flag = readu8(b, p) p += 1 if flag % 2 == 1 then local k = if flag // 2 % 2 == 1 then 6 else 5 if p + 4 * k > limit then return nil, "truncated physical properties" end local v = table.create(6, 1) -- acoustic absorption 1 when the file leaves it out for c = 1, k do v[c] = readf32(b, p + 4 * (c - 1)) end out[i] = v p += 4 * k elseif flag ~= 0 then return nil, "unknown" end end return out, p elseif t == 0x1A then local out = table.create(n) for i = 1, n do out[i] = { readu8(b, p + i - 1), readu8(b, p + n + i - 1), readu8(b, p + 2 * n + i - 1) } end return out, p + 3 * n elseif t == 0x1B or t == 0x21 then return i64s(b, p, n), p + 8 * n elseif t == 0x1C then local idx = u32s(b, p, n) local shared: { string } = ctx.shared or {} local out: { string } = table.create(n, "") for i = 1, n do local s = shared[idx[i] + 1] if s == nil then return nil, "corrupt: shared string index out of range" end out[i] = s end return out, p + 4 * n elseif t == 0x1E then if p >= limit or readu8(b, p) ~= 0x10 then return nil, "unknown" end local cfs, q = cframes(b, p + 1, limit, n) if not cfs then return nil, if q == -1 then "corrupt: bad CFrame rotation id" else "truncated CFrame values" end if q + 1 + n > limit or readu8(b, q) ~= 0x02 then return nil, "truncated optional CFrame flags" end local out = table.create(n) for i = 1, n do out[i] = if readu8(b, q + i) ~= 0 then cfs[i] else nil end return out, q + 1 + n elseif t == 0x1F then local out = table.create(n, "") local hex = table.create(16, "") for i = 0, n - 1 do local by = table.create(16, 0) for k = 0, 15 do by[k + 1] = readu8(b, p + k * n + i) end -- bytes 8..15: the random part rotated left by one bit (as a 64-bit integer); undo it local carry = by[16] % 2 for k = 9, 16 do local v = by[k] by[k] = v // 2 + carry * 128 carry = v % 2 end for k = 1, 8 do hex[k] = string.format("%02x", by[8 + k]) end for k = 1, 8 do hex[8 + k] = string.format("%02x", by[k + 4 - (if k > 4 then 8 else 0)]) end out[i + 1] = table.concat(hex) end return out, p + 16 * n elseif t == 0x20 then local out = table.create(n) for i = 1, n do local fam, q = str(b, p, limit) if not fam or q + 3 > limit then return nil, "truncated font" end local weight, style = readu16(b, q), readu8(b, q + 2) local face, q2 = str(b, q + 3, limit) if not face then return nil, "truncated font" end out[i] = { family = fam, weight = weight, style = if style == 1 then "Italic" else "Normal", cachedFaceId = if face ~= "" then face else nil } p = q2 end return out, p elseif t == 0x22 then if p + 4 * n > limit then return nil, "truncated content" end local kinds = i32s(b, p, n) p += 4 * n if p + 4 > limit then return nil, "truncated content" end local nu = readu32(b, p) p += 4 local uris = table.create(math.min(nu, n)) for j = 1, nu do local s, q = str(b, p, limit) if not s then return nil, "truncated content" end uris[j] = s p = q end if p + 4 > limit then return nil, "truncated content" end local no = readu32(b, p) local objs, q = Binary.readRefs(b, p + 4, limit, no) if not objs or q + 4 > limit then return nil, "truncated content" end if readu32(b, q) ~= 0 then return nil, "unknown" -- external objects: kept raw end p = q + 4 local out = table.create(n) local iu, io = 1, 1 for i = 1, n do local k = kinds[i] if k == 1 then if iu > nu then return nil, "corrupt: content uri count" end out[i] = uris[iu] iu += 1 elseif k == 2 then if io > no then return nil, "corrupt: content object count" end local r = objs[io] out[i] = if ctx.ref and r >= 0 then ctx.ref(r) else nil io += 1 elseif k ~= 0 then return nil, "unknown" end end return out, p end return nil, "unknown" end -- ------------------------------------------------------------------------------------------------ encode local function num(v: any): number return if type(v) == "number" and v == v then v else 0 end -- column c of n array values (missing components 0) local function column(vals: { any }, n: number, c: number): { number } local out = table.create(n, 0) for i = 1, n do local v = vals[i] out[i] = if type(v) == "table" then num(v[c]) else 0 end return out end local function putString(w: Bytes.Writer, s: string) w:u32le(#s) w:string(s) end local function putCFrames(w: Bytes.Writer, vals: { any }, n: number) for i = 1, n do local v = vals[i] local cf = if type(v) == "table" then v else { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 } local id = Rbx.rotationId(cf) w:u8(id) if id == 0 then for k = 4, 12 do w:f32le(num(cf[k])) end end end putF32s(w, column(vals, n, 1)) putF32s(w, column(vals, n, 2)) putF32s(w, column(vals, n, 3)) end local function hexByte(s: string, i: number): number return tonumber(string.sub(s, 2 * i - 1, 2 * i), 16) or 0 end -- encode(typeId, values, n, w, ctx) -> nil | message: values[1 .. n] in the codecs/model/Rbx forms (nil entries are -- the type's none / default) function Binary.encode(t: number, vals: { any }, n: number, w: Bytes.Writer, ctx: Ctx): string? if t == 0x01 or t == 0x1D then for i = 1, n do local v = vals[i] putString(w, if type(v) == "string" then v else "") end elseif t == 0x02 then for i = 1, n do w:u8(if vals[i] then 1 else 0) end elseif t == 0x03 then local c = table.create(n, 0) for i = 1, n do c[i] = num(vals[i]) end putI32s(w, c) elseif t == 0x04 then local c = table.create(n, 0) for i = 1, n do c[i] = num(vals[i]) end putF32s(w, c) elseif t == 0x05 then for i = 1, n do w:f64le(num(vals[i])) end elseif t == 0x06 then putF32s(w, column(vals, n, 1)) putI32s(w, column(vals, n, 2)) elseif t == 0x07 then putF32s(w, column(vals, n, 1)) putF32s(w, column(vals, n, 3)) putI32s(w, column(vals, n, 2)) putI32s(w, column(vals, n, 4)) elseif t == 0x08 then for i = 1, n do local v = vals[i] for c = 1, 6 do w:f32le(if type(v) == "table" then num(v[c]) else 0) end end elseif t == 0x09 or t == 0x0A then for i = 1, n do w:u8(num(vals[i]) % 256) end elseif t == 0x0B or t == 0x12 then local c = table.create(n, 0) for i = 1, n do c[i] = floor(num(vals[i])) end putU32s(w, c) elseif t == 0x0C or t == 0x0E or t == 0x0D or t == 0x18 then local k = if t == 0x0D then 2 elseif t == 0x18 then 4 else 3 for c = 1, k do putF32s(w, column(vals, n, c)) end elseif t == 0x0F or t == 0x14 then local k = if t == 0x0F then 2 else 3 for i = 1, n do local v = vals[i] for c = 1, k do local x = floor(if type(v) == "table" then num(v[c]) else 0) % 65536 w:u16le(x) end end elseif t == 0x10 then putCFrames(w, vals, n) elseif t == 0x13 then local refs = table.create(n, -1) local refOf = ctx.refOf for i = 1, n do refs[i] = if refOf and vals[i] ~= nil then refOf(vals[i]) else -1 end Binary.writeRefs(w, refs) elseif t == 0x15 or t == 0x16 then local k = if t == 0x15 then 3 else 5 for i = 1, n do local seq = vals[i] if type(seq) ~= "table" then seq = {} end w:u32le(#seq) for _, key in seq do for c = 1, k do w:f32le(if type(key) == "table" then num(key[c]) else 0) end end end elseif t == 0x17 then for i = 1, n do local v = vals[i] w:f32le(if type(v) == "table" then num(v[1]) else 0) w:f32le(if type(v) == "table" then num(v[2]) else 0) end elseif t == 0x19 then for i = 1, n do local v = vals[i] if type(v) ~= "table" then w:u8(0) else local acoustic = v[6] ~= nil and v[6] ~= 1 -- (rbx-dom leaves the default out) w:u8(if acoustic then 3 else 1) for c = 1, if acoustic then 6 else 5 do w:f32le(num(v[c])) end end end elseif t == 0x1A then for c = 1, 3 do for i = 1, n do local v = vals[i] w:u8(math.clamp(floor(if type(v) == "table" then num(v[c]) else 0), 0, 255)) end end elseif t == 0x1B or t == 0x21 then local c = table.create(n, 0) for i = 1, n do c[i] = num(vals[i]) end putI64s(w, c) elseif t == 0x1C then local idx = table.create(n, 0) local si = ctx.sharedIndex for i = 1, n do local v = vals[i] idx[i] = if si then si(if type(v) == "string" then v else "") else 0 end putU32s(w, idx) elseif t == 0x1E then w:u8(0x10) putCFrames(w, vals, n) w:u8(0x02) for i = 1, n do w:u8(if type(vals[i]) == "table" then 1 else 0) end elseif t == 0x1F then local p = w:reserve(16 * n) local b = w.buf for i = 0, n - 1 do local s = vals[i + 1] if type(s) ~= "string" or #s ~= 32 or string.find(s, "[^%x]") then return "a UniqueId is 32 hex digits" end -- index (hex 25..32) and time (17..24) first, then the random part (1..16) rotated left by one bit local by = table.create(16, 0) for k = 1, 4 do by[k] = hexByte(s, 12 + k) by[4 + k] = hexByte(s, 8 + k) end local top = hexByte(s, 1) // 128 for k = 1, 8 do local v = hexByte(s, k) local nxt = if k < 8 then hexByte(s, k + 1) // 128 else top by[8 + k] = (v * 2) % 256 + nxt end for k = 0, 15 do buffer.writeu8(b, p + k * n + i, by[k + 1]) end end w.pos = p + 16 * n elseif t == 0x20 then for i = 1, n do local v = vals[i] if type(v) ~= "table" then v = Rbx.default("Font") end putString(w, if type(v.family) == "string" then v.family else "") w:u16le(math.clamp(floor(num(v.weight)), 0, 65535)) w:u8(if v.style == "Italic" then 1 else 0) putString(w, if type(v.cachedFaceId) == "string" then v.cachedFaceId else "") end elseif t == 0x22 then local kinds, uris, objs = table.create(n, 0), {}, {} local refOf = ctx.refOf for i = 1, n do local v = vals[i] if type(v) == "string" then kinds[i] = 1 table.insert(uris, v) elseif type(v) == "table" then kinds[i] = 2 table.insert(objs, if refOf then refOf(v) else -1) end end putI32s(w, kinds) w:u32le(#uris) for _, s in uris do putString(w, s) end w:u32le(#objs) Binary.writeRefs(w, objs) w:u32le(0) else return "unknown type id " .. t end return nil end return Binary