feat(save): 添加快照存档系统核心框架

This commit is contained in:
2026-06-03 19:56:45 +08:00
parent c845af68b9
commit 33a028f071
49 changed files with 1619 additions and 0 deletions
@@ -0,0 +1,77 @@
using System.Collections;
using System.Collections.Generic;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.UI;
using Newtonsoft.Json.Linq;
using UnityEngine;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 存读档流程编排层:连接 UI、<see cref="SnapshotService"/> 与 <see cref="SnapshotPersistence"/>。
/// <para>
/// 对外入口:<see cref="SaveToTestPath"/><<auto_save>>)、<see cref="RestoreFromFile"/>(读档)。
/// P4 在此扩展淡入淡出与完整时序;P2 将替换测试路径为槽位 API。
/// </para>
/// </summary>
public static class SaveRestoreOrchestrator
{
/// <summary>捕获快照并写入 P1 测试路径;含保存 Loading UI。</summary>
public static void SaveToTestPath()
{
var host = YarnVariableStorage.Instance;
if (host == null)
{
Debug.LogError("[SaveRestoreOrchestrator] YarnVariableStorage 未初始化");
return;
}
ActionKit.Sequence()
.Callback(() => UIManager.Instance.GetPanel<InfoPanel>().ShowSaveLoading())
.Delay(1f)
.Callback(() => UIManager.Instance.GetPanel<InfoPanel>().HideSaveLoading())
.Start(host);
using (new CodeTimer("SaveSnapshot"))
{
var snapshot = SnapshotService.Capture();
SnapshotPersistence.Save(snapshot, SnapshotPersistence.GetTestFilePath());
}
}
/// <summary>从文件读档并还原;自动区分新快照格式与 legacy 格式。</summary>
public static IEnumerator RestoreFromFile(string savePath)
{
if (SnapshotPersistence.IsLegacyFormat(savePath))
{
Debug.LogWarning("[SaveRestoreOrchestrator] 检测到旧格式存档,请使用新快照格式重新保存。");
yield return RestoreLegacy(savePath);
yield break;
}
SaveSnapshot snapshot;
using (new CodeTimer("LoadSnapshot"))
{
snapshot = SnapshotPersistence.Load(savePath);
}
yield return SnapshotService.Restore(snapshot);
}
/// <summary>旧档:Yarn 变量 + DeepRepairDataRegistry systemData。</summary>
private static IEnumerator RestoreLegacy(string savePath)
{
var saveData = SnapshotPersistence.ReadLegacySaveRoot(savePath);
var floatDict = saveData["floatDict"]?.ToObject<Dictionary<string, float>>();
var stringDict = saveData["stringDict"]?.ToObject<Dictionary<string, string>>();
var boolDict = saveData["boolDict"]?.ToObject<Dictionary<string, bool>>();
YarnVariableStorage.Instance.SetAllVariables(floatDict, stringDict, boolDict);
if (saveData["systemData"] is JObject systemDataJson)
{
yield return DeepRepairDataRegistry.LoadByJson(systemDataJson);
}
}
}
}