using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Linq; using UnityEngine; namespace CryingSnow.FarmingIsland { public static class SaveSystem { /// /// Saves data to a file with a specified name. /// /// The type of the data to be saved. /// The data to be saved. /// The name of the file to save the data to. public static void SaveData(T data, string fileName) { // Construct the full path to the save file string filePath = Application.persistentDataPath + "/" + fileName + ".dat"; // Create a new BinaryFormatter for serialization BinaryFormatter formatter = new BinaryFormatter(); // Open a file stream to create or overwrite the file using (FileStream fileStream = new FileStream(filePath, FileMode.Create)) { // Serialize the data to the file formatter.Serialize(fileStream, data); } } /// /// Loads data from a file with a specified name. /// /// The type of the data to be loaded. /// The name of the file to load the data from. /// The loaded data, or default(T) if the file does not exist. public static T LoadData(string fileName) { // Construct the full path to the save file string filePath = Application.persistentDataPath + "/" + fileName + ".dat"; // Check if the file exists before attempting to load it if (File.Exists(filePath)) { // Create a new BinaryFormatter for deserialization BinaryFormatter formatter = new BinaryFormatter(); // Open a file stream to read the file using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { // Deserialize the data from the file T data = (T)formatter.Deserialize(fileStream); return data; } } else { // Optional: Log an error message if the file does not exist // Debug.LogError("Save file not found in " + filePath); // Return default value if the file does not exist return default(T); } } /// /// Retrieves the name of the most recently modified save file (without the extension). /// /// The name of the latest save file without its extension, or null if no save files are found. public static string GetLatestSaveFileName() { // Get all .dat save files from the persistent data path var saveFiles = Directory.GetFiles(Application.persistentDataPath, "*.dat"); // Order the files by their last write time in descending order and return the file name without the extension return saveFiles.OrderByDescending(File.GetLastWriteTime) .Select(Path.GetFileNameWithoutExtension) .FirstOrDefault(); // Returns null if no save files are found } /// /// Checks whether a save file with the specified name exists. /// /// The name of the save file (without extension) to check. /// true if a save file with the given name exists; otherwise, false. public static bool SaveFileExists(string fileName) { // Get all .dat save files from the persistent data path and extract their names without extensions var saveFiles = Directory.GetFiles(Application.persistentDataPath, "*.dat") .Select(Path.GetFileNameWithoutExtension) .ToList(); // Check if the list contains the specified file name return saveFiles.Contains(fileName); } } }