78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
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);
|
||
}
|
||
}
|
||
}
|
||
}
|