Files
2026-10-11 16:01:50 -07:00

154 lines
6.7 KiB
Swift

import Foundation
/// A node of Valve's KeyValues format: a name with either a text value or
/// children. Steam describes apps in the text form and packages in the
/// binary form; both are read into this one shape.
struct KeyValues {
var name: String
var value: String?
var children: [KeyValues] = []
/// The first child with this name. Steam is not consistent about the
/// case of key names, so they are compared without it.
subscript(_ key: String) -> KeyValues? {
children.first { $0.name.caseInsensitiveCompare(key) == .orderedSame }
}
/// A child's text value.
func text(_ key: String) -> String? {
self[key]?.value
}
// MARK: Text form
/// Parses `"name" { "key" "value" ... }`. Returns the root node, or nil
/// when the text does not start with one.
static func parse(text data: Data) -> KeyValues? {
var scanner = TextScanner(bytes: [UInt8](data))
guard case .word(let name)? = scanner.token() else { return nil }
guard case .open? = scanner.token() else { return KeyValues(name: name, value: nil) }
return KeyValues(name: name, value: nil, children: scanner.children(depth: 0))
}
private struct TextScanner {
enum Token { case word(String), open, close }
let bytes: [UInt8]
var at = 0
mutating func token() -> Token? {
// Whitespace, and comments that run to the end of the line.
while at < bytes.count {
let byte = bytes[at]
if byte == 0x20 || byte == 0x09 || byte == 0x0A || byte == 0x0D || byte == 0 {
at += 1
} else if byte == 0x2F, at + 1 < bytes.count, bytes[at + 1] == 0x2F {
while at < bytes.count, bytes[at] != 0x0A { at += 1 }
} else {
break
}
}
guard at < bytes.count else { return nil }
switch bytes[at] {
case 0x7B: at += 1; return .open
case 0x7D: at += 1; return .close
case 0x22:
at += 1
var text: [UInt8] = []
while at < bytes.count, bytes[at] != 0x22 {
if bytes[at] == 0x5C, at + 1 < bytes.count {
// Escapes: \" and \\ stand for themselves, \n and \t for their control characters.
switch bytes[at + 1] {
case 0x22, 0x5C: text.append(bytes[at + 1]); at += 2; continue
case 0x6E: text.append(0x0A); at += 2; continue
case 0x74: text.append(0x09); at += 2; continue
default: break
}
}
text.append(bytes[at])
at += 1
}
at += 1
return .word(String(decoding: text, as: UTF8.self))
default:
let start = at
while at < bytes.count, ![0x20, 0x09, 0x0A, 0x0D, 0x7B, 0x7D, 0x22].contains(bytes[at]) { at += 1 }
return .word(String(decoding: bytes[start..<at], as: UTF8.self))
}
}
mutating func children(depth: Int) -> [KeyValues] {
var nodes: [KeyValues] = []
while let key = token() {
guard case .word(let name) = key else { break } // a closing brace ends this level
switch token() {
case .word(let value)?:
nodes.append(KeyValues(name: name, value: value))
case .open?:
nodes.append(KeyValues(name: name, value: nil, children: depth < 64 ? children(depth: depth + 1) : []))
default:
return nodes
}
}
return nodes
}
}
// MARK: Binary form
/// Parses the binary form, starting at `offset`: a stream of typed
/// entries (object, string, 32-bit integer, ...) ended by an end mark.
/// Returns the first entry, the root.
static func parse(binary data: Data, offset: Int = 0) -> KeyValues? {
var scanner = BinaryScanner(bytes: [UInt8](data), at: offset)
return scanner.entries(depth: 0).first
}
private struct BinaryScanner {
let bytes: [UInt8]
var at: Int
mutating func cString() -> String? {
guard let end = bytes[at...].firstIndex(of: 0) else { return nil }
defer { at = end + 1 }
return String(decoding: bytes[at..<end], as: UTF8.self)
}
mutating func integer(_ width: Int) -> UInt64? {
guard bytes.count - at >= width else { return nil }
defer { at += width }
return (0..<width).reduce(UInt64(0)) { $0 | UInt64(bytes[at + $1]) << (8 * UInt64($1)) }
}
mutating func entries(depth: Int) -> [KeyValues] {
var nodes: [KeyValues] = []
while at < bytes.count {
let kind = bytes[at]
at += 1
if kind == 0x08 || kind == 0x0B { break } // end of this object
guard let name = cString() else { break }
switch kind {
case 0x00: // object
guard depth < 64 else { return nodes }
nodes.append(KeyValues(name: name, value: nil, children: entries(depth: depth + 1)))
case 0x01: // string
guard let value = cString() else { return nodes }
nodes.append(KeyValues(name: name, value: value))
case 0x02, 0x04, 0x06: // 32-bit integer, pointer, colour
guard let value = integer(4) else { return nodes }
nodes.append(KeyValues(name: name, value: kind == 0x02 ? String(Int32(truncatingIfNeeded: value)) : String(value)))
case 0x03: // float
guard let value = integer(4) else { return nodes }
nodes.append(KeyValues(name: name, value: String(Float(bitPattern: UInt32(truncatingIfNeeded: value)))))
case 0x07, 0x0A: // 64-bit integers
guard let value = integer(8) else { return nodes }
nodes.append(KeyValues(name: name, value: kind == 0x07 ? String(value) : String(Int64(bitPattern: value))))
default:
return nodes // unknown type: stop rather than misread
}
}
return nodes
}
}
}