update operation logic

pull/326/head
何冠峰 2024-07-07 16:01:55 +08:00
parent 25231ecd32
commit 260867b588
46 changed files with 525 additions and 678 deletions

View File

@ -189,7 +189,7 @@ namespace YooAsset.Editor
// Status
StyleColor textColor;
if (bundleInfo.Status == BundleFileLoader.EStatus.Failed.ToString())
if (bundleInfo.Status == EOperationStatus.Failed)
textColor = new StyleColor(Color.yellow);
else
textColor = label1.style.color;

View File

@ -25,7 +25,7 @@ namespace YooAsset
/// <summary>
/// 加载状态
/// </summary>
public string Status;
public EOperationStatus Status;
public int CompareTo(DebugBundleInfo other)
{

View File

@ -43,7 +43,7 @@ namespace YooAsset
#elif UNITY_OPENHARMONY
return StringUtility.Format("file://{0}", path);
#else
return path;
throw new System.NotImplementedException();
#endif
}
}

View File

@ -84,19 +84,17 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
while (true)
{
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
public override void AbortDownloadOperation()
@ -170,17 +168,15 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
public override void AbortDownloadOperation()

View File

@ -94,8 +94,6 @@ namespace YooAsset
// 等待验证完成
if (_steps == ESteps.CheckVerifyTempFile)
{
// 注意:同步解压文件更新
_verifyOperation.InternalOnUpdate();
if (_verifyOperation.IsDone == false)
return;
@ -103,21 +101,21 @@ namespace YooAsset
{
if (_fileSystem.WriteCacheFile(Bundle, _tempFilePath))
{
Status = EOperationStatus.Succeed;
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
else
{
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
Status = EOperationStatus.Failed;
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"{_fileSystem.GetType().FullName} failed to write file !";
YooLogger.Error(Error);
}
}
else
{
Error = _verifyOperation.Error;
_steps = ESteps.TryAgain;
Error = _verifyOperation.Error;
}
// 注意:验证完成后直接删除文件
@ -150,23 +148,30 @@ namespace YooAsset
_steps = ESteps.Done;
DisposeWebRequest();
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
bool isReuqestLocalFile = IsRequestLocalFile();
while (true)
{
// 文件验证
if (_verifyOperation != null)
_verifyOperation.WaitForAsyncComplete();
// 注意:如果是导入或解压本地文件,执行等待完毕
if (isReuqestLocalFile)
{
if (_verifyOperation.IsDone == false)
_verifyOperation.WaitForAsyncComplete();
InternalOnUpdate();
if (IsDone)
break;
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
@ -353,23 +358,30 @@ namespace YooAsset
_steps = ESteps.Done;
DisposeWebRequest();
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
bool isReuqestLocalFile = IsRequestLocalFile();
while (true)
{
// 文件验证
if (_verifyOperation != null)
_verifyOperation.WaitForAsyncComplete();
// 注意:如果是导入或解压本地文件,执行等待完毕
if (isReuqestLocalFile)
{
if (_verifyOperation.IsDone == false)
_verifyOperation.WaitForAsyncComplete();
InternalOnUpdate();
if (IsDone)
break;
}
else
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}

View File

@ -159,31 +159,23 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
int frame = 1000;
while (true)
{
// 保险机制
// 注意:如果需要从远端下载资源,可能会触发保险机制!
frame--;
if (frame == 0)
if(_downloadFileOp != null)
_downloadFileOp.WaitForAsyncComplete();
if (ExecuteWhileDone())
{
Status = EOperationStatus.Failed;
Error = $"{nameof(WaitForAsyncComplete)} failed ! Try load bundle {_bundle.BundleName} from remote with sync load method !";
if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
_steps = ESteps.Done;
YooLogger.Error(Error);
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
}
public override void AbortDownloadOperation()
@ -299,29 +291,21 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
int frame = 1000;
while (true)
{
// 保险机制
// 注意:如果需要从远端下载资源,可能会触发保险机制!
frame--;
if (frame == 0)
if (_downloadFileOp != null)
_downloadFileOp.WaitForAsyncComplete();
if (ExecuteWhileDone())
{
Status = EOperationStatus.Failed;
Error = $"{nameof(WaitForAsyncComplete)} failed ! Try load bundle {_bundle.BundleName} from remote with sync load method !";
if (_downloadFileOp != null && _downloadFileOp.Status == EOperationStatus.Failed)
YooLogger.Error($"Try load bundle {_bundle.BundleName} from remote !");
_steps = ESteps.Done;
YooLogger.Error(Error);
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
}
public override void AbortDownloadOperation()

View File

@ -85,14 +85,12 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
// 驱动流程
// 注意:等待子线程验证文件完毕
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}

View File

@ -20,8 +20,7 @@ namespace YooAsset
internal override void InternalOnUpdate()
{
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
}
public override void AbortDownloadOperation()

View File

@ -17,20 +17,5 @@ namespace YooAsset
_saveFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.SaveFilesFolderName);
_tempFileRoot = PathUtility.Combine(_packageRoot, DefaultUnpackFileSystemDefine.TempFilesFolderName);
}
public override FSLoadBundleOperation LoadBundleFile(PackageBundle bundle)
{
if (RawFileBuildPipeline)
{
var operation = new DUFSLoadRawBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
else
{
var operation = new DUFSLoadAssetBundleOperation(this, bundle);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
}
}
}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 59b123e460757694180175970bf28826
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,57 +0,0 @@

namespace YooAsset
{
internal class DUFSLoadAssetBundleOperation : DCFSLoadAssetBundleOperation
{
public DUFSLoadAssetBundleOperation(DefaultUnpackFileSystem fileSystem, PackageBundle bundle) : base(fileSystem, bundle)
{
}
public override void WaitForAsyncComplete()
{
_isWaitForAsyncComplete = true;
while (true)
{
// 文件解压
if (_downloadFileOp != null)
{
if (_downloadFileOp.IsDone == false)
_downloadFileOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
}
internal class DUFSLoadRawBundleOperation : DCFSLoadRawBundleOperation
{
public DUFSLoadRawBundleOperation(DefaultUnpackFileSystem fileSystem, PackageBundle bundle) : base(fileSystem, bundle)
{
}
public override void WaitForAsyncComplete()
{
while (true)
{
// 文件解压
if (_downloadFileOp != null)
{
if (_downloadFileOp.IsDone == false)
_downloadFileOp.WaitForAsyncComplete();
}
// 驱动流程
InternalOnUpdate();
// 完成后退出
if (IsDone)
break;
}
}
}
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 47f6fe3a815d28e49815db7c09c1fa76
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -62,10 +62,9 @@ namespace YooAsset
}
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
if (IsDone == false)
if (_steps != ESteps.Done)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;

View File

@ -89,7 +89,7 @@ namespace YooAsset
float offset = UnityEngine.Time.realtimeSinceStartup - _latestDownloadRealtime;
if (offset > Param.Timeout)
{
YooLogger.Warning($"Web file request timeout : {_requestURL}");
YooLogger.Warning($"Download request timeout : {_requestURL}");
if (_webRequest != null)
_webRequest.Abort();
_isAbort = true;
@ -126,5 +126,19 @@ namespace YooAsset
}
#endif
}
/// <summary>
/// 是否请求的本地文件
/// </summary>
protected bool IsRequestLocalFile()
{
//TODO : UNITY_STANDALONE_OSX平台目前无法确定
if (Param.MainURL.StartsWith("file:"))
return true;
if (Param.MainURL.StartsWith("jar:file:"))
return true;
return false;
}
}
}

View File

@ -175,10 +175,6 @@ namespace YooAsset
/// </summary>
public abstract class InitializeParameters
{
/// <summary>
/// 自动销毁不再使用的资源提供者
/// </summary>
public bool AutoDestroyAssetProvider = false;
}
/// <summary>

View File

@ -8,13 +8,13 @@ namespace YooAsset
{
public abstract class AsyncOperationBase : IEnumerator, IComparable<AsyncOperationBase>
{
// 用户请求的回调
private Action<AsyncOperationBase> _callback;
// 所属包裹
private string _packageName = null;
private int _whileFrame = 1000;
// 是否已经完成
/// <summary>
/// 是否已经完成
/// </summary>
internal bool IsFinish = false;
/// <summary>
@ -85,7 +85,13 @@ namespace YooAsset
internal abstract void InternalOnStart();
internal abstract void InternalOnUpdate();
internal virtual void InternalOnAbort() { }
internal virtual void InternalOnAbort()
{
}
internal virtual void InternalWaitForAsyncComplete()
{
throw new System.NotImplementedException(this.GetType().Name);
}
internal string GetPackageName()
{
@ -125,19 +131,36 @@ namespace YooAsset
}
/// <summary>
/// 等待异步执行完毕
/// 执行While循环
/// </summary>
public virtual void WaitForAsyncComplete()
protected bool ExecuteWhileDone()
{
throw new System.NotImplementedException(this.GetType().Name);
if (IsDone == false)
{
// 执行更新逻辑
InternalOnUpdate();
// 当执行次数用完时
_whileFrame--;
if (_whileFrame == 0)
{
Status = EOperationStatus.Failed;
Error = $"Operation {this.GetType().Name} failed to wait for async complete !";
YooLogger.Error(Error);
}
}
return IsDone;
}
/// <summary>
/// 清空完成回调
/// 等待异步执行完毕
/// </summary>
protected void ClearCompletedCallback()
public void WaitForAsyncComplete()
{
_callback = null;
if (IsDone)
return;
InternalWaitForAsyncComplete();
}
#region 排序接口实现
@ -159,19 +182,5 @@ namespace YooAsset
private TaskCompletionSource<object> _taskCompletionSource;
#endregion
#region 调试方法
[Conditional("DEBUG")]
protected void DebugCheckWaitForAsyncComplete(string error = null)
{
if (IsDone == false)
{
if (string.IsNullOrEmpty(error))
YooLogger.Error($"Operation {this.GetType().Name} failed to wait for async complete !");
else
YooLogger.Error($"Operation {this.GetType().Name} failed to wait for async complete ! {error}");
}
}
#endregion
}
}

View File

@ -7,7 +7,7 @@ namespace YooAsset
{
private System.Action<AllAssetsHandle> _callback;
internal AllAssetsHandle(ProviderBase provider) : base(provider)
internal AllAssetsHandle(ProviderOperation provider) : base(provider)
{
}
internal override void InvokeCallback()

View File

@ -8,7 +8,7 @@ namespace YooAsset
{
private System.Action<AssetHandle> _callback;
internal AssetHandle(ProviderBase provider) : base(provider)
internal AssetHandle(ProviderOperation provider) : base(provider)
{
}
internal override void InvokeCallback()

View File

@ -6,9 +6,9 @@ namespace YooAsset
public abstract class HandleBase : IEnumerator
{
private readonly AssetInfo _assetInfo;
internal ProviderBase Provider { private set; get; }
internal ProviderOperation Provider { private set; get; }
internal HandleBase(ProviderBase provider)
internal HandleBase(ProviderOperation provider)
{
Provider = provider;
_assetInfo = provider.MainAssetInfo;

View File

@ -8,7 +8,7 @@ namespace YooAsset
{
private System.Action<RawFileHandle> _callback;
internal RawFileHandle(ProviderBase provider) : base(provider)
internal RawFileHandle(ProviderOperation provider) : base(provider)
{
}
internal override void InvokeCallback()

View File

@ -7,7 +7,7 @@ namespace YooAsset
private System.Action<SceneHandle> _callback;
internal string PackageName { set; get; }
internal SceneHandle(ProviderBase provider) : base(provider)
internal SceneHandle(ProviderOperation provider) : base(provider)
{
}
internal override void InvokeCallback()

View File

@ -7,7 +7,7 @@ namespace YooAsset
{
private System.Action<SubAssetsHandle> _callback;
internal SubAssetsHandle(ProviderBase provider) : base(provider)
internal SubAssetsHandle(ProviderOperation provider) : base(provider)
{
}
internal override void InvokeCallback()

View File

@ -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);
}
}
}
}

View File

@ -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>

View File

@ -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);
}
}
}
}

