68 lines
2.2 KiB
C#
68 lines
2.2 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream.SaveSystem
|
|
{
|
|
/// <summary>
|
|
/// 从运行时组装 <see cref="SaveSnapshot"/>(纯内存,不写盘)。
|
|
/// <para>
|
|
/// 流程:元数据 → 锚点 → Yarn 变量 → 各 Provider.Capture。
|
|
/// 宏观信息(scene/macro)由对应 Provider 写入 sections,不再额外汇总到顶层。
|
|
/// </para>
|
|
/// </summary>
|
|
public static class SnapshotCapture
|
|
{
|
|
/// <summary>捕获当前完整快照。调用前需已注册全部 provider。</summary>
|
|
public static SaveSnapshot Capture(YarnVariableStorage storage)
|
|
{
|
|
SnapshotRegistry.ValidateRequiredProviders();
|
|
|
|
var snapshot = new SaveSnapshot
|
|
{
|
|
gameVersion = Application.version,
|
|
savedAt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
|
|
};
|
|
|
|
CaptureAnchor(snapshot);
|
|
CaptureYarnVariables(storage, snapshot);
|
|
|
|
foreach (var provider in SnapshotRegistry.GetOrderedProviders())
|
|
{
|
|
var dto = provider.Capture();
|
|
if (dto == null) continue;
|
|
snapshot.sections[provider.SaveId] = dto;
|
|
}
|
|
|
|
return snapshot;
|
|
}
|
|
|
|
private static void CaptureAnchor(SaveSnapshot snapshot)
|
|
{
|
|
var dialog = DialogController.Instance;
|
|
if (dialog == null) return;
|
|
|
|
var (nodeName, _) = dialog.GetCurrentNodeContext();
|
|
var yarnProject = dialog.DialogueRunner?.YarnProject;
|
|
var projectId = yarnProject != null ? yarnProject.name : string.Empty;
|
|
|
|
snapshot.anchor = new AnchorSnapshot
|
|
{
|
|
yarnProjectId = projectId,
|
|
nodeName = nodeName ?? string.Empty
|
|
};
|
|
}
|
|
|
|
private static void CaptureYarnVariables(YarnVariableStorage storage, SaveSnapshot snapshot)
|
|
{
|
|
var (floats, strings, bools) = storage.GetAllVariables();
|
|
snapshot.yarnVariables = new YarnVariablesSnapshot
|
|
{
|
|
floats = floats ?? new(),
|
|
strings = strings ?? new(),
|
|
bools = bools ?? new()
|
|
};
|
|
}
|
|
}
|
|
}
|