--!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