Skip to main content

Saver

Important Attention Reminder

Coding Style wiki​


Saver is the abstract saver base class of the SaverSystem (implements ISaver and IDisposable). It defines the basic string/int/float key/value storage as abstract methods (the storage backend is decided by subclasses) and provides the text-content Data Map feature (multiple key value entries packed into one stored string key, with a parse cache). Built-in implementations are PlayerPrefsSaver and EditorPrefsSaver, and you can also extend it yourself.

NamespaceOxGKit.SaverSystem
Typepublic abstract class Saver : ISaver, IDisposable
SourceSaver.cs, ISaver.cs
using OxGKit.SaverSystem;

Quick Start​

using OxGKit.SaverSystem;

// Create a saver (built-in PlayerPrefsSaver as the example)
Saver saver = new PlayerPrefsSaver();

// Basic access
saver.SaveInt("language", 2);
int language = saver.GetInt("language", 0);

// Text-content storage (multiple key value entries inside ONE contentKey)
saver.SaveData("appPrefs", "lastLogin", "2026-07-29");
string lastLogin = saver.GetData("appPrefs", "lastLogin", null);

General Rules​

ISaver Interface​

public interface ISaver
{
void SaveString(string key, string value);

string GetString(string key, string defaultValue = "");

void SaveInt(string key, int value);

int GetInt(string key, int defaultValue = 0);

void SaveFloat(string key, float value);

float GetFloat(string key, float defaultValue = 0f);

bool HasKey(string key);

void DeleteKey(string key);

void DeleteAll();
}

All basic storage methods of Saver are abstract (matching the ISaver interface) and are overridden by subclasses to decide the storage backend; the text-content data feature (SaveData / GetData / DeleteData / DeleteContext) is implemented by the base class, so every subclass can use it directly.

Text-Content Format​

SaveData stores multiple entries in a one key value entry per line format inside the text content of one stored string key (contentKey):

# lines starting with '#' are comments
lastLogin 2026-07-29
resolution 1920x1080

Important Each line is split on the first space into key and value: keys must not contain spaces; values may contain spaces but must not contain newlines (line-based parsing).

Reminder Reuse one Saver instance — the GetData parse cache is per instance; re-creating an instance for every call defeats the cache.

Storage Scope​

Attention This system targets key/value storage for settings and small data (see the Module Intro for an overview). There is no bool or object API — store bools as int (0/1), and serialize objects to JSON strings yourself and store them via SaveString.


Basic Storage​

Method Overview​

MethodDescription
SaveStringSaves a string.
GetStringGets a string.
SaveIntSaves an int.
GetIntGets an int.
SaveFloatSaves a float.
GetFloatGets a float.
HasKeyChecks whether a key exists.
DeleteKeyDeletes a key.
DeleteAllDeletes all keys.

SaveString​

public abstract void SaveString(string key, string value)

Saves a string (storage backend implemented by subclasses).

saver.SaveString("name", "michael");

GetString​

public abstract string GetString(string key, string defaultValue = "")

Gets a string; returns defaultValue when the key does not exist.

SaveInt​

public abstract void SaveInt(string key, int value)

Saves an int.

saver.SaveInt("language", 2);

GetInt​

public abstract int GetInt(string key, int defaultValue = 0)

Gets an int; returns defaultValue when the key does not exist.

SaveFloat​

public abstract void SaveFloat(string key, float value)

Saves a float.

saver.SaveFloat("bgmVolume", 0.8f);

GetFloat​

public abstract float GetFloat(string key, float defaultValue = 0)

Gets a float; returns defaultValue when the key does not exist.

HasKey​

public abstract bool HasKey(string key)

Checks whether the specified key exists.

DeleteKey​

public abstract void DeleteKey(string key)

Deletes the specified key.

DeleteAll​

public abstract void DeleteAll()

Deletes all keys (the affected scope depends on the storage backend — see the implementation pages).


Text-Content Data (Data Map)​

Method Overview​

MethodDescription
SaveDataStores data in text form (adds or updates one key value entry).
GetDataGets a specific entry from the text (parse-cached).
DeleteDataDeletes a specific entry from the text.
DeleteContextDeletes the whole text content (including the parse cache).
ParsingDataMap(Static) Parses text content into a dictionary.
DisposeReleases the parse caches.

SaveData​

public void SaveData(string contentKey, string key, string value)

Adds or updates one key value entry inside the text content of contentKey (internally reads/writes the whole text via GetString / SaveString), and marks it dirty so GetData re-parses.

saver.SaveData("appPrefs", "lastLogin", "2026-07-29");
saver.SaveData("appPrefs", "resolution", "1920x1080");

GetData​

public string GetData(string contentKey, string key, string defaultValue = null)

Gets the entry of key from the text of contentKey; returns defaultValue when the text does not exist or the entry is empty. The parsed result is cached per contentKey (Dirty Flag mechanism) and the text is re-parsed only after SaveData / DeleteData.

string lastLogin = saver.GetData("appPrefs", "lastLogin", null);

DeleteData​

public void DeleteData(string contentKey, string key)

Deletes the entry line of key from the text of contentKey, and marks it dirty.

saver.DeleteData("appPrefs", "lastLogin");

DeleteContext​

public void DeleteContext(string contextKey)

Deletes the whole text content (internally calls DeleteKey to remove the stored key), and also clears the associated parse cache and dirty flag.

saver.DeleteContext("appPrefs");

ParsingDataMap​

public static Dictionary<string, string> ParsingDataMap(string content)

(Static method) Parses text content into a Dictionary<string, string>: splits line by line on newlines, skips comment lines starting with #, and splits each line on the first space into key and value; for duplicate keys only the first entry wins.

// Can also be used standalone as a parser for "key value" line-format config files
string content = saver.GetString("appPrefs");
Dictionary<string, string> dataMap = Saver.ParsingDataMap(content);

Reminder Since # comment lines are supported, the format also works as a hand-editable config file format (for example, OxGFrame parses its media URL config files with Saver.ParsingDataMap).

Dispose​

public virtual void Dispose()

Releases the parse caches (Data Map caches and dirty flags).

Attention After Dispose, the cache dictionaries are set to null — the instance's text-content data feature must not be used anymore; create a new Saver instance instead.


Custom Saver​

Inherit Saver and override the abstract methods to persist data anywhere (file, encrypted storage, cloud, etc.); the text-content data feature (Data Map) is provided by the base class — no extra implementation needed.

using OxGKit.SaverSystem;

public class FileSaver : Saver
{
public override void SaveString(string key, string value) { /* write file */ }

public override string GetString(string key, string defaultValue = "") { /* read file */ return defaultValue; }

public override void SaveInt(string key, int value) => this.SaveString(key, value.ToString());

public override int GetInt(string key, int defaultValue = 0) => int.TryParse(this.GetString(key), out var value) ? value : defaultValue;

public override void SaveFloat(string key, float value) => this.SaveString(key, value.ToString());

public override float GetFloat(string key, float defaultValue = 0f) => float.TryParse(this.GetString(key), out var value) ? value : defaultValue;

public override bool HasKey(string key) { /* ... */ return false; }

public override void DeleteKey(string key) { /* ... */ }

public override void DeleteAll() { /* ... */ }
}