mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 实现了完整的资源管理系统,包括资源加载、缓存和卸载功能 - 添加了 IResourceHandle、IResourceLoader、IResourceManager 和 IResourceReleaseStrategy 接口定义 - 实现了 AutoReleaseStrategy 和 ManualReleaseStrategy 两种资源释放策略 - 创建了 ResourceCache 缓存系统和 ResourceHandle 资源句柄管理 - 在 ConfigurationManager 和 CoroutineScheduler 中集成了 ILogger 日志功能 - 添加了全面的 ResourceManagerTests 单元测试覆盖各种使用场景
144 lines
3.3 KiB
C#
144 lines
3.3 KiB
C#
using GFramework.Core.Abstractions.logging;
|
|
using GFramework.Core.Abstractions.resource;
|
|
using GFramework.Core.logging;
|
|
|
|
namespace GFramework.Core.resource;
|
|
|
|
/// <summary>
|
|
/// 资源句柄实现,管理资源的生命周期和引用计数
|
|
/// 线程安全:所有公共方法都是线程安全的
|
|
/// </summary>
|
|
/// <typeparam name="T">资源类型</typeparam>
|
|
internal sealed class ResourceHandle<T> : IResourceHandle<T> where T : class
|
|
{
|
|
private readonly object _lock = new();
|
|
private readonly ILogger _logger = LoggerFactoryResolver.Provider.CreateLogger(nameof(ResourceHandle<T>));
|
|
private readonly Action<string> _onDispose;
|
|
private bool _disposed;
|
|
private int _referenceCount;
|
|
|
|
/// <summary>
|
|
/// 创建资源句柄
|
|
/// </summary>
|
|
/// <param name="resource">资源实例</param>
|
|
/// <param name="path">资源路径</param>
|
|
/// <param name="onDispose">释放时的回调</param>
|
|
public ResourceHandle(T resource, string path, Action<string> onDispose)
|
|
{
|
|
Resource = resource ?? throw new ArgumentNullException(nameof(resource));
|
|
Path = path ?? throw new ArgumentNullException(nameof(path));
|
|
_onDispose = onDispose ?? throw new ArgumentNullException(nameof(onDispose));
|
|
_referenceCount = 1;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取资源实例
|
|
/// </summary>
|
|
public T? Resource { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 获取资源路径
|
|
/// </summary>
|
|
public string Path { get; }
|
|
|
|
/// <summary>
|
|
/// 获取资源是否有效
|
|
/// </summary>
|
|
public bool IsValid
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return !_disposed && Resource != null;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取当前引用计数
|
|
/// </summary>
|
|
public int ReferenceCount
|
|
{
|
|
get
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _referenceCount;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加引用计数
|
|
/// </summary>
|
|
public void AddReference()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_disposed)
|
|
throw new ObjectDisposedException(nameof(ResourceHandle<T>));
|
|
|
|
_referenceCount++;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 减少引用计数
|
|
/// </summary>
|
|
public void RemoveReference()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_referenceCount--;
|
|
|
|
if (_referenceCount <= 0)
|
|
{
|
|
DisposeInternal();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 释放资源句柄
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_referenceCount--;
|
|
|
|
if (_referenceCount <= 0)
|
|
{
|
|
DisposeInternal();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 内部释放方法(必须在锁内调用)
|
|
/// </summary>
|
|
private void DisposeInternal()
|
|
{
|
|
if (_disposed)
|
|
return;
|
|
|
|
_disposed = true;
|
|
Resource = null;
|
|
|
|
try
|
|
{
|
|
_onDispose(Path);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.Error($"[ResourceHandle] Error disposing resource '{Path}': {ex.Message}");
|
|
}
|
|
}
|
|
} |