Files
aibis-dream/Assets/Scripts/SaveSystem/SnapshotPersistence.cs
T

52 lines
2.0 KiB
C#

using System.IO;
using AibisDream.Utility;
using Newtonsoft.Json.Linq;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 快照与磁盘的边界:读写 JSON 文件、检测旧档格式。
/// <para>
/// P1 使用固定测试路径(<see cref="GetTestFilePath"/>);
/// P2 将扩展为槽位目录、原子写、sidecar,接口可保持不变。
/// </para>
/// </summary>
public static class SnapshotPersistence
{
/// <summary>P1 自测落盘:<c>SnapshotTestPath/latest_snapshot.json</c>。</summary>
public static string GetTestFilePath()
{
return Path.Combine(ConstRef.SnapshotTestPath, ConstRef.SnapshotTestFileName);
}
/// <summary>将快照序列化并写入 path(自动补 .json 后缀)。</summary>
public static void Save(SaveSnapshot snapshot, string pathWithoutExtension)
{
SnapshotSerializer.SaveToFile(snapshot, pathWithoutExtension);
}
/// <summary>从 path 读取并反序列化为 <see cref="SaveSnapshot"/>。</summary>
public static SaveSnapshot Load(string pathWithoutExtension)
{
return SnapshotSerializer.LoadFromFile(pathWithoutExtension);
}
/// <summary>
/// 是否为旧 StorageSystem 格式(含 systemData、无 schemaVersion)。
/// 此类文件应走 <see cref="SaveRestoreOrchestrator"/> 的 legacy 分支。
/// </summary>
public static bool IsLegacyFormat(string savePath)
{
var json = File.ReadAllText(JsonUtil.FormatAsJsonPath(savePath));
var jobj = JObject.Parse(json);
return jobj.Value<int?>("schemaVersion") == null && jobj["systemData"] != null;
}
/// <summary>读取旧格式存档根 JObject,供 legacy 还原使用。</summary>
public static JObject ReadLegacySaveRoot(string savePath)
{
return JsonUtil.ReadJObject(savePath);
}
}
}