model/godot + data/tres + codecs/text/GodotVariant: Godot text scenes (.tscn / .escn) read + write against Godot 4's loader, .tres resources as data
docs/STYLE.md: the layout block lists codecs/text/GodotVariant (DOCS lint).
This commit is contained in:
1 parent
9c02ebdee5
commit
8f26e32a71
24 files changed
+3649
-5
No files matched your search
@@ -0,0 +1,657 @@
|
||||
--!strict
|
||||
-- codecs/text/GodotVariant: Godot's text resource syntax (.tscn / .tres / .escn, formats 2-4; Godot's
|
||||
-- variant_parser.cpp and resource_format_text.cpp): [section key=value ...] headers with `key = value` properties,
|
||||
-- and the Variant text syntax (numbers, strings, &"StringName", ^"NodePath", arrays, dictionaries, constructors such
|
||||
-- as Vector3(...), Transform3D(...), PackedFloat32Array(...), SubResource("id"), Object(Class, "k": v)).
|
||||
-- Constructor values are { type, args } tables (GodotVariant.isCtor); dictionaries keep their key order.
|
||||
local Types = require("@rosetta/Types")
|
||||
local Base64 = require("@rosetta/core/Base64")
|
||||
|
||||
local GodotVariant = {}
|
||||
|
||||
GodotVariant.null = Types.null
|
||||
|
||||
local CTOR = { __godot = true }
|
||||
|
||||
-- integral numbers written as floats ("1.0"): Godot keeps int and float apart (Curve._data mixes them), so the keys
|
||||
-- holding them are remembered per container (array, dictionary, property table) and written back with ".0"
|
||||
local FLOATS: { [any]: { [any]: boolean } } = setmetatable({}, { __mode = "k" }) :: any
|
||||
local lastFloat = false
|
||||
|
||||
local function mark(t: any, k: any, v: any)
|
||||
if lastFloat and type(v) == "number" and v == math.floor(v) then
|
||||
local m = FLOATS[t]
|
||||
if not m then
|
||||
m = {}
|
||||
FLOATS[t] = m
|
||||
end
|
||||
m[k] = true
|
||||
end
|
||||
end
|
||||
|
||||
function GodotVariant.isFloat(t: any, k: any): boolean
|
||||
local m = FLOATS[t]
|
||||
return m ~= nil and m[k] == true
|
||||
end
|
||||
|
||||
function GodotVariant.markFloat(t: any, k: any)
|
||||
local m = FLOATS[t]
|
||||
if not m then
|
||||
m = {}
|
||||
FLOATS[t] = m
|
||||
end
|
||||
m[k] = true
|
||||
end
|
||||
|
||||
function GodotVariant.copyFloats(from: any, to: any)
|
||||
local m = FLOATS[from]
|
||||
if m then
|
||||
FLOATS[to] = table.clone(m)
|
||||
end
|
||||
end
|
||||
|
||||
export type Ctor = { type: string, args: { any }, class: string? }
|
||||
export type Section = { tag: string, attrs: { [string]: any }, attrOrder: { string }, props: { [string]: any }, order: { string } }
|
||||
export type Document = { sections: { Section } }
|
||||
|
||||
function GodotVariant.ctor(name: string, args: { any }?): Ctor
|
||||
return setmetatable({ type = name, args = args or {} }, CTOR) :: any
|
||||
end
|
||||
|
||||
function GodotVariant.isCtor(v: any): boolean
|
||||
return type(v) == "table" and getmetatable(v) == CTOR
|
||||
end
|
||||
|
||||
-- the numbers of a constructor, or nil when an argument is not a number
|
||||
function GodotVariant.numbers(v: any, name: string?): { number }?
|
||||
if not GodotVariant.isCtor(v) or (name and v.type ~= name) then
|
||||
return nil
|
||||
end
|
||||
for _, x in v.args do
|
||||
if type(x) ~= "number" then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
return v.args :: { number }
|
||||
end
|
||||
|
||||
-- PackedByteArray(1, 2, ...) or PackedByteArray("base64") (Godot 4.3+), PoolByteArray / ByteArray (format 2)
|
||||
function GodotVariant.bytes(v: any): buffer?
|
||||
if not GodotVariant.isCtor(v) then
|
||||
return nil
|
||||
end
|
||||
local a = v.args
|
||||
if #a == 1 and type(a[1]) == "string" then
|
||||
return (Base64.decode(a[1]))
|
||||
end
|
||||
local b = buffer.create(#a)
|
||||
for i, x in a do
|
||||
if type(x) ~= "number" then
|
||||
return nil
|
||||
end
|
||||
buffer.writeu8(b, i - 1, x % 256)
|
||||
end
|
||||
return b
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ parse
|
||||
type P = { s: string, i: number, n: number, depth: number, maxDepth: number }
|
||||
|
||||
local NUMERIC_ARRAYS: { [string]: boolean } = {
|
||||
PackedByteArray = true, PackedInt32Array = true, PackedInt64Array = true, PackedFloat32Array = true,
|
||||
PackedFloat64Array = true, PackedVector2Array = true, PackedVector3Array = true, PackedVector4Array = true,
|
||||
PackedColorArray = true, PoolByteArray = true, PoolIntArray = true, PoolRealArray = true, PoolVector2Array = true,
|
||||
PoolVector3Array = true, PoolColorArray = true, ByteArray = true, IntArray = true, FloatArray = true, RealArray = true,
|
||||
Vector2Array = true, Vector3Array = true, ColorArray = true,
|
||||
}
|
||||
local WORDS: { [string]: any } = { ["true"] = true, ["false"] = false, ["inf"] = math.huge, ["-inf"] = -math.huge, inf_neg = -math.huge, nan = 0 / 0 }
|
||||
|
||||
local function skip(p: P)
|
||||
local s, i = p.s, p.i
|
||||
while true do
|
||||
local a = string.find(s, "[^ \t\r\n]", i)
|
||||
if not a then
|
||||
p.i = p.n + 1
|
||||
return
|
||||
end
|
||||
if string.byte(s, a) == 59 then -- ';' comment to the end of the line
|
||||
local e = string.find(s, "\n", a, true)
|
||||
if not e then
|
||||
p.i = p.n + 1
|
||||
return
|
||||
end
|
||||
i = e + 1
|
||||
else
|
||||
p.i = a
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local ESC: { [string]: string } = { n = "\n", t = "\t", r = "\r", b = "\b", f = "\f", ["\""] = "\"", ["\\"] = "\\", ["'"] = "'", ["/"] = "/" }
|
||||
|
||||
local function utf8char(c: number): string
|
||||
return utf8.char(math.clamp(c, 0, 0x10FFFF))
|
||||
end
|
||||
|
||||
local function str(p: P): (string?, string?)
|
||||
local s = p.s
|
||||
local i = p.i + 1
|
||||
local parts = {}
|
||||
while true do
|
||||
local a = string.find(s, "[\"\\]", i)
|
||||
if not a then
|
||||
return nil, "unterminated string"
|
||||
end
|
||||
table.insert(parts, string.sub(s, i, a - 1))
|
||||
if string.byte(s, a) == 34 then
|
||||
p.i = a + 1
|
||||
return table.concat(parts), nil
|
||||
end
|
||||
local c = string.sub(s, a + 1, a + 1)
|
||||
if c == "u" or c == "U" then
|
||||
local len = if c == "u" then 4 else 6
|
||||
local h = tonumber(string.sub(s, a + 2, a + 1 + len), 16)
|
||||
if not h then
|
||||
return nil, "bad \\u escape"
|
||||
end
|
||||
-- surrogate pairs
|
||||
if h >= 0xD800 and h < 0xDC00 and string.sub(s, a + 6, a + 7) == "\\u" then
|
||||
local lo = tonumber(string.sub(s, a + 8, a + 11), 16)
|
||||
if lo and lo >= 0xDC00 and lo < 0xE000 then
|
||||
h = 0x10000 + (h - 0xD800) * 1024 + (lo - 0xDC00)
|
||||
len += 6
|
||||
end
|
||||
end
|
||||
table.insert(parts, utf8char(h))
|
||||
i = a + 2 + len
|
||||
else
|
||||
table.insert(parts, ESC[c] or c)
|
||||
i = a + 2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local value: (p: P) -> (any, string?)
|
||||
|
||||
local function numberList(p: P, name: string): (any, string?)
|
||||
local s = p.s
|
||||
local close = string.find(s, ")", p.i, true)
|
||||
if not close then
|
||||
return nil, "unterminated " .. name
|
||||
end
|
||||
local body = string.sub(s, p.i, close - 1)
|
||||
-- base64 PackedByteArray, or a list of plain numbers
|
||||
local q = string.match(body, "^%s*\"([^\"]*)\"%s*$")
|
||||
if q then
|
||||
p.i = close + 1
|
||||
return GodotVariant.ctor(name, { q }), nil
|
||||
end
|
||||
local out = {}
|
||||
local k = 0
|
||||
for tok in string.gmatch(body, "[^,%s]+") do
|
||||
local v = tonumber(tok)
|
||||
if v == nil then
|
||||
v = WORDS[tok]
|
||||
if type(v) ~= "number" then
|
||||
return nil, nil -- not purely numeric: parse it generically
|
||||
end
|
||||
end
|
||||
k += 1
|
||||
out[k] = v
|
||||
end
|
||||
p.i = close + 1
|
||||
return GodotVariant.ctor(name, out), nil
|
||||
end
|
||||
|
||||
local function list(p: P, close: number, into: { any }): string?
|
||||
skip(p)
|
||||
if string.byte(p.s, p.i) == close then
|
||||
p.i += 1
|
||||
return nil
|
||||
end
|
||||
while true do
|
||||
local v, err = value(p)
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
table.insert(into, v)
|
||||
mark(into, #into, v)
|
||||
skip(p)
|
||||
local c = string.byte(p.s, p.i)
|
||||
if c == 44 then -- ','
|
||||
p.i += 1
|
||||
skip(p)
|
||||
if string.byte(p.s, p.i) == close then
|
||||
p.i += 1
|
||||
return nil
|
||||
end
|
||||
elseif c == close then
|
||||
p.i += 1
|
||||
return nil
|
||||
else
|
||||
return "expected ',' or '" .. string.char(close) .. "' at byte " .. (p.i - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function dict(p: P): (any, string?)
|
||||
p.i += 1
|
||||
local t, keys = {}, {}
|
||||
skip(p)
|
||||
if string.byte(p.s, p.i) == 125 then
|
||||
p.i += 1
|
||||
return Types.object(t, keys), nil
|
||||
end
|
||||
while true do
|
||||
local k, err = value(p)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
skip(p)
|
||||
if string.byte(p.s, p.i) ~= 58 then
|
||||
return nil, "expected ':' at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
local v, verr = value(p)
|
||||
if verr then
|
||||
return nil, verr
|
||||
end
|
||||
local key = if type(k) == "string" or type(k) == "number" then k else GodotVariant.format(k)
|
||||
if t[key] == nil then
|
||||
table.insert(keys, key)
|
||||
end
|
||||
t[key] = v
|
||||
mark(t, key, v)
|
||||
skip(p)
|
||||
local c = string.byte(p.s, p.i)
|
||||
if c == 44 then
|
||||
p.i += 1
|
||||
skip(p)
|
||||
if string.byte(p.s, p.i) == 125 then
|
||||
p.i += 1
|
||||
break
|
||||
end
|
||||
elseif c == 125 then
|
||||
p.i += 1
|
||||
break
|
||||
else
|
||||
return nil, "expected ',' or '}' at byte " .. (p.i - 1)
|
||||
end
|
||||
end
|
||||
return Types.object(t, keys), nil
|
||||
end
|
||||
|
||||
value = function(p: P): (any, string?)
|
||||
skip(p)
|
||||
if p.i > p.n then
|
||||
return nil, "truncated: value expected"
|
||||
end
|
||||
p.depth += 1
|
||||
if p.depth > p.maxDepth then
|
||||
return nil, "limit: nested deeper than " .. p.maxDepth
|
||||
end
|
||||
local s = p.s
|
||||
local c = string.byte(s, p.i)
|
||||
local v: any, err: string? = nil, nil
|
||||
local isFloat = false
|
||||
if c == 34 then
|
||||
v, err = str(p)
|
||||
elseif c == 38 or c == 94 then -- &"StringName", ^"NodePath"
|
||||
p.i += 1
|
||||
local x, e = str(p)
|
||||
v, err = if x then GodotVariant.ctor(if c == 38 then "StringName" else "NodePath", { x }) else nil, e
|
||||
elseif c == 91 then -- '['
|
||||
p.i += 1
|
||||
local out = {}
|
||||
err = list(p, 93, out)
|
||||
v = out
|
||||
elseif c == 123 then -- '{'
|
||||
v, err = dict(p)
|
||||
else
|
||||
local num = string.match(s, "^[-+]?%d[%w%.+-]*", p.i) or string.match(s, "^[-+]?%.%d[%w%.+-]*", p.i)
|
||||
if num then
|
||||
-- 1e-05, 0x1F, 1.5; a trailing "f" is not Godot's
|
||||
local x = tonumber(num)
|
||||
if x == nil then
|
||||
local m = string.match(num, "^[-+]?[%d%.]+[eE][-+]?%d+") or string.match(num, "^[-+]?[%d%.]+")
|
||||
x = if m then tonumber(m) else nil
|
||||
num = m or num
|
||||
end
|
||||
if x == nil then
|
||||
return nil, "bad number " .. num .. " at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += #num
|
||||
v = x
|
||||
isFloat = string.find(num, "[%.eE]") ~= nil and string.sub(num, 1, 2) ~= "0x"
|
||||
else
|
||||
local word = string.match(s, "^[%a_][%w_]*", p.i) or string.match(s, "^%-inf", p.i)
|
||||
if not word then
|
||||
return nil, "unexpected character at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += #word
|
||||
if word == "null" or word == "nil" then
|
||||
v = Types.null
|
||||
elseif WORDS[word] ~= nil then
|
||||
v = WORDS[word]
|
||||
else
|
||||
-- typed array: Array[int]([...]); Dictionary[String, int]({...})
|
||||
local typed = string.match(s, "^%[[%w_, ]*%]", p.i)
|
||||
if typed and (word == "Array" or word == "Dictionary") then
|
||||
word = word .. typed
|
||||
p.i += #typed
|
||||
end
|
||||
skip(p)
|
||||
if string.byte(s, p.i) ~= 40 then
|
||||
return nil, "expected '(' after " .. word .. " at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
if NUMERIC_ARRAYS[word] then
|
||||
local save = p.i
|
||||
v, err = numberList(p, word)
|
||||
if v == nil and err == nil then
|
||||
p.i = save
|
||||
local args = {}
|
||||
err = list(p, 41, args)
|
||||
v = GodotVariant.ctor(word, args)
|
||||
end
|
||||
elseif word == "Object" then
|
||||
skip(p)
|
||||
local cls = string.match(s, "^[%w_]+", p.i)
|
||||
if not cls then
|
||||
return nil, "Object without a class at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += #cls
|
||||
local props, keys = {}, {}
|
||||
skip(p)
|
||||
while string.byte(s, p.i) == 44 do
|
||||
p.i += 1
|
||||
local k, kerr = value(p)
|
||||
if kerr then
|
||||
return nil, kerr
|
||||
end
|
||||
skip(p)
|
||||
if string.byte(s, p.i) ~= 58 then
|
||||
return nil, "expected ':' in Object at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
local x, xerr = value(p)
|
||||
if xerr then
|
||||
return nil, xerr
|
||||
end
|
||||
if type(k) == "string" then
|
||||
props[k] = x
|
||||
table.insert(keys, k)
|
||||
end
|
||||
skip(p)
|
||||
end
|
||||
if string.byte(s, p.i) ~= 41 then
|
||||
return nil, "expected ')' after Object at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
local o = GodotVariant.ctor("Object", { Types.object(props, keys) })
|
||||
o.class = cls
|
||||
v = o
|
||||
else
|
||||
local args = {}
|
||||
err = list(p, 41, args)
|
||||
v = GodotVariant.ctor(word, args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
p.depth -= 1
|
||||
lastFloat = isFloat
|
||||
return v, err
|
||||
end
|
||||
|
||||
function GodotVariant.value(text: string, maxDepth: number?): (any, string?)
|
||||
local p: P = { s = text, i = 1, n = #text, depth = 0, maxDepth = maxDepth or 256 }
|
||||
return value(p)
|
||||
end
|
||||
|
||||
-- a property key: up to '=' (keys hold '/', ':', '.', or are quoted)
|
||||
local function key(p: P): string?
|
||||
local s = p.s
|
||||
if string.byte(s, p.i) == 34 then
|
||||
local k = str(p)
|
||||
return k
|
||||
end
|
||||
local k = string.match(s, "^[^=\n%[]+", p.i)
|
||||
if not k then
|
||||
return nil
|
||||
end
|
||||
p.i += #k
|
||||
return (string.gsub(k, "%s+$", ""))
|
||||
end
|
||||
|
||||
function GodotVariant.parse(text: string, opts: { maxDepth: number?, maxSections: number? }?): (Document?, string?)
|
||||
local o: { maxDepth: number?, maxSections: number? } = opts or {}
|
||||
if string.sub(text, 1, 3) == "\239\187\191" then
|
||||
text = string.sub(text, 4)
|
||||
end
|
||||
local p: P = { s = text, i = 1, n = #text, depth = 0, maxDepth = o.maxDepth or 256 }
|
||||
local maxSections = o.maxSections or 2 ^ 22
|
||||
local sections: { Section } = {}
|
||||
local cur: Section? = nil
|
||||
local s = text
|
||||
while true do
|
||||
skip(p)
|
||||
if p.i > p.n then
|
||||
break
|
||||
end
|
||||
if string.byte(s, p.i) == 91 then
|
||||
p.i += 1
|
||||
skip(p)
|
||||
local tag = string.match(s, "^[%w_]+", p.i)
|
||||
if not tag then
|
||||
return nil, "bad section header at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += #tag
|
||||
local sec: Section = { tag = tag, attrs = {}, attrOrder = {}, props = {}, order = {} }
|
||||
while true do
|
||||
skip(p)
|
||||
local c = string.byte(s, p.i)
|
||||
if c == 93 then
|
||||
p.i += 1
|
||||
break
|
||||
end
|
||||
local k = string.match(s, "^[%w_/]+", p.i)
|
||||
if not k then
|
||||
return nil, "bad attribute in [" .. tag .. "] at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += #k
|
||||
skip(p)
|
||||
if string.byte(s, p.i) ~= 61 then
|
||||
return nil, "expected '=' in [" .. tag .. "] at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
local v, err = value(p)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
if sec.attrs[k] == nil then
|
||||
table.insert(sec.attrOrder, k)
|
||||
end
|
||||
sec.attrs[k] = v
|
||||
end
|
||||
if #sections >= maxSections then
|
||||
return nil, "limit: over " .. maxSections .. " sections"
|
||||
end
|
||||
table.insert(sections, sec)
|
||||
cur = sec
|
||||
else
|
||||
local at = p.i
|
||||
local k = key(p)
|
||||
if not k or k == "" then
|
||||
return nil, "bad property at byte " .. (at - 1)
|
||||
end
|
||||
skip(p)
|
||||
if string.byte(s, p.i) ~= 61 then
|
||||
return nil, "expected '=' after " .. k .. " at byte " .. (p.i - 1)
|
||||
end
|
||||
p.i += 1
|
||||
local v, err = value(p)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
if not cur then
|
||||
cur = { tag = "", attrs = {}, attrOrder = {}, props = {}, order = {} }
|
||||
table.insert(sections, cur :: Section)
|
||||
end
|
||||
local c = cur :: Section
|
||||
if c.props[k] == nil then
|
||||
table.insert(c.order, k)
|
||||
end
|
||||
c.props[k] = v
|
||||
mark(c.props, k, v)
|
||||
end
|
||||
end
|
||||
return { sections = sections }, nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ format
|
||||
local function num(x: number): string
|
||||
if x ~= x then
|
||||
return "nan"
|
||||
elseif x == math.huge then
|
||||
return "inf"
|
||||
elseif x == -math.huge then
|
||||
return "-inf"
|
||||
elseif x == math.floor(x) and math.abs(x) < 2 ^ 53 then
|
||||
return string.format("%d", x)
|
||||
end
|
||||
for d = 6, 17 do
|
||||
local s = string.format("%." .. d .. "g", x)
|
||||
if tonumber(s) == x then
|
||||
return s
|
||||
end
|
||||
end
|
||||
return string.format("%.17g", x)
|
||||
end
|
||||
GodotVariant.number = num
|
||||
|
||||
local function quote(s: string): string
|
||||
return "\"" .. string.gsub(s, "[\\\"\n\t\r]", { ["\\"] = "\\\\", ["\""] = "\\\"", ["\n"] = "\\n", ["\t"] = "\\t", ["\r"] = "\\r" }) .. "\""
|
||||
end
|
||||
GodotVariant.quote = quote
|
||||
|
||||
local format: (v: any, depth: number, indent: boolean) -> string
|
||||
|
||||
local function item(t: any, k: any, depth: number, indent: boolean): string
|
||||
local v = t[k]
|
||||
if type(v) == "number" and v == math.floor(v) and math.abs(v) < 2 ^ 53 and GodotVariant.isFloat(t, k) then
|
||||
return string.format("%d", v) .. ".0"
|
||||
end
|
||||
return format(v, depth, indent)
|
||||
end
|
||||
GodotVariant.item = function(t: any, k: any): string
|
||||
return item(t, k, 0, false)
|
||||
end
|
||||
|
||||
format = function(v: any, depth: number, indent: boolean): string
|
||||
if depth > 256 then
|
||||
return "null"
|
||||
end
|
||||
local t = type(v)
|
||||
if t == "number" then
|
||||
return num(v)
|
||||
elseif t == "string" then
|
||||
return quote(v)
|
||||
elseif t == "boolean" then
|
||||
return if v then "true" else "false"
|
||||
elseif v == nil or v == Types.null then
|
||||
return "null"
|
||||
elseif t == "buffer" then
|
||||
local parts = table.create(buffer.len(v))
|
||||
for i = 0, buffer.len(v) - 1 do
|
||||
parts[i + 1] = tostring(buffer.readu8(v, i))
|
||||
end
|
||||
return "PackedByteArray(" .. table.concat(parts, ", ") .. ")"
|
||||
elseif t ~= "table" then
|
||||
return "null"
|
||||
end
|
||||
if GodotVariant.isCtor(v) then
|
||||
if v.type == "StringName" and type(v.args[1]) == "string" then
|
||||
return "&" .. quote(v.args[1])
|
||||
elseif v.type == "Object" then
|
||||
local props = v.args[1]
|
||||
local parts = { tostring(v.class or "Object") }
|
||||
if type(props) == "table" then
|
||||
for _, k in Types.keys(props) do
|
||||
table.insert(parts, quote(tostring(k)) .. ": " .. format(props[k], depth + 1, false))
|
||||
end
|
||||
end
|
||||
return "Object(" .. table.concat(parts, ",") .. ")"
|
||||
end
|
||||
local parts: { string } = table.create(#v.args) :: any
|
||||
for i, x in v.args do
|
||||
parts[i] = format(x, depth + 1, false)
|
||||
end
|
||||
local vt: string = tostring(v.type)
|
||||
if string.match(vt, "^(%a+)%[") then
|
||||
local inner: string = parts[1] or "[]"
|
||||
return vt .. "(" .. inner .. ")"
|
||||
end
|
||||
return vt .. "(" .. table.concat(parts, ", ") .. ")"
|
||||
end
|
||||
if Types.isObject(v) or not Types.isArray(v) then
|
||||
local keys = Types.keys(v)
|
||||
if #keys == 0 then
|
||||
return "{}"
|
||||
end
|
||||
local parts = {}
|
||||
for _, k in keys do
|
||||
local ks = if type(k) == "string" then quote(k) else format(k, depth + 1, false)
|
||||
table.insert(parts, ks .. ": " .. item(v, k, depth + 1, false))
|
||||
end
|
||||
if indent then
|
||||
return "{\n" .. table.concat(parts, ",\n") .. "\n}"
|
||||
end
|
||||
return "{\n" .. table.concat(parts, ",\n") .. "\n}"
|
||||
end
|
||||
local parts = table.create(#v)
|
||||
for i in v do
|
||||
parts[i] = item(v, i, depth + 1, true)
|
||||
end
|
||||
return "[" .. table.concat(parts, ", ") .. "]"
|
||||
end
|
||||
|
||||
function GodotVariant.format(v: any): string
|
||||
return format(v, 0, false)
|
||||
end
|
||||
|
||||
function GodotVariant.encode(doc: Document): string
|
||||
local out = {}
|
||||
for i, sec in doc.sections do
|
||||
if sec.tag ~= "" then
|
||||
local parts = { "[" .. sec.tag }
|
||||
local order = sec.attrOrder
|
||||
if #order == 0 then
|
||||
order = Types.keys(sec.attrs)
|
||||
end
|
||||
for _, k in order do
|
||||
if sec.attrs[k] ~= nil then
|
||||
table.insert(parts, k .. "=" .. GodotVariant.format(sec.attrs[k]))
|
||||
end
|
||||
end
|
||||
if i > 1 then
|
||||
table.insert(out, "")
|
||||
end
|
||||
table.insert(out, table.concat(parts, " ") .. "]")
|
||||
end
|
||||
local order = sec.order
|
||||
if #order == 0 then
|
||||
order = Types.keys(sec.props)
|
||||
end
|
||||
for _, k in order do
|
||||
if sec.props[k] ~= nil then
|
||||
local ks = if string.find(k, "^[%w_/:%.]+$") then k else quote(k)
|
||||
table.insert(out, ks .. " = " .. item(sec.props, k, 0, false))
|
||||
end
|
||||
end
|
||||
end
|
||||
return table.concat(out, "\n") .. "\n"
|
||||
end
|
||||
|
||||
return GodotVariant
|
||||
Reference in new issue
Block a user