179 lines
6.6 KiB
Luau
179 lines
6.6 KiB
Luau
--!strict
|
|
-- vector/tiled: Tiled maps, TMX (XML) / TMJ (JSON), and TSX / TSJ tilesets (Tiled 1.12 docs: "TMX Map Format", "JSON
|
|
-- Map Format"). Supports: every orientation, tile / object / image / group layers, infinite maps, typed properties,
|
|
-- tilesets. Not supported: zstd layer data (Err.UNSUPPORTED), Wang sets / terrains, loading external tilesets / images.
|
|
|
|
-- ------------------------------------------------------------------------------------------------ requires
|
|
local Err = require("@rosetta/core/Err")
|
|
local Sniff = require("@rosetta/core/Sniff")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Types = require("@rosetta/Types")
|
|
local Xml = require("@rosetta/codecs/text/Xml")
|
|
local Json = require("@rosetta/codecs/text/Json")
|
|
local Tmx = require("@rosetta/vector/tiled/Tmx")
|
|
local Tmj = require("@rosetta/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/text/Xml", "codecs/text/Json", "codecs/compress/Deflate", "core/Sniff" },
|
|
caps = { decode = true, encode = true, tilesets = true, infinite = true },
|
|
}
|
|
|
|
export type DecodeOptions = {
|
|
maxBytes: number?, -- default 256 MB
|
|
}
|
|
export type EncodeOptions = {
|
|
format: string?, -- "tmx" | "tsx" | "tmj" | "tsj" | "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?, -- nil | "zlib" | "gzip"
|
|
level: number?, -- default 6
|
|
indent: number?, -- default 1 (TMX), 2 (TMJ); 0 = compact JSON
|
|
}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ sniff
|
|
local function head(r: Bytes.Reader): string
|
|
local h = Sniff.text(r, 1024)
|
|
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
|
|
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: Tmx.WriteOptions = { encoding = o.encoding :: string?, 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
|