mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 将所有小写的命名空间导入更正为首字母大写格式 - 统一 GFramework 框架的命名空间引用规范 - 修复 core、ecs、godot 等模块的命名空间导入错误 - 标准化文档示例代码中的 using 语句格式 - 确保所有文档中的命名空间引用保持一致性 - 更新 global using 语句以匹配正确的命名空间格式
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}");
|
|
}
|
|
}
|
|
} |