190 lines
7.5 KiB
Luau
190 lines
7.5 KiB
Luau
--!strict
|
|
-- vector/tiled: Tiled maps, TMX (XML) and TMJ (JSON), and standalone TSX / TSJ tilesets (Tiled 1.12 docs: "TMX Map
|
|
-- Format", "JSON Map Format"). Supports: every orientation's attributes, tile layers (CSV, base64 uncompressed / zlib /
|
|
-- gzip, the old <tile gid> XML), infinite maps (chunks), flip bits kept in the gids, object groups (rectangles,
|
|
-- ellipses, points, polygons, polylines, text, tile objects, templates as references), image layers, group layers,
|
|
-- typed custom properties (string / int / float / bool / color / file / object / class) on everything, tilesets
|
|
-- (embedded, or external references kept as `source`), their image as a path (imageSource: tileset images stay
|
|
-- references), per-tile class, properties, images, animations and collision object groups. Both flavours decode to the
|
|
-- same Types.TileMap and encode from it: opts.format = "tmx" (default: the source's flavour) or "tmj" / "json".
|
|
-- Not supported: zstd-compressed layer data (Err.UNSUPPORTED), Wang sets / terrains (dropped), loading external
|
|
-- tilesets or images (callers resolve `source` / `imageSource` paths themselves).
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@formats/core/Err")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Types = require("@formats/Types")
|
|
local Xml = require("@formats/codecs/Xml")
|
|
local Json = require("@formats/codecs/Json")
|
|
local Tmx = require("@formats/vector/tiled/Tmx")
|
|
local Tmj = require("@formats/vector/tiled/Tmj")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
local Tiled = {
|
|
name = "tiled",
|
|
category = "vector",
|
|
extensions = { "tmx", "tmj", "tsx", "tsj" },
|
|
mime = { "application/x-tiled-tmx", "application/x-tiled-tmj" },
|
|
deps = { "core/Err", "core/Bytes", "core/Base64", "Types", "codecs/Xml", "codecs/Json", "codecs/Deflate" },
|
|
caps = { decode = true, encode = true, tilesets = true, infinite = true },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- refuse larger files (default 256 MB)
|
|
}
|
|
export type EncodeOptions = {
|
|
format: string?, -- "tmx" | "tsx" (XML) or "tmj" | "tsj" | "json" (JSON); default: the source's flavour, else tmx
|
|
encoding: string?, -- tile data: "csv" (TMX default) | "base64" | "xml" (TMX only); TMJ: arrays unless "base64"
|
|
compression: string?, -- base64 data: nil | "zlib" | "gzip"
|
|
level: number?, -- deflate level for compressed data (default 6)
|
|
indent: number?, -- spaces per level (default 1 for TMX, 2 for TMJ; 0 = compact JSON)
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
local function head(r: Bytes.Reader): string
|
|
local h = r:peek(1024)
|
|
if string.sub(h, 1, 3) == "\239\187\191" then
|
|
h = string.sub(h, 4)
|
|
end
|
|
return h
|
|
end
|
|
|
|
function Tiled.sniff(r: Bytes.Reader): number
|
|
local h = head(r)
|
|
if string.match(h, "^%s*<") then
|
|
if string.find(h, "<map[%s>]") or string.find(h, "<tileset[%s>]") then
|
|
return 0.9
|
|
end
|
|
return 0
|
|
end
|
|
if string.match(h, "^%s*{") then
|
|
-- Tiled writes its keys sorted: "type" comes last, so look at the tail too
|
|
local n = r:remaining()
|
|
local tail = if n > 1024 then r:peek(1024, n - 1024) else ""
|
|
for _, part in { h, tail } do
|
|
if string.find(part, "\"type\"%s*:%s*\"map\"") or string.find(part, "\"type\"%s*:%s*\"tileset\"") then
|
|
return 0.8
|
|
end
|
|
end
|
|
if string.find(h, "\"tilewidth\"", 1, true) and (string.find(h, "\"orientation\"", 1, true) or string.find(h, "\"tilesets\"", 1, true)) then
|
|
return 0.6
|
|
end
|
|
end
|
|
return 0
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode
|
|
-- decode(r, opts?) -> TileMap (or, for a .tsx / .tsj file, a TileMap with that one tileset and no layers; its
|
|
-- meta.tileset is true) | nil, Err
|
|
function Tiled.decode(r: Bytes.Reader, opts: DecodeOptions?): (Types.TileMap?, Err.Err?)
|
|
local maxBytes = opts and opts.maxBytes or 2 ^ 28
|
|
if r.limit - r.base > maxBytes then
|
|
return nil, Err.new(Err.LIMIT, "file is over the byte limit")
|
|
end
|
|
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
|
local h = head(r)
|
|
local map: any, msg: string?, unsupported: boolean?
|
|
local flavour
|
|
if string.match(h, "^%s*<") then
|
|
local doc, xerr = Xml.decode(text)
|
|
if not doc then
|
|
return nil, xerr
|
|
end
|
|
local root = (doc :: any).root
|
|
if root == nil then
|
|
return nil, Err.new(Err.CORRUPT, "no root element")
|
|
end
|
|
if root.name == "tileset" then
|
|
map = Types.TileMap.new(0, 0, 0, 0)
|
|
map.tilesets = { Tmx.tileset(root) }
|
|
map.meta.tileset = true
|
|
flavour = "tsx"
|
|
else
|
|
map, msg, unsupported = Tmx.read(root)
|
|
flavour = "tmx"
|
|
end
|
|
else
|
|
local v, jerr = Json.decode(text)
|
|
if v == nil then
|
|
return nil, jerr
|
|
end
|
|
if type(v) == "table" and v.type == "tileset" then
|
|
map = Types.TileMap.new(0, 0, 0, 0)
|
|
map.tilesets = { Tmj.tileset(v) }
|
|
map.meta.tileset = true
|
|
flavour = "tsj"
|
|
else
|
|
map, msg, unsupported = Tmj.read(v)
|
|
flavour = "tmj"
|
|
end
|
|
end
|
|
if not map then
|
|
return nil, Err.new(if unsupported then Err.UNSUPPORTED else Err.CORRUPT, msg or "bad map")
|
|
end
|
|
map.meta.source = { format = flavour }
|
|
return map, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ encode
|
|
function Tiled.encode(map: Types.TileMap, opts: EncodeOptions?): (buffer?, Err.Err?)
|
|
if type(map) ~= "table" or type(map.layers) ~= "table" or type(map.tilesets) ~= "table" then
|
|
return nil, Err.new(Err.ARG, "needs a TileMap value")
|
|
end
|
|
local o: EncodeOptions = opts or {}
|
|
local meta: any = if type(map.meta) == "table" then map.meta else {}
|
|
local fmt = o.format or (if type(meta.source) == "table" then meta.source.format else nil) or "tmx"
|
|
fmt = string.lower(fmt)
|
|
if fmt == "tiled" then
|
|
fmt = if type(meta.source) == "table" and meta.source.format then meta.source.format else "tmx"
|
|
end
|
|
local tilesetOnly = meta.tileset == true or fmt == "tsx" or fmt == "tsj"
|
|
if o.encoding and o.encoding ~= "csv" and o.encoding ~= "base64" and o.encoding ~= "xml" then
|
|
return nil, Err.new(Err.ARG, "encoding must be csv, base64 or xml")
|
|
end
|
|
if o.compression and o.compression ~= "zlib" and o.compression ~= "gzip" then
|
|
return nil, Err.new(Err.ARG, "compression must be zlib or gzip")
|
|
end
|
|
local wo = { encoding = o.encoding, compression = o.compression, level = o.level }
|
|
if fmt == "tmx" or fmt == "tsx" or fmt == "xml" then
|
|
local doc: any, err: string?
|
|
if tilesetOnly then
|
|
if not map.tilesets[1] then
|
|
return nil, Err.new(Err.ARG, "no tileset to write")
|
|
end
|
|
doc = Xml.document(Tmx.tilesetNode(map.tilesets[1], false), { version = "1.0", encoding = "UTF-8" })
|
|
else
|
|
doc, err = Tmx.write(map, wo)
|
|
end
|
|
if not doc then
|
|
return nil, Err.new(Err.ARG, err or "bad map")
|
|
end
|
|
local s, xerr = Xml.encode(doc, { indent = o.indent or 1 })
|
|
if not s then
|
|
return nil, xerr
|
|
end
|
|
return buffer.fromstring(s .. "\n"), nil
|
|
elseif fmt == "tmj" or fmt == "tsj" or fmt == "json" then
|
|
local v: any, err: string?
|
|
if tilesetOnly then
|
|
if not map.tilesets[1] then
|
|
return nil, Err.new(Err.ARG, "no tileset to write")
|
|
end
|
|
v = Tmj.tilesetValue(map.tilesets[1], false)
|
|
else
|
|
v, err = Tmj.write(map, wo)
|
|
end
|
|
if v == nil then
|
|
return nil, Err.new(Err.ARG, err or "bad map")
|
|
end
|
|
local indent = if o.indent == 0 then nil else (o.indent or 2)
|
|
local s, jerr = Json.encode(v, { indent = indent })
|
|
if not s then
|
|
return nil, jerr
|
|
end
|
|
return buffer.fromstring(s .. "\n"), nil
|
|
end
|
|
return nil, Err.new(Err.ARG, "unknown format " .. tostring(fmt) .. " (tmx, tmj, tsx, tsj)")
|
|
end
|
|
|
|
return Tiled
|