From 880d4986185885a93bdf67697b01e4df566dda8e Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 27 Sep 2023 16:54:44 +0800 Subject: [PATCH] update download system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit webgl平台支持下载器 --- .../Loader/AssetBundleWebLoader.cs | 3 - .../Downloader/WebDownloader.cs | 48 ++++----- .../PackageSystem/PlayMode/WebPlayModeImpl.cs | 102 +++++++++++++++++- 3 files changed, 122 insertions(+), 31 deletions(-) diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs index fe81b44..fa64c68 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleWebLoader.cs @@ -95,9 +95,6 @@ namespace YooAsset _steps = ESteps.Done; Status = EStatus.Succeed; } - - // 注意:释放下载句柄 - _downloader.DisposeHandler(); } } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs index 3464457..405c3a7 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/Downloader/WebDownloader.cs @@ -22,10 +22,10 @@ namespace YooAsset Done, } - private bool _keepDownloadHandleLife = false; private DownloadHandlerAssetBundle _downloadhandler; private ESteps _steps = ESteps.None; - + private bool _getAssetBundle = false; + private AssetBundle _cacheAssetBundle; public WebDownloader(BundleInfo bundleInfo, int failedTryAgain, int timeout) : base(bundleInfo, failedTryAgain, timeout) { @@ -34,7 +34,10 @@ namespace YooAsset { if (_steps == ESteps.None) { - _keepDownloadHandleLife = (bool)param[0]; + if (param.Length > 0) + { + _getAssetBundle = (bool)param[0]; + } _steps = ESteps.PrepareDownload; } } @@ -135,11 +138,18 @@ namespace YooAsset _lastCode = 0; } - // 最终释放请求 - DisposeRequest(); + if (_getAssetBundle) + { + _cacheAssetBundle = _downloadhandler.assetBundle; + if (_cacheAssetBundle == null) + { + _lastError = "assetBundle is null"; + _steps = ESteps.TryAgain; + } + } - if (_keepDownloadHandleLife == false) - DisposeHandler(); + // 最终释放请求 + DisposeRequest(); } // 重新尝试下载 @@ -148,7 +158,6 @@ namespace YooAsset if (_failedTryAgain <= 0) { DisposeRequest(); - DisposeHandler(); ReportError(); _status = EStatus.Failed; _steps = ESteps.Done; @@ -175,7 +184,6 @@ namespace YooAsset _lastCode = 0; DisposeRequest(); - DisposeHandler(); } } private void DisposeRequest() @@ -185,6 +193,11 @@ namespace YooAsset _webRequest.Dispose(); _webRequest = null; } + if (_downloadhandler != null) + { + _downloadhandler.Dispose(); + _downloadhandler = null; + } } /// @@ -192,22 +205,7 @@ namespace YooAsset /// public AssetBundle GetAssetBundle() { - if (_downloadhandler != null) - return _downloadhandler.assetBundle; - - return null; - } - - /// - /// 释放下载句柄 - /// - public void DisposeHandler() - { - if (_downloadhandler != null) - { - _downloadhandler.Dispose(); - _downloadhandler = null; - } + return _cacheAssetBundle; } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PackageSystem/PlayMode/WebPlayModeImpl.cs b/Assets/YooAsset/Runtime/PackageSystem/PlayMode/WebPlayModeImpl.cs index 24a846f..2ab2e58 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/PlayMode/WebPlayModeImpl.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/PlayMode/WebPlayModeImpl.cs @@ -33,6 +33,16 @@ namespace YooAsset } // 下载相关 + private List ConvertToDownloadList(List downloadList) + { + List result = new List(downloadList.Count); + foreach (var packageBundle in downloadList) + { + var bundleInfo = ConvertToDownloadInfo(packageBundle); + result.Add(bundleInfo); + } + return result; + } private BundleInfo ConvertToDownloadInfo(PackageBundle packageBundle) { string remoteMainURL = _remoteServices.GetRemoteMainURL(packageBundle.FileName); @@ -84,15 +94,101 @@ namespace YooAsset ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByAll(int downloadingMaxNumber, int failedTryAgain, int timeout) { - return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout); + List downloadList = GetDownloadListByAll(_activeManifest); + var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; } + public List GetDownloadListByAll(PackageManifest manifest) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; + + downloadList.Add(packageBundle); + } + + return ConvertToDownloadList(downloadList); + } + ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByTags(string[] tags, int downloadingMaxNumber, int failedTryAgain, int timeout) { - return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout); + List downloadList = GetDownloadListByTags(_activeManifest, tags); + var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; } + public List GetDownloadListByTags(PackageManifest manifest, string[] tags) + { + List downloadList = new List(1000); + foreach (var packageBundle in manifest.BundleList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; + + // 如果未带任何标记,则统一下载 + if (packageBundle.HasAnyTags() == false) + { + downloadList.Add(packageBundle); + } + else + { + // 查询DLC资源 + if (packageBundle.HasTag(tags)) + { + downloadList.Add(packageBundle); + } + } + } + + return ConvertToDownloadList(downloadList); + } + ResourceDownloaderOperation IPlayModeServices.CreateResourceDownloaderByPaths(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain, int timeout) { - return ResourceDownloaderOperation.CreateEmptyDownloader(downloadingMaxNumber, failedTryAgain, timeout); + List downloadList = GetDownloadListByPaths(_activeManifest, assetInfos); + var operation = new ResourceDownloaderOperation(downloadList, downloadingMaxNumber, failedTryAgain, timeout); + return operation; + } + public List GetDownloadListByPaths(PackageManifest manifest, AssetInfo[] assetInfos) + { + // 获取资源对象的资源包和所有依赖资源包 + List checkList = new List(); + foreach (var assetInfo in assetInfos) + { + if (assetInfo.IsInvalid) + { + YooLogger.Warning(assetInfo.Error); + continue; + } + + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle mainBundle = manifest.GetMainPackageBundle(assetInfo.AssetPath); + if (checkList.Contains(mainBundle) == false) + checkList.Add(mainBundle); + + // 注意:如果清单里未找到资源包会抛出异常! + PackageBundle[] dependBundles = manifest.GetAllDependencies(assetInfo.AssetPath); + foreach (var dependBundle in dependBundles) + { + if (checkList.Contains(dependBundle) == false) + checkList.Add(dependBundle); + } + } + + List downloadList = new List(1000); + foreach (var packageBundle in checkList) + { + // 忽略APP资源 + if (IsBuildinPackageBundle(packageBundle)) + continue; + + downloadList.Add(packageBundle); + } + + return ConvertToDownloadList(downloadList); } ResourceUnpackerOperation IPlayModeServices.CreateResourceUnpackerByAll(int upackingMaxNumber, int failedTryAgain, int timeout)