From 54bed12056963e5e61a9031d965944305f5fbe6a Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Thu, 5 Mar 2026 08:27:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(config):=20=E4=BF=AE=E5=A4=8D=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E7=AE=A1=E7=90=86=E5=99=A8=E5=8F=98=E6=9B=B4=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=92=8C=E8=B5=84=E6=BA=90=E9=85=8D=E7=BD=AE=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 ConfigurationManager 中的 AddOrUpdate 逻辑,先获取旧值再更新以正确检测变更 - 只有在配置值真正发生变化时才触发监听器回调 - 更新异常日志记录方式,移除冗余的标签前缀 - 将 ConfigWatcherUnRegister 移动到正确的命名空间 - 修复 ResourceManager 中的引用计数逻辑,移除重复的 AddReference 调用 - 优化资源加载和卸载时的异常处理和日志记录 - 更新测试注释以反映正确的引用计数行为 --- .../resource/ResourceManagerTests.cs | 2 +- .../configuration/ConfigWatcherUnRegister.cs | 2 +- .../configuration/ConfigurationManager.cs | 11 +++++++---- GFramework.Core/resource/ResourceManager.cs | 17 +++++------------ 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/GFramework.Core.Tests/resource/ResourceManagerTests.cs b/GFramework.Core.Tests/resource/ResourceManagerTests.cs index 61a12ea..d5eb90c 100644 --- a/GFramework.Core.Tests/resource/ResourceManagerTests.cs +++ b/GFramework.Core.Tests/resource/ResourceManagerTests.cs @@ -200,7 +200,7 @@ public class ResourceManagerTests var handle = _resourceManager.GetHandle("test/resource1.txt"); handle!.Dispose(); - // 资源应该仍然存在,因为还有一个初始引用 + // 引用计数降为 0,但手动释放策略不会自动卸载 Assert.That(_resourceManager.IsLoaded("test/resource1.txt"), Is.True); } diff --git a/GFramework.Core/configuration/ConfigWatcherUnRegister.cs b/GFramework.Core/configuration/ConfigWatcherUnRegister.cs index 78b6199..84066bc 100644 --- a/GFramework.Core/configuration/ConfigWatcherUnRegister.cs +++ b/GFramework.Core/configuration/ConfigWatcherUnRegister.cs @@ -1,6 +1,6 @@ using GFramework.Core.Abstractions.events; -namespace GFramework.Core.Abstractions.configuration; +namespace GFramework.Core.configuration; /// /// 配置监听取消注册接口 diff --git a/GFramework.Core/configuration/ConfigurationManager.cs b/GFramework.Core/configuration/ConfigurationManager.cs index bc515de..73e47a0 100644 --- a/GFramework.Core/configuration/ConfigurationManager.cs +++ b/GFramework.Core/configuration/ConfigurationManager.cs @@ -92,9 +92,13 @@ public class ConfigurationManager : IConfigurationManager if (string.IsNullOrWhiteSpace(key)) throw new ArgumentException(KeyCannotBeNullOrEmptyMessage, nameof(key)); - var oldValue = _configs.AddOrUpdate(key, value!, (_, _) => value!); + // 先获取旧值,以便正确检测变更 + _configs.TryGetValue(key, out var oldValue); - // 触发监听器 + // 更新配置值 + _configs[key] = value!; + + // 只有在值真正变化时才触发监听器 if (!EqualityComparer.Default.Equals(oldValue, value)) { NotifyWatchers(key, value); @@ -293,8 +297,7 @@ public class ConfigurationManager : IConfigurationManager catch (Exception ex) { // 防止监听器异常影响其他监听器 - _logger.Error( - $"[ConfigurationManager] Error in config watcher for key '{key}': {ex.Message}"); + _logger.Error($"Error in config watcher for key '{key}'", ex); } } } diff --git a/GFramework.Core/resource/ResourceManager.cs b/GFramework.Core/resource/ResourceManager.cs index 3801a14..8ef383e 100644 --- a/GFramework.Core/resource/ResourceManager.cs +++ b/GFramework.Core/resource/ResourceManager.cs @@ -48,7 +48,6 @@ public class ResourceManager : IResourceManager var cached = _cache.Get(path); if (cached != null) { - _cache.AddReference(path); return cached; } @@ -59,7 +58,6 @@ public class ResourceManager : IResourceManager cached = _cache.Get(path); if (cached != null) { - _cache.AddReference(path); return cached; } @@ -73,12 +71,11 @@ public class ResourceManager : IResourceManager { var resource = loader.Load(path); _cache.Add(path, resource); - _cache.AddReference(path); return resource; } catch (Exception ex) { - _logger.Error($"[ResourceManager] Failed to load resource '{path}': {ex.Message}"); + _logger.Error($"Failed to load resource '{path}'", ex); return null; } } @@ -96,7 +93,6 @@ public class ResourceManager : IResourceManager var cached = _cache.Get(path); if (cached != null) { - _cache.AddReference(path); return cached; } @@ -117,19 +113,17 @@ public class ResourceManager : IResourceManager { // 已经被其他线程加载了,卸载当前加载的资源 loader.Unload(resource); - _cache.AddReference(path); return cached; } _cache.Add(path, resource); - _cache.AddReference(path); } return resource; } catch (Exception ex) { - _logger.Error($"[ResourceManager] Failed to load resource '{path}': {ex.Message}"); + _logger.Error($"Failed to load resource '{path}'", ex); return null; } } @@ -168,8 +162,7 @@ public class ResourceManager : IResourceManager var refCount = _cache.GetReferenceCount(path); if (refCount > 0) { - _logger.Error( - $"[ResourceManager] Cannot unload resource '{path}' with {refCount} active references"); + _logger.Error($"Cannot unload resource '{path}' with {refCount} active references"); return false; } @@ -287,7 +280,7 @@ public class ResourceManager : IResourceManager } catch (Exception ex) { - _logger.Error($"[ResourceManager] Error unloading resource: {ex.Message}"); + _logger.Error("Error unloading resource", ex); } } @@ -300,7 +293,7 @@ public class ResourceManager : IResourceManager } catch (Exception ex) { - _logger.Error($"[ResourceManager] Error disposing resource: {ex.Message}"); + _logger.Error("Error disposing resource", ex); } } }