using System; using System.Collections; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using MessagePack; using Directory = System.IO.Directory; using File = System.IO.File; namespace PerformanceSolution.Tools { /// /// Allows the save and load of objects in a specific folder and file. /// public static class SaveLoadManager { private static readonly string _baseFolderName = Path.DirectorySeparatorChar + "BMData"; private const string _defaultFolderName = "BlueWest"; /// /// Determines the save path to use when loading and saving a file based on a folder name. /// /// The save path. /// Folder name. 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; } /// /// Determines the name of the file to save /// /// The save file name. /// File name. static string DetermineSaveFileName(string fileName) { return fileName + ".binary"; } /// /// Save the specified saveObject, fileName and foldername into a file on disk. /// /// Save object. /// File name. /// Foldername. public static void Save(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 var byteData = MessagePackSerializer.Serialize(saveObject); File.WriteAllText(savePath + saveFileName + ".json", saveObject.ToString()); File.WriteAllBytes(savePath + saveFileName, byteData); } /// /// Load the specified file based on a file name into a specified folder /// /// File name. /// Foldername. public static T Load(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); var finalObject = MessagePackSerializer.Deserialize(readByte); return finalObject; } /// /// Removes a save from disk /// /// File name. /// Folder name. 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); } } }