using System; using System.Collections.Generic; using UnityEngine; [Serializable] public class SongDataSerializable { // --- 动态变动数据 (需要存储) --- public int game_enterTimes; public float time_totalPlayingTime; public float current_levelProgress; public bool _if_level_is_EASE; public int max_comboRecord; public int personalRecord; public int selectedDifficultyId; public string dataHash; // 防篡改校验和 // public List enemyList = new List(); // Deprecated and removed // --- 字典映射表 (JsonUtility 不支持 Dictionary,需转为 List 存储) --- [System.Serializable] public struct IntIntEntry { public int key; public int value; } public List difficultyNumberList = new List(); public List personalRecordList = new List(); public List idolRecordList = new List(); public List chartScoreList = new List(); [System.Serializable] public struct IntFloatEntry { public int key; public float value; } public List levelProgressList = new List(); public SongDataSerializable() { } public SongDataSerializable(SongData source) { if (source == null) return; // 提取动态数据 this.game_enterTimes = source.game_enterTimes; this.time_totalPlayingTime = source.time_totalPlayingTime; this.current_levelProgress = source.current_levelProgress; this._if_level_is_EASE = source._if_level_is_EASE; this.max_comboRecord = source.max_comboRecord; this.personalRecord = source.personalRecord; this.selectedDifficultyId = source.thisLevel_selectedDifficultyID; // this.enemyList = new List(source.enemyList); // Deprecated and removed // 将字典转换为 List this.difficultyNumberList = DictToList(source.difficultyNumberMap); this.personalRecordList = DictToList(source.personalRecordMap); this.idolRecordList = DictToList(source.idolRecordMap); this.chartScoreList = DictToList(source.chartScoreMap); this.levelProgressList = DictToListFloat(source.levelProgressMap); } /// /// 将序列化的动态数据应用回 SongData SO。 /// public void ApplyTo(SongData target) { if (target == null) return; target.game_enterTimes = this.game_enterTimes; target.time_totalPlayingTime = this.time_totalPlayingTime; target.current_levelProgress = this.current_levelProgress; target._if_level_is_EASE = this._if_level_is_EASE; target.max_comboRecord = this.max_comboRecord; target.personalRecord = this.personalRecord; target.thisLevel_selectedDifficultyID = this.selectedDifficultyId; // target.enemyList = new List(this.enemyList); // Deprecated and removed // 将 List 还原回字典 ListToDict(this.difficultyNumberList, target.difficultyNumberMap); ListToDict(this.personalRecordList, target.personalRecordMap); ListToDict(this.idolRecordList, target.idolRecordMap); ListToDict(this.chartScoreList, target.chartScoreMap); ListToDictFloat(this.levelProgressList, target.levelProgressMap); } private List DictToList(Dictionary dict) { List list = new List(); if (dict != null) { foreach (var kvp in dict) { list.Add(new IntIntEntry { key = kvp.Key, value = kvp.Value }); } } return list; } private void ListToDict(List list, Dictionary dict) { if (list == null || dict == null) return; dict.Clear(); foreach (var entry in list) { dict[entry.key] = entry.value; } } private List DictToListFloat(Dictionary dict) { List list = new List(); if (dict != null) { foreach (var kvp in dict) { list.Add(new IntFloatEntry { key = kvp.Key, value = kvp.Value }); } } return list; } private void ListToDictFloat(List list, Dictionary dict) { if (list == null || dict == null) return; dict.Clear(); foreach (var entry in list) { dict[entry.key] = entry.value; } } }