修了很多bug和增加功能,优化不少问题

This commit is contained in:
2026-02-28 06:59:22 +08:00
parent 508a40bba0
commit e11cc1da7a
345 changed files with 173803 additions and 69332 deletions
+74 -24
View File
@@ -116,9 +116,7 @@ public class SongData : ScriptableObject
[TextArea(3, 10)]
public string thisLevel_overallDescription;
// [Header("Enemy Configuration")]
// [Tooltip("DEPRECATED: Use enemyList in ChartFileEntry instead. List of enemy IDs for this song (0-5 enemies)")]
// public List<int> enemyList = new List<int>();
private bool _isPersistentDataLoaded = false;
private void OnValidate()
{
@@ -291,6 +289,9 @@ public class SongData : ScriptableObject
if (chartFileMap == null) chartFileMap = new Dictionary<int, TextAsset>();
if (chartFiles == null) chartFiles = new List<ChartFileEntry>();
// 尝试从持久化存储加载数据
LoadPersistent();
SyncRecordsFromChartFiles();
SyncChartFileMap();
}
@@ -320,8 +321,16 @@ public class SongData : ScriptableObject
/// </summary>
public void SavePersistent()
{
if (songID == 0)
{
Debug.LogWarning($"[SongData] {songName} has songID=0. Saving to 'SongData_0.json' may overwrite other songs!");
}
try
{
// 在保存前确保 chartFiles 已同步,这样在编辑器中查看 SO 时数据也是最新的
SyncEntriesFromMaps();
string path = System.IO.Path.Combine(Application.persistentDataPath, $"SongData_{songID}.json");
SongDataSerializable serializable = new SongDataSerializable(this);
@@ -333,8 +342,15 @@ public class SongData : ScriptableObject
// 3. 再次序列化包含 Hash 的最终数据
string finalJson = JsonUtility.ToJson(serializable, true);
// 确保目录存在
string dir = System.IO.Path.GetDirectoryName(path);
if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
System.IO.File.WriteAllText(path, finalJson);
Debug.Log($"[SongData] {songName} (ID:{songID}) saved to {path}");
#if UNITY_EDITOR
if (!Application.isPlaying)
{
@@ -342,7 +358,6 @@ public class SongData : ScriptableObject
UnityEditor.AssetDatabase.SaveAssets();
}
#endif
Debug.Log($"[SongData] {songName} saved with hash validation.");
}
catch (System.Exception e)
{
@@ -356,6 +371,8 @@ public class SongData : ScriptableObject
/// </summary>
public void LoadPersistent()
{
if (_isPersistentDataLoaded) return;
string path = System.IO.Path.Combine(Application.persistentDataPath, $"SongData_{songID}.json");
if (System.IO.File.Exists(path))
{
@@ -368,36 +385,35 @@ public class SongData : ScriptableObject
{
serializable.ApplyTo(this);
SyncEntriesFromMaps();
// Debug.Log($"[SongData] {songName} data restored.");
_isPersistentDataLoaded = true;
Debug.Log($"[SongData] {songName} (ID:{songID}) data restored from persistent storage.");
}
}
catch (System.Exception e)
{
Debug.LogWarning($"[SongData] {songName} 基础加载跳过: {e.Message}");
Debug.LogWarning($"[SongData] {songName} 基础加载失败: {e.Message}");
}
}
else
{
// 如果不存在存档文件,也标记为已加载,防止重复检查
_isPersistentDataLoaded = true;
}
}
public void ClearSaveDataAndReload()
{
game_enterTimes = 0;
time_totalPlayingTime = 0f;
current_levelProgress = 0f;
_if_level_is_EASE = false;
max_comboRecord = 0;
personalRecord = 0;
if (personalRecordMap != null) personalRecordMap.Clear();
else personalRecordMap = new Dictionary<int, int>();
if (personalRecordMap == null) personalRecordMap = new Dictionary<int, int>();
else personalRecordMap.Clear();
if (idolRecordMap != null) idolRecordMap.Clear();
else idolRecordMap = new Dictionary<int, int>();
if (idolRecordMap == null) idolRecordMap = new Dictionary<int, int>();
else idolRecordMap.Clear();
if (chartScoreMap != null) chartScoreMap.Clear();
else chartScoreMap = new Dictionary<int, int>();
if (chartScoreMap == null) chartScoreMap = new Dictionary<int, int>();
else chartScoreMap.Clear();
if (levelProgressMap == null) levelProgressMap = new Dictionary<int, float>();
else levelProgressMap.Clear();
if (levelProgressMap != null) levelProgressMap.Clear();
else levelProgressMap = new Dictionary<int, float>();
if (chartFiles != null)
{
@@ -410,12 +426,47 @@ public class SongData : ScriptableObject
entry.idolPersonalRecordForThisDifficulty = 0;
entry.totalPersonalRecordForThisDifficulty = 0;
entry.levelProgressForThisDifficulty = 0f;
int diff = entry.difficulty;
personalRecordMap[diff] = 0;
idolRecordMap[diff] = 0;
chartScoreMap[diff] = 0;
levelProgressMap[diff] = 0f;
}
}
if (difficultyNumberMap != null && difficultyNumberMap.Count > 0)
{
foreach (var kvp in difficultyNumberMap)
{
int diff = kvp.Key;
if (!personalRecordMap.ContainsKey(diff)) personalRecordMap[diff] = 0;
if (!idolRecordMap.ContainsKey(diff)) idolRecordMap[diff] = 0;
if (!chartScoreMap.ContainsKey(diff)) chartScoreMap[diff] = 0;
if (!levelProgressMap.ContainsKey(diff)) levelProgressMap[diff] = 0f;
}
}
game_enterTimes = 0;
time_totalPlayingTime = 0f;
current_levelProgress = 0f;
_if_level_is_EASE = false;
max_comboRecord = 0;
personalRecord = 0;
UpdateOverallRecordsFromEntries();
string path = System.IO.Path.Combine(Application.persistentDataPath, $"SongData_{songID}.json");
if (System.IO.File.Exists(path))
{
try { System.IO.File.Delete(path); } catch { }
}
SavePersistent();
LoadPersistent();
#if UNITY_EDITOR
UnityEditor.EditorUtility.SetDirty(this);
#endif
}
/// <summary>
@@ -564,8 +615,7 @@ public class SongData : ScriptableObject
chartScoreMap[difficulty] = total;
UpdateOverallRecordsFromEntries();
ApplySerializableRoundTrip();
SyncEntriesFromMaps();
SavePersistent();
}