C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
157 lines
4.4 KiB
Luau
157 lines
4.4 KiB
Luau
--!strict
|
|
-- vector/tiled/Data: tile layer data shared by the TMX and TMJ halves: CSV text, base64 (uncompressed, zlib, gzip)
|
|
-- to gids and back; property values and their types; number formatting. Never throws.
|
|
local Base64 = require("@formats/core/Base64")
|
|
local Deflate = require("@formats/codecs/compress/Deflate")
|
|
|
|
local Data = {}
|
|
|
|
-- csv(text) -> { gid } (any whitespace / commas between the numbers) | nil, message
|
|
function Data.csv(text: string): ({ number }?, string?)
|
|
local out = {}
|
|
for tok in string.gmatch(text, "[^,%s]+") do
|
|
local v = tonumber(tok)
|
|
if v == nil or v < 0 or v >= 4294967296 or v % 1 ~= 0 then
|
|
return nil, "bad gid " .. string.sub(tok, 1, 20)
|
|
end
|
|
table.insert(out, v)
|
|
end
|
|
return out, nil
|
|
end
|
|
|
|
-- base64(text, compression) -> { gid } | nil, message, unsupported?
|
|
function Data.base64(text: string, compression: string?): ({ number }?, string?, boolean?)
|
|
local raw, err = Base64.decode(text)
|
|
if not raw then
|
|
return nil, err, nil
|
|
end
|
|
local bytes: buffer = raw
|
|
if compression == "zlib" then
|
|
local z, zerr = Deflate.zlib(bytes)
|
|
if not z then
|
|
return nil, "zlib: " .. tostring(zerr), nil
|
|
end
|
|
bytes = z
|
|
elseif compression == "gzip" then
|
|
local z, zerr = Deflate.gunzip(bytes)
|
|
if not z then
|
|
return nil, "gzip: " .. tostring(zerr), nil
|
|
end
|
|
bytes = z
|
|
elseif compression ~= nil and compression ~= "" then
|
|
return nil, "compression " .. compression .. " is not supported", true
|
|
end
|
|
local n = buffer.len(bytes) // 4
|
|
local out: { number } = table.create(n) :: { number }
|
|
for i = 0, n - 1 do
|
|
out[i + 1] = buffer.readu32(bytes, i * 4)
|
|
end
|
|
return out, nil, nil
|
|
end
|
|
|
|
-- toBase64(gids, compression?, level?) -> text | nil, message
|
|
function Data.toBase64(gids: { number }, compression: string?, level: number?): (string?, string?)
|
|
local b = buffer.create(#gids * 4)
|
|
for i, g in gids do
|
|
buffer.writeu32(b, (i - 1) * 4, g % 4294967296)
|
|
end
|
|
if compression == "zlib" then
|
|
local z = Deflate.zlibCompress(b, { level = level or 6 })
|
|
if not z then
|
|
return nil, "zlib failed"
|
|
end
|
|
b = z
|
|
elseif compression == "gzip" then
|
|
local z = Deflate.gzip(b, { level = level or 6 })
|
|
if not z then
|
|
return nil, "gzip failed"
|
|
end
|
|
b = z
|
|
elseif compression ~= nil and compression ~= "" then
|
|
return nil, "compression " .. compression .. " is not supported"
|
|
end
|
|
return Base64.encode(b), nil
|
|
end
|
|
|
|
-- num(v) -> text: integers without a fraction (gids up to 2^32 too), others in the shortest form both runtimes print
|
|
function Data.num(v: number): string
|
|
if v % 1 == 0 and math.abs(v) < 2 ^ 53 then
|
|
return string.format("%.0f", v)
|
|
end
|
|
local s = string.format("%.15g", v)
|
|
if tonumber(s) ~= v then
|
|
s = string.format("%.17g", v)
|
|
end
|
|
return s
|
|
end
|
|
|
|
-- csvText(gids, width) -> "1,2,3,\n4,5,6" (Tiled's layout: one row per line)
|
|
function Data.csvText(gids: { number }, width: number): string
|
|
local rows = {}
|
|
local row = {}
|
|
for i, g in gids do
|
|
table.insert(row, Data.num(g))
|
|
if width > 0 and i % width == 0 then
|
|
table.insert(rows, table.concat(row, ","))
|
|
row = {}
|
|
end
|
|
end
|
|
if #row > 0 then
|
|
table.insert(rows, table.concat(row, ","))
|
|
end
|
|
return "\n" .. table.concat(rows, ",\n") .. "\n"
|
|
end
|
|
|
|
-- color(s) -> Tiled's canonical colour text: lower case "#rrggbb", or "#aarrggbb" when not opaque (nil stays nil)
|
|
function Data.color(s: string?): string?
|
|
if type(s) ~= "string" then
|
|
return nil
|
|
end
|
|
local h = string.lower((string.gsub(s, "^#", "")))
|
|
if #h == 8 and string.sub(h, 1, 2) == "ff" then
|
|
h = string.sub(h, 3)
|
|
end
|
|
return "#" .. h
|
|
end
|
|
|
|
-- property values: TMX / TMJ type names -> a Luau value from text
|
|
function Data.value(typ: string, text: string): any
|
|
if typ == "int" or typ == "float" or typ == "object" then
|
|
return tonumber(text) or 0
|
|
elseif typ == "bool" then
|
|
return text == "true" or text == "1"
|
|
elseif typ == "color" then
|
|
return Data.color(text) or ""
|
|
end
|
|
return text
|
|
end
|
|
|
|
-- typeOf(v, declared?) -> the Tiled type to write for v
|
|
function Data.typeOf(v: any, declared: string?): string
|
|
if declared then
|
|
return declared
|
|
end
|
|
if type(v) == "boolean" then
|
|
return "bool"
|
|
elseif type(v) == "number" then
|
|
return if v % 1 == 0 then "int" else "float"
|
|
elseif type(v) == "table" then
|
|
return "class"
|
|
end
|
|
return "string"
|
|
end
|
|
|
|
-- sortedKeys(t) -> its string keys, sorted
|
|
function Data.sortedKeys(t: { [any]: any }): { string }
|
|
local out = {}
|
|
for k in pairs(t) do
|
|
if type(k) == "string" then
|
|
table.insert(out, k)
|
|
end
|
|
end
|
|
table.sort(out)
|
|
return out
|
|
end
|
|
|
|
return Data
|