SwiftCST is a library for parsing Maxis' key-value pair format. It can be used with custom frameworks or the original UIText APIs.
Caret-Separated Text (or CST) is a string-text format: a plain-text resource format with digits or words as keys and each value enclosed between carets. (e.g. <key> ^<value>^) Maxis used it as early as Sims Steering Committee, a prototype of The Sims, for animation, file, and texture names, and later in The Sims Online for its interface text. It's handy on its own for any project that wants a simple table of strings or a clear path to translation.
- TSO compatible, checked against the beta, base game, and New & Improved
- Values with no key, and names above values
- Keys with spaces, and indented keys
- Variables
- UTF-8 and Windows-1252 files
- References between tables
- Sims Steering Committee compatible
- Numbered keys such as
1.labeland1: -
.dirfolders of#<id>#<name>.csttables, laid over one another
- Numbered keys such as
- Multiline values
- C-style comments
- Malformed entries reported rather than stopping the read
- UIText
The format is described in the CST specification. API documentation is at tonytins.github.io/swift-cst.
Basic VS Code syntax highlighting is available in addons/vscode.
Values can be placed on the same line as their key or on a later line. Blank lines and comments between them don't matter, and multiline values are supported in both layouts. A key must match whole, so ok doesn't find ok button.
Whole-line // and # comments are ignored. Block comments beginning with
/* are ignored through the line containing */. These comment lines are also
ignored inside multiline values.
An unterminated value is treated as missing if a line holding a whole entry comes
before its closing caret. This prevents a malformed entry from consuming later
entries. CSTTable reports these in its problems.
let package = Package(
// name, platforms, products, etc.
dependencies: [
// other dependencies
.package(url: "https://github.com/tonytins/swift-cst.git", from: "0.4.1"),
],
targets: [
.executableTarget(name: "<your-game>", dependencies: [
// other dependencies
.product(name: "SwiftCST", package: "swift-cst"),
]),
// other targets
]
)Or in Xcode: File -> Add Package Dependencies, then paste the repo URL.
let content = "1 ^Hello %s!^" // %s represents a string variable
let helloWorld = CST.parse(content, key: 1, variables: "World")
print(helloWorld)CST.parse returns *** MISSING *** for a missing key. To tell that apart from
real text, or to look up many keys without reading the document each time, use
CSTTable:
let table = CSTTable(content)
if let hello = table.value(forKey: 1) {
print(CST.format(hello, with: ["World"]))
}
print(table.entries.count) // Every value, in file order
print(table.problems) // Anything malformedUIText looks up a key in a language directory containing files named
_<id>_<name>.cst, the way The Sims Online does. It reads UTF-8 and Windows-1252
files, finds values with no key by their position from 0, and follows references
between tables. By default it first searches an uitext directory beside the
running executable, then falls back to the app bundle's uitext resources. A
different first-choice directory can be provided with basePath; the bundle lookup
remains the fallback.
let text = UIText(language: "english")
let greeting = try text.text(for: 154, key: 1)
print(greeting)Each table is read once, so changes to a file after its first lookup aren't seen.
To read an install, pass its gamedata directory, and UIText finds the layout
itself, whether New & Improved's uitext/<language>.dir or the earlier uitext.dir:
let install = UIText(gameData: URL(fileURLWithPath: "/path/to/gamedata"))For a game's own tables kept directly in a folder, with no language directory, use
UIText(directory:).
Missing file IDs, keys, and language directories return *** MISSING ***. To tell a
missing string from real text, use textIfPresent, which returns nil instead:
if let greeting = try text.textIfPresent(for: 154, key: 1) {
print(greeting)
}Other filesystem errors are thrown to the caller.
Parsing and file lookup are synchronous. Async overloads remain temporarily available for source compatibility and are deprecated.
| swift-cst | Minimum Swift Version |
|---|---|
0.1.0 ... |
6.3 |
I license this project under BSD-3-Clause license — see the LICENSE file for full text.