using System; using System.Collections; using System.Collections.Generic; using Ichni.Menu; using Ichni.RhythmGame; using Ichni.Story; using Sirenix.OdinInspector; using UnityEngine; namespace Ichni { public class GameSaveManager : SerializedMonoBehaviour { public static GameSaveManager instance; public SongSaveModule SongSaveModule; public StorySaveModule StorySaveModule; private void Awake() { if (instance == null) { instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } private void Start() { SongSaveModule = new SongSaveModule(); SongSaveModule.LoadSongStatuses(); SongSaveModule.LoadStoryUnlockKeys(); StorySaveModule = new StorySaveModule(); StorySaveModule.LoadStoryVariables(); StorySaveModule.LoadChoices(); } } public partial class SongSaveModule { public HashSet storyUnlockKeys; public HashSet paymentUnlockKeys; public Dictionary songStatusSaves; private string songSavePath => Application.persistentDataPath + "/GameSaves/SongSaves.json"; private string storyUnlockKeysPath => Application.persistentDataPath + "/GameSaves/UnlockKeys.json"; private string paymentUnlockKeysPath => Application.persistentDataPath + "/GameSaves/PaymentUnlockKeys.json"; public SongSaveModule() { songStatusSaves = new Dictionary(); storyUnlockKeys = new HashSet(); paymentUnlockKeys = new HashSet(); //Debug.Log("Song save path: " + songSavePath); } } public partial class SongSaveModule { public void SaveStoryUnlockKeys() { ES3.Save("UnlockKeys", storyUnlockKeys, storyUnlockKeysPath); } public void LoadStoryUnlockKeys() { if (ES3.FileExists(storyUnlockKeysPath)) { storyUnlockKeys = ES3.Load>("UnlockKeys", storyUnlockKeysPath); } else { storyUnlockKeys = new HashSet(); SaveStoryUnlockKeys(); } } public bool CheckStoryKey(string key) { return key == string.Empty || storyUnlockKeys.Contains(key); } public void ClearStoryKeys() { storyUnlockKeys.Clear(); SaveStoryUnlockKeys(); } } public partial class SongSaveModule { public void SavePaymentUnlockKeys() { ES3.Save("UnlockKeys", paymentUnlockKeys, paymentUnlockKeysPath); } public void LoadPaymentUnlockKeys() { if (ES3.FileExists(paymentUnlockKeysPath)) { paymentUnlockKeys = ES3.Load>("UnlockKeys", paymentUnlockKeysPath); } else { paymentUnlockKeys = new HashSet(); SavePaymentUnlockKeys(); } } public bool CheckPaymentKey(string key) { return key == string.Empty || paymentUnlockKeys.Contains(key); } public void ClearPaymentKeys() { paymentUnlockKeys.Clear(); SavePaymentUnlockKeys(); } } public partial class SongSaveModule { private void InitializeSongStatuses() { foreach (ChapterSelectionUnit chapter in ChapterSelectionManager.instance.chapters) { foreach (SongItemData song in chapter.songs) { SongStatusSave songStatus = new SongStatusSave(false, string.Empty, new List()); foreach (DifficultyData difficulty in song.difficultyDataList) { songStatus.beatmapSaves.Add(new BeatmapSave(0f,false, false)); } songStatusSaves.Add(song.songName, songStatus); } } SaveSongStatuses(); } private void CheckSongStatuses() { foreach (ChapterSelectionUnit chapter in ChapterSelectionManager.instance.chapters) { foreach (SongItemData song in chapter.songs) { if (songStatusSaves.TryGetValue(song.songName, out SongStatusSave songStatus)) { int difficultiesCount = song.difficultyDataList.Count; if (songStatus.beatmapSaves.Count < difficultiesCount) { for (int i = songStatus.beatmapSaves.Count; i < difficultiesCount; i++) { songStatus.beatmapSaves.Add(new BeatmapSave(0f, false, false)); } } else if (songStatus.beatmapSaves.Count > difficultiesCount) { songStatus.beatmapSaves.RemoveRange(difficultiesCount, songStatus.beatmapSaves.Count - difficultiesCount); } } else { songStatus = new SongStatusSave(false, string.Empty, new List()); foreach (DifficultyData difficulty in song.difficultyDataList) { songStatus.beatmapSaves.Add(new BeatmapSave(0f, false, false)); } songStatusSaves.Add(song.songName, songStatus); } } } SaveSongStatuses(); } [Button] public void SaveSongStatuses() { ES3.Save("SongSaves", songStatusSaves, songSavePath); } public void LoadSongStatuses() { if (ES3.FileExists(songSavePath)) { songStatusSaves = ES3.Load>("SongSaves", songSavePath); CheckSongStatuses(); } else { InitializeSongStatuses(); } } [Button] public void ClearBeatmapRecords() { foreach (var songStatus in songStatusSaves.Values) { foreach (var beatmapSave in songStatus.beatmapSaves) { beatmapSave.accuracy = 0.0f; beatmapSave.isFullCombo = false; beatmapSave.isAllPerfect = false; } } SaveSongStatuses(); } public SongStatusSave GetSongStatusSave(string songName) { if (songStatusSaves.TryGetValue(songName, out SongStatusSave save)) { return save; } Debug.LogWarning("Song status save for " + songName + " does not exist."); return null; } } public partial class StorySaveModule { public Dictionary> tutorialBlockSaves; public Dictionary> songBlockSaves; public Dictionary> dialogBlockSaves; public Dictionary> connectorSaves; public Dictionary storyVariables; public Dictionary selectedChoices; private string GetStorySavePath(string chapterName) => Application.persistentDataPath + "/StorySaves/" + chapterName + ".json"; private string StoryVariablesPath => Application.persistentDataPath + "/StorySaves/StoryVariables.json"; private string ChoicesPath => Application.persistentDataPath + "/StorySaves/Choices.json"; public StorySaveModule() { tutorialBlockSaves = new Dictionary>(); songBlockSaves = new Dictionary>(); dialogBlockSaves = new Dictionary>(); connectorSaves = new Dictionary>(); storyVariables = new Dictionary(); selectedChoices = new Dictionary(); //Debug.Log("Story Variables path: " + StoryVariablesPath); } } public partial class StorySaveModule { public bool IsNewStoryline(string chapterName) { return !ES3.FileExists(GetStorySavePath(chapterName)); } public void LoadStoryline(string chapterName) { tutorialBlockSaves[chapterName] = ES3.Load>("TutorialBlockSaves", GetStorySavePath(chapterName)); songBlockSaves[chapterName] = ES3.Load>("SongBlockSaves", GetStorySavePath(chapterName)); dialogBlockSaves[chapterName] = ES3.Load>("TextBlockSaves", GetStorySavePath(chapterName)); connectorSaves[chapterName] = ES3.Load>("BlockConnectorSaves", GetStorySavePath(chapterName)); } public void SaveStoryline(string chapterName, List tutorialBlocks, List songBlocks, List dialogBlocks, List connectors) { ES3.Save("TutorialBlockSaves", tutorialBlocks, GetStorySavePath(chapterName)); ES3.Save("SongBlockSaves", songBlocks, GetStorySavePath(chapterName)); ES3.Save("TextBlockSaves", dialogBlocks, GetStorySavePath(chapterName)); ES3.Save("BlockConnectorSaves", connectors, GetStorySavePath(chapterName)); SaveStoryVariables(); SaveChoices(); } public void ClearAllStoryline() { string path = GetStorySavePath("Chapter0"); if (ES3.FileExists(path)) { ES3.DeleteFile(path); Debug.Log("Cleared all story saves at: " + path); } else { Debug.LogWarning("No story saves found at: " + path); } } } public partial class StorySaveModule { public void SaveStoryVariables() { string path = Application.persistentDataPath + "/StorySaves/" + "StoryVariables.json"; ES3.Save("StoryVariables", storyVariables, path); } public void LoadStoryVariables() { string path = Application.persistentDataPath + "/StorySaves/" + "StoryVariables.json"; if (ES3.FileExists(path)) { storyVariables = ES3.Load>("StoryVariables", path); } else { storyVariables = new Dictionary(); } } public void SaveChoices() { string path = Application.persistentDataPath + "/StorySaves/" + "Choices.json"; ES3.Save("Choices", selectedChoices, path); } public void LoadChoices() { string path = Application.persistentDataPath + "/StorySaves/" + "Choices.json"; if (ES3.FileExists(path)) { selectedChoices = ES3.Load>("Choices", path); } else { selectedChoices = new Dictionary(); } } } }