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

84 lines
3.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using AibisDream.Utility;
using Newtonsoft.Json;
using UnityEngine;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 快照与磁盘的边界:序列化和读写 JSON 文件。
/// <para>
/// P2 起,业务存档统一通过 <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>P1 自测落盘:<c>SnapshotTestPath/latest_snapshot.json</c>P2 迁移源)。</summary>
public static string GetTestFilePath()
{
return Path.Combine(ConstRef.SnapshotTestPath, ConstRef.SnapshotTestFileName);
}
/// <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;
}
/// <summary>将快照序列化并写入 path(自动补 .json 后缀)。</summary>
public static void Save(SaveSnapshot snapshot, string pathWithoutExtension)
{
var json = Serialize(snapshot);
var path = JsonUtil.FormatAsJsonPath(pathWithoutExtension);
var dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir))
{
Directory.CreateDirectory(dir);
}
File.WriteAllText(path, json);
}
/// <summary>从 path 读取并反序列化为 <see cref="SaveSnapshot"/>。</summary>
public static SaveSnapshot Load(string pathWithoutExtension)
{
var json = File.ReadAllText(JsonUtil.FormatAsJsonPath(pathWithoutExtension));
return Deserialize(json);
}
}
}