rmp::save
rmp/save.h — rmp::Value, and saving the game.
rmp::Value v;
v["level"] = 7;
v["name"] = "Omar";
v["unlocked"].push("forest");
rmp::save::write("slot1", v);
rmp::Value v;
if (rmp::save::read("slot1", v)) {
int level = v["level"].as_int(1); // 1 when the key is not there
}EVERY READ HAS A DEFAULT, AND NOTHING THROWS. A save written by yesterday's version is not corrupt, it is missing the keys that were added since — and a missing key reading as its default is what lets an update ship without a migration nine times out of ten. A key of the wrong type reads as the default too. For the tenth time, [save] version in the .toml is stored in the file and v.version() hands it back:
if (v.version() < 2) v["coins"] = v["gold"].as_int(0);WHAT A FAILED READ TELLS YOU. read() is true or false in an if, and when it is false, .status says why — because "there is no save yet", "the file was cut short" (the power went during a write) and "the file has been changed" are three different messages to show a player:
const rmp::save::Result r = rmp::save::read("slot1", v);
if (r.status == rmp::save::Status::MODIFIED) show("this save was edited");WHERE IT GOES. The user's data folder, or next to the executable with [save] portable — see directory(). A write goes to a temporary file first and replaces the old save only once it is complete, so a crash mid-write leaves the previous save intact. On the web it lands in the browser's IndexedDB and survives a reload.
SEALED SAVES are tamper RESISTANCE, not security: the key is in the binary. They stop a player opening the file in a text editor and giving themselves a thousand coins, and any change to a sealed file is detected. Nothing stops someone determined, and nothing here pretends to.
The JSON underneath is cJSON, and it appears in no header: this file is all a game ever sees of it.
Status
Why read() did or did not fill the Value. A slot name read() refuses is UNREADABLE too.
| Value | Means | |
|---|---|---|
OK | Read, checked, and out filled | |
MISSING | Nothing saved in that slot yet: the first run | |
TRUNCATED | Cut short, as a write the power went out on would be | |
MODIFIED | The checksum or the seal does not match: edited, or damaged | |
UNREADABLE | Not a save at all, or one from a newer framework |
Result
A read's outcome. True in an if only when it is OK, so the three-line load stays three lines; .status is there when the reason matters.
| Field | Type | Default | What it is |
|---|---|---|---|
status | Status | Status::MISSING | Why; MISSING until a |
OK, and only OK
WriteOptions
How write() stores a save. Declaration order is the order they are written in, as C++20 requires.
| Field | Type | Default | What it is |
|---|---|---|---|
encrypted | bool | RMP_SAVE_ENCRYPT != 0 |
|
ReadOptions
What read() accepts as a save.
| Field | Type | Default | What it is |
|---|---|---|---|
sealed_only | bool | RMP_SAVE_ENCRYPT != 0 | With |
write
A slot is a file name without the extension: letters, digits, '_', '-' and '.', not starting with '.', at most 64 of them. Anything else is refused and said once — a slot name is not a place to put a path.
False when it could not be written; the previous save, if any, is intact. Also false, and said once, for a Value that could not be read back: more than 64 lists and objects inside each other, a NUL character in a string or a key, or a number that is not finite — a NaN or an infinity, which JSON cannot hold.
read
Fills out and says so. On anything but OK, out is left exactly as it was, so a game can fill it with defaults first and read over them. A file holding a number too large for a double (1e400) is UNREADABLE: no save holds one.
exists, remove
Whether there is a save in that slot, in any folder read() looks in. It does not open it, so a file that would read as TRUNCATED or MODIFIED exists too. False, without a warning, for a slot name write() would refuse.
remove: False when there was nothing to remove
directory
Where saves are written: the folder [save] portable asked for if it can be written, otherwise the user's data folder — never where the .toml asked for if that turned out to be read-only. With a trailing separator. With [save] portable, read() looks in both folders and takes the newer save, so a session that had to fall back and the next one that did not agree.