diff --git a/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs b/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs new file mode 100644 index 0000000..2086fbd --- /dev/null +++ b/GFramework.Game.Abstractions/setting/ISettingsPersistence.cs @@ -0,0 +1,39 @@ +using System.Threading.Tasks; + +namespace GFramework.Game.Abstractions.setting; + +/// +/// 设置持久化接口 +/// 定义了设置数据的异步加载、保存、检查存在性和删除操作 +/// +public interface ISettingsPersistence +{ + /// + /// 异步加载指定类型的设置节 + /// + /// 设置节类型,必须实现ISettingsSection接口并具有无参构造函数 + /// 返回加载的设置节实例 + Task LoadAsync() where T : class, ISettingsSection, new(); + + /// + /// 异步保存指定的设置节 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 要保存的设置节实例 + /// 异步操作任务 + Task SaveAsync(T section) where T : class, ISettingsSection; + + /// + /// 异步检查指定类型的设置节是否存在 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 如果设置节存在则返回true,否则返回false + Task ExistsAsync() where T : class, ISettingsSection; + + /// + /// 异步删除指定类型的设置节 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 异步操作任务 + Task DeleteAsync() where T : class, ISettingsSection; +} \ No newline at end of file diff --git a/GFramework.Game/setting/SettingsPersistence.cs b/GFramework.Game/setting/SettingsPersistence.cs new file mode 100644 index 0000000..2e67f2a --- /dev/null +++ b/GFramework.Game/setting/SettingsPersistence.cs @@ -0,0 +1,80 @@ +using GFramework.Core.Abstractions.storage; +using GFramework.Core.extensions; +using GFramework.Core.utility; +using GFramework.Game.Abstractions.setting; + +namespace GFramework.Game.setting; + +/// +/// 设置持久化服务类,负责处理设置数据的加载、保存、删除等操作 +/// +public class SettingsPersistence : AbstractContextUtility, ISettingsPersistence +{ + private IStorage _storage = null!; + + /// + /// 异步加载指定类型的设置节数据 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 如果存在则返回已保存的设置数据,否则返回新创建的默认设置实例 + public async Task LoadAsync() where T : class, ISettingsSection, new() + { + var key = GetKey(); + + if (await _storage.ExistsAsync(key)) + { + return await _storage.ReadAsync(key); + } + + return new T(); + } + + /// + /// 异步保存设置节数据到存储中 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 要保存的设置节实例 + public async Task SaveAsync(T section) where T : class, ISettingsSection + { + var key = GetKey(); + await _storage.WriteAsync(key, section); + } + + /// + /// 异步检查指定类型的设置节是否存在 + /// + /// 设置节类型,必须实现ISettingsSection接口 + /// 如果设置节存在返回true,否则返回false + public async Task ExistsAsync() where T : class, ISettingsSection + { + var key = GetKey(); + return await _storage.ExistsAsync(key); + } + + /// + /// 异步删除指定类型的设置节数据 + /// + /// 设置节类型,必须实现ISettingsSection接口 + public async Task DeleteAsync() where T : class, ISettingsSection + { + var key = GetKey(); + _storage.Delete(key); + await Task.CompletedTask; + } + + /// + /// 初始化方法,获取存储服务实例 + /// + protected override void OnInit() + { + _storage = this.GetUtility()!; + } + + /// + /// 获取设置节对应的存储键名 + /// + /// 设置节类型 + /// 格式为"Settings_类型名称"的键名字符串 + private static string GetKey() where T : ISettingsSection + => $"Settings_{typeof(T).Name}"; +} \ No newline at end of file