Files
YooAsset/Assets/YooAsset/Runtime/ResourceManager/Operation/Internal/LoadBundleFileOperation.cs

241 lines
7.1 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
2024-07-07 16:01:55 +08:00
internal class LoadBundleFileOperation : AsyncOperationBase
2023-12-21 19:10:46 +08:00
{
2024-07-07 16:01:55 +08:00
private enum ESteps
2023-12-21 19:10:46 +08:00
{
2024-07-07 16:01:55 +08:00
None,
CheckConcurrency,
LoadBundleFile,
2024-07-07 16:01:55 +08:00
Done,
2023-12-21 19:10:46 +08:00
}
private readonly ResourceManager _resourceManager;
2024-07-07 16:01:55 +08:00
private readonly List<ProviderOperation> _providers = new List<ProviderOperation>(100);
private readonly List<ProviderOperation> _removeList = new List<ProviderOperation>(100);
private FSLoadBundleOperation _loadBundleOp;
2024-07-07 16:01:55 +08:00
private ESteps _steps = ESteps.None;
2023-12-21 19:10:46 +08:00
/// <summary>
/// 资源包文件信息
/// </summary>
public BundleInfo LoadBundleInfo { private set; get; }
2023-12-21 19:10:46 +08:00
/// <summary>
/// 是否已经销毁
/// </summary>
public bool IsDestroyed { private set; get; } = false;
/// <summary>
/// 引用计数
/// </summary>
public int RefCount { private set; get; } = 0;
/// <summary>
/// 下载进度
/// </summary>
public float DownloadProgress { set; get; } = 0;
/// <summary>
/// 下载大小
/// </summary>
public long DownloadedBytes { set; get; } = 0;
2023-12-21 19:10:46 +08:00
/// <summary>
/// 加载结果
/// </summary>
public BundleResult Result { set; get; }
2023-12-21 19:10:46 +08:00
2024-07-07 16:01:55 +08:00
internal LoadBundleFileOperation(ResourceManager resourceManager, BundleInfo bundleInfo)
2023-12-21 19:10:46 +08:00
{
_resourceManager = resourceManager;
LoadBundleInfo = bundleInfo;
2023-12-21 19:10:46 +08:00
}
2025-02-22 16:29:25 +08:00
internal override void InternalStart()
2024-07-07 16:01:55 +08:00
{
_steps = ESteps.CheckConcurrency;
2024-07-07 16:01:55 +08:00
}
2025-02-22 16:29:25 +08:00
internal override void InternalUpdate()
{
2024-07-07 16:01:55 +08:00
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckConcurrency)
{
if (IsWaitForAsyncComplete)
{
_steps = ESteps.LoadBundleFile;
}
else
{
if (_resourceManager.BundleLoadingIsBusy())
return;
_steps = ESteps.LoadBundleFile;
}
}
if (_steps == ESteps.LoadBundleFile)
2024-07-07 16:01:55 +08:00
{
if (_loadBundleOp == null)
{
_resourceManager.BundleLoadingCounter++;
_loadBundleOp = LoadBundleInfo.LoadBundleFile();
_loadBundleOp.StartOperation();
AddChildOperation(_loadBundleOp);
}
2024-12-10 16:48:08 +08:00
if (IsWaitForAsyncComplete)
_loadBundleOp.WaitForAsyncComplete();
_loadBundleOp.UpdateOperation();
2024-07-07 16:01:55 +08:00
DownloadProgress = _loadBundleOp.DownloadProgress;
DownloadedBytes = _loadBundleOp.DownloadedBytes;
if (_loadBundleOp.IsDone == false)
return;
2024-07-07 16:01:55 +08:00
if (_loadBundleOp.Status == EOperationStatus.Succeed)
{
2024-12-10 15:17:49 +08:00
if (_loadBundleOp.Result == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The bundle loader result is null ! {LoadBundleInfo.Bundle.BundleName}";
2024-12-10 15:17:49 +08:00
}
else
{
_steps = ESteps.Done;
Result = _loadBundleOp.Result;
Status = EOperationStatus.Succeed;
}
2024-07-07 16:01:55 +08:00
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadBundleOp.Error;
}
// 统计计数减少
_resourceManager.BundleLoadingCounter--;
}
}
2024-07-07 16:01:55 +08:00
internal override void InternalWaitForAsyncComplete()
{
2024-07-07 16:01:55 +08:00
while (true)
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
2025-02-28 18:38:18 +08:00
internal override string InternalGetDesc()
{
return $"BundleName : {LoadBundleInfo.Bundle.BundleName}";
}
2023-12-21 19:10:46 +08:00
/// <summary>
/// 引用(引用计数递加)
/// </summary>
public void Reference()
{
RefCount++;
}
/// <summary>
/// 释放(引用计数递减)
/// </summary>
public void Release()
{
RefCount--;
}
/// <summary>
2024-07-07 16:01:55 +08:00
/// 销毁
2023-12-21 19:10:46 +08:00
/// </summary>
2024-07-07 16:01:55 +08:00
public void DestroyLoader()
2023-12-21 19:10:46 +08:00
{
2024-07-07 16:01:55 +08:00
IsDestroyed = true;
// Check fatal
if (RefCount > 0)
throw new Exception($"Bundle file loader ref is not zero : {LoadBundleInfo.Bundle.BundleName}");
2024-07-07 16:01:55 +08:00
if (IsDone == false)
throw new Exception($"Bundle file loader is not done : {LoadBundleInfo.Bundle.BundleName}");
2024-07-07 16:01:55 +08:00
if (Result != null)
Result.UnloadBundleFile();
2023-12-21 19:10:46 +08:00
}
/// <summary>
/// 是否可以销毁
/// </summary>
2024-07-07 16:01:55 +08:00
public bool CanDestroyLoader()
2023-12-21 19:10:46 +08:00
{
2024-07-07 16:01:55 +08:00
if (IsDone == false)
2023-12-21 19:10:46 +08:00
return false;
if (RefCount > 0)
return false;
2025-02-21 15:29:42 +08:00
// YOOASSET_LEGACY_DEPENDENCY
// 检查引用链上的资源包是否已经全部销毁
// 注意:互相引用的资源包无法卸载!
2025-02-21 15:29:42 +08:00
if (LoadBundleInfo.Bundle.ReferenceBundleIDs.Count > 0)
{
foreach (var bundleID in LoadBundleInfo.Bundle.ReferenceBundleIDs)
{
if (_resourceManager.CheckBundleDestroyed(bundleID) == false)
return false;
}
}
return true;
2023-12-21 19:10:46 +08:00
}
/// <summary>
/// 添加附属的资源提供者
/// </summary>
2024-07-07 16:01:55 +08:00
public void AddProvider(ProviderOperation provider)
2023-12-21 19:10:46 +08:00
{
if (_providers.Contains(provider) == false)
_providers.Add(provider);
}
/// <summary>
/// 尝试销毁资源提供者
/// </summary>
public void TryDestroyProviders()
{
// 获取移除列表
_removeList.Clear();
foreach (var provider in _providers)
{
2024-07-07 16:01:55 +08:00
if (provider.CanDestroyProvider())
2023-12-21 19:10:46 +08:00
{
_removeList.Add(provider);
}
}
// 销毁资源提供者
foreach (var provider in _removeList)
{
_providers.Remove(provider);
2024-07-07 16:01:55 +08:00
provider.DestroyProvider();
2023-12-21 19:10:46 +08:00
}
// 移除资源提供者
if (_removeList.Count > 0)
{
_resourceManager.RemoveBundleProviders(_removeList);
2023-12-21 19:10:46 +08:00
_removeList.Clear();
}
}
}
}