Class DataPath
A utility class used for handling multiple save slots. Different DataPath instances can have different properties and rules.
public class DataPath
- Inheritance
-
DataPath
- Inherited Members
Constructors
DataPath(string, bool)
Creates a new DataPath
public DataPath(string sub_dir, bool allow_delete = true)
Parameters
sub_dirstringThe path which serves as the root of this data path
allow_deleteboolwhether or not this data path allows deletion operations.
Fields
META_DATA_FILE_PATH
private const string META_DATA_FILE_PATH = "save_slot_meta.json"
Field Value
_allowDelete
Whether or not this data path allows deletion operations.
private readonly bool _allowDelete
Field Value
_saveSlotMetaData
Any save slot metadata. Helpful for learning more about the save slot in question when presenting multiple slots.
private readonly Dictionary<string, string> _saveSlotMetaData
Field Value
Properties
CurrentPath
The path to the current data path. I.e. the root directory to save/load to/from
public string CurrentPath { get; private set; }
Property Value
JsonSettings
Settings for JSON Serialization.
public JsonSerializerOptions JsonSettings { get; set; }
Property Value
Methods
AddSlotMetaData(string, string)
Assigns a key-value for the metadata of this save slot. Useful for setting identifying information for different save slots. See ISaveSlotInformationProvider for information on how save slot names are generated.
public void AddSlotMetaData(string key, string value)
Parameters
DeleteSaveSlot()
Deletes this save slot and all data within it. Only allowed if _allowDelete is true. This rudimentarily prevents deleting certain save slots (i.e. the root slot) but is not a hard limit because we're programmers. If there's a will and a page on Stack Overflow, life will find a way.
public void DeleteSaveSlot()
EnsureDirectoryPaths(string)
private static void EnsureDirectoryPaths(string file_path)
Parameters
file_pathstring
GetMetaData(string)
Gets the value of a particular metadata for the given key.
public string GetMetaData(string key)
Parameters
keystring
Returns
- string
the metadata value, or "" if not found.
LoadDefaultJsonSettings()
private void LoadDefaultJsonSettings()
LoadDict(string, bool)
public Dictionary LoadDict(string path, bool print_errors)
Parameters
Returns
- Dictionary
LoadMetaData()
Loads metadata from disk.
public void LoadMetaData()
LoadText(string, bool)
Loads the full text of a file as a single string.
public string LoadText(string path, bool print_errors)
Parameters
pathstringthe file path to save (relative from this data path's root), no '/' prefix needed
print_errorsboolWhether or not to print out errors to console when met. Used to handle situations where a file not existing is expected and handled otherwise. Only has an effect in a DEBUG context
Returns
- string
the full text of the file as a string. Or null if it failed to load the file.
Load<T>(string, bool)
Loads data from a json file to the serializable class.
public T Load<T>(string path, bool print_errors = true) where T : class
Parameters
pathstringThe path of a json file relative to the assigned path (likely a save slot)
print_errorsbool
Returns
- T
The data of type T that was loaded from file.
Type Parameters
Tdata type to load. Must be specified when it cannot be inferred.
SaveDict(Dictionary, string, bool, bool)
public void SaveDict(Dictionary dict, string path, bool do_flush = false, bool print_errors = true)
Parameters
SaveMetaData()
Saves the metadata out to disk.
public void SaveMetaData()
SaveText(string, string, bool, bool)
public void SaveText(string text, string path, bool do_flush = false, bool print_errors = true)
Parameters
Save<T>(T, string, bool, bool)
Saves data in JSON format to the specified path
[MarkForRefactor("Currently Borked", "")]
public void Save<T>(T data, string path, bool do_flush = false, bool print_errors = true) where T : class
Parameters
dataTthe serializable class to save
pathstringthe path relative to the assigned path (likely a save slot)
do_flushboolWhether or not to force an IO flush. This is typically unnecessary and will cause a small pause in the game. But forcing a flush does prevent loss of data in the case of a crash. So this is recommended for level save data.
print_errorsbool
Type Parameters
Tinferred type of data
SetJsonSettings(JsonSerializerOptions)
Changes the JsonSerializerOptions for this given DataPath. It affects the way types are serialized into JSON when using that functionality.
public void SetJsonSettings(JsonSerializerOptions options)
Parameters
optionsJsonSerializerOptionsthe new options to set