Files
bansonic_beta_main/Assets/songsDatabase/SongDataSerializable.cs
T

98 lines
3.4 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 string dataHash; // 防篡改校验和
public List<int> enemyList = new List<int>();
// --- 字典映射表 (JsonUtility 不支持 Dictionary,需转为 List 存储) ---
[System.Serializable]
public struct IntIntEntry
{
public int key;
public int value;
}
public List<IntIntEntry> difficultyNumberList = new List<IntIntEntry>();
public List<IntIntEntry> personalRecordList = new List<IntIntEntry>();
public List<IntIntEntry> idolRecordList = new List<IntIntEntry>();
public List<IntIntEntry> chartScoreList = new List<IntIntEntry>();
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.enemyList = new List<int>(source.enemyList);
// 将字典转换为 List
this.difficultyNumberList = DictToList(source.difficultyNumberMap);
this.personalRecordList = DictToList(source.personalRecordMap);
this.idolRecordList = DictToList(source.idolRecordMap);
this.chartScoreList = DictToList(source.chartScoreMap);
}
/// <summary>
/// 将序列化的动态数据应用回 SongData SO。
/// </summary>
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.enemyList = new List<int>(this.enemyList);
// 将 List 还原回字典
ListToDict(this.difficultyNumberList, target.difficultyNumberMap);
ListToDict(this.personalRecordList, target.personalRecordMap);
ListToDict(this.idolRecordList, target.idolRecordMap);
ListToDict(this.chartScoreList, target.chartScoreMap);
}
private List<IntIntEntry> DictToList(Dictionary<int, int> dict)
{
List<IntIntEntry> list = new List<IntIntEntry>();
if (dict != null)
{
foreach (var kvp in dict)
{
list.Add(new IntIntEntry { key = kvp.Key, value = kvp.Value });
}
}
return list;
}
private void ListToDict(List<IntIntEntry> list, Dictionary<int, int> dict)
{
if (list == null || dict == null) return;
dict.Clear();
foreach (var entry in list)
{
dict[entry.key] = entry.value;
}
}
}