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); } } }