From eb60b0514497136a685eb122eb9b10fb8a704f9e Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Fri, 30 Jan 2026 22:53:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E7=BB=9F=E4=B8=80=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E6=95=B0=E6=8D=AE=E4=BB=93=E5=BA=93=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E7=BA=BF=E7=A8=8B=E5=AE=89=E5=85=A8=E5=92=8C=E9=94=AE=E7=AE=A1?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 UnifiedKey 属性用于获取统一键值 - 在 SaveAsync 方法中添加异步锁确保线程安全 - 使用 UnifiedKey 替换 fileName 进行文件读写操作 - 在 EnsureLoadedAsync 中使用统一键进行存在性检查 - 优化 SaveAllToUnifiedFile 方法使用新的键属性 - 移除多余的空行以改善代码格式 - [release ci] --- .../data/UnifiedSettingsDataRepository.cs | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/GFramework.Game/data/UnifiedSettingsDataRepository.cs b/GFramework.Game/data/UnifiedSettingsDataRepository.cs index 1de78fd..d2c020d 100644 --- a/GFramework.Game/data/UnifiedSettingsDataRepository.cs +++ b/GFramework.Game/data/UnifiedSettingsDataRepository.cs @@ -45,6 +45,9 @@ public class UnifiedSettingsDataRepository( private UnifiedSettingsFile File => _file ?? throw new InvalidOperationException("UnifiedSettingsFile not set."); + + private string UnifiedKey => GetUnifiedKey(); + // ========================= // IDataRepository // ========================= @@ -76,16 +79,24 @@ public class UnifiedSettingsDataRepository( public async Task SaveAsync(IDataLocation location, T data) where T : class, IData { - await EnsureLoadedAsync(); + await _lock.WaitAsync(); + try + { + await EnsureLoadedAsync(); - var key = location.Key; - var serialized = Serializer.Serialize(data); + var key = location.Key; + var serialized = Serializer.Serialize(data); - _file!.Sections[key] = serialized; + _file!.Sections[key] = serialized; - await Storage.WriteAsync(fileName, _file); - if (_options.EnableEvents) - this.SendEvent(new DataSavedEvent(data)); + await Storage.WriteAsync(UnifiedKey, _file); + if (_options.EnableEvents) + this.SendEvent(new DataSavedEvent(data)); + } + finally + { + _lock.Release(); + } } /// @@ -175,9 +186,11 @@ public class UnifiedSettingsDataRepository( { if (_loaded) return; - if (await Storage.ExistsAsync(fileName)) + var key = UnifiedKey; + + if (await Storage.ExistsAsync(key)) { - _file = await Storage.ReadAsync(fileName); + _file = await Storage.ReadAsync(key); } else { @@ -192,6 +205,7 @@ public class UnifiedSettingsDataRepository( } } + /// /// 将缓存中的所有数据保存到统一文件 /// @@ -200,7 +214,7 @@ public class UnifiedSettingsDataRepository( await _lock.WaitAsync(); try { - await Storage.WriteAsync(GetUnifiedKey(), File); + await Storage.WriteAsync(UnifiedKey, _file); } finally {