CodeLiturgy.Dashboard/BlueWest/Tools/SaveLoader.cs

149 lines
5.2 KiB
C#
Raw Normal View History

2021-12-06 02:49:27 +03:00
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Directory = System.IO.Directory;
using File = System.IO.File;
namespace PerformanceSolution.Tools
{
/// <summary>
/// Allows the save and load of objects in a specific folder and file.
/// </summary>
public static class SaveLoadManager
{
private static readonly string _baseFolderName = Path.DirectorySeparatorChar + "BMData";
private const string _defaultFolderName = "BlueWest";
/// <summary>
/// Determines the save path to use when loading and saving a file based on a folder name.
/// </summary>
/// <returns>The save path.</returns>
/// <param name="folderName">Folder name.</param>
static string DetermineSavePath(string folderName = _defaultFolderName)
{
string savePath;
// depending on the device we're on, we assemble the path
//savePath = OS.GetUserDataDir() + "/";
savePath = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData));
// #if UNITY_EDITOR
// savePath = Application.dataPath + _baseFolderName;
// #endif
var pathSeparator = Path.DirectorySeparatorChar;
savePath = savePath + pathSeparator + folderName + pathSeparator;
return savePath;
}
/// <summary>
/// Determines the name of the file to save
/// </summary>
/// <returns>The save file name.</returns>
/// <param name="fileName">File name.</param>
static string DetermineSaveFileName(string fileName)
{
return fileName + ".binary";
}
/// <summary>
/// Save the specified saveObject, fileName and foldername into a file on disk.
/// </summary>
/// <param name="saveObject">Save object.</param>
/// <param name="fileName">File name.</param>
/// <param name="foldername">Foldername.</param>
public static void Save<T>(T saveObject, string fileName, string foldername = _defaultFolderName)
where T : class
{
string savePath = DetermineSavePath(foldername);
string saveFileName = DetermineSaveFileName(fileName);
// if the directory doesn't already exist, we create it
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
// we serialize and write our object into a file on disk
2022-08-13 08:34:20 +03:00
//var byteData = MessagePackSerializer.Serialize(saveObject);
2021-12-06 02:49:27 +03:00
File.WriteAllText(savePath + saveFileName + ".json", saveObject.ToString());
2022-08-13 08:34:20 +03:00
//File.WriteAllBytes(savePath + saveFileName, byteData);
2021-12-06 02:49:27 +03:00
}
/// <summary>
/// Load the specified file based on a file name into a specified folder
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="foldername">Foldername.</param>
public static T Load<T>(string fileName, string foldername = _defaultFolderName) where T : class
{
string savePath = DetermineSavePath(foldername);
string saveFileName = savePath + DetermineSaveFileName(fileName);
object returnObject;
// if the MMSaves directory or the save file doesn't exist, there's nothing to load, we do nothing and exit
if (!Directory.Exists(savePath) || !File.Exists(saveFileName))
{
return null;
}
byte[] readByte = File.ReadAllBytes(saveFileName);
2022-08-13 08:34:20 +03:00
//var finalObject = MessagePackSerializer.Deserialize<T>(readByte);
2021-12-06 02:49:27 +03:00
2022-08-13 08:34:20 +03:00
//return finalObject;
return null;
2021-12-06 02:49:27 +03:00
}
/// <summary>
/// Removes a save from disk
/// </summary>
/// <param name="fileName">File name.</param>
/// <param name="folderName">Folder name.</param>
public static void DeleteSave(string fileName, string folderName = _defaultFolderName)
{
string savePath = DetermineSavePath(folderName);
string saveFileName = DetermineSaveFileName(fileName);
if (File.Exists(savePath + saveFileName))
{
File.Delete(savePath + saveFileName);
}
}
public static void DeleteSaveFolder(string folderName = _defaultFolderName)
{
string savePath = DetermineSavePath(folderName);
if (Directory.Exists(savePath))
{
DeleteDirectory(savePath);
}
}
public static void DeleteDirectory(string target_dir)
{
string[] files = Directory.GetFiles(target_dir);
string[] dirs = Directory.GetDirectories(target_dir);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
File.Delete(file);
}
foreach (string dir in dirs)
{
DeleteDirectory(dir);
}
Directory.Delete(target_dir, false);
}
}
}