GFramework/GFramework.Game/data/DataRepository.cs
GeWuYou 0b7c64fd99 feat(data): 添加数据仓库功能并重构设置系统接口
- 新增 DataRepository 类实现数据存储和读取功能
- 添加数据仓库配置选项类 DataRepositoryOptions
- 定义 IData 接口作为通用数据标记接口
- 实现数据加载、保存、删除等异步操作方法
- 添加数据事件系统包括加载、保存、删除等事件类型
- 将 ISettingsData 接口重命名为 IResettable 并更新相关实现
- 更新 SettingsModel 和 SettingsPersistence 使用新的接口
- 修改 SettingsBatchChangedEvent 和 SettingsBatchSavedEvent 使用 IResettable 类型
- 重构 AudioSettings、GraphicsSettings、LocalizationSettings 继承新接口
- 更新 IPersistentApplyAbleSettings 接口依赖为 IResettable
2026-01-28 20:08:34 +08:00

153 lines
4.9 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 GFramework.Core.Abstractions.storage;
using GFramework.Core.extensions;
using GFramework.Core.utility;
using GFramework.Game.Abstractions.data;
using GFramework.Game.Abstractions.data.events;
namespace GFramework.Game.data;
/// <summary>
/// 数据仓库类,用于管理游戏数据的存储和读取
/// </summary>
/// <param name="storage">存储接口实例</param>
/// <param name="options">数据仓库配置选项</param>
public class DataRepository(IStorage? storage, DataRepositoryOptions? options = null)
: AbstractContextUtility, IDataRepository
{
private readonly DataRepositoryOptions _options = options ?? new DataRepositoryOptions();
private IStorage? _storage = storage;
private IStorage Storage => _storage ??
throw new InvalidOperationException(
"Failed to initialize storage. No IStorage utility found in context.");
/// <summary>
/// 异步加载指定类型的数据
/// </summary>
/// <typeparam name="T">要加载的数据类型必须实现IData接口</typeparam>
/// <returns>加载的数据对象</returns>
public async Task<T> LoadAsync<T>() where T : class, IData, new()
{
var key = GetKey<T>();
T result;
if (await Storage.ExistsAsync(key))
{
result = await Storage.ReadAsync<T>(key);
}
else
{
result = new T();
}
if (_options.EnableEvents)
this.SendEvent(new DataLoadedEvent<T>(result));
return result;
}
/// <summary>
/// 异步保存指定类型的数据
/// </summary>
/// <typeparam name="T">要保存的数据类型</typeparam>
/// <param name="data">要保存的数据对象</param>
public async Task SaveAsync<T>(T data) where T : class, IData
{
var key = GetKey<T>();
// 自动备份
if (_options.AutoBackup && await Storage.ExistsAsync(key))
{
var backupKey = $"{key}.backup";
var existing = await Storage.ReadAsync<T>(key);
await Storage.WriteAsync(backupKey, existing);
}
await Storage.WriteAsync(key, data);
if (_options.EnableEvents)
this.SendEvent(new DataSavedEvent<T>(data));
}
/// <summary>
/// 检查指定类型的数据是否存在
/// </summary>
/// <typeparam name="T">要检查的数据类型</typeparam>
/// <returns>如果数据存在返回true否则返回false</returns>
public async Task<bool> ExistsAsync<T>() where T : class, IData
{
var key = GetKey<T>();
return await Storage.ExistsAsync(key);
}
/// <summary>
/// 异步删除指定类型的数据
/// </summary>
/// <typeparam name="T">要删除的数据类型</typeparam>
public async Task DeleteAsync<T>() where T : class, IData
{
var key = GetKey<T>();
await Storage.DeleteAsync(key);
if (_options.EnableEvents)
this.SendEvent(new DataDeletedEvent(typeof(T)));
}
/// <summary>
/// 批量异步保存多个数据对象
/// </summary>
/// <param name="dataList">要保存的数据对象集合</param>
public async Task SaveAllAsync(IEnumerable<IData> dataList)
{
var list = dataList.ToList();
foreach (var data in list)
{
var type = data.GetType();
var key = GetKey(type);
await Storage.WriteAsync(key, data);
}
if (_options.EnableEvents)
this.SendEvent(new DataBatchSavedEvent(list));
}
protected override void OnInit()
{
_storage ??= this.GetUtility<IStorage>()!;
}
/// <summary>
/// 根据类型生成存储键
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <returns>生成的存储键</returns>
private string GetKey<T>() where T : IData => GetKey(typeof(T));
/// <summary>
/// 根据类型生成存储键
/// </summary>
/// <param name="type">数据类型</param>
/// <returns>生成的存储键</returns>
private string GetKey(Type type)
{
var fileName = $"{_options.KeyPrefix}_{type.Name}";
if (string.IsNullOrEmpty(_options.BasePath))
return fileName;
var basePath = _options.BasePath.TrimEnd('/');
return $"{basePath}/{fileName}";
}
}