229 lines
7.3 KiB
C#
229 lines
7.3 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.LoadVariables();
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
// StorySaveModule 已迁移至 NewStorySystem/Save/StorySaveModule.cs(按章节存树/选项,全局存变量)
|
||
} |