using System; using System.IO; using UnityEngine; public class SaveSystem { private static SaveData _saveData = new SaveData(); [System.Serializable] public struct SaveData { public QuestSaveData QuestData; public StoryboolSaveData StoryboolData; public InventorySaveData InventorySaveData; } public static string SaveFileName() { return Application.persistentDataPath + "/save.data"; } public static void GenerateCorruptedSaveReadme() { string path = Application.persistentDataPath + "/README.txt"; string errorText = "Your save was corrupted.\n\n" + "This can happen when you edit the save manually, or if the game closed while saving. Your\n" + "corrupted save file has been saved in this directory as 'save.data.backup' if you'd like to\n" + "try manually fixing it. It should follow standard JSON formatting.\n\n" + "Once you fix it, just delete the current save.data file, and remove the '.backup' from your \n" + "old save file. Good luck, and sorry for the inconvenience!"; File.WriteAllText(path, errorText); } public static void Save() { HandleSaveData(); File.WriteAllText(SaveFileName(), JsonUtility.ToJson(_saveData, true)); } private static void HandleSaveData() { GameManager.Instance.SaveStoryBools(ref _saveData.StoryboolData); GameManager.Instance.Inventory.SaveInventory(ref _saveData.InventorySaveData); QuestRegistry.Instance.SaveQuestData(ref _saveData.QuestData); } public static void Load() { string saveContent; try { saveContent = File.ReadAllText(SaveFileName()); } catch (FileNotFoundException) { Save(); // create new save file if one does not exist saveContent = File.ReadAllText(SaveFileName()); } try { _saveData = JsonUtility.FromJson(saveContent); } catch (ArgumentException) { // Likely a JSON parse error. Let's back up the old version of the save and // make a new one for now File.WriteAllText(SaveFileName() + ".backup", saveContent); GenerateCorruptedSaveReadme(); Save(); } HandleLoadData(); } public static void HandleLoadData() { GameManager.Instance.LoadStoryBools(_saveData.StoryboolData); GameManager.Instance.Inventory.LoadInventory(_saveData.InventorySaveData); QuestRegistry.Instance.LoadQuestData(_saveData.QuestData); } }