Skip to content

Your own storage

A storage is where the bytes of a save live. The slots hand a storage a key (the slot name) and the bytes of a save, and ask for them back later. They do not know whether the bytes sit in a file, in memory, in a cloud or in a console’s save API. The Save System ships with a file storage. You can write another one.

  • You want slots in a cloud or a platform’s own save service, and you still want the slot list, backups and the load report.
  • You want saves in memory for tests.
  • You plan to sync the save folder with a service such as Steam Auto-Cloud.

If you only want the bytes, and no slots, see Storing the bytes yourself.

ISaveStorage has eight members. No method throws. A problem comes back as a StorageResult with a status and a plain-English reason.

Member What it does
Task<StorageResult> WriteAsync(string key, byte[] bytes) Stores the bytes. An existing save under the key is replaced as a whole or not at all.
Task<StorageResult<byte[]>> ReadAsync(string key) Reads all the bytes. A storage with backups answers from the backup only when the main copy is missing or empty, and then sets FromBackup.
Task<StorageResult<byte[]>> ReadHeaderAsync(string key, int maxBytes) Reads the first bytes, enough for the list to read a header.
Task<StorageResult<byte[]>> ReadBackupAsync(string key) Reads the save as it was before the last overwrite. NotFound when there is none.
Task<StorageResult<IReadOnlyList<StorageEntry>>> ListAsync() Lists the keys that hold a save, newest first. An empty storage gives an empty list, not an error.
Task<StorageResult> DeleteAsync(string key) Removes the save and every copy. Nothing stored gives NotFound, which is not a failure.
Task<StorageResult<bool>> ExistsAsync(string key) Whether a save can be read. The answer is in Value.
StorageCaps Caps { get; } What the storage can promise.

sources: Runtime/Save/Storage/ISaveStorage.cs.

The rules in the code’s remarks: a write leaves the new bytes whole or the old bytes whole, and never half a save. Caps must say honestly whether the storage can promise that. A key is checked with StorageKeys.IsValid before anything touches a disk. The tasks may finish on a worker thread, so the caller continues on the main thread by itself before it touches Unity objects.

StorageCaps is a snapshot with these properties.

Property Meaning
Atomic The last overwrite replaced the old save in one step.
SupportsRange ReadHeaderAsync reads only the first bytes instead of the whole save.
SynchronousOnly Every task is finished when the call returns, with no thread. WebGL needs this.
MaxBytes The largest save the storage takes. The default is 512 MiB.
HasBackup The storage keeps the previous copy, so ReadBackupAsync can answer.

The Save System’s load of a damaged slot only tries the backup when HasBackup is true.

StorageStatus Meaning
Ok The call did what was asked.
NotFound Nothing is stored under the key.
InvalidKey The key is not allowed. Nothing was touched.
InvalidArgument An argument is not usable. Nothing was touched.
TooLarge The bytes are larger than MaxBytes.
IoError The disk or the platform refused. The reason says what.

Every storage of Intrigues shares one key rule, so a slot name works the same in a file, in memory and in a test. StorageKeys.IsValid(key, out reason) checks it.

  • At most 100 characters (StorageKeys.MaxLength) and at most 120 bytes in UTF-8 (MaxUtf8Bytes).
  • Letters, digits, space, -, _ and .. Nothing else.
  • No leading or trailing dot or space.
  • Not a name Windows reserves for devices: CON, PRN, AUX, NUL, COM0 to COM9, LPT0 to LPT9, also with a dot part such as con.x.
  • Case does not count. “Slot” and “slot” are one save, so do not use two names that differ only in case.
Class What it does
FileSaveStorage One file per key in one folder. A write goes to a temporary file, then replaces the old save in one step, and the old save stays as <file>.bak. FileSaveStorage.CreateDefault() uses Intrigues Saves under the persistent data path and the extension .isav.
MemorySaveStorage Keeps saves in memory for tests, replays or undo snapshots. Nothing survives the end of the program. It keeps the previous bytes of a key as its backup, like the file storage.

The FileSaveStorage constructor is (folder, extension, synchronous, maxBytes). It throws an ArgumentException for an empty folder or a bad extension. That is a programming error, not a result. The extension needs a dot, only letters and digits after it, and cannot be .bak or .tmp.

Give the storage to a SlotManager: new SlotManager(storage, settings). The settings are a SlotSettings; leave them null for the defaults. The manager has SaveAsync, AutosaveAsync, LoadAsync, DeleteAsync and ListAsync, as the Save System’s Slots property does.

Example, not compiled. It is built from the signatures above:

var storage = new MemorySaveStorage();
var slots = new SlotManager(storage);
var saved = await slots.SaveAsync("slot1", new SaveRequest { Title = "Test" });

A slot manager still needs a Save System in the scene. Storage work may finish on a worker thread, and the Save System brings it back to the main thread at the end of the next frame. A manager you build raises IntrigueSave.State.Saved after a write, but not the component’s onSave event: the component raises onSave only from its own SaveAsync, SaveCurrentAsync and AutosaveAsync. It also does not read the component’s settings. Set encryption and the other options on its Settings yourself.

For Steam Auto-Cloud, one .isav file per slot in the Save Folder is the unit to sync. Ignore the .bak and .tmp files next to it. The README says this has not been tested with Steam.