--!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 = require("@rosetta/core/Err") local Bytes = require("@rosetta/core/Bytes") local Sniff = require("@rosetta/core/Sniff") local KeyValues = require("@rosetta/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 export type DecodeOptions = { pairs: boolean?, -- the KeyValues pair list instead of an object duplicates: ("array" | "last" | "first")?, -- default "array" escapes: boolean?, -- default true defines: ({ [string]: boolean } | false)?, -- [$X] conditions (codecs/text/KeyValues), default PC resolve: ((name: string) -> (buffer | string)?)?, -- #include / #base binary: boolean?, -- force binary (default: sniffed) } export type EncodeOptions = { binary: boolean?, -- default false escapes: boolean?, -- default true indent: string?, -- default "\t" } -- ------------------------------------------------------------------------------------------------ sniff function Vdf.sniff(r: Bytes.Reader): number 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: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) local o: DecodeOptions = 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: any, opts: EncodeOptions?): (buffer?, Err.Err?) 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