86 lines
3.1 KiB
C#
86 lines
3.1 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.SaveSystem
|
|
{
|
|
/// <summary>
|
|
/// 将 <see cref="SaveSnapshot"/> 逐项写回运行时(纯内存,不读盘)。
|
|
/// <para>
|
|
/// 顺序:Yarn 变量 → 各 Provider(按 RestoreOrder)→(可选)锚点重进节点。
|
|
/// 完整淡入淡出时序由 P4 在 <see cref="SaveRestoreOrchestrator"/> 扩展。
|
|
/// </para>
|
|
/// </summary>
|
|
public static class SnapshotRestore
|
|
{
|
|
/// <summary>还原 Yarn 变量与各 section;不包含锚点重进(由 <see cref="RestoreAnchor"/> 或上层编排)。</summary>
|
|
public static IEnumerator Restore(YarnVariableStorage storage, SaveSnapshot snapshot)
|
|
{
|
|
if (snapshot == null)
|
|
{
|
|
Debug.LogError("[SnapshotRestore] snapshot 为 null");
|
|
yield break;
|
|
}
|
|
|
|
RestoreYarnVariables(storage, snapshot);
|
|
|
|
foreach (var provider in SnapshotRegistry.GetOrderedProviders())
|
|
{
|
|
if (!snapshot.sections.TryGetValue(provider.SaveId, out var dto) || dto == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
dto = SnapshotSectionDeserializer.Coerce(provider.SaveId, dto);
|
|
yield return provider.Restore(dto);
|
|
}
|
|
|
|
if (snapshot.deepRepair != null && snapshot.deepRepair.sections != null
|
|
&& snapshot.deepRepair.sections.Count > 0)
|
|
{
|
|
Debug.LogWarning("[SnapshotRestore] DeepRepair 段尚未实现,已跳过(P5)。");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按 <see cref="AnchorSnapshot"/> 重新进入 Yarn 节点,实现「所见即所存」。
|
|
/// 调用前应已完成场景加载与各 provider 还原。
|
|
/// </summary>
|
|
public static IEnumerator RestoreAnchor(SaveSnapshot snapshot)
|
|
{
|
|
if (snapshot?.anchor == null || string.IsNullOrEmpty(snapshot.anchor.nodeName))
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
var dialog = DialogController.Instance;
|
|
if (dialog == null) yield break;
|
|
|
|
var runner = dialog.DialogueRunner;
|
|
if (runner == null) yield break;
|
|
|
|
if (!string.IsNullOrEmpty(snapshot.anchor.yarnProjectId)
|
|
&& runner.YarnProject != null
|
|
&& runner.YarnProject.name != snapshot.anchor.yarnProjectId)
|
|
{
|
|
Debug.LogWarning(
|
|
$"[SnapshotRestore] YarnProject 不匹配:当前 {runner.YarnProject.name},存档 {snapshot.anchor.yarnProjectId}");
|
|
}
|
|
|
|
if (runner.IsDialogueRunning)
|
|
{
|
|
yield return runner.Stop();
|
|
}
|
|
|
|
yield return runner.StartDialogue(snapshot.anchor.nodeName);
|
|
}
|
|
|
|
private static void RestoreYarnVariables(YarnVariableStorage storage, SaveSnapshot snapshot)
|
|
{
|
|
var vars = snapshot.yarnVariables;
|
|
if (vars == null) return;
|
|
|
|
storage.SetAllVariables(vars.floats, vars.strings, vars.bools);
|
|
}
|
|
}
|
|
}
|