mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-23 17:20:12 +00:00
refactor : remove weak reference handle
This commit is contained in:
@@ -52,15 +52,6 @@ namespace YooAsset
|
|||||||
/// WebGL平台强制同步加载资源对象
|
/// WebGL平台强制同步加载资源对象
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public bool WebGLForceSyncLoadAsset = false;
|
public bool WebGLForceSyncLoadAsset = false;
|
||||||
|
|
||||||
#if YOOASSET_EXPERIMENTAL
|
|
||||||
/// <summary>
|
|
||||||
/// 启用弱引用资源句柄
|
|
||||||
/// </summary>
|
|
||||||
public bool UseWeakReferenceHandle = false;
|
|
||||||
#else
|
|
||||||
internal bool UseWeakReferenceHandle = false;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -83,7 +83,6 @@ namespace YooAsset
|
|||||||
private readonly LoadBundleFileOperation _mainBundleLoader;
|
private readonly LoadBundleFileOperation _mainBundleLoader;
|
||||||
private readonly List<LoadBundleFileOperation> _bundleLoaders = new List<LoadBundleFileOperation>(10);
|
private readonly List<LoadBundleFileOperation> _bundleLoaders = new List<LoadBundleFileOperation>(10);
|
||||||
private readonly HashSet<HandleBase> _handles = new HashSet<HandleBase>();
|
private readonly HashSet<HandleBase> _handles = new HashSet<HandleBase>();
|
||||||
private readonly LinkedList<WeakReference<HandleBase>> _weakReferences = new LinkedList<WeakReference<HandleBase>>();
|
|
||||||
|
|
||||||
public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
|
public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
|
||||||
{
|
{
|
||||||
@@ -230,11 +229,6 @@ namespace YooAsset
|
|||||||
if (IsLoading)
|
if (IsLoading)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (_resManager.UseWeakReferenceHandle)
|
|
||||||
{
|
|
||||||
TryCleanupWeakReference();
|
|
||||||
}
|
|
||||||
|
|
||||||
return RefCount <= 0;
|
return RefCount <= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -247,15 +241,7 @@ namespace YooAsset
|
|||||||
RefCount++;
|
RefCount++;
|
||||||
|
|
||||||
HandleBase handle = HandleFactory.CreateHandle(this, typeof(T));
|
HandleBase handle = HandleFactory.CreateHandle(this, typeof(T));
|
||||||
if (_resManager.UseWeakReferenceHandle)
|
_handles.Add(handle);
|
||||||
{
|
|
||||||
var weakRef = new WeakReference<HandleBase>(handle);
|
|
||||||
_weakReferences.AddLast(weakRef);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_handles.Add(handle);
|
|
||||||
}
|
|
||||||
return handle as T;
|
return handle as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -267,17 +253,8 @@ namespace YooAsset
|
|||||||
if (RefCount <= 0)
|
if (RefCount <= 0)
|
||||||
throw new YooInternalException($"Attempting to release handle when RefCount is already zero. Asset : {MainAssetInfo.AssetPath}");
|
throw new YooInternalException($"Attempting to release handle when RefCount is already zero. Asset : {MainAssetInfo.AssetPath}");
|
||||||
|
|
||||||
if (_resManager.UseWeakReferenceHandle)
|
if (_handles.Remove(handle) == false)
|
||||||
{
|
throw new YooInternalException($"Handle not found in cache list. Asset: {MainAssetInfo.AssetPath}");
|
||||||
// TODO 高危风险:如果移除弱引用失败,会导致资源永远无法释放。
|
|
||||||
if (RemoveWeakReference(handle) == false)
|
|
||||||
throw new YooInternalException($"Handle not found in weak reference list. Asset: {MainAssetInfo.AssetPath}");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (_handles.Remove(handle) == false)
|
|
||||||
throw new YooInternalException($"Handle not found in cache list. Asset: {MainAssetInfo.AssetPath}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 引用计数减少
|
// 引用计数减少
|
||||||
RefCount--;
|
RefCount--;
|
||||||
@@ -288,24 +265,10 @@ namespace YooAsset
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void ReleaseAllHandles()
|
public void ReleaseAllHandles()
|
||||||
{
|
{
|
||||||
if (_resManager.UseWeakReferenceHandle)
|
List<HandleBase> tempers = _handles.ToList();
|
||||||
|
foreach (var handle in tempers)
|
||||||
{
|
{
|
||||||
List<WeakReference<HandleBase>> tempers = _weakReferences.ToList();
|
handle.Release();
|
||||||
foreach (var weakRef in tempers)
|
|
||||||
{
|
|
||||||
if (weakRef.TryGetTarget(out HandleBase target))
|
|
||||||
{
|
|
||||||
target.Release();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<HandleBase> tempers = _handles.ToList();
|
|
||||||
foreach (var handle in tempers)
|
|
||||||
{
|
|
||||||
handle.Release();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -330,43 +293,18 @@ namespace YooAsset
|
|||||||
Status = status;
|
Status = status;
|
||||||
|
|
||||||
// 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
|
// 注意:创建临时列表是为了防止外部逻辑在回调函数内创建或者释放资源句柄。
|
||||||
// 注意:回调方法如果发生异常,会阻断列表里的后续回调方法!
|
List<HandleBase> tempers = _handles.ToList();
|
||||||
if (_resManager.UseWeakReferenceHandle)
|
foreach (var handle in tempers)
|
||||||
{
|
{
|
||||||
List<WeakReference<HandleBase>> tempers = _weakReferences.ToList();
|
if (handle.IsValid)
|
||||||
foreach (var weakRef in tempers)
|
|
||||||
{
|
{
|
||||||
if (weakRef.TryGetTarget(out HandleBase handle))
|
try
|
||||||
{
|
{
|
||||||
if (handle.IsValid)
|
handle.InvokeCallback();
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
handle.InvokeCallback();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
catch (Exception ex)
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
List<HandleBase> tempers = _handles.ToList();
|
|
||||||
foreach (var handle in tempers)
|
|
||||||
{
|
|
||||||
if (handle.IsValid)
|
|
||||||
{
|
{
|
||||||
try
|
YooLogger.Error($"Exception in completion callback: {ex}");
|
||||||
{
|
|
||||||
handle.InvokeCallback();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
YooLogger.Error($"Exception in completion callback: {ex}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -392,50 +330,6 @@ namespace YooAsset
|
|||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 移除指定句柄的弱引用对象
|
|
||||||
/// </summary>
|
|
||||||
private bool RemoveWeakReference(HandleBase handle)
|
|
||||||
{
|
|
||||||
bool removed = false;
|
|
||||||
var currentNode = _weakReferences.First;
|
|
||||||
while (currentNode != null)
|
|
||||||
{
|
|
||||||
var nextNode = currentNode.Next;
|
|
||||||
if (currentNode.Value.TryGetTarget(out HandleBase target))
|
|
||||||
{
|
|
||||||
if (ReferenceEquals(target, handle))
|
|
||||||
{
|
|
||||||
_weakReferences.Remove(currentNode);
|
|
||||||
removed = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
currentNode = nextNode;
|
|
||||||
}
|
|
||||||
return removed;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 清理所有失效的弱引用
|
|
||||||
/// </summary>
|
|
||||||
private void TryCleanupWeakReference()
|
|
||||||
{
|
|
||||||
var currentNode = _weakReferences.First;
|
|
||||||
while (currentNode != null)
|
|
||||||
{
|
|
||||||
var nextNode = currentNode.Next;
|
|
||||||
if (currentNode.Value.TryGetTarget(out HandleBase target) == false)
|
|
||||||
{
|
|
||||||
_weakReferences.Remove(currentNode);
|
|
||||||
|
|
||||||
// 引用计数减少
|
|
||||||
RefCount--;
|
|
||||||
}
|
|
||||||
currentNode = nextNode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#region 调试信息
|
#region 调试信息
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 出生的场景
|
/// 出生的场景
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ namespace YooAsset
|
|||||||
// 开发者配置选项
|
// 开发者配置选项
|
||||||
public bool AutoUnloadBundleWhenUnused { private set; get; }
|
public bool AutoUnloadBundleWhenUnused { private set; get; }
|
||||||
public bool WebGLForceSyncLoadAsset { private set; get; }
|
public bool WebGLForceSyncLoadAsset { private set; get; }
|
||||||
public bool UseWeakReferenceHandle { private set; get; }
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 所属包裹
|
/// 所属包裹
|
||||||
@@ -50,7 +49,6 @@ namespace YooAsset
|
|||||||
_bundleLoadingMaxConcurrency = parameters.BundleLoadingMaxConcurrency;
|
_bundleLoadingMaxConcurrency = parameters.BundleLoadingMaxConcurrency;
|
||||||
AutoUnloadBundleWhenUnused = parameters.AutoUnloadBundleWhenUnused;
|
AutoUnloadBundleWhenUnused = parameters.AutoUnloadBundleWhenUnused;
|
||||||
WebGLForceSyncLoadAsset = parameters.WebGLForceSyncLoadAsset;
|
WebGLForceSyncLoadAsset = parameters.WebGLForceSyncLoadAsset;
|
||||||
UseWeakReferenceHandle = parameters.UseWeakReferenceHandle;
|
|
||||||
_bundleQuery = bundleServices;
|
_bundleQuery = bundleServices;
|
||||||
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
SceneManager.sceneUnloaded += OnSceneUnloaded;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user