vector formats: comments are specs only

This commit is contained in:
4DBug committed 2026-11-01 01:53:48 -05:00
1 parent d91dc6c597
commit 89a2047349
10 files changed
+34 -126

No files matched your search

+4 -11
View File
@@ -1,12 +1,10 @@
--!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.
-- 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 = {}
-- 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
@@ -19,7 +17,6 @@ function Data.csv(text: string): ({ number }?, string?)
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
@@ -49,7 +46,6 @@ function Data.base64(text: string, compression: string?): ({ number }?, string?,
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
@@ -73,7 +69,7 @@ function Data.toBase64(gids: { number }, compression: string?, level: number?):
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
-- 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)
@@ -85,7 +81,7 @@ function Data.num(v: number): string
return s
end
-- csvText(gids, width) -> "1,2,3,\n4,5,6" (Tiled's layout: one row per line)
-- Tiled's CSV layout: one row per line
function Data.csvText(gids: { number }, width: number): string
local rows = {}
local row = {}
@@ -102,7 +98,7 @@ function Data.csvText(gids: { number }, width: number): string
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)
-- 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
@@ -114,7 +110,6 @@ function Data.color(s: string?): string?
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
@@ -126,7 +121,6 @@ function Data.value(typ: string, text: string): any
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
@@ -141,7 +135,6 @@ function Data.typeOf(v: any, declared: string?): string
return "string"
end
-- sortedKeys(t) -> its string keys, sorted
function Data.sortedKeys(t: { [any]: any }): { string }
local out = {}
for k in pairs(t) do