150 lines
3.8 KiB
Luau
150 lines
3.8 KiB
Luau
--!strict
|
|
-- vector/tiled/Data: tile layer data shared by TMX and TMJ: CSV, base64 (raw / zlib / gzip), properties, numbers.
|
|
local Base64 = require("@rosetta/core/Base64")
|
|
local Deflate = require("@rosetta/codecs/compress/Deflate")
|
|
|
|
local Data = {}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
-- integers without a fraction (gids up to 2^32), 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
|
|
|
|
-- Tiled's CSV 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
|
|
|
|
-- Tiled's canonical colour text: lower case "#rrggbb", or "#aarrggbb" when not opaque
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|