55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.SaveSystem
|
|
{
|
|
/// <summary>
|
|
/// 快照与磁盘的边界:序列化和读写 JSON 文件。
|
|
/// <para>
|
|
/// 业务存档统一通过 <see cref="SlotManager"/> 写入槽位目录;
|
|
/// 本类只负责快照序列化、反序列化与槽位入口。
|
|
/// </para>
|
|
/// </summary>
|
|
public static class SnapshotPersistence
|
|
{
|
|
private static readonly JsonSerializerSettings Settings = new()
|
|
{
|
|
TypeNameHandling = TypeNameHandling.None,
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
NullValueHandling = NullValueHandling.Ignore
|
|
};
|
|
|
|
/// <summary>将快照序列化并写入指定槽位。</summary>
|
|
public static void SaveToSlot(int slotIndex, SaveSnapshot snapshot)
|
|
{
|
|
SlotManager.SaveToSlot(slotIndex, snapshot, null);
|
|
}
|
|
|
|
/// <summary>从指定槽位读取快照。</summary>
|
|
public static SaveSnapshot LoadFromSlot(int slotIndex)
|
|
{
|
|
return SlotManager.LoadSnapshot(slotIndex);
|
|
}
|
|
|
|
/// <summary>将快照序列化为 JSON 字符串(紧凑格式)。</summary>
|
|
public static string Serialize(SaveSnapshot snapshot)
|
|
{
|
|
return JsonConvert.SerializeObject(snapshot, Formatting.None, Settings);
|
|
}
|
|
|
|
/// <summary>从 JSON 字符串反序列化为快照。</summary>
|
|
public static SaveSnapshot Deserialize(string json)
|
|
{
|
|
var snapshot = JsonConvert.DeserializeObject<SaveSnapshot>(json, Settings);
|
|
if (snapshot != null && snapshot.schemaVersion != SaveSnapshotSchema.CurrentVersion)
|
|
{
|
|
Debug.LogWarning(
|
|
$"[SnapshotPersistence] schemaVersion {snapshot.schemaVersion} != {SaveSnapshotSchema.CurrentVersion},按当前结构读取。");
|
|
}
|
|
|
|
return snapshot;
|
|
}
|
|
|
|
}
|
|
}
|