Save folder and Peek
These read the saved data of mods that used SaveData.Register, straight from the save folder on disk.
SaveData the save folder and an early read
Folder is the folder of the save you are playing. Peek<T>(modId) reads the data a mod saved with SaveData.Register from that folder before the load has finished.
public static class SaveData
{
public static string Folder { get; }
public static T Peek<T>(string modId) where T : class, new();
}
| Method | Returns | What it does |
|---|---|---|
Peek<T>(modId) | T | Reads your mod’s saved data early, before it is loaded for you. |
| Property | Type | What it means |
|---|---|---|
Folder | string | The folder of the last save that finished loading. null when there is none. |
Things to know
Folderis the folder of the save you are playing. It is set when a load finishes, just beforeEvents.Loadedfires. It isnullon the main menu and in a new game until its first save is loaded.Peek<T>(modId)reads your mod’s saved data file without waiting for the load. It gives you the data from the previous save untilEvents.Loaded. It returnsdefault(T)(that isnull) whenFolderisnullor the file does not exist. (it reads<Folder>\mods\<modId>.json, picking the file the same wayRegisterdoes, and reads it the same way (Newtonsoft.Json, withJsonUtilityas the fallback).)Peeknever writes anything and does not needRegisterfirst. (a bad id is empty, over 64 characters, has a path character, has.., or is a reserved Windows name, the same checkRegistermakes.)Registerrepairs a file that the main reader rejects. If Newtonsoft rejects the file andJsonUtilityreads it,Registerloads those values, writes a warning, and keeps a copy of the file as<modId>.json.bad, replacing the previous copy, so at most one copy is kept per mod. The next save rewrites the file in the Newtonsoft format.Peekdoes none of this.
Limits
- Until
Events.Loaded,SaveData.FolderandPeekstill point at the previous save. So in anEvents.SceneLoadedhandler of a load, they do not yet show the save being loaded. - After a load that failed,
Folderstill points at the save before that load, or isnullif there was none. - A file that cannot be read or parsed is logged and also gives
default(T). - A bad
modIdthrowsArgumentException, before any folder is read. - A save file over 64 MB is refused. The framework logs an error and keeps the previous file. (the limit is 67,108,864 bytes.)