From 516a9e22810946fd7ffff021ed8db5c3238f2aef Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 16 Jan 2026 23:25:33 +0800 Subject: [PATCH] =?UTF-8?q?feat(setting):=20=E6=B7=BB=E5=8A=A0=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=8C=81=E4=B9=85=E5=8C=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现了 SettingsPersistence 类提供设置数据的加载、保存、删除等操作 - 定义了 ISettingsPersistence 接口规范设置持久化行为 - 集成存储服务支持异步读写设置节数据 - 实现类型安全的设置节存取机制 - 提供设置节存在性检查和删除功能 - 采用 "Settings_类型名称" 格式生成存储键名 --- .../setting/ISettingsPersistence.cs | 39 +++++++++ .../setting/SettingsPersistence.cs | 80 +++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 GFramework.Game.Abstractions/setting/ISettingsPersistence.cs create mode 100644 GFramework.Game/setting/SettingsPersistence.cs 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