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:
4DBug committed 2026-11-01 18:35:33 -06:00
1 parent 01c044805f
commit 1c79609ecb
15 files changed
+4391 -1074

No files matched your search

+22 -1
View File
@@ -306,6 +306,26 @@ local xml = assert(F.saveString(p, { format = "plist" }))
assert(string.find(xml, "<key>CFBundleName</key>\n\t<string>Demo</string>", 1, true))
```
## VDF (Valve KeyValues)
`.vdf`, `.acf`, `.res` and other KeyValues files decode to `Types.object`s of strings; a key that repeats gets an array
of its values (`duplicates = "last"` / `"first"` keep one). `[$CONDITION]`s are evaluated for a PC (`defines =
{ OSX = true }` changes the flags, `defines = false` keeps every pair), `#include` / `#base` files come through
`resolve`, `escapes = false` reads backslashes as they are. `pairs = true` gives the ordered pair list of
`codecs/text/KeyValues` (`{ key, value, cond?, type? }`), which keeps everything. Binary KeyValues (`shortcuts.vdf`)
are sniffed; encode with `binary = true`.
```luau
local acf = assert(F.data.load('"AppState"\n{\n\t"appid"\t"440"\n\t"x" "1"\n\t"x" "2"\n}\n', { format = "vdf" }))
assert(acf.AppState.appid == "440" and acf.AppState.x[2] == "2")
local text = assert(F.saveString(acf, { format = "vdf" }))
assert(string.find(text, '\t"x"\t"1"\n\t"x"\t"2"\n', 1, true))
local pairs = assert(F.data.load('"r" { "a" "1" [$X360] }', { format = "vdf", pairs = true, defines = false }))
assert(pairs[1].value[1].cond == "$X360")
local bin = assert(F.save(acf, nil, { format = "vdf", binary = true }))
assert(F.data.load(bin, { format = "vdf" }).AppState.appid == "440")
```
## Save and convert
Data values have no `kind`, so saving needs `opts.format` or a destination extension (an `XmlNode` document too):
@@ -337,6 +357,7 @@ is an error there), CSV needs rows, INI needs sections of scalars, JSON has no b
| toml (`.toml`) | yes | yes | ordered tables, Dates |
| csv (`.csv`, `.tsv`) | yes | yes | rows (arrays or records) |
| ini (`.ini`, `.cfg`, `.conf`, `.desktop`) | yes | yes | sections of strings |
| vdf (`.vdf`, `.acf`, `.res`, `.kv`) | text + binary | text + binary | ordered objects of strings, or pairs |
| msgpack (`.msgpack`, `.mpk`) | yes | yes | values, buffers, Dates |
| cbor (`.cbor`) | yes | yes | values, buffers, Dates, tagged values |
| plist (`.plist`) | XML + binary | XML | values, buffers, Dates |
@@ -351,7 +372,7 @@ Full capability list: [FORMATS.md](../FORMATS.md).
- Luau has one number type: `1.0` reads back as `1`, and TOML / plist write integral floats as integers. Integers
past 2^53 need `bigInts = "string"` to survive.
- An empty plain table `{}` is written as an array; use `F.Types.object()` (or `emptyTable = "object"`) for `{}`.
- Text formats sniff weakly: give `format` (or a file name) for YAML, TOML, INI, CSV, MessagePack and CBOR without
- Text formats sniff weakly: give `format` (or a file name) for YAML, TOML, INI, VDF, CSV, MessagePack and CBOR without
its self-describe tag.
- XML whitespace-only text between elements is dropped by default (`whitespace = true` keeps it); text is never
trimmed otherwise.