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
{
///
/// 存读档流程编排层:连接 UI、 与 。
///
/// 对外入口:(<>)、(读档)。
/// P4 在此扩展淡入淡出与完整时序;P2 将替换测试路径为槽位 API。
///
///
public static class SaveRestoreOrchestrator
{
/// 捕获快照并写入 P1 测试路径;含保存 Loading UI。
public static void SaveToTestPath()
{
var host = YarnVariableStorage.Instance;
if (host == null)
{
Debug.LogError("[SaveRestoreOrchestrator] YarnVariableStorage 未初始化");
return;
}
ActionKit.Sequence()
.Callback(() => UIManager.Instance.GetPanel().ShowSaveLoading())
.Delay(1f)
.Callback(() => UIManager.Instance.GetPanel().HideSaveLoading())
.Start(host);
using (new CodeTimer("SaveSnapshot"))
{
var snapshot = SnapshotService.Capture();
SnapshotPersistence.Save(snapshot, SnapshotPersistence.GetTestFilePath());
}
}
/// 从文件读档并还原;自动区分新快照格式与 legacy 格式。
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);
}
/// 旧档:Yarn 变量 + DeepRepairDataRegistry systemData。
private static IEnumerator RestoreLegacy(string savePath)
{
var saveData = SnapshotPersistence.ReadLegacySaveRoot(savePath);
var floatDict = saveData["floatDict"]?.ToObject>();
var stringDict = saveData["stringDict"]?.ToObject>();
var boolDict = saveData["boolDict"]?.ToObject>();
YarnVariableStorage.Instance.SetAllVariables(floatDict, stringDict, boolDict);
if (saveData["systemData"] is JObject systemDataJson)
{
yield return DeepRepairDataRegistry.LoadByJson(systemDataJson);
}
}
}
}