On this page
  1. Status
  2. Result
  3. WriteOptions
  4. ReadOptions
  5. write
  6. read
  7. exists, remove
  8. directory

rmp::save

namespace #include <rmp/save.h>

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

enum class Status

Why read() did or did not fill the Value. A slot name read() refuses is UNREADABLE too.

ValueMeans
OKRead, checked, and out filled
MISSINGNothing saved in that slot yet: the first run
TRUNCATEDCut short, as a write the power went out on would be
MODIFIEDThe checksum or the seal does not match: edited, or damaged
UNREADABLENot a save at all, or one from a newer framework

Result

struct 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.

FieldTypeDefaultWhat it is
statusStatusStatus::MISSING

Why; MISSING until a read() sets it

explicit operator bool() const

OK, and only OK

WriteOptions

struct WriteOptions

How write() stores a save. Declaration order is the order they are written in, as C++20 requires.

FieldTypeDefaultWhat it is
encryptedboolRMP_SAVE_ENCRYPT != 0

[save] encrypt decides the default

ReadOptions

struct ReadOptions

What read() accepts as a save.

FieldTypeDefaultWhat it is
sealed_onlyboolRMP_SAVE_ENCRYPT != 0

With [save] encrypt on, a PLAIN file reads as MODIFIED: anyone can write one — the format is documented and a plain save shows it — and accepting it would let a player replace a sealed save with their own. A game that turns sealing on in an update reads its players' old plain saves with { .sealed_only = false } for as long as they may have one.

write

bool write(std::string_view slot, const Value &value, const WriteOptions &options = {})

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

Result read(std::string_view slot, Value &out, const ReadOptions &options = {})

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

bool exists(std::string_view slot)
bool remove(std::string_view slot)

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

std::string 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.