PlainSave - Technical Documentation

This page provides an overview of the methods and properties of the PlainSaveManager class in the PlainSave namespace.


Namespace: PlainSave

The PlainSave namespace contains the PlainSaveManager class, which handles save data creation, loading, and management for the game.


Class: PlainSaveManager

The PlainSaveManager class is responsible for managing save files and folders, initializing data, and handling game saves. It provides methods for creating and loading save files.


Static Constructor: PlainSaveManager()

The static constructor for the PlainSaveManager class is called automatically before any static members are accessed or any instances are created. It performs essential initialization tasks to prepare the class for use, including directory checks and validation of default file content.

Static Fields

Enumeration: VALIDATIONFOLDER

The VALIDATIONFOLDER enumeration defines the folder types used for validation:

CreateSave

Creates a new save file (folder) based on the default data files. This method checks for existing folders with the same name and appends a counter if necessary. It copies default files into the new save folder. Use this method when a player starts a new game.

public static void CreateSave(string filename)

LoadSave

Loads save files from the specified folder into memory, populating the LoadedSaveFileData dictionary for later use, such as loading player progress. Use this method when a player loads a save file. It checks for the existence of the folder and handles multiple save files.

public static void LoadSave(string foldername)

DeleteSave

Deletes a save folder under SaveData. This method is used when a player deletes save files from a menu. If the game is running in the Unity Editor, it deletes the folder using UnityEditor.AssetDatabase.DeleteAsset. Otherwise, it uses Directory.Delete() to remove the folder and all its contents.

public static void DeleteSave(string filename)

If the game is running in the Unity Editor, the folder will be deleted using UnityEditor.AssetDatabase.DeleteAsset. Otherwise, it will use Directory.Delete().

Save

Updates the value in memory for the specified file and ID. If the new value contains a subdelimiter, the method will update the value while keeping the tag unchanged. Note that this method does not persist changes to the file until CommitSave() is called.

public static void Save(string filename, int id, string newValue)

CommitSave

Writes the modified data from memory to the specified file in the current save folder. This method should be called after any changes have been made using the Save() method. If there are no changes to commit or the necessary data is not available, the operation will be aborted.

public static void CommitSave(string filename)

GetPlainSaveObject

Retrieves a PlainSaveData object from memory based on the specified filename and ID. This method checks if the data has been loaded successfully and if the specified ID is valid.

public static PlainSaveData GetPlainSaveObject(string filename, int id)

Returns: A PlainSaveData object if found; otherwise, returns null. If the data is not found, a warning is logged to indicate the issue.

GetSaveDataByID

Retrieves a single data value from memory based on the specified filename and ID. This method is best used when needing to retrieve only one value. An exact ID is required; otherwise, it will return an error.

public static string GetSaveDataByID(string filename, int id)

Returns: The data value associated with the given filename and ID. If the data is not found or an error occurs, this method returns null.

GetSaveDataByIDs

Gets multiple data values from memory based on filename and IDs. Best when needing to retrieve multiple values quickly. Note that an exact ID is needed, otherwise this will return an error.

public static List<string> GetSaveDataByIDs(string filename, List<int> ids)

Returns: A list of strings containing the data values from the given file based on the provided IDs.

GetSaveDataByTag

Gets all data values from a file that are associated with a specific tag.

public static List<string> GetSaveDataByTag(string filename, string tag)

Returns: A list of data values associated with the specified tag. If no values are found, an empty list is returned.

GetSaveDataByTags

Retrieves all data values from a file that correspond to multiple specified tags. This method is best for gathering multiple values associated with various tags.

public static List<string> GetSaveDataByTags(string filename, List<string> tags)

Returns: A list of string values associated with the provided tags. If no values are found, an empty list is returned.

GetSaveFileFromMemory

Retrieves all data from memory for the specified filename. Note that this method returns data from memory, not from a file.

public static List<PlainSaveData> GetSaveFileFromMemory(string filename)

Returns: A list of PlainSaveData containing the data values stored in memory for the specified filename. If no data is found, an empty list is returned.

ReadFileWithoutLoading

Reads data directly from a specified file without loading it into memory. This method is particularly useful for one-time reads, such as loading settings during the game startup. It returns a list of PlainSaveData objects containing the data read from the file. If the file does not exist, validation fails, or no data is found, it returns an empty list.

public static List<PlainSaveData> ReadFileWithoutLoading(string filepath)

ListSaveFiles

Retrieves a list of all available save files within the save data folder. This is useful for displaying save options in menus, such as load or save game screens.

public static List<string> ListSaveFiles()

DirectoryCheck

Checks the existence of required directories for the PlainSave system and logs warnings if any are missing or empty.

private static void DirectoryCheck()

Returns: This method does not return a value. It logs warnings based on the directory checks.

Notes:

IsDirectoryEmpty

Checks whether a directory is empty by confirming that it contains no files or subdirectories.

private static bool IsDirectoryEmpty(string path)

Returns: A bool value. Returns true if the directory is empty, false otherwise.

InitScriptableObject

Ensures the PlainSave system has access to the required ScriptableObject containing file name settings. If the asset is missing, it logs a warning and provides instructions on how to fix the issue.

private static void InitScriptableObject()

GetFinalFileName

Retrieves the appropriate save file name based on the default file name. It ensures consistency with names set in the PlainSaveSettings ScriptableObject.

private static string GetFinalFileName(string defaultFileName)

ValidateAllFileContent

Validates the content of all files within a folder. It checks each file's format and structure to ensure that they conform to the expected save file standards. This method can be used for batch validation of files in either the default folder or the save data folder.

internal static void ValidateAllFileContent(VALIDATIONFOLDER folder)

Sets: Updates internal flags (defaultValidationSuccess or saveValidationSuccess) to indicate validation status for the default or save folder, respectively.

ValidateFileContent

Validates the contents of a file against specified criteria including:

This method logs warnings for any validation failures and returns true if the file is valid; otherwise, false.

internal static bool ValidateFile(string[] lines)

Returns: true if the file format is valid; otherwise, false.