update operation logic

This commit is contained in:
何冠峰
2024-07-07 16:01:55 +08:00
parent 25231ecd32
commit 260867b588
46 changed files with 525 additions and 678 deletions
@@ -1,112 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class DependFileLoaders
{
/// <summary>
/// 依赖的资源包加载器列表
/// </summary>
internal readonly List<BundleFileLoader> DependList;
public DependFileLoaders(List<BundleFileLoader> dpendList)
{
DependList = dpendList;
}
/// <summary>
/// 是否已经完成(无论成功或失败)
/// </summary>
public bool IsDone()
{
foreach (var loader in DependList)
{
if (loader.IsDone() == false)
return false;
}
return true;
}
/// <summary>
/// 依赖资源包是否全部加载成功
/// </summary>
public bool IsSucceed()
{
foreach (var loader in DependList)
{
if (loader.Status != BundleFileLoader.EStatus.Succeed)
{
return false;
}
}
return true;
}
/// <summary>
/// 获取某个加载失败的资源包错误信息
/// </summary>
public string GetLastError()
{
foreach (var loader in DependList)
{
if (loader.Status != BundleFileLoader.EStatus.Succeed)
{
return loader.LastError;
}
}
return string.Empty;
}
/// <summary>
/// 主线程等待异步操作完毕
/// </summary>
public void WaitForAsyncComplete()
{
foreach (var loader in DependList)
{
if (loader.IsDone() == false)
loader.WaitForAsyncComplete();
}
}
/// <summary>
/// 增加引用计数
/// </summary>
public void Reference()
{
foreach (var loader in DependList)
{
loader.Reference();
}
}
/// <summary>
/// 减少引用计数
/// </summary>
public void Release()
{
foreach (var loader in DependList)
{
loader.Release();
}
}
/// <summary>
/// 获取资源包的调试信息列表
/// </summary>
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
{
foreach (var loader in DependList)
{
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = loader.MainBundleInfo.Bundle.BundleName;
bundleInfo.RefCount = loader.RefCount;
bundleInfo.Status = loader.Status.ToString();
output.Add(bundleInfo);
}
}
}
}
@@ -1,39 +1,28 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
internal class BundleFileLoader
internal class LoadBundleFileOperation : AsyncOperationBase
{
public enum EStatus
private enum ESteps
{
None = 0,
Succeed,
Failed
None,
LoadFile,
Done,
}
private readonly ResourceManager _resourceManager;
private readonly List<ProviderBase> _providers = new List<ProviderBase>(100);
private readonly List<ProviderBase> _removeList = new List<ProviderBase>(100);
private readonly List<ProviderOperation> _providers = new List<ProviderOperation>(100);
private readonly List<ProviderOperation> _removeList = new List<ProviderOperation>(100);
private FSLoadBundleOperation _loadBundleOp;
private bool _isDone = false;
private ESteps _steps = ESteps.None;
/// <summary>
/// 资源包文件信息
/// </summary>
public BundleInfo MainBundleInfo { private set; get; }
/// <summary>
/// 加载状态
/// </summary>
public EStatus Status { protected set; get; }
/// <summary>
/// 最近的错误信息
/// </summary>
public string LastError { protected set; get; } = string.Empty;
public BundleInfo BundleFileInfo { private set; get; }
/// <summary>
/// 是否已经销毁
@@ -61,57 +50,57 @@ namespace YooAsset
public object Result { set; get; }
public BundleFileLoader(ResourceManager resourceManager, BundleInfo bundleInfo)
internal LoadBundleFileOperation(ResourceManager resourceManager, BundleInfo bundleInfo)
{
_resourceManager = resourceManager;
MainBundleInfo = bundleInfo;
Status = EStatus.None;
BundleFileInfo = bundleInfo;
}
/// <summary>
/// 轮询更新
/// </summary>
public void Update()
internal override void InternalOnStart()
{
if (_isDone)
_steps = ESteps.LoadFile;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_loadBundleOp == null)
_loadBundleOp = MainBundleInfo.LoadBundleFile();
DownloadProgress = _loadBundleOp.DownloadProgress;
DownloadedBytes = _loadBundleOp.DownloadedBytes;
if (_loadBundleOp.IsDone == false)
return;
if (_loadBundleOp.Status == EOperationStatus.Succeed)
if (_steps == ESteps.LoadFile)
{
_isDone = true;
Status = EStatus.Succeed;
Result = _loadBundleOp.Result;
}
else
{
_isDone = true;
Status = EStatus.Failed;
LastError = _loadBundleOp.Error;
if (_loadBundleOp == null)
_loadBundleOp = BundleFileInfo.LoadBundleFile();
DownloadProgress = _loadBundleOp.DownloadProgress;
DownloadedBytes = _loadBundleOp.DownloadedBytes;
if (_loadBundleOp.IsDone == false)
return;
if (_loadBundleOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.Done;
Result = _loadBundleOp.Result;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _loadBundleOp.Error;
}
}
}
/// <summary>
/// 销毁
/// </summary>
public void Destroy()
internal override void InternalWaitForAsyncComplete()
{
IsDestroyed = true;
while (true)
{
if (_loadBundleOp != null)
_loadBundleOp.WaitForAsyncComplete();
// Check fatal
if (RefCount > 0)
throw new Exception($"Bundle file loader ref is not zero : {MainBundleInfo.Bundle.BundleName}");
if (IsDone() == false)
throw new Exception($"Bundle file loader is not done : {MainBundleInfo.Bundle.BundleName}");
MainBundleInfo.UnloadBundleFile(Result);
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
/// <summary>
@@ -131,19 +120,27 @@ namespace YooAsset
}
/// <summary>
/// 是否完毕(无论成功或失败)
/// 销毁
/// </summary>
public bool IsDone()
public void DestroyLoader()
{
return Status == EStatus.Succeed || Status == EStatus.Failed;
IsDestroyed = true;
// Check fatal
if (RefCount > 0)
throw new Exception($"Bundle file loader ref is not zero : {BundleFileInfo.Bundle.BundleName}");
if (IsDone == false)
throw new Exception($"Bundle file loader is not done : {BundleFileInfo.Bundle.BundleName}");
BundleFileInfo.UnloadBundleFile(Result);
}
/// <summary>
/// 是否可以销毁
/// </summary>
public bool CanDestroy()
public bool CanDestroyLoader()
{
if (IsDone() == false)
if (IsDone == false)
return false;
return RefCount <= 0;
@@ -152,7 +149,7 @@ namespace YooAsset
/// <summary>
/// 添加附属的资源提供者
/// </summary>
public void AddProvider(ProviderBase provider)
public void AddProvider(ProviderOperation provider)
{
if (_providers.Contains(provider) == false)
_providers.Add(provider);
@@ -167,7 +164,7 @@ namespace YooAsset
_removeList.Clear();
foreach (var provider in _providers)
{
if (provider.CanDestroy())
if (provider.CanDestroyProvider())
{
_removeList.Add(provider);
}
@@ -177,7 +174,7 @@ namespace YooAsset
foreach (var provider in _removeList)
{
_providers.Remove(provider);
provider.Destroy();
provider.DestroyProvider();
}
// 移除资源提供者
@@ -188,28 +185,6 @@ namespace YooAsset
}
}
/// <summary>
/// 主线程等待异步操作完毕
/// </summary>
public void WaitForAsyncComplete()
{
while (true)
{
if (_loadBundleOp != null)
{
if (_loadBundleOp.IsDone == false)
_loadBundleOp.WaitForAsyncComplete();
}
// 驱动流程
Update();
// 完成后退出
if (IsDone())
break;
}
}
/// <summary>
/// 终止下载任务
/// </summary>
@@ -0,0 +1,126 @@
using System;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset
{
internal class LoadDependBundleFileOperation : AsyncOperationBase
{
private enum ESteps
{
None,
CheckDepend,
CheckResult,
Done,
}
/// <summary>
/// 依赖的资源包加载器列表
/// </summary>
internal readonly List<LoadBundleFileOperation> Depends;
private ESteps _steps = ESteps.None;
internal LoadDependBundleFileOperation(List<LoadBundleFileOperation> dpends)
{
Depends = dpends;
}
internal override void InternalOnStart()
{
_steps = ESteps.CheckDepend;
}
internal override void InternalOnUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.CheckDepend)
{
foreach (var loader in Depends)
{
if (loader.IsDone == false)
return;
}
_steps = ESteps.CheckResult;
}
if (_steps == ESteps.CheckResult)
{
LoadBundleFileOperation failedLoader = null;
foreach (var loader in Depends)
{
if (loader.Status != EOperationStatus.Succeed)
{
failedLoader = loader;
break;
}
}
if (failedLoader == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = failedLoader.Error;
}
}
}
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
foreach (var loader in Depends)
{
loader.WaitForAsyncComplete();
}
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
/// <summary>
/// 增加引用计数
/// </summary>
public void Reference()
{
foreach (var loader in Depends)
{
loader.Reference();
}
}
/// <summary>
/// 减少引用计数
/// </summary>
public void Release()
{
foreach (var loader in Depends)
{
loader.Release();
}
}
/// <summary>
/// 获取资源包的调试信息列表
/// </summary>
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
{
foreach (var loader in Depends)
{
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = loader.BundleFileInfo.Bundle.BundleName;
bundleInfo.RefCount = loader.RefCount;
bundleInfo.Status = loader.Status;
output.Add(bundleInfo);
}
}
}
}