Files
aibis-dream/Assets/Scripts/SaveSystem/SnapshotRegistry.cs
T

55 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AibisDream.SaveSystem
{
/// <summary>
/// 运行时登记全部 <see cref="ISnapshotProvider"/>。
/// Capture/Restore 时按 <see cref="ISnapshotProvider.RestoreOrder"/> 迭代;
/// 不使用「谁注册了谁才存」的隐式行为,RequiredForCapture 用于显式校验。
/// </summary>
public static class SnapshotRegistry
{
private static readonly Dictionary<string, ISnapshotProvider> Providers = new();
/// <summary>注册 provider;同 SaveId 后者覆盖前者。</summary>
public static void Register(ISnapshotProvider provider)
{
if (provider == null) return;
Providers[provider.SaveId] = provider;
}
public static void Unregister(string saveId)
{
Providers.Remove(saveId);
}
public static bool TryGet(string saveId, out ISnapshotProvider provider)
{
return Providers.TryGetValue(saveId, out provider);
}
/// <summary>按 RestoreOrder 升序返回,供还原流水线使用。</summary>
public static IReadOnlyList<ISnapshotProvider> GetOrderedProviders()
{
return Providers.Values.OrderBy(p => p.RestoreOrder).ToList();
}
/// <summary>
/// 校验 <see cref="SnapshotProviderIds.RequiredForCapture"/>
/// 缺失时 <c>LogWarning</c> 但不阻断 Capture。
/// </summary>
public static void ValidateRequiredProviders()
{
foreach (var saveId in SnapshotProviderIds.RequiredForCapture)
{
if (!Providers.ContainsKey(saveId))
{
Debug.LogWarning($"[SnapshotRegistry] 缺少快照 provider: {saveId}");
}
}
}
}
}