Files
ichni_Official/Assets/Scripts/Saving/GameSaveManager.cs
SoulliesOfficial 6aa498f6be
2025-08-11 14:04:06 -04:00

338 lines
12 KiB
C#

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<string> storyUnlockKeys;
public HashSet<string> paymentUnlockKeys;
public Dictionary<string, SongStatusSave> 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<string, SongStatusSave>();
storyUnlockKeys = new HashSet<string>();
paymentUnlockKeys = new HashSet<string>();
//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<HashSet<string>>("UnlockKeys", storyUnlockKeysPath);
}
else
{
storyUnlockKeys = new HashSet<string>();
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<HashSet<string>>("UnlockKeys", paymentUnlockKeysPath);
}
else
{
paymentUnlockKeys = new HashSet<string>();
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<BeatmapSave>());
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<BeatmapSave>());
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<Dictionary<string, SongStatusSave>>("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<string, List<TutorialBlockSave>> tutorialBlockSaves;
public Dictionary<string, List<SongBlockSave>> songBlockSaves;
public Dictionary<string, List<DialogBlockSave>> dialogBlockSaves;
public Dictionary<string, List<BlockConnectorSave>> connectorSaves;
public Dictionary<string, int> storyVariables;
public Dictionary<string, int> 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<string, List<TutorialBlockSave>>();
songBlockSaves = new Dictionary<string, List<SongBlockSave>>();
dialogBlockSaves = new Dictionary<string, List<DialogBlockSave>>();
connectorSaves = new Dictionary<string, List<BlockConnectorSave>>();
storyVariables = new Dictionary<string, int>();
selectedChoices = new Dictionary<string, int>();
//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<List<TutorialBlockSave>>("TutorialBlockSaves", GetStorySavePath(chapterName));
songBlockSaves[chapterName] = ES3.Load<List<SongBlockSave>>("SongBlockSaves", GetStorySavePath(chapterName));
dialogBlockSaves[chapterName] = ES3.Load<List<DialogBlockSave>>("TextBlockSaves", GetStorySavePath(chapterName));
connectorSaves[chapterName] = ES3.Load<List<BlockConnectorSave>>("BlockConnectorSaves", GetStorySavePath(chapterName));
}
public void SaveStoryline(string chapterName, List<TutorialBlockSave> tutorialBlocks,
List<SongBlockSave> songBlocks, List<DialogBlockSave> dialogBlocks,
List<BlockConnectorSave> 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<Dictionary<string, int>>("StoryVariables", path);
}
else
{
storyVariables = new Dictionary<string, int>();
}
}
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<Dictionary<string, int>>("Choices", path);
}
else
{
selectedChoices = new Dictionary<string, int>();
}
}
}
}