codecs/text/KeyValues: Valve KeyValues text (conditions, #include / #base, duplicates, escapes, entity lump) and binary; data/vdf over it
STYLE.md: KeyValues listed in the layout block and the section 2a table.
This commit is contained in:
1 parent
01c044805f
commit
1c79609ecb
15 files changed
+4391
-1074
No files matched your search
@@ -3209,6 +3209,18 @@ local list= {
|
||||
return __include("data/toml")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "vdf",
|
||||
category = "data",
|
||||
path = "data/vdf",
|
||||
extensions = { "vdf", "acf", "res", "kv" },
|
||||
mime = { "text/x-vdf" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "codecs/text/KeyValues" },
|
||||
caps = { decode = true, encode = true, ordered = true, binary = true },
|
||||
load = function()
|
||||
return __include("data/vdf")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "xml",
|
||||
category = "data",
|
||||
@@ -88485,6 +88497,868 @@ end
|
||||
|
||||
return Encode
|
||||
end
|
||||
__FILES["codecs/text/KeyValues"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- codecs/text/KeyValues: Valve KeyValues text (tier1/KeyValues.cpp of the Source SDK 2013, srctools' reading of it)
|
||||
-- and binary KeyValues (Steam appinfo / shortcuts.vdf). Pair lists keep order, duplicates and [$CONDITION]s;
|
||||
-- #include / #base go through opts.resolve; the BSP entity lump ({ ... } blocks without keys) too.
|
||||
local Err = __include("core/Err")
|
||||
local Types = __include("Types")
|
||||
local Binary = __include("codecs/text/KeyValues/Binary")
|
||||
|
||||
local KeyValues = {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local DEFAULT_DEFINES= { win32 = true }
|
||||
|
||||
local ESCAPE= { n = "\n", t = "\t", v = "\v", b = "\b", r = "\r", f = "\f", a = "\a", ["\\"] = "\\", ["?"] = "?", ["'"] = "'", ['"'] = '"' }
|
||||
|
||||
-- kind: "s" string, "{", "}", "[" condition (its text), nil at the end; err on an unterminated string
|
||||
|
||||
|
||||
local function token(lx)
|
||||
local s, n = lx.s, lx.n
|
||||
local i = lx.i
|
||||
while true do
|
||||
local a = string.find(s, "[^ \t\r\n\f\v]", i)
|
||||
if not a then
|
||||
lx.i = n + 1
|
||||
return nil, nil, n, nil
|
||||
end
|
||||
i = a
|
||||
if string.sub(s, i, i + 1) == "//" then
|
||||
local e = string.find(s, "\n", i, true)
|
||||
if not e then
|
||||
lx.i = n + 1
|
||||
return nil, nil, n, nil
|
||||
end
|
||||
i = e + 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
local c = string.sub(s, i, i)
|
||||
if c == "{" or c == "}" then
|
||||
lx.i = i + 1
|
||||
return c, nil, i - 1, nil
|
||||
elseif c == '"' then
|
||||
local start = i
|
||||
i =i+ 1
|
||||
if not lx.escapes then
|
||||
local e = string.find(s, '"', i, true)
|
||||
if not e then
|
||||
lx.i = n + 1
|
||||
return nil, nil, start - 1, "unterminated string"
|
||||
end
|
||||
lx.i = e + 1
|
||||
return "s", string.sub(s, i, e - 1), start - 1, nil
|
||||
end
|
||||
local parts= {}
|
||||
while true do
|
||||
local e = string.find(s, '["\\]', i)
|
||||
if not e then
|
||||
lx.i = n + 1
|
||||
return nil, nil, start - 1, "unterminated string"
|
||||
end
|
||||
table.insert(parts, string.sub(s, i, e - 1))
|
||||
if string.sub(s, e, e) == '"' then
|
||||
lx.i = e + 1
|
||||
return "s", table.concat(parts), start - 1, nil
|
||||
end
|
||||
local x = string.sub(s, e + 1, e + 1)
|
||||
local r = ESCAPE[x]
|
||||
if r then
|
||||
table.insert(parts, r)
|
||||
i = e + 2
|
||||
else
|
||||
table.insert(parts, "\\")
|
||||
i = e + 1
|
||||
end
|
||||
end
|
||||
elseif c == "[" then
|
||||
local e = string.find(s, "]", i, true)
|
||||
local sp = string.find(s, "[\r\n]", i)
|
||||
if e and (not sp or sp > e) then
|
||||
lx.i = e + 1
|
||||
return "[", string.sub(s, i + 1, e - 1), i - 1, nil
|
||||
end
|
||||
end
|
||||
local e = string.find(s, '[ \t\r\n\f\v"{}]', i)
|
||||
local stop = (e and((e - 1 ))or(n
|
||||
)) lx.i = stop + 1
|
||||
return "s", string.sub(s, i, stop), i - 1, nil
|
||||
end
|
||||
|
||||
-- "$A || !$B && $C": || of && terms; a term is [!]$NAME (KeyValues::EvaluateConditional)
|
||||
function KeyValues.condition(cond, defines)
|
||||
local d = defines or DEFAULT_DEFINES
|
||||
local function flag(name)
|
||||
local l = string.lower(name)
|
||||
for k, v in __iter(d) do
|
||||
if string.lower(k) == l then
|
||||
return v == true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
local any = false
|
||||
for alt in __iter(string.gmatch(cond .. "||", "(.-)||")) do
|
||||
local all = true
|
||||
for term in __iter(string.gmatch(alt .. "&&", "(.-)&&")) do
|
||||
local neg, name = string.match(term, "^%s*(!?)%s*%$?([%w_]*)%s*$")
|
||||
if not name then
|
||||
all = false
|
||||
else
|
||||
local v = flag(name )
|
||||
if neg == "!" then
|
||||
v = not v
|
||||
end
|
||||
all = all and v
|
||||
end
|
||||
end
|
||||
any = any or all
|
||||
end
|
||||
return any
|
||||
end
|
||||
|
||||
local function keyEq(a, b)
|
||||
return a == b or string.lower(a) == string.lower(b)
|
||||
end
|
||||
|
||||
local function parseText(text, opts, depth0)
|
||||
local o= opts or {}
|
||||
local lx= { s = text, i = 1, n = #text, escapes = o.escapes ~= false }
|
||||
if string.sub(text, 1, 3) == "\239\187\191" then
|
||||
lx.i = 4
|
||||
end
|
||||
local defines= nil
|
||||
local evaluate = o.defines ~= false
|
||||
if type(o.defines) == "table" then
|
||||
defines = o.defines
|
||||
end
|
||||
local maxDepth = o.maxDepth or 256
|
||||
local root= {}
|
||||
local stack= { root }
|
||||
local skipping = 0 -- depth inside a block whose condition failed
|
||||
local canReplace = false
|
||||
local includes= {}
|
||||
while true do
|
||||
local kind, v, at, terr = token(lx)
|
||||
if terr then
|
||||
return nil, Err.new(Err.TRUNCATED, terr, at)
|
||||
end
|
||||
if kind == nil then
|
||||
if #stack > 1 or skipping > 0 then
|
||||
return nil, Err.new(Err.TRUNCATED, "unclosed block", at)
|
||||
end
|
||||
break
|
||||
end
|
||||
local cur = stack[#stack]
|
||||
if kind == "}" then
|
||||
if skipping > 0 then
|
||||
skipping =skipping- 1
|
||||
elseif #stack == 1 then
|
||||
return nil, Err.new(Err.CORRUPT, "unbalanced '}'", at)
|
||||
else
|
||||
table.remove(stack)
|
||||
end
|
||||
canReplace = false
|
||||
elseif kind ~= "s" then
|
||||
return nil, Err.new(Err.CORRUPT, kind == "{" and"block without a key" or"condition without a key", at)
|
||||
else
|
||||
local key = v
|
||||
local k2, v2, at2, terr2 = token(lx)
|
||||
if terr2 then
|
||||
return nil, Err.new(Err.TRUNCATED, terr2, at2)
|
||||
end
|
||||
local cond= nil
|
||||
if k2 == "[" then
|
||||
cond = v2
|
||||
k2, v2, at2, terr2 = token(lx)
|
||||
if terr2 then
|
||||
return nil, Err.new(Err.TRUNCATED, terr2, at2)
|
||||
end
|
||||
if k2 ~= "{" then
|
||||
return nil, Err.new(Err.CORRUPT, "a condition after a key needs a block", at2)
|
||||
end
|
||||
end
|
||||
if k2 == "{" then
|
||||
if #stack + skipping > maxDepth then
|
||||
return nil, Err.new(Err.LIMIT, "nesting deeper than maxDepth", at2)
|
||||
end
|
||||
local pass = skipping == 0 and (cond == nil or not evaluate or KeyValues.condition(cond , defines))
|
||||
if not pass then
|
||||
skipping =skipping+ 1
|
||||
else
|
||||
local pair= { key = key, value = {}, cond = (evaluate and{nil }or{cond })[1]}
|
||||
local last = cur[#cur]
|
||||
if cond and evaluate and canReplace and last and type(last.value) == "table" and last.key == key then
|
||||
cur[#cur] = pair
|
||||
else
|
||||
table.insert(cur, pair)
|
||||
end
|
||||
table.insert(stack, pair.value )
|
||||
end
|
||||
canReplace = false
|
||||
elseif k2 == "s" then
|
||||
local value = v2 -- a condition after a value: only on the same line
|
||||
|
||||
local save = lx.i
|
||||
local k3, v3 = token(lx)
|
||||
local c3= nil
|
||||
if k3 == "[" and not string.find(string.sub(text, save, lx.i - 1), "\n", 1, true) then
|
||||
c3 = v3
|
||||
else
|
||||
lx.i = save
|
||||
end
|
||||
if skipping == 0 then
|
||||
local pass = c3 == nil or not evaluate or KeyValues.condition(c3 , defines)
|
||||
if pass then
|
||||
local pair= { key = key, value = value, cond = (evaluate and{nil }or{c3 })[1]}
|
||||
local last = cur[#cur]
|
||||
if c3 and evaluate and canReplace and last and type(last.value) == "string" and last.key == key then
|
||||
cur[#cur] = pair
|
||||
canReplace = false
|
||||
else
|
||||
table.insert(cur, pair)
|
||||
canReplace = c3 == nil
|
||||
end
|
||||
if #stack == 1 and (key == "#include" or key == "#base") and o.resolve then
|
||||
table.remove(cur)
|
||||
table.insert(includes, { kind = key, name = value })
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif k2 == nil then
|
||||
return nil, Err.new(Err.TRUNCATED, "key without a value", at)
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "unexpected '" .. tostring(k2) .. "' after a key", at2)
|
||||
end
|
||||
end
|
||||
end
|
||||
if #includes > 0 then
|
||||
if depth0 >= 16 then
|
||||
return nil, Err.new(Err.LIMIT, "#include / #base nested more than 16 deep")
|
||||
end
|
||||
local resolve = o.resolve
|
||||
for _, inc in __iter(includes) do
|
||||
local ok, data = pcall(resolve, inc.name)
|
||||
if not ok or data == nil then
|
||||
return nil, Err.new(Err.IO, inc.kind .. ' "' .. inc.name .. '" not found')
|
||||
end
|
||||
local s = (type(data) == "buffer" and{(buffer.tostring(data) )}or{(tostring(data)
|
||||
)})[1] local sub, err = parseText(s, o, depth0 + 1)
|
||||
if not sub then
|
||||
return nil, Err.new((err ).code, inc.name .. ": " .. (err ).message)
|
||||
end
|
||||
if inc.kind == "#include" then
|
||||
for _, p in __iter(sub) do
|
||||
table.insert(root, p)
|
||||
end
|
||||
else
|
||||
KeyValues.merge(root, sub)
|
||||
end
|
||||
end
|
||||
end
|
||||
return root, nil
|
||||
end
|
||||
|
||||
function KeyValues.parse(text, opts)
|
||||
local s = (type(text) == "buffer" and{(buffer.tostring(text) )}or{text
|
||||
})[1] if type(s) ~= "string" then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.parse: expected a string or a buffer")
|
||||
end
|
||||
return parseText(s, opts, 0)
|
||||
end
|
||||
|
||||
-- #base: keys of base missing from into are added; blocks present in both merge recursively (RecursiveMergeKeyValues)
|
||||
function KeyValues.merge(into, base)
|
||||
for _, bp in __iter(base) do
|
||||
local found= nil
|
||||
for _, p in __iter(into) do
|
||||
if keyEq(p.key, bp.key) then
|
||||
found = p
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
table.insert(into, KeyValues.copy({ bp })[1])
|
||||
elseif type(found.value) == "table" and type(bp.value) == "table" then
|
||||
KeyValues.merge(found.value , bp.value )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function KeyValues.copy(b)
|
||||
local out= (table.create(#b) )
|
||||
for i, p in __iter(b) do
|
||||
local v= p.value
|
||||
out[i] = { key = p.key, value = (type(v) == "table" and{(KeyValues.copy(v) )}or{v})[1], cond = p.cond, type = p.type }
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- the first pair with this key (case-insensitive, as KeyValues::FindKey); "a/b/c" walks blocks
|
||||
function KeyValues.find(b, path)
|
||||
local cur= b
|
||||
local found= nil
|
||||
for part in __iter(string.gmatch(path, "[^/]+")) do
|
||||
if not cur then
|
||||
return nil
|
||||
end
|
||||
found = nil
|
||||
for _, p in __iter(cur) do
|
||||
if keyEq(p.key, part) then
|
||||
found = p
|
||||
break
|
||||
end
|
||||
end
|
||||
if not found then
|
||||
return nil
|
||||
end
|
||||
cur = (type(found.value) == "table" and{found.value }or{nil
|
||||
})[1] end
|
||||
return found
|
||||
end
|
||||
|
||||
function KeyValues.get(b, path)
|
||||
local p = KeyValues.find(b, path)
|
||||
return p and p.value
|
||||
end
|
||||
|
||||
function KeyValues.all(b, key)
|
||||
local out= {}
|
||||
for _, p in __iter(b) do
|
||||
if keyEq(p.key, key) then
|
||||
table.insert(out, p)
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function quote(s, escapes)
|
||||
if escapes then
|
||||
s = string.gsub(s, '[\\"\n\t\r]', { ["\\"] = "\\\\", ['"'] = '\\"', ["\n"] = "\\n", ["\t"] = "\\t", ["\r"] = "\\r" })
|
||||
end
|
||||
return '"' .. s .. '"'
|
||||
end
|
||||
|
||||
local function writeBlock(b, out, indent, level, escapes, depth)
|
||||
if depth > 256 then
|
||||
return "nesting deeper than 256"
|
||||
end
|
||||
local pad = string.rep(indent, level)
|
||||
for _, p in __iter(b) do
|
||||
if type(p) ~= "table" or type(p.key) ~= "string" then
|
||||
return "a pair needs a string key"
|
||||
end
|
||||
local v= p.value
|
||||
local cond = (type(p.cond) == "string" and((" [" .. p.cond .. "]" ))or(""
|
||||
)) if type(v) == "table" then
|
||||
table.insert(out, pad .. quote(p.key, escapes) .. cond .. "\n" .. pad .. "{\n")
|
||||
local err = writeBlock(v, out, indent, level + 1, escapes, depth + 1)
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
table.insert(out, pad .. "}\n")
|
||||
elseif type(v) == "string" or type(v) == "number" or type(v) == "boolean" then
|
||||
local s = (type(v) == "boolean" and{(v and"1" or"0") }or{(tostring(v)
|
||||
)})[1] table.insert(out, pad .. quote(p.key, escapes) .. "\t" .. quote(s, escapes) .. cond .. "\n")
|
||||
else
|
||||
return "unwritable value for key " .. p.key
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function KeyValues.write(b, opts)
|
||||
if type(b) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.write: expected a pair list")
|
||||
end
|
||||
local out= {}
|
||||
local err = writeBlock(b, out, opts and opts.indent or "\t", 0, not (opts and opts.escapes == false), 0)
|
||||
if err then
|
||||
return nil, Err.new(Err.ARG, err)
|
||||
end
|
||||
return table.concat(out), nil
|
||||
end
|
||||
|
||||
function KeyValues.toObject(b, opts)
|
||||
local mode = opts and opts.duplicates or "array"
|
||||
local o = Types.object()
|
||||
local multi= {}
|
||||
for _, p in __iter(b) do
|
||||
local v= p.value
|
||||
if type(v) == "table" then
|
||||
v = KeyValues.toObject(v, opts)
|
||||
end
|
||||
local old = o[p.key]
|
||||
if old == nil then
|
||||
Types.set(o, p.key, v)
|
||||
elseif mode == "last" then
|
||||
o[p.key] = v
|
||||
elseif mode == "array" then
|
||||
if not multi[p.key] then
|
||||
o[p.key] = { old }
|
||||
multi[p.key] = true
|
||||
end
|
||||
table.insert(o[p.key], v)
|
||||
end
|
||||
end
|
||||
return o
|
||||
end
|
||||
|
||||
local function fromValue(key, v, out, depth)
|
||||
if depth > 256 then
|
||||
return "nesting deeper than 256"
|
||||
end
|
||||
if type(v) == "table" and not Types.isObject(v) and Types.isArray(v, true) then
|
||||
for _, x in __iter(v) do
|
||||
if type(x) == "table" and not Types.isObject(x) and Types.isArray(x, true) then
|
||||
return "nested array under key " .. key
|
||||
end
|
||||
local err = fromValue(key, x, out, depth + 1)
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
end
|
||||
return nil
|
||||
elseif type(v) == "table" then
|
||||
local sub= {}
|
||||
local t= v
|
||||
for _, k in __iter(Types.keys(t)) do
|
||||
if type(k) ~= "string" and type(k) ~= "number" then
|
||||
return "unwritable key"
|
||||
end
|
||||
local err = fromValue(tostring(k), t[k], sub, depth + 1)
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
end
|
||||
table.insert(out, { key = key, value = sub })
|
||||
return nil
|
||||
elseif type(v) == "string" or type(v) == "number" or type(v) == "boolean" then
|
||||
table.insert(out, { key = key, value = (type(v) == "string" and{v }or{(type(v) == "boolean" and{(v and"1" or"0") }or{(tostring(v) )})[1]})[1]})
|
||||
return nil
|
||||
end
|
||||
return "unwritable value for key " .. key
|
||||
end
|
||||
|
||||
-- a Types.object / plain table -> pairs (arrays repeat their key; booleans become "1" / "0")
|
||||
function KeyValues.fromObject(v)
|
||||
if type(v) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.fromObject: expected a table")
|
||||
end
|
||||
local out= {}
|
||||
for _, k in __iter(Types.keys(v)) do
|
||||
if type(k) ~= "string" and type(k) ~= "number" then
|
||||
return nil, Err.new(Err.ARG, "unwritable key")
|
||||
end
|
||||
local err = fromValue(tostring(k), v[k], out, 0)
|
||||
if err then
|
||||
return nil, Err.new(Err.ARG, err)
|
||||
end
|
||||
end
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- pair list or object
|
||||
function KeyValues.pairs(v)
|
||||
if type(v) == "table" and not Types.isObject(v) and (#v > 0 or next(v) == nil) then
|
||||
local first = v[1]
|
||||
if first == nil or (type(first) == "table" and type(first.key) == "string") then
|
||||
return v, nil
|
||||
end
|
||||
end
|
||||
return KeyValues.fromObject(v)
|
||||
end
|
||||
|
||||
-- BSP entity lump: { "k" "v" ... } blocks, no escapes (CMapEntities / ParseEntity)
|
||||
function KeyValues.entities(text)
|
||||
text = (string.match(text, "^[^%z]*") )
|
||||
local lx= { s = text, i = 1, n = #text, escapes = false }
|
||||
local out= {}
|
||||
local cur= nil
|
||||
while true do
|
||||
local kind, v, at, terr = token(lx)
|
||||
if terr then
|
||||
return nil, Err.new(Err.TRUNCATED, terr, at)
|
||||
end
|
||||
if kind == nil then
|
||||
if cur then
|
||||
return nil, Err.new(Err.TRUNCATED, "unclosed entity", at)
|
||||
end
|
||||
return out, nil
|
||||
elseif kind == "{" then
|
||||
if cur then
|
||||
return nil, Err.new(Err.CORRUPT, "'{' inside an entity", at)
|
||||
end
|
||||
cur = {}
|
||||
elseif kind == "}" then
|
||||
if not cur then
|
||||
return nil, Err.new(Err.CORRUPT, "unbalanced '}'", at)
|
||||
end
|
||||
table.insert(out, cur )
|
||||
cur = nil
|
||||
elseif not cur then
|
||||
return nil, Err.new(Err.CORRUPT, "key outside an entity", at)
|
||||
else
|
||||
local k2, v2, at2, terr2 = token(lx)
|
||||
if terr2 then
|
||||
return nil, Err.new(Err.TRUNCATED, terr2, at2)
|
||||
end
|
||||
if k2 ~= "s" then
|
||||
return nil, Err.new(Err.CORRUPT, "entity key without a value", at)
|
||||
end
|
||||
table.insert(cur, { key = v , value = v2 })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function KeyValues.writeEntities(list)
|
||||
local out= {}
|
||||
for _, e in __iter(list) do
|
||||
table.insert(out, "{\n")
|
||||
for _, p in __iter(e) do
|
||||
if type(p.value) ~= "string" then
|
||||
return nil, Err.new(Err.ARG, "entity values are strings")
|
||||
end
|
||||
table.insert(out, '"' .. p.key .. '" "' .. (p.value ) .. '"\n')
|
||||
end
|
||||
table.insert(out, "}\n")
|
||||
end
|
||||
return table.concat(out), nil
|
||||
end
|
||||
|
||||
KeyValues.readBinary = Binary.read
|
||||
KeyValues.writeBinary = Binary.write
|
||||
KeyValues.isBinary = Binary.sniff
|
||||
|
||||
return KeyValues
|
||||
end
|
||||
__FILES["codecs/text/KeyValues/Binary"] = function()
|
||||
local __compat = __compat local buffer, utf8, type, __iter = __compat.buffer, __compat.utf8, __compat.type, __compat.__iter --!strict
|
||||
-- codecs/text/KeyValues/Binary: binary KeyValues (KeyValues::ReadAsBinary; Steam shortcuts.vdf / appinfo values).
|
||||
-- Types: 0 block, 1 string, 2 int32, 3 float32, 4 pointer, 5 wide string (UTF-16LE), 6 color, 7 uint64, 8 end,
|
||||
-- 10 int64, 11 end (alternative). 64-bit values are numbers below 2^53, decimal strings above.
|
||||
local Err = __include("core/Err")
|
||||
local Text = __include("codecs/text/Text")
|
||||
|
||||
local Binary = {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
local readu8, readu32, readi32, readf32 = buffer.readu8, buffer.readu32, buffer.readi32, buffer.readf32
|
||||
|
||||
local TYPES= { [2] = "int32", [3] = "float32", [4] = "pointer", [5] = "wstring", [6] = "color", [7] = "uint64", [10] = "int64" }
|
||||
local CODES= { int32 = 2, float32 = 3, pointer = 4, wstring = 5, color = 6, uint64 = 7, int64 = 10 }
|
||||
|
||||
-- (hi, lo) u32 halves -> number below 2^53, else the decimal digits
|
||||
local function u64text(hi, lo)
|
||||
if hi < 2 ^ 21 then
|
||||
return hi * 4294967296 + lo
|
||||
end
|
||||
local digits= {}
|
||||
-- base 10^6 long division over 16-bit limbs
|
||||
local limbs = { math.floor(hi / 65536), hi % 65536, math.floor(lo / 65536), lo % 65536 }
|
||||
while limbs[1] + limbs[2] + limbs[3] + limbs[4] > 0 do
|
||||
local rem = 0
|
||||
for i = 1, 4 do
|
||||
local cur = rem * 65536 + limbs[i]
|
||||
limbs[i] = math.floor(cur / 1000000
|
||||
) rem = cur % 1000000
|
||||
end
|
||||
table.insert(digits, 1, string.format("%06d", rem))
|
||||
end
|
||||
local s = string.gsub(table.concat(digits), "^0+", "")
|
||||
return s == "" and 0 or s
|
||||
end
|
||||
|
||||
-- number or decimal string -> (hi, lo), two's complement for negatives
|
||||
local function u64parts(v)
|
||||
if type(v) == "number" then
|
||||
if v ~= v or math.abs(v) >= 2 ^ 63 then
|
||||
return nil, nil
|
||||
end
|
||||
v = math.floor(v)
|
||||
local m = math.abs(v)
|
||||
local hi = math.floor(m / 4294967296
|
||||
) local lo = m - hi * 4294967296
|
||||
if v < 0 then
|
||||
return (4294967295 - hi + (lo == 0 and 1 or 0)) % 4294967296, (4294967296 - lo) % 4294967296
|
||||
end
|
||||
return hi, lo
|
||||
elseif type(v) == "string" then
|
||||
local neg, digits = string.match(v, "^(%-?)(%d+)$")
|
||||
if not digits or #digits > 20 then
|
||||
return nil, nil
|
||||
end
|
||||
local limbs = { 0, 0, 0, 0 } -- 16-bit, most significant first
|
||||
for i = 1, #digits do
|
||||
local carry = (tonumber(string.sub(digits, i, i)) )
|
||||
for k = 4, 1, -1 do
|
||||
local cur = limbs[k] * 10 + carry
|
||||
limbs[k] = cur % 65536
|
||||
carry = math.floor(cur / 65536
|
||||
) end
|
||||
if carry > 0 then
|
||||
return nil, nil
|
||||
end
|
||||
end
|
||||
if neg == "-" then
|
||||
local carry = 1
|
||||
for k = 4, 1, -1 do
|
||||
local cur = (65535 - limbs[k]) + carry
|
||||
limbs[k] = cur % 65536
|
||||
carry = math.floor(cur / 65536
|
||||
) end
|
||||
end
|
||||
return limbs[1] * 65536 + limbs[2], limbs[3] * 65536 + limbs[4]
|
||||
end
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
function Binary.read(buf, offset, length, opts)
|
||||
local b= (type(buf) == "string" and{(buffer.fromstring(buf) )}or{buf
|
||||
})[1] if type(b) ~= "buffer" then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.readBinary: expected a buffer")
|
||||
end
|
||||
local o = offset or 0
|
||||
local e = o + (length or (buffer.len(b) - o))
|
||||
if o < 0 or e > buffer.len(b) or e < o then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.readBinary: range out of the buffer")
|
||||
end
|
||||
local maxDepth = opts and opts.maxDepth or 256
|
||||
local p = o
|
||||
local function cstr()
|
||||
local q = p
|
||||
while q < e and readu8(b, q) ~= 0 do
|
||||
q =q+ 1
|
||||
end
|
||||
if q >= e then
|
||||
return nil
|
||||
end
|
||||
local s = buffer.readstring(b, p, q - p)
|
||||
p = q + 1
|
||||
return s
|
||||
end
|
||||
local root= {}
|
||||
local stack= { root }
|
||||
while true do
|
||||
if p >= e then
|
||||
if #stack > 1 then
|
||||
return nil, Err.new(Err.TRUNCATED, "unclosed block", p - o)
|
||||
end
|
||||
break
|
||||
end
|
||||
local t = readu8(b, p)
|
||||
p =p+ 1
|
||||
if t == 8 or t == 11 then
|
||||
if #stack == 1 then
|
||||
break
|
||||
end
|
||||
table.remove(stack)
|
||||
else
|
||||
local cur = stack[#stack]
|
||||
local key = cstr()
|
||||
if not key then
|
||||
return nil, Err.new(Err.TRUNCATED, "key", p - o)
|
||||
end
|
||||
if t == 0 then
|
||||
if #stack >= maxDepth then
|
||||
return nil, Err.new(Err.LIMIT, "nesting deeper than maxDepth", p - o)
|
||||
end
|
||||
local sub= {}
|
||||
table.insert(cur, { key = key, value = sub })
|
||||
table.insert(stack, sub)
|
||||
elseif t == 1 then
|
||||
local v = cstr()
|
||||
if not v then
|
||||
return nil, Err.new(Err.TRUNCATED, "string value", p - o)
|
||||
end
|
||||
table.insert(cur, { key = key, value = v })
|
||||
elseif t == 5 then
|
||||
local q = p
|
||||
while q + 1 < e and buffer.readu16(b, q) ~= 0 do
|
||||
q =q+ 2
|
||||
end
|
||||
if q + 1 >= e then
|
||||
return nil, Err.new(Err.TRUNCATED, "wide string value", p - o)
|
||||
end
|
||||
table.insert(cur, { key = key, value = Text.utf16(b, p, q - p), type = "wstring" })
|
||||
p = q + 2
|
||||
elseif t == 2 or t == 3 or t == 4 or t == 6 then
|
||||
if p + 4 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "value", p - o)
|
||||
end
|
||||
local v = (t == 2 and{(readi32(b, p) )}or{(t == 3 and{(readf32(b, p) )}or{(readu32(b, p)
|
||||
)})[1]})[1] table.insert(cur, { key = key, value = v, type = TYPES[t] })
|
||||
p =p+ 4
|
||||
elseif t == 7 or t == 10 then
|
||||
if p + 8 > e then
|
||||
return nil, Err.new(Err.TRUNCATED, "value", p - o)
|
||||
end
|
||||
local lo, hi = readu32(b, p), readu32(b, p + 4)
|
||||
local v
|
||||
if t == 10 and hi >= 2 ^ 31 then
|
||||
-- negative: magnitude of the two's complement
|
||||
local nlo = (4294967296 - lo) % 4294967296
|
||||
local nhi = 4294967295 - hi + (lo == 0 and 1 or 0)
|
||||
local m = u64text(nhi, nlo)
|
||||
v = (type(m) == "number" and{(-m )}or{("-" .. m
|
||||
)})[1] else
|
||||
v = u64text(hi, lo)
|
||||
end
|
||||
table.insert(cur, { key = key, value = v, type = TYPES[t] })
|
||||
p =p+ 8
|
||||
else
|
||||
return nil, Err.new(Err.CORRUPT, "unknown value type " .. t, p - o - 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
return root, nil
|
||||
end
|
||||
|
||||
local function utf16(s)
|
||||
local out= {}
|
||||
for _, c in __iter(utf8.codes(s)) do
|
||||
if c >= 0x10000 then
|
||||
local v = c - 0x10000
|
||||
local hs = 0xD800 + math.floor(v / 1024
|
||||
) table.insert(out, string.char(hs % 256, math.floor(hs / 256)))
|
||||
local lo = 0xDC00 + v % 1024
|
||||
table.insert(out, string.char(lo % 256, math.floor(lo / 256)))
|
||||
else
|
||||
table.insert(out, string.char(c % 256, math.floor(c / 256)))
|
||||
end
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
local function writeBlock(blk, out, depth)
|
||||
if depth > 256 then
|
||||
return "nesting deeper than 256"
|
||||
end
|
||||
for _, pr in __iter(blk) do
|
||||
if type(pr) ~= "table" or type(pr.key) ~= "string" or string.find(pr.key, "%z") then
|
||||
return "a pair needs a string key without NUL"
|
||||
end
|
||||
local v= pr.value
|
||||
local k = pr.key .. "\0"
|
||||
if type(v) == "table" then
|
||||
table.insert(out, "\0" .. k)
|
||||
local err = writeBlock(v, out, depth + 1)
|
||||
if err then
|
||||
return err
|
||||
end
|
||||
table.insert(out, "\8")
|
||||
else
|
||||
local code = (pr.type and{CODES[pr.type] }or{((type(v) == "number" and{(v % 1 == 0 and v >= -2 ^ 31 and v < 2 ^ 31 and 2 or 3) }or{1})[1])
|
||||
})[1] if not code then
|
||||
return "unknown value type " .. tostring(pr.type)
|
||||
end
|
||||
local w = buffer.create(8)
|
||||
if code == 1 then
|
||||
if type(v) ~= "string" or string.find(v, "%z") then
|
||||
return "string value expected for key " .. pr.key
|
||||
end
|
||||
table.insert(out, "\1" .. k .. v .. "\0")
|
||||
elseif code == 5 then
|
||||
if type(v) ~= "string" or not utf8.len(v) then
|
||||
return "UTF-8 string value expected for key " .. pr.key
|
||||
end
|
||||
table.insert(out, "\5" .. k .. utf16(v) .. "\0\0")
|
||||
elseif code == 7 or code == 10 then
|
||||
local hi, lo = u64parts(v)
|
||||
if not hi then
|
||||
return "64-bit value expected for key " .. pr.key
|
||||
end
|
||||
buffer.writeu32(w, 0, lo )
|
||||
buffer.writeu32(w, 4, hi )
|
||||
table.insert(out, string.char(code) .. k .. buffer.readstring(w, 0, 8))
|
||||
else
|
||||
if type(v) ~= "number" then
|
||||
return "number value expected for key " .. pr.key
|
||||
end
|
||||
if code == 3 then
|
||||
buffer.writef32(w, 0, v)
|
||||
elseif code == 2 then
|
||||
if v % 1 ~= 0 or v < -2 ^ 31 or v >= 2 ^ 31 then
|
||||
return "int32 out of range for key " .. pr.key
|
||||
end
|
||||
buffer.writei32(w, 0, v)
|
||||
else
|
||||
if v % 1 ~= 0 or v < 0 or v >= 2 ^ 32 then
|
||||
return "u32 out of range for key " .. pr.key
|
||||
end
|
||||
buffer.writeu32(w, 0, v)
|
||||
end
|
||||
table.insert(out, string.char(code) .. k .. buffer.readstring(w, 0, 4))
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
function Binary.write(blk)
|
||||
if type(blk) ~= "table" then
|
||||
return nil, Err.new(Err.ARG, "KeyValues.writeBinary: expected a pair list")
|
||||
end
|
||||
local out= {}
|
||||
local err = writeBlock(blk, out, 0)
|
||||
if err then
|
||||
return nil, Err.new(Err.ARG, err)
|
||||
end
|
||||
table.insert(out, "\8")
|
||||
return buffer.fromstring(table.concat(out)), nil
|
||||
end
|
||||
|
||||
-- a type byte 0 / 1 / 2 then a printable NUL-terminated key
|
||||
function Binary.sniff(b, o, n)
|
||||
if n < 3 then
|
||||
return false
|
||||
end
|
||||
local t = readu8(b, o)
|
||||
if t ~= 0 and t ~= 1 and t ~= 2 then
|
||||
return false
|
||||
end
|
||||
for i = 1, math.min(n - 1, 64) do
|
||||
local c = readu8(b, o + i)
|
||||
if c == 0 then
|
||||
return i > 1
|
||||
elseif c < 32 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
return Binary
|
||||
end
|
||||
__FILES["codecs/text/Nbt"] = function()
|
||||
local __compat = __compat local buffer, type, __iter = __compat.buffer, __compat.type, __compat.__iter --!strict
|
||||
-- codecs/text/Nbt: Minecraft's Named Binary Tag format and SNBT (Minecraft wiki "NBT format", wiki.vg "NBT").
|
||||
@@ -121385,6 +122259,107 @@ end
|
||||
|
||||
return Encode
|
||||
end
|
||||
__FILES["data/vdf"] = function()
|
||||
local __compat = __compat local buffer = __compat.buffer --!strict
|
||||
-- data/vdf: Valve KeyValues files (.vdf, .acf, .res, ...) as data over codecs/text/KeyValues, read and written; binary
|
||||
-- KeyValues (shortcuts.vdf) too. Values are ordered objects, a repeated key's values an array (opts.duplicates);
|
||||
-- opts.pairs gives the pair list with conditions and binary types. Not supported: appinfo.vdf / packageinfo.vdf headers.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = __include("core/Err")
|
||||
local Bytes = __include("core/Bytes")
|
||||
local Sniff = __include("core/Sniff")
|
||||
local KeyValues = __include("codecs/text/KeyValues")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Vdf = {
|
||||
name = "vdf",
|
||||
category = "data",
|
||||
extensions = { "vdf", "acf", "res", "kv" },
|
||||
mime = { "text/x-vdf" },
|
||||
deps = { "core/Err", "core/Bytes", "core/Sniff", "codecs/text/KeyValues" },
|
||||
caps = { decode = true, encode = true, ordered = true, binary = true },
|
||||
}
|
||||
-- ------------------------------------------------------------------------------------------------ types
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function Vdf.sniff(r)
|
||||
local n = r:remaining()
|
||||
if KeyValues.isBinary(r.buf, r.pos, n) then
|
||||
return 0.3
|
||||
end
|
||||
local head = Sniff.text(r, 256, true)
|
||||
if string.find(head, "%z") then
|
||||
return 0
|
||||
end
|
||||
head = string.gsub(head, "^//[^\n]*\n%s*", "")
|
||||
if string.match(head, '^"[^"\n]+"%s*{') or string.match(head, '^"[^"\n]+"%s*%[[^%]\n]*%]%s*{') then
|
||||
return 0.6
|
||||
end
|
||||
if string.match(head, "^#base%s") or string.match(head, "^#include%s") then
|
||||
return 0.5
|
||||
end
|
||||
if string.match(head, '^[%w_]+%s*\n%s*{%s*\n%s*"') then
|
||||
return 0.3
|
||||
end
|
||||
return 0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
function Vdf.decode(r, opts)
|
||||
local o= opts or {}
|
||||
local n = r.limit - r.base
|
||||
local blk, err
|
||||
if o.binary or (o.binary == nil and KeyValues.isBinary(r.buf, r.base, n)) then
|
||||
blk, err = KeyValues.readBinary(r.buf, r.base, n)
|
||||
else
|
||||
blk, err = KeyValues.parse(buffer.readstring(r.buf, r.base, n), {
|
||||
escapes = o.escapes,
|
||||
defines = o.defines,
|
||||
resolve = o.resolve,
|
||||
})
|
||||
end
|
||||
if not blk then
|
||||
return nil, err
|
||||
end
|
||||
if o.pairs then
|
||||
return blk, nil
|
||||
end
|
||||
return KeyValues.toObject(blk, { duplicates = o.duplicates }), nil
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encode
|
||||
function Vdf.encode(value, opts)
|
||||
local blk, err = KeyValues.pairs(value)
|
||||
if not blk then
|
||||
return nil, err
|
||||
end
|
||||
if opts and opts.binary then
|
||||
return KeyValues.writeBinary(blk)
|
||||
end
|
||||
local s, werr = KeyValues.write(blk, { escapes = opts and opts.escapes, indent = opts and opts.indent })
|
||||
if not s then
|
||||
return nil, werr
|
||||
end
|
||||
return buffer.fromstring(s), nil
|
||||
end
|
||||
|
||||
return Vdf
|
||||
end
|
||||
__FILES["data/xml"] = function()
|
||||
local __compat = __compat local buffer, type = __compat.buffer, __compat.type --!strict
|
||||
-- data/xml: XML 1.0 over codecs/text/Xml. Supports: decode to a "#document" XmlNode tree, encode a document or an
|
||||
|
||||
Reference in new issue
Block a user