Class LocalFileSystem
- Namespace
- WitShells.DesignPatterns.Core
A static async helper for persisting data to the local file system with AES-256 encryption and GZip compression.
public static class LocalFileSystem
- Inheritance
-
LocalFileSystem
- Inherited Members
Remarks
Typical use-case: save-game data, player preferences, or any sensitive runtime data
that should not be readable as plain text on disk.
All read/write operations are async to avoid blocking the Unity main thread.
Methods
CompressStringGZip(string)
Compresses a string using GZip and returns the compressed byte array.
public static byte[] CompressStringGZip(string input)
Parameters
inputstring
Returns
- byte[]
DecompressStringGZip(byte[])
Decompresses a GZip byte array back into a string.
public static string DecompressStringGZip(byte[] compressedBytes)
Parameters
compressedBytesbyte[]
Returns
Decrypt(string, string)
Decrypts a Base64 AES-256 cipher string produced by Encrypt(string, string).
public static string Decrypt(string cipherText, string password)
Parameters
Returns
DeleteFileAsync(string, string, string, string)
Asynchronously deletes a file after verifying the password via decryption.
public static Task<bool> DeleteFileAsync(string savePath, string fileName, string password, string extension = ".json")
Parameters
savePathstringDirectory containing the file.
fileNamestringFile name without extension.
passwordstringPassword used to verify ownership before deletion.
extensionstringFile extension (default
".json").
Returns
Exceptions
- LocalFileSystemException
Thrown when the file is not found or decryption fails.
Encrypt(string, string)
AES-256 encrypts plainText, prepends the IV, and returns a Base64 string.
public static string Encrypt(string plainText, string password)
Parameters
Returns
GetKey(string)
Derives a 256-bit AES key from a plain-text password using SHA-256.
public static byte[] GetKey(string password)
Parameters
passwordstringThe user-supplied password.
Returns
- byte[]
A 32-byte key array.
LoadAllFileNames(string, string)
Returns the file names (without extensions) of all files with the given extension
found under path.
public static IEnumerable<string> LoadAllFileNames(string path, string extension = ".json")
Parameters
Returns
- IEnumerable<string>
Enumerable of file names without extension.
LoadFileAsync(string, string, string, string)
Asynchronously reads, decrypts (AES-256), and decompresses (GZip) a file saved by SaveFileAsync(string, string, string, string, string).
public static Task<string> LoadFileAsync(string savePath, string fileName, string password, string extension = ".json")
Parameters
savePathstringDirectory containing the file.
fileNamestringFile name without extension.
passwordstringThe password used when the file was saved.
extensionstringFile extension (default
".json").
Returns
Exceptions
- LocalFileSystemException
Thrown when decryption fails (wrong password or corrupt file).
SaveFileAsync(string, string, string, string, string)
Asynchronously compresses (GZip) and encrypts (AES-256) jsonData
then writes it to <savePath>/<fileName><extension>.
The directory is created if it does not exist.
public static Task SaveFileAsync(string savePath, string fileName, string password, string jsonData, string extension = ".json")
Parameters
savePathstringTarget directory path.
fileNamestringFile name without extension.
passwordstringEncryption password (SHA-256 derived key).
jsonDatastringThe raw content to persist.
extensionstringFile extension (default
".json").