View File

@ -71,17 +71,19 @@ namespace YooAsset
Status = EOperationStatus.Succeed;
}
}
/// <summary>
/// 等待异步实例化结束
/// </summary>
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
if (_steps == ESteps.Clone)
while (true)
{
_handle.WaitForAsyncComplete();
InternalOnUpdate();
DebugCheckWaitForAsyncComplete();
// 等待句柄完成
if (_handle != null)
_handle.WaitForAsyncComplete();
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}

View File

@ -33,8 +33,8 @@ namespace YooAsset
if (_steps == ESteps.AbortDownload)
{
// 注意:终止所有下载任务
var loaderList = _resManager._loaderList;
foreach (var loader in loaderList)
var loaderDic = _resManager._loaderDic;
foreach (var loader in loaderDic.Values)
{
loader.AbortDownloadOperation();
}
@ -55,7 +55,6 @@ namespace YooAsset
if (_steps == ESteps.UnloadAll)
{
var loaderList = _resManager._loaderList;
var loaderDic = _resManager._loaderDic;
var providerDic = _resManager._providerDic;
@ -68,18 +67,17 @@ namespace YooAsset
// 强制销毁资源提供者
foreach (var provider in providerDic.Values)
{
provider.Destroy();
provider.DestroyProvider();
}
// 强制销毁文件加载器
foreach (var loader in loaderList)
foreach (var loader in loaderDic.Values)
{
loader.Destroy();
loader.DestroyLoader();
}
// 清空数据
providerDic.Clear();
loaderList.Clear();
loaderDic.Clear();
_resManager.ClearSceneHandle();

View File

@ -20,14 +20,14 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
private readonly string _error;
private readonly ProviderBase _provider;
private readonly ProviderOperation _provider;
private AsyncOperation _asyncOp;
internal UnloadSceneOperation(string error)
{
_error = error;
}
internal UnloadSceneOperation(ProviderBase provider)
internal UnloadSceneOperation(ProviderOperation provider)
{
_error = null;
_provider = provider;

View File

@ -1,4 +1,6 @@
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace YooAsset
{
@ -29,27 +31,32 @@ namespace YooAsset
if (_steps == ESteps.UnloadUnused)
{
var loaderList = _resManager._loaderList;
var loaderDic = _resManager._loaderDic;
var removeList = new List<LoadBundleFileOperation>(loaderDic.Count);
for (int i = loaderList.Count - 1; i >= 0; i--)
// 注意:优先销毁资源提供者
foreach (var loader in loaderDic.Values)
{
BundleFileLoader loader = loaderList[i];
loader.TryDestroyProviders();
}
for (int i = loaderList.Count - 1; i >= 0; i--)
// 获取销毁列表
foreach (var loader in loaderDic.Values)
{
BundleFileLoader loader = loaderList[i];
if (loader.CanDestroy())
if (loader.CanDestroyLoader())
{
string bundleName = loader.MainBundleInfo.Bundle.BundleName;
loader.Destroy();
loaderList.RemoveAt(i);
loaderDic.Remove(bundleName);
removeList.Add(loader);
}
}
// 销毁文件加载器
foreach (var loader in removeList)
{
string bundleName = loader.BundleFileInfo.Bundle.BundleName;
loader.DestroyLoader();
_resManager._loaderDic.Remove(bundleName);
}
// 注意:调用底层接口释放所有资源
Resources.UnloadUnusedAssets();
@ -57,10 +64,16 @@ namespace YooAsset
Status = EOperationStatus.Succeed;
}
}
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
InternalOnUpdate();
DebugCheckWaitForAsyncComplete();
while (true)
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
}
}

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class BundledAllAssetsProvider : ProviderBase
internal sealed class BundledAllAssetsProvider : ProviderOperation
{
private AssetBundle _assetBundle;
private AssetBundleRequest _cacheRequest;
@ -29,45 +29,37 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependLoaders.WaitForAsyncComplete();
FileLoader.WaitForAsyncComplete();
}
if (DependLoaders.IsDone() == false)
if (LoadDependBundleFileOp.IsDone == false)
return;
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (DependLoaders.IsSucceed() == false)
if (LoadDependBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = DependLoaders.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadDependBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Result == null)
if (LoadBundleFileOp.Result == null)
{
ProcessFatalEvent();
return;
}
if (FileLoader.Result is AssetBundle == false)
if (LoadBundleFileOp.Result is AssetBundle == false)
{
string error = "Try load raw file using load assetbundle method !";
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
_assetBundle = FileLoader.Result as AssetBundle;
_assetBundle = LoadBundleFileOp.Result as AssetBundle;
_steps = ESteps.Loading;
}
@ -115,9 +107,9 @@ namespace YooAsset
{
string error;
if (MainAssetInfo.AssetType == null)
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
else
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load all assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class BundledAssetProvider : ProviderBase
internal sealed class BundledAssetProvider : ProviderOperation
{
private AssetBundle _assetBundle;
private AssetBundleRequest _cacheRequest;
@ -29,45 +29,37 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependLoaders.WaitForAsyncComplete();
FileLoader.WaitForAsyncComplete();
}
if (DependLoaders.IsDone() == false)
if (LoadDependBundleFileOp.IsDone == false)
return;
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (DependLoaders.IsSucceed() == false)
if (LoadDependBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = DependLoaders.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadDependBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Result == null)
if (LoadBundleFileOp.Result == null)
{
ProcessFatalEvent();
return;
}
if (FileLoader.Result is AssetBundle == false)
if (LoadBundleFileOp.Result is AssetBundle == false)
{
string error = "Try load raw file using load assetbundle method !";
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
_assetBundle = FileLoader.Result as AssetBundle;
_assetBundle = LoadBundleFileOp.Result as AssetBundle;
_steps = ESteps.Loading;
}
@ -115,9 +107,9 @@ namespace YooAsset
{
string error;
if (MainAssetInfo.AssetType == null)
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
else
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load asset : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}

View File

@ -1,7 +1,7 @@

namespace YooAsset
{
internal class BundledRawFileProvider : ProviderBase
internal class BundledRawFileProvider : ProviderOperation
{
public BundledRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
@ -23,20 +23,16 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Result is string == false)
if (LoadBundleFileOp.Result is string == false)
{
string error = "Try load AssetBundle file using load raw file method !";
InvokeCompletion(error, EOperationStatus.Failed);
@ -49,7 +45,7 @@ namespace YooAsset
// 2. 检测加载结果
if (_steps == ESteps.Checking)
{
RawFilePath = FileLoader.Result as string;
RawFilePath = LoadBundleFileOp.Result as string;
InvokeCompletion(string.Empty, EOperationStatus.Succeed);
}
}

View File

@ -6,7 +6,7 @@ using UnityEngine.SceneManagement;
namespace YooAsset
{
internal sealed class BundledSceneProvider : ProviderBase
internal sealed class BundledSceneProvider : ProviderOperation
{
public readonly LoadSceneMode SceneMode;
private AsyncOperation _asyncOperation;
@ -35,28 +35,20 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependLoaders.WaitForAsyncComplete();
FileLoader.WaitForAsyncComplete();
}
if (DependLoaders.IsDone() == false)
if (LoadDependBundleFileOp.IsDone == false)
return;
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (DependLoaders.IsSucceed() == false)
if (LoadDependBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = DependLoaders.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadDependBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class BundledSubAssetsProvider : ProviderBase
internal sealed class BundledSubAssetsProvider : ProviderOperation
{
private AssetBundle _assetBundle;
private AssetBundleRequest _cacheRequest;
@ -29,45 +29,37 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
{
DependLoaders.WaitForAsyncComplete();
FileLoader.WaitForAsyncComplete();
}
if (DependLoaders.IsDone() == false)
if (LoadDependBundleFileOp.IsDone == false)
return;
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (DependLoaders.IsSucceed() == false)
if (LoadDependBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = DependLoaders.GetLastError();
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadDependBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}
if (FileLoader.Result == null)
if (LoadBundleFileOp.Result == null)
{
ProcessFatalEvent();
return;
}
if (FileLoader.Result is AssetBundle == false)
if (LoadBundleFileOp.Result is AssetBundle == false)
{
string error = "Try load raw file using load assetbundle method !";
InvokeCompletion(error, EOperationStatus.Failed);
return;
}
_assetBundle = FileLoader.Result as AssetBundle;
_assetBundle = LoadBundleFileOp.Result as AssetBundle;
_steps = ESteps.Loading;
}
@ -115,9 +107,9 @@ namespace YooAsset
{
string error;
if (MainAssetInfo.AssetType == null)
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : null AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
else
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {FileLoader.MainBundleInfo.Bundle.BundleName}";
error = $"Failed to load sub assets : {MainAssetInfo.AssetPath} AssetType : {MainAssetInfo.AssetType} AssetBundle : {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName}";
YooLogger.Error(error);
InvokeCompletion(error, EOperationStatus.Failed);
}

View File

@ -1,9 +1,9 @@

namespace YooAsset
{
internal sealed class CompletedProvider : ProviderBase
internal sealed class CompletedProvider : ProviderOperation
{
public CompletedProvider(AssetInfo assetInfo) : base(null, string.Empty, assetInfo)
public CompletedProvider(ResourceManager manager, AssetInfo assetInfo) : base(manager, string.Empty, assetInfo)
{
}

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseAllAssetsProvider : ProviderBase
internal sealed class DatabaseAllAssetsProvider : ProviderOperation
{
public DatabaseAllAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
@ -41,16 +41,12 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}
@ -63,7 +59,7 @@ namespace YooAsset
if (MainAssetInfo.AssetType == null)
{
List<UnityEngine.Object> result = new List<Object>();
foreach (var assetPath in FileLoader.MainBundleInfo.IncludeAssetsInEditor)
foreach (var assetPath in LoadBundleFileOp.BundleFileInfo.IncludeAssetsInEditor)
{
UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadMainAssetAtPath(assetPath);
if (mainAsset != null)
@ -74,7 +70,7 @@ namespace YooAsset
else
{
List<UnityEngine.Object> result = new List<Object>();
foreach (var assetPath in FileLoader.MainBundleInfo.IncludeAssetsInEditor)
foreach (var assetPath in LoadBundleFileOp.BundleFileInfo.IncludeAssetsInEditor)
{
UnityEngine.Object mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, MainAssetInfo.AssetType);
if (mainAsset != null)

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseAssetProvider : ProviderBase
internal sealed class DatabaseAssetProvider : ProviderOperation
{
public DatabaseAssetProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
@ -41,16 +41,12 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}

View File

@ -1,7 +1,7 @@

namespace YooAsset
{
internal class DatabaseRawFileProvider : ProviderBase
internal class DatabaseRawFileProvider : ProviderOperation
{
public DatabaseRawFileProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
@ -38,16 +38,12 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}

View File

@ -6,7 +6,7 @@ using UnityEngine.SceneManagement;
namespace YooAsset
{
internal sealed class DatabaseSceneProvider : ProviderBase
internal sealed class DatabaseSceneProvider : ProviderOperation
{
public readonly LoadSceneMode SceneMode;
private bool _suspendLoadMode;
@ -36,16 +36,12 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}

View File

@ -4,7 +4,7 @@ using UnityEngine;
namespace YooAsset
{
internal sealed class DatabaseSubAssetsProvider : ProviderBase
internal sealed class DatabaseSubAssetsProvider : ProviderOperation
{
public DatabaseSubAssetsProvider(ResourceManager manager, string providerGUID, AssetInfo assetInfo) : base(manager, providerGUID, assetInfo)
{
@ -41,16 +41,12 @@ namespace YooAsset
// 1. 检测资源包
if (_steps == ESteps.CheckBundle)
{
if (IsWaitForAsyncComplete)
FileLoader.WaitForAsyncComplete();
if (FileLoader.IsDone() == false)
if (LoadBundleFileOp.IsDone == false)
return;
if (FileLoader.Status != BundleFileLoader.EStatus.Succeed)
if (LoadBundleFileOp.Status != EOperationStatus.Succeed)
{
string error = FileLoader.LastError;
InvokeCompletion(error, EOperationStatus.Failed);
InvokeCompletion(LoadBundleFileOp.Error, EOperationStatus.Failed);
return;
}

View File

@ -5,7 +5,7 @@ using System;
namespace YooAsset
{
internal abstract class ProviderBase : AsyncOperationBase
internal abstract class ProviderOperation : AsyncOperationBase
{
protected enum ESteps
{
@ -68,47 +68,52 @@ namespace YooAsset
protected ESteps _steps = ESteps.None;
protected BundleFileLoader FileLoader { private set; get; }
protected DependFileLoaders DependLoaders { private set; get; }
protected LoadBundleFileOperation LoadBundleFileOp { private set; get; }
protected LoadDependBundleFileOperation LoadDependBundleFileOp { private set; get; }
protected bool IsWaitForAsyncComplete { private set; get; } = false;
private readonly List<HandleBase> _handles = new List<HandleBase>();
public ProviderBase(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
public ProviderOperation(ResourceManager manager, string providerGUID, AssetInfo assetInfo)
{
ResourceMgr = manager;
ProviderGUID = providerGUID;
MainAssetInfo = assetInfo;
// 创建资源包加载器
if (manager != null)
if (string.IsNullOrEmpty(providerGUID) == false)
{
FileLoader = manager.CreateOwnerFileLoader(assetInfo);
FileLoader.Reference();
FileLoader.AddProvider(this);
LoadBundleFileOp = manager.CreateMainBundleFileLoader(assetInfo);
LoadBundleFileOp.Reference();
LoadBundleFileOp.AddProvider(this);
var dependList = manager.CreateDependFileLoaders(assetInfo);
DependLoaders = new DependFileLoaders(dependList);
DependLoaders.Reference();
LoadDependBundleFileOp = manager.CreateDependFileLoaders(assetInfo);
LoadDependBundleFileOp.Reference();
}
}
/// <summary>
/// 等待异步执行完毕
/// </summary>
public override void WaitForAsyncComplete()
internal override void InternalWaitForAsyncComplete()
{
IsWaitForAsyncComplete = true;
// 注意:主动轮询更新完成同步加载
InternalOnUpdate();
DebugCheckWaitForAsyncComplete($"Loading asset {MainAssetInfo.AssetPath}");
while (true)
{
if (LoadDependBundleFileOp != null)
LoadDependBundleFileOp.WaitForAsyncComplete();
if (LoadBundleFileOp != null)
LoadBundleFileOp.WaitForAsyncComplete();
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
/// <summary>
/// 销毁资源提供者
/// </summary>
public void Destroy()
public void DestroyProvider()
{
IsDestroyed = true;
@ -120,22 +125,22 @@ namespace YooAsset
}
// 释放资源包加载器
if (FileLoader != null)
if (LoadBundleFileOp != null)
{
FileLoader.Release();
FileLoader = null;
LoadBundleFileOp.Release();
LoadBundleFileOp = null;
}
if (DependLoaders != null)
if (LoadDependBundleFileOp != null)
{
DependLoaders.Release();
DependLoaders = null;
LoadDependBundleFileOp.Release();
LoadDependBundleFileOp = null;
}
}
/// <summary>
/// 是否可以销毁
/// </summary>
public bool CanDestroy()
public bool CanDestroyProvider()
{
// 注意:在进行资源加载过程时不可以销毁
if (_steps == ESteps.Loading || _steps == ESteps.Checking)
@ -202,10 +207,10 @@ namespace YooAsset
/// </summary>
protected void ProcessFatalEvent()
{
if (FileLoader.IsDestroyed)
if (LoadBundleFileOp.IsDestroyed)
throw new System.Exception("Should never get here !");
string error = $"The bundle {FileLoader.MainBundleInfo.Bundle.BundleName} has been destroyed by unity bugs !";
string error = $"The bundle {LoadBundleFileOp.BundleFileInfo.Bundle.BundleName} has been destroyed by unity bugs !";
YooLogger.Error(error);
InvokeCompletion(Error, EOperationStatus.Failed);
}
@ -239,11 +244,11 @@ namespace YooAsset
public DownloadStatus GetDownloadStatus()
{
DownloadStatus status = new DownloadStatus();
status.TotalBytes = FileLoader.MainBundleInfo.Bundle.FileSize;
status.DownloadedBytes = FileLoader.DownloadedBytes;
foreach (var dependBundle in DependLoaders.DependList)
status.TotalBytes = LoadBundleFileOp.BundleFileInfo.Bundle.FileSize;
status.DownloadedBytes = LoadBundleFileOp.DownloadedBytes;
foreach (var dependBundle in LoadDependBundleFileOp.Depends)
{
status.TotalBytes += dependBundle.MainBundleInfo.Bundle.FileSize;
status.TotalBytes += dependBundle.BundleFileInfo.Bundle.FileSize;
status.DownloadedBytes += dependBundle.DownloadedBytes;
}
@ -313,12 +318,12 @@ namespace YooAsset
internal void GetBundleDebugInfos(List<DebugBundleInfo> output)
{
var bundleInfo = new DebugBundleInfo();
bundleInfo.BundleName = FileLoader.MainBundleInfo.Bundle.BundleName;
bundleInfo.RefCount = FileLoader.RefCount;
bundleInfo.Status = FileLoader.Status.ToString();
bundleInfo.BundleName = LoadBundleFileOp.BundleFileInfo.Bundle.BundleName;
bundleInfo.RefCount = LoadBundleFileOp.RefCount;
bundleInfo.Status = LoadBundleFileOp.Status;
output.Add(bundleInfo);
DependLoaders.GetBundleDebugInfos(output);
LoadDependBundleFileOp.GetBundleDebugInfos(output);
}
#endregion
}

View File

@ -13,12 +13,10 @@ namespace YooAsset
private readonly static Dictionary<string, SceneHandle> _sceneHandles = new Dictionary<string, SceneHandle>(100);
private static long _sceneCreateCount = 0;
internal readonly Dictionary<string, ProviderBase> _providerDic = new Dictionary<string, ProviderBase>(5000);
internal readonly Dictionary<string, BundleFileLoader> _loaderDic = new Dictionary<string, BundleFileLoader>(5000);
internal readonly List<BundleFileLoader> _loaderList = new List<BundleFileLoader>(5000);
internal readonly Dictionary<string, ProviderOperation> _providerDic = new Dictionary<string, ProviderOperation>(5000);
internal readonly Dictionary<string, LoadBundleFileOperation> _loaderDic = new Dictionary<string, LoadBundleFileOperation>(5000);
private bool _simulationOnEditor;
private bool _autoDestroyAssetProvider;
private IBundleQuery _bundleQuery;
/// <summary>
@ -38,24 +36,9 @@ namespace YooAsset
public void Initialize(InitializeParameters initializeParameters, IBundleQuery bundleServices)
{
_simulationOnEditor = initializeParameters is EditorSimulateModeParameters;
_autoDestroyAssetProvider = initializeParameters.AutoDestroyAssetProvider;
_bundleQuery = bundleServices;
}
/// <summary>
/// 更新
/// </summary>
public void Update()
{
foreach (var loader in _loaderList)
{
loader.Update();
if (_autoDestroyAssetProvider)
loader.TryDestroyProviders();
}
}
/// <summary>
/// 尝试卸载指定资源的资源包(包括依赖资源)
/// </summary>
@ -73,11 +56,10 @@ namespace YooAsset
if (mainLoader != null)
{
mainLoader.TryDestroyProviders();
if (mainLoader.CanDestroy())
if (mainLoader.CanDestroyLoader())
{
string bundleName = mainLoader.MainBundleInfo.Bundle.BundleName;
mainLoader.Destroy();
_loaderList.Remove(mainLoader);
string bundleName = mainLoader.BundleFileInfo.Bundle.BundleName;
mainLoader.DestroyLoader();
_loaderDic.Remove(bundleName);
}
}
@ -89,11 +71,10 @@ namespace YooAsset
var dependLoader = TryGetFileLoader(dependBundleName);
if (dependLoader != null)
{
if (dependLoader.CanDestroy())
if (dependLoader.CanDestroyLoader())
{
string bundleName = dependLoader.MainBundleInfo.Bundle.BundleName;
dependLoader.Destroy();
_loaderList.Remove(dependLoader);
string bundleName = dependLoader.BundleFileInfo.Bundle.BundleName;
dependLoader.DestroyLoader();
_loaderDic.Remove(bundleName);
}
}
@ -110,7 +91,7 @@ namespace YooAsset
if (assetInfo.IsInvalid)
{
YooLogger.Error($"Failed to load scene ! {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<SceneHandle>();
}
@ -123,7 +104,7 @@ namespace YooAsset
// 注意同一个场景的ProviderGUID每次加载都会变化
string providerGUID = $"{assetInfo.GUID}-{++_sceneCreateCount}";
ProviderBase provider;
ProviderOperation provider;
{
if (_simulationOnEditor)
provider = new DatabaseSceneProvider(this, providerGUID, assetInfo, sceneMode, suspendLoad);
@ -149,13 +130,13 @@ namespace YooAsset
if (assetInfo.IsInvalid)
{
YooLogger.Error($"Failed to load asset ! {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<AssetHandle>();
}
string providerGUID = nameof(LoadAssetAsync) + assetInfo.GUID;
ProviderBase provider = TryGetProvider(providerGUID);
ProviderOperation provider = TryGetProvider(providerGUID);
if (provider == null)
{
if (_simulationOnEditor)
@ -179,13 +160,13 @@ namespace YooAsset
if (assetInfo.IsInvalid)
{
YooLogger.Error($"Failed to load sub assets ! {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<SubAssetsHandle>();
}
string providerGUID = nameof(LoadSubAssetsAsync) + assetInfo.GUID;
ProviderBase provider = TryGetProvider(providerGUID);
ProviderOperation provider = TryGetProvider(providerGUID);
if (provider == null)
{
if (_simulationOnEditor)
@ -209,13 +190,13 @@ namespace YooAsset
if (assetInfo.IsInvalid)
{
YooLogger.Error($"Failed to load all assets ! {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<AllAssetsHandle>();
}
string providerGUID = nameof(LoadAllAssetsAsync) + assetInfo.GUID;
ProviderBase provider = TryGetProvider(providerGUID);
ProviderOperation provider = TryGetProvider(providerGUID);
if (provider == null)
{
if (_simulationOnEditor)
@ -239,13 +220,13 @@ namespace YooAsset
if (assetInfo.IsInvalid)
{
YooLogger.Error($"Failed to load raw file ! {assetInfo.Error}");
CompletedProvider completedProvider = new CompletedProvider(assetInfo);
CompletedProvider completedProvider = new CompletedProvider(this, assetInfo);
completedProvider.SetCompleted(assetInfo.Error);
return completedProvider.CreateHandle<RawFileHandle>();
}
string providerGUID = nameof(LoadRawFileAsync) + assetInfo.GUID;
ProviderBase provider = TryGetProvider(providerGUID);
ProviderOperation provider = TryGetProvider(providerGUID);
if (provider == null)
{
if (_simulationOnEditor)
@ -311,23 +292,25 @@ namespace YooAsset
}
}
internal BundleFileLoader CreateOwnerFileLoader(AssetInfo assetInfo)
internal LoadBundleFileOperation CreateMainBundleFileLoader(AssetInfo assetInfo)
{
BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo);
return CreateFileLoaderInternal(bundleInfo);
}
internal List<BundleFileLoader> CreateDependFileLoaders(AssetInfo assetInfo)
internal LoadDependBundleFileOperation CreateDependFileLoaders(AssetInfo assetInfo)
{
BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo);
List<BundleFileLoader> result = new List<BundleFileLoader>(depends.Length);
foreach (var bundleInfo in depends)
BundleInfo[] bundleInfos = _bundleQuery.GetDependBundleInfos(assetInfo);
List<LoadBundleFileOperation> depends = new List<LoadBundleFileOperation>(bundleInfos.Length);
foreach (var bundleInfo in bundleInfos)
{
BundleFileLoader dependLoader = CreateFileLoaderInternal(bundleInfo);
result.Add(dependLoader);
LoadBundleFileOperation dependLoader = CreateFileLoaderInternal(bundleInfo);
depends.Add(dependLoader);
}
return result;
var operation = new LoadDependBundleFileOperation(depends);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
internal void RemoveBundleProviders(List<ProviderBase> removeList)
internal void RemoveBundleProviders(List<ProviderOperation> removeList)
{
foreach (var provider in removeList)
{
@ -336,33 +319,33 @@ namespace YooAsset
}
internal bool HasAnyLoader()
{
return _loaderList.Count > 0;
return _loaderDic.Count > 0;
}
private BundleFileLoader CreateFileLoaderInternal(BundleInfo bundleInfo)
private LoadBundleFileOperation CreateFileLoaderInternal(BundleInfo bundleInfo)
{
// 如果加载器已经存在
string bundleName = bundleInfo.Bundle.BundleName;
BundleFileLoader loader = TryGetFileLoader(bundleName);
if (loader != null)
return loader;
LoadBundleFileOperation loaderOperation = TryGetFileLoader(bundleName);
if (loaderOperation != null)
return loaderOperation;
// 新增下载需求
loader = new BundleFileLoader(this, bundleInfo);
_loaderList.Add(loader);
_loaderDic.Add(bundleName, loader);
return loader;
loaderOperation = new LoadBundleFileOperation(this, bundleInfo);
OperationSystem.StartOperation(PackageName, loaderOperation);
_loaderDic.Add(bundleName, loaderOperation);
return loaderOperation;
}
private BundleFileLoader TryGetFileLoader(string bundleName)
private LoadBundleFileOperation TryGetFileLoader(string bundleName)
{
if (_loaderDic.TryGetValue(bundleName, out BundleFileLoader value))
if (_loaderDic.TryGetValue(bundleName, out LoadBundleFileOperation value))
return value;
else
return null;
}
private ProviderBase TryGetProvider(string providerGUID)
private ProviderOperation TryGetProvider(string providerGUID)
{
if (_providerDic.TryGetValue(providerGUID, out ProviderBase value))
if (_providerDic.TryGetValue(providerGUID, out ProviderOperation value))
return value;
else
return null;

View File

@ -42,9 +42,6 @@ namespace YooAsset
/// </summary>
internal void UpdatePackage()
{
if (_resourceManager != null)
_resourceManager.Update();
if (_playModeImpl != null)
_playModeImpl.UpdatePlayMode();
}