using System.Collections.Generic;
using GameFramework;
namespace SepCore.Definition
{
///
/// 游戏存档根数据。
/// 对应 Docs/Tech/02_SaveData.md。
///
[System.Serializable]
public sealed class SaveData
{
///
/// 当前存档结构版本。
///
public const int CurrentVersion = 1;
///
/// 存档结构版本。
///
public int version = CurrentVersion;
///
/// 最后写入时间,Unix 毫秒。
///
public long updatedAt;
///
/// 主仓库内容,无容量上限。
///
public List mainWarehouse;
///
/// 拥有的角色,数组顺序即入队顺序。
///
public List characters;
///
/// 当前战备配置。
///
public LoadoutSave loadout;
///
/// 已结束单局的结算记录。
///
public List runHistory;
///
/// 序列化为 JSON 字符串。
///
public string ToJson()
{
return GameFramework.Utility.Json.ToJson(this);
}
///
/// 从 JSON 字符串反序列化;json 为空或反序列化失败时返回 null。
///
public static SaveData FromJson(string json)
{
if (string.IsNullOrEmpty(json))
{
return null;
}
SaveData saveData = GameFramework.Utility.Json.ToObject(json);
if (saveData == null)
{
return null;
}
saveData.Normalize();
return saveData;
}
///
/// 补齐缺失字段,避免反序列化后出现空引用。
///
private void Normalize()
{
if (mainWarehouse == null)
{
mainWarehouse = new List();
}
if (characters == null)
{
characters = new List();
}
if (loadout == null)
{
loadout = new LoadoutSave();
}
loadout.Normalize();
if (runHistory == null)
{
runHistory = new List();
}
}
}
}