From 260867b588820e1ae5eb6060f2d886639c98f324 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Sun, 7 Jul 2024 16:01:55 +0800 Subject: [PATCH] update operation logic --- .../VisualViewers/DebuggerBundleListViewer.cs | 2 +- .../DiagnosticSystem/DebugBundleInfo.cs | 2 +- .../DownloadSystem/DownloadSystemHelper.cs | 2 +- .../Operation/DBFSLoadBundleOperation.cs | 24 ++- .../Operation/DCFSDownloadFileOperation.cs | 68 ++++---- .../Operation/DCFSLoadBundleOperation.cs | 52 ++---- .../internal/VerifyTempFileOperation.cs | 6 +- .../Operation/DEFSLoadBundleOperation.cs | 3 +- .../DefaultUnpackFileSystem.cs | 15 -- .../DefaultUnpackFileSystem/Operation.meta | 8 - .../Operation/DUFSLoadBundleOperation.cs | 57 ------- .../Operation/DUFSLoadBundleOperation.cs.meta | 11 -- .../Operation/DWFSLoadBundleOperation.cs | 5 +- .../Internal/DefaultDownloadFileOperation.cs | 16 +- .../YooAsset/Runtime/InitializeParameters.cs | 4 - .../OperationSystem/AsyncOperationBase.cs | 59 ++++--- .../ResourceManager/Handle/AllAssetsHandle.cs | 2 +- .../ResourceManager/Handle/AssetHandle.cs | 2 +- .../ResourceManager/Handle/HandleBase.cs | 4 +- .../ResourceManager/Handle/RawFileHandle.cs | 2 +- .../ResourceManager/Handle/SceneHandle.cs | 2 +- .../ResourceManager/Handle/SubAssetsHandle.cs | 2 +- .../Loader/DependFileLoaders.cs | 112 ------------- ...leLoader.cs => LoadBundleFileOperation.cs} | 155 ++++++++---------- ...s.meta => LoadBundleFileOperation.cs.meta} | 0 .../Loader/LoadDependFileOperation.cs | 126 ++++++++++++++ ...s.meta => LoadDependFileOperation.cs.meta} | 0 .../Operation/InstantiateOperation.cs | 20 ++- .../Operation/UnloadAllAssetsOperation.cs | 12 +- .../Operation/UnloadSceneOperation.cs | 4 +- .../Operation/UnloadUnusedAssetsOperation.cs | 41 +++-- .../Provider/BundledAllAssetsProvider.cs | 32 ++-- .../Provider/BundledAssetProvider.cs | 32 ++-- .../Provider/BundledRawFileProvider.cs | 16 +- .../Provider/BundledSceneProvider.cs | 22 +-- .../Provider/BundledSubAssetsProvider.cs | 32 ++-- .../Provider/CompletedProvider.cs | 4 +- .../Provider/DatabaseAllAssetsProvider.cs | 16 +- .../Provider/DatabaseAssetProvider.cs | 12 +- .../Provider/DatabaseRawFileProvider.cs | 12 +- .../Provider/DatabaseSceneProvider.cs | 12 +- .../Provider/DatabaseSubAssetsProvider.cs | 12 +- .../{ProviderBase.cs => ProviderOperation.cs} | 79 ++++----- ...Base.cs.meta => ProviderOperation.cs.meta} | 0 .../ResourceManager/ResourceManager.cs | 101 +++++------- .../ResourcePackage/ResourcePackage.cs | 3 - 46 files changed, 525 insertions(+), 678 deletions(-) delete mode 100644 Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation.meta delete mode 100644 Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs delete mode 100644 Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs.meta delete mode 100644 Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs rename Assets/YooAsset/Runtime/ResourceManager/Loader/{BundleFileLoader.cs => LoadBundleFileOperation.cs} (55%) rename Assets/YooAsset/Runtime/ResourceManager/Loader/{BundleFileLoader.cs.meta => LoadBundleFileOperation.cs.meta} (100%) create mode 100644 Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs rename Assets/YooAsset/Runtime/ResourceManager/Loader/{DependFileLoaders.cs.meta => LoadDependFileOperation.cs.meta} (100%) rename Assets/YooAsset/Runtime/ResourceManager/Provider/{ProviderBase.cs => ProviderOperation.cs} (78%) rename Assets/YooAsset/Runtime/ResourceManager/Provider/{ProviderBase.cs.meta => ProviderOperation.cs.meta} (100%) diff --git a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerBundleListViewer.cs b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerBundleListViewer.cs index 70cd977..953ae61 100644 --- a/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerBundleListViewer.cs +++ b/Assets/YooAsset/Editor/AssetBundleDebugger/VisualViewers/DebuggerBundleListViewer.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs index fa564c4..dcf34c3 100644 --- a/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs +++ b/Assets/YooAsset/Runtime/DiagnosticSystem/DebugBundleInfo.cs @@ -25,7 +25,7 @@ namespace YooAsset /// /// 加载状态 /// - public string Status; + public EOperationStatus Status; public int CompareTo(DebugBundleInfo other) { diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystemHelper.cs b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystemHelper.cs index 16534c6..55a7a9b 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystemHelper.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DownloadSystemHelper.cs @@ -43,7 +43,7 @@ namespace YooAsset #elif UNITY_OPENHARMONY return StringUtility.Format("file://{0}", path); #else - return path; + throw new System.NotImplementedException(); #endif } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs index e17ef8e..e5f52fc 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs @@ -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() diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSDownloadFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSDownloadFileOperation.cs index fefeb0a..e5bf7d2 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSDownloadFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSDownloadFileOperation.cs @@ -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; } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs index 879d726..46460aa 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs @@ -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() diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs index 18e4888..cd4a101 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/internal/VerifyTempFileOperation.cs @@ -85,14 +85,12 @@ namespace YooAsset } } } - public override void WaitForAsyncComplete() + internal override void InternalWaitForAsyncComplete() { while (true) { - // 驱动流程 + // 注意:等待子线程验证文件完毕 InternalOnUpdate(); - - // 完成后退出 if (IsDone) break; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/Operation/DEFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/Operation/DEFSLoadBundleOperation.cs index c5119f1..ed20925 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/Operation/DEFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/Operation/DEFSLoadBundleOperation.cs @@ -20,8 +20,7 @@ namespace YooAsset internal override void InternalOnUpdate() { } - - public override void WaitForAsyncComplete() + internal override void InternalWaitForAsyncComplete() { } public override void AbortDownloadOperation() diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/DefaultUnpackFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/DefaultUnpackFileSystem.cs index ebb6776..edc3ad1 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/DefaultUnpackFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/DefaultUnpackFileSystem.cs @@ -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; - } - } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation.meta b/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation.meta deleted file mode 100644 index ab868c7..0000000 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 59b123e460757694180175970bf28826 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs deleted file mode 100644 index c2bab46..0000000 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs +++ /dev/null @@ -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; - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs.meta b/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs.meta deleted file mode 100644 index 2015b9e..0000000 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultUnpackFileSystem/Operation/DUFSLoadBundleOperation.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 47f6fe3a815d28e49815db7c09c1fa76 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSLoadBundleOperation.cs index ed99327..fe4f8f6 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/Operation/DWFSLoadBundleOperation.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/FileSystem/Operation/Internal/DefaultDownloadFileOperation.cs b/Assets/YooAsset/Runtime/FileSystem/Operation/Internal/DefaultDownloadFileOperation.cs index c92ae04..976ebf6 100644 --- a/Assets/YooAsset/Runtime/FileSystem/Operation/Internal/DefaultDownloadFileOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/Operation/Internal/DefaultDownloadFileOperation.cs @@ -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 } + + /// + /// 是否请求的本地文件 + /// + protected bool IsRequestLocalFile() + { + //TODO : UNITY_STANDALONE_OSX平台目前无法确定 + if (Param.MainURL.StartsWith("file:")) + return true; + if (Param.MainURL.StartsWith("jar:file:")) + return true; + + return false; + } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index ebc63d6..72d62e2 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -175,10 +175,6 @@ namespace YooAsset /// public abstract class InitializeParameters { - /// - /// 自动销毁不再使用的资源提供者 - /// - public bool AutoDestroyAssetProvider = false; } /// diff --git a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs index 29302c5..cd26f90 100644 --- a/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs +++ b/Assets/YooAsset/Runtime/OperationSystem/AsyncOperationBase.cs @@ -8,13 +8,13 @@ namespace YooAsset { public abstract class AsyncOperationBase : IEnumerator, IComparable { - // 用户请求的回调 private Action _callback; - - // 所属包裹 private string _packageName = null; + private int _whileFrame = 1000; - // 是否已经完成 + /// + /// 是否已经完成 + /// internal bool IsFinish = false; /// @@ -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 } /// - /// 等待异步执行完毕 + /// 执行While循环 /// - 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; } /// - /// 清空完成回调 + /// 等待异步执行完毕 /// - protected void ClearCompletedCallback() + public void WaitForAsyncComplete() { - _callback = null; + if (IsDone) + return; + + InternalWaitForAsyncComplete(); } #region 排序接口实现 @@ -159,19 +182,5 @@ namespace YooAsset private TaskCompletionSource _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 } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs index abce66e..cd2ef53 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/AllAssetsHandle.cs @@ -7,7 +7,7 @@ namespace YooAsset { private System.Action _callback; - internal AllAssetsHandle(ProviderBase provider) : base(provider) + internal AllAssetsHandle(ProviderOperation provider) : base(provider) { } internal override void InvokeCallback() diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs index a9125a1..fee4c63 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/AssetHandle.cs @@ -8,7 +8,7 @@ namespace YooAsset { private System.Action _callback; - internal AssetHandle(ProviderBase provider) : base(provider) + internal AssetHandle(ProviderOperation provider) : base(provider) { } internal override void InvokeCallback() diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs index 1a6262c..5638d20 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/HandleBase.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs index 446e2fe..da890a8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs @@ -8,7 +8,7 @@ namespace YooAsset { private System.Action _callback; - internal RawFileHandle(ProviderBase provider) : base(provider) + internal RawFileHandle(ProviderOperation provider) : base(provider) { } internal override void InvokeCallback() diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs index bdeaab3..b26914f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/SceneHandle.cs @@ -7,7 +7,7 @@ namespace YooAsset private System.Action _callback; internal string PackageName { set; get; } - internal SceneHandle(ProviderBase provider) : base(provider) + internal SceneHandle(ProviderOperation provider) : base(provider) { } internal override void InvokeCallback() diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs index 48421a0..449df0b 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/SubAssetsHandle.cs @@ -7,7 +7,7 @@ namespace YooAsset { private System.Action _callback; - internal SubAssetsHandle(ProviderBase provider) : base(provider) + internal SubAssetsHandle(ProviderOperation provider) : base(provider) { } internal override void InvokeCallback() diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs deleted file mode 100644 index c484afd..0000000 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs +++ /dev/null @@ -1,112 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; - -namespace YooAsset -{ - internal class DependFileLoaders - { - /// - /// 依赖的资源包加载器列表 - /// - internal readonly List DependList; - - - public DependFileLoaders(List dpendList) - { - DependList = dpendList; - } - - /// - /// 是否已经完成(无论成功或失败) - /// - public bool IsDone() - { - foreach (var loader in DependList) - { - if (loader.IsDone() == false) - return false; - } - return true; - } - - /// - /// 依赖资源包是否全部加载成功 - /// - public bool IsSucceed() - { - foreach (var loader in DependList) - { - if (loader.Status != BundleFileLoader.EStatus.Succeed) - { - return false; - } - } - return true; - } - - /// - /// 获取某个加载失败的资源包错误信息 - /// - public string GetLastError() - { - foreach (var loader in DependList) - { - if (loader.Status != BundleFileLoader.EStatus.Succeed) - { - return loader.LastError; - } - } - return string.Empty; - } - - /// - /// 主线程等待异步操作完毕 - /// - public void WaitForAsyncComplete() - { - foreach (var loader in DependList) - { - if (loader.IsDone() == false) - loader.WaitForAsyncComplete(); - } - } - - /// - /// 增加引用计数 - /// - public void Reference() - { - foreach (var loader in DependList) - { - loader.Reference(); - } - } - - /// - /// 减少引用计数 - /// - public void Release() - { - foreach (var loader in DependList) - { - loader.Release(); - } - } - - /// - /// 获取资源包的调试信息列表 - /// - internal void GetBundleDebugInfos(List 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); - } - } - } -} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleFileLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs similarity index 55% rename from Assets/YooAsset/Runtime/ResourceManager/Loader/BundleFileLoader.cs rename to Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs index cfc49ba..7274af8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleFileLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs @@ -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 _providers = new List(100); - private readonly List _removeList = new List(100); + private readonly List _providers = new List(100); + private readonly List _removeList = new List(100); private FSLoadBundleOperation _loadBundleOp; - private bool _isDone = false; + private ESteps _steps = ESteps.None; /// /// 资源包文件信息 /// - public BundleInfo MainBundleInfo { private set; get; } - - /// - /// 加载状态 - /// - public EStatus Status { protected set; get; } - - /// - /// 最近的错误信息 - /// - public string LastError { protected set; get; } = string.Empty; + public BundleInfo BundleFileInfo { private set; get; } /// /// 是否已经销毁 @@ -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; } - - /// - /// 轮询更新 - /// - 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; + } } } - - /// - /// 销毁 - /// - 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; + } + } } /// @@ -131,19 +120,27 @@ namespace YooAsset } /// - /// 是否完毕(无论成功或失败) + /// 销毁 /// - 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); } /// /// 是否可以销毁 /// - public bool CanDestroy() + public bool CanDestroyLoader() { - if (IsDone() == false) + if (IsDone == false) return false; return RefCount <= 0; @@ -152,7 +149,7 @@ namespace YooAsset /// /// 添加附属的资源提供者 /// - 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 } } - /// - /// 主线程等待异步操作完毕 - /// - public void WaitForAsyncComplete() - { - while (true) - { - if (_loadBundleOp != null) - { - if (_loadBundleOp.IsDone == false) - _loadBundleOp.WaitForAsyncComplete(); - } - - // 驱动流程 - Update(); - - // 完成后退出 - if (IsDone()) - break; - } - } - /// /// 终止下载任务 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/BundleFileLoader.cs.meta b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/ResourceManager/Loader/BundleFileLoader.cs.meta rename to Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs new file mode 100644 index 0000000..d1c53a4 --- /dev/null +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs @@ -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, + } + + /// + /// 依赖的资源包加载器列表 + /// + internal readonly List Depends; + private ESteps _steps = ESteps.None; + + + internal LoadDependBundleFileOperation(List 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; + } + } + } + + /// + /// 增加引用计数 + /// + public void Reference() + { + foreach (var loader in Depends) + { + loader.Reference(); + } + } + + /// + /// 减少引用计数 + /// + public void Release() + { + foreach (var loader in Depends) + { + loader.Release(); + } + } + + /// + /// 获取资源包的调试信息列表 + /// + internal void GetBundleDebugInfos(List 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); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs.meta b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/ResourceManager/Loader/DependFileLoaders.cs.meta rename to Assets/YooAsset/Runtime/ResourceManager/Loader/LoadDependFileOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs index 1ecc5a2..35741cb 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/InstantiateOperation.cs @@ -71,17 +71,19 @@ namespace YooAsset Status = EOperationStatus.Succeed; } } - - /// - /// 等待异步实例化结束 - /// - 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; + } } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadAllAssetsOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadAllAssetsOperation.cs index b0563fb..659d409 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadAllAssetsOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadAllAssetsOperation.cs @@ -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(); diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs index 9d4a750..c367bf5 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadSceneOperation.cs @@ -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; diff --git a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs index b99b302..097f7e5 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Operation/UnloadUnusedAssetsOperation.cs @@ -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(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; + } + } } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs index 0266506..65299ea 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAllAssetsProvider.cs @@ -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); } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs index 7af1d01..4937f6a 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledAssetProvider.cs @@ -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); } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs index 1ef2a9d..4d56b3f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs @@ -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); } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs index b684bd9..fcf7097 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSceneProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs index 7bc8e72..ad74dcd 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledSubAssetsProvider.cs @@ -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); } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs index 8fea77f..665426c 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/CompletedProvider.cs @@ -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) { } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs index 388a427..1c65608 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAllAssetsProvider.cs @@ -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 result = new List(); - 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 result = new List(); - 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) diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs index 23a6e8e..9d5304f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseAssetProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs index 12e47c9..ec0cf7a 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs index 87e708d..50de189 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSceneProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs index 9f064d5..b141431 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseSubAssetsProvider.cs @@ -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; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs similarity index 78% rename from Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs rename to Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs index cceef4b..fe0543b 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs @@ -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 _handles = new List(); - 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(); } } - /// - /// 等待异步执行完毕 - /// - 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; + } + } } /// /// 销毁资源提供者 /// - 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; } } /// /// 是否可以销毁 /// - public bool CanDestroy() + public bool CanDestroyProvider() { // 注意:在进行资源加载过程时不可以销毁 if (_steps == ESteps.Loading || _steps == ESteps.Checking) @@ -202,10 +207,10 @@ namespace YooAsset /// 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 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 } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs.meta b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs.meta similarity index 100% rename from Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs.meta rename to Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs index b07237b..5307db8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceManager.cs @@ -13,12 +13,10 @@ namespace YooAsset private readonly static Dictionary _sceneHandles = new Dictionary(100); private static long _sceneCreateCount = 0; - internal readonly Dictionary _providerDic = new Dictionary(5000); - internal readonly Dictionary _loaderDic = new Dictionary(5000); - internal readonly List _loaderList = new List(5000); + internal readonly Dictionary _providerDic = new Dictionary(5000); + internal readonly Dictionary _loaderDic = new Dictionary(5000); private bool _simulationOnEditor; - private bool _autoDestroyAssetProvider; private IBundleQuery _bundleQuery; /// @@ -38,24 +36,9 @@ namespace YooAsset public void Initialize(InitializeParameters initializeParameters, IBundleQuery bundleServices) { _simulationOnEditor = initializeParameters is EditorSimulateModeParameters; - _autoDestroyAssetProvider = initializeParameters.AutoDestroyAssetProvider; _bundleQuery = bundleServices; } - /// - /// 更新 - /// - public void Update() - { - foreach (var loader in _loaderList) - { - loader.Update(); - - if (_autoDestroyAssetProvider) - loader.TryDestroyProviders(); - } - } - /// /// 尝试卸载指定资源的资源包(包括依赖资源) /// @@ -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(); } @@ -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(); } 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(); } 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(); } 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(); } 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 CreateDependFileLoaders(AssetInfo assetInfo) + internal LoadDependBundleFileOperation CreateDependFileLoaders(AssetInfo assetInfo) { - BundleInfo[] depends = _bundleQuery.GetDependBundleInfos(assetInfo); - List result = new List(depends.Length); - foreach (var bundleInfo in depends) + BundleInfo[] bundleInfos = _bundleQuery.GetDependBundleInfos(assetInfo); + List depends = new List(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 removeList) + internal void RemoveBundleProviders(List 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; diff --git a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs index 079fad8..5348564 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/ResourcePackage.cs @@ -42,9 +42,6 @@ namespace YooAsset /// internal void UpdatePackage() { - if (_resourceManager != null) - _resourceManager.Update(); - if (_playModeImpl != null) _playModeImpl.UpdatePlayMode(); }