GFramework/GFramework.Game/Data/SaveRepository.cs
GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

146 lines
4.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.

// Copyright (c) 2026 GeWuYou
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System.Globalization;
using GFramework.Core.Abstractions.Storage;
using GFramework.Core.Utility;
using GFramework.Game.Abstractions.Data;
using GFramework.Game.Storage;
namespace GFramework.Game.Data;
/// <summary>
/// 基于槽位的存档仓库实现
/// </summary>
/// <typeparam name="TSaveData">存档数据类型</typeparam>
public class SaveRepository<TSaveData> : AbstractContextUtility, ISaveRepository<TSaveData>
where TSaveData : class, IData, new()
{
private readonly SaveConfiguration _config;
private readonly IStorage _rootStorage;
/// <summary>
/// 初始化存档仓库
/// </summary>
/// <param name="storage">存储实例</param>
/// <param name="config">存档配置</param>
public SaveRepository(IStorage storage, SaveConfiguration config)
{
ArgumentNullException.ThrowIfNull(storage);
ArgumentNullException.ThrowIfNull(config);
_config = config;
_rootStorage = new ScopedStorage(storage, config.SaveRoot);
}
/// <summary>
/// 检查指定槽位是否存在存档
/// </summary>
/// <param name="slot">存档槽位编号</param>
/// <returns>如果存档存在返回true否则返回false</returns>
public async Task<bool> ExistsAsync(int slot)
{
var storage = GetSlotStorage(slot);
return await storage.ExistsAsync(_config.SaveFileName);
}
/// <summary>
/// 加载指定槽位的存档
/// </summary>
/// <param name="slot">存档槽位编号</param>
/// <returns>存档数据对象,如果不存在则返回新实例</returns>
public async Task<TSaveData> LoadAsync(int slot)
{
var storage = GetSlotStorage(slot);
if (await storage.ExistsAsync(_config.SaveFileName))
return await storage.ReadAsync<TSaveData>(_config.SaveFileName);
return new TSaveData();
}
/// <summary>
/// 保存存档到指定槽位
/// </summary>
/// <param name="slot">存档槽位编号</param>
/// <param name="data">要保存的存档数据</param>
public async Task SaveAsync(int slot, TSaveData data)
{
var slotPath = $"{_config.SaveSlotPrefix}{slot}";
// 确保槽位目录存在
if (!await _rootStorage.DirectoryExistsAsync(slotPath))
await _rootStorage.CreateDirectoryAsync(slotPath);
var storage = GetSlotStorage(slot);
await storage.WriteAsync(_config.SaveFileName, data);
}
/// <summary>
/// 删除指定槽位的存档
/// </summary>
/// <param name="slot">存档槽位编号</param>
public async Task DeleteAsync(int slot)
{
var storage = GetSlotStorage(slot);
await storage.DeleteAsync(_config.SaveFileName);
}
/// <summary>
/// 列出所有有效的存档槽位
/// </summary>
/// <returns>包含所有有效存档槽位编号的只读列表,按升序排列</returns>
public async Task<IReadOnlyList<int>> ListSlotsAsync()
{
// 列举所有槽位目录
var directories = await _rootStorage.ListDirectoriesAsync();
var slots = new List<int>();
foreach (var dirName in directories)
{
// 检查目录名是否符合槽位前缀
if (!dirName.StartsWith(_config.SaveSlotPrefix, StringComparison.Ordinal))
continue;
// 提取槽位编号
var slotStr = dirName[_config.SaveSlotPrefix.Length..];
if (!int.TryParse(slotStr, CultureInfo.InvariantCulture, out var slot))
continue;
// 直接检查存档文件是否存在,避免重复创建 ScopedStorage
var saveFilePath = $"{dirName}/{_config.SaveFileName}";
if (await _rootStorage.ExistsAsync(saveFilePath))
slots.Add(slot);
}
return slots.OrderBy(x => x).ToList();
}
/// <summary>
/// 获取指定槽位的存储对象
/// </summary>
/// <param name="slot">存档槽位编号</param>
/// <returns>对应槽位的存储对象</returns>
private IStorage GetSlotStorage(int slot)
{
return new ScopedStorage(_rootStorage, $"{_config.SaveSlotPrefix}{slot}");
}
/// <summary>
/// 初始化逻辑
/// </summary>
protected override void OnInit()
{
}
}