实现 P2 槽位/落盘层核心: - SlotTypes:槽位常量、sidecar 元数据、UI 视图模型 - SlotFileSystem:槽位路径工具与原子写 - SlotManager:自动档、手动档、读档、继续游戏、P1 迁移 - SlotThumbnailCapture:存档截图 helper - CloudSave:Steam/TapTap 云存档适配器接口与协调器 在 ConstRef 中补充槽位、截图、文件名常量。
103 lines
3.2 KiB
C#
103 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace AibisDream.SaveSystem.CloudSave
|
|
{
|
|
/// <summary>
|
|
/// 云存档协调器:负责选择当前平台的 backend,并在槽位写入/读取后触发同步钩子。
|
|
/// <para>
|
|
/// P2 不实现真实云同步逻辑,仅保留扩展点。
|
|
/// </para>
|
|
/// </summary>
|
|
public static class CloudSaveManager
|
|
{
|
|
private static readonly List<ICloudSaveBackend> Backends = new();
|
|
|
|
/// <summary>注册一个云存档后端。应在游戏启动时调用一次。</summary>
|
|
public static void RegisterBackend(ICloudSaveBackend backend)
|
|
{
|
|
if (backend == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Backends.Add(backend);
|
|
}
|
|
|
|
/// <summary>清空已注册后端(主要用于测试)。</summary>
|
|
public static void ClearBackends()
|
|
{
|
|
Backends.Clear();
|
|
}
|
|
|
|
/// <summary>槽位写入后调用:触发所有可用后端的异步上传。</summary>
|
|
public static void OnSlotWritten(int slotIndex)
|
|
{
|
|
_ = UploadSlotAsync(slotIndex);
|
|
}
|
|
|
|
/// <summary>槽位读取前调用:可触发下载(P2 默认不自动下载,避免覆盖本地进度)。</summary>
|
|
public static void OnSlotRestored(int slotIndex)
|
|
{
|
|
// P2 占位:未来可在此触发冲突检测与下载。
|
|
}
|
|
|
|
/// <summary>手动触发指定槽位的云同步上传。</summary>
|
|
public static async Task UploadSlotAsync(int slotIndex)
|
|
{
|
|
var filePaths = CollectSlotFilePaths(slotIndex);
|
|
if (filePaths.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var backend in Backends)
|
|
{
|
|
if (!backend.IsAvailable)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
try
|
|
{
|
|
await backend.UploadSlotAsync(slotIndex, filePaths.ToArray());
|
|
Debug.Log($"[CloudSaveManager] {backend.PlatformName} 上传槽位 {slotIndex} 完成。");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Debug.LogWarning($"[CloudSaveManager] {backend.PlatformName} 上传槽位 {slotIndex} 失败: {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>收集槽位下需要同步的文件路径。</summary>
|
|
private static List<string> CollectSlotFilePaths(int slotIndex)
|
|
{
|
|
var paths = new List<string>();
|
|
|
|
var snapshotPath = SlotDirectory.GetSnapshotPath(slotIndex);
|
|
if (File.Exists(snapshotPath))
|
|
{
|
|
paths.Add(snapshotPath);
|
|
}
|
|
|
|
var metaPath = SlotDirectory.GetMetaPath(slotIndex);
|
|
if (File.Exists(metaPath))
|
|
{
|
|
paths.Add(metaPath);
|
|
}
|
|
|
|
var thumbnailPath = SlotDirectory.GetThumbnailPath(slotIndex);
|
|
if (File.Exists(thumbnailPath))
|
|
{
|
|
paths.Add(thumbnailPath);
|
|
}
|
|
|
|
return paths;
|
|
}
|
|
}
|
|
}
|