From da8d050b6780879e9858aef42a3b8f2c96d4c8b3 Mon Sep 17 00:00:00 2001 From: hevinci Date: Wed, 23 Mar 2022 00:29:19 +0800 Subject: [PATCH] Update AssetSystem --- .../Runtime/AssetSystem/AssetSystem.cs | 12 ++ .../AssetSystem/Loader/AssetBundleLoader.cs | 13 -- .../Operations/RawFileOperation.cs | 180 ++++++++++++++++++ .../Operations/RawFileOperation.cs.meta | 11 ++ 4 files changed, 203 insertions(+), 13 deletions(-) create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs create mode 100644 Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs.meta diff --git a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs index a58c023..4fc688e 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/AssetSystem.cs @@ -127,6 +127,18 @@ namespace YooAsset } + /// + /// 异步加载原生文件 + /// + public static RawFileOperation LoadRawFileAsync(string assetPath, string savePath) + { + string bundleName = BundleServices.GetBundleName(assetPath); + BundleInfo bundleInfo = BundleServices.GetBundleInfo(bundleName); + RawFileOperation operation = new RawFileOperation(bundleInfo, savePath); + OperationSystem.ProcessOperaiton(operation); + return operation; + } + /// /// 异步加载场景 /// diff --git a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs index 1561d11..25572a3 100644 --- a/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs +++ b/Assets/YooAsset/Runtime/AssetSystem/Loader/AssetBundleLoader.cs @@ -53,19 +53,6 @@ namespace YooAsset Status = EStatus.None; } - /// - /// 是否为场景加载器 - /// - public bool IsSceneLoader() - { - foreach (var provider in _providers) - { - if (provider is BundledSceneProvider) - return true; - } - return false; - } - /// /// 添加附属的资源提供者 /// diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs new file mode 100644 index 0000000..6ecec2d --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs @@ -0,0 +1,180 @@ +using System.IO; + +namespace YooAsset +{ + public class RawFileOperation : AsyncOperationBase + { + private enum ESteps + { + None, + Prepare, + DownloadFromWeb, + CheckDownloadFromWeb, + CheckFile, + DownloadFromApk, + CheckDownloadFromApk, + Done, + } + + private readonly BundleInfo _bundleInfo; + private readonly string _savePath; + private ESteps _steps = ESteps.None; + private FileDownloader _fileDownloader; + private UnityWebFileRequester _fileRequester; + + /// + /// 原生文件的存储路径 + /// + public string SavePath + { + get { return _savePath; } + } + + + internal RawFileOperation(BundleInfo bundleInfo, string savePath) + { + _bundleInfo = bundleInfo; + _savePath = savePath; + } + internal override void Start() + { + _steps = ESteps.Prepare; + } + internal override void Update() + { + if (_steps == ESteps.None || _steps == ESteps.Done) + return; + + // 1. 准备工作 + if (_steps == ESteps.Prepare) + { + // 检测加载地址是否为空 + if (string.IsNullOrEmpty(_bundleInfo.LocalPath)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = "Local path is null or empty."; + return; + } + + if (string.IsNullOrEmpty(_bundleInfo.RemoteMainURL)) + _steps = ESteps.CheckFile; + else + _steps = ESteps.DownloadFromWeb; + } + + // 2. 从服务器下载 + if (_steps == ESteps.DownloadFromWeb) + { + int failedTryAgain = int.MaxValue; + _fileDownloader = DownloadSystem.BeginDownload(_bundleInfo, failedTryAgain); + _steps = ESteps.CheckDownloadFromWeb; + } + + // 3. 检测服务器下载结果 + if (_steps == ESteps.CheckDownloadFromWeb) + { + if (_fileDownloader.IsDone() == false) + return; + + if (_fileDownloader.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _fileDownloader.GetLastError(); + } + else + { + // 注意:当文件更新之后,需要删除旧文件 + if (File.Exists(_savePath)) + File.Delete(_savePath); + _steps = ESteps.CheckFile; + } + } + + // 4. 检测文件 + if (_steps == ESteps.CheckFile) + { + // 注意:本地已经存在的文件不保证完整性 + if (File.Exists(_savePath)) + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + return; + } + + if (_bundleInfo.IsBuildinJarFile()) + { + _steps = ESteps.DownloadFromApk; + } + else + { + try + { + File.Copy(_bundleInfo.LocalPath, _savePath, true); + } + catch (System.Exception e) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = e.ToString(); + return; + } + + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + } + + // 5. 从APK拷贝文件 + if (_steps == ESteps.DownloadFromApk) + { + string downloadURL = PathHelper.ConvertToWWWPath(_bundleInfo.LocalPath); + _fileRequester = new UnityWebFileRequester(); + _fileRequester.SendRequest(downloadURL, _savePath); + _steps = ESteps.CheckDownloadFromApk; + } + + // 6. 检测APK拷贝文件结果 + if (_steps == ESteps.CheckDownloadFromApk) + { + if (_fileRequester.IsDone() == false) + return; + + if (_fileRequester.HasError()) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = _fileRequester.GetError(); + } + else + { + _steps = ESteps.Done; + Status = EOperationStatus.Succeed; + } + + _fileRequester.Dispose(); + } + } + + /// + /// 获取原生文件的二进制数据 + /// + public byte[] GetFileData() + { + if (File.Exists(_savePath) == false) + return null; + return File.ReadAllBytes(_savePath); + } + + /// + /// 获取原生文件的文本数据 + /// + public string GetFileText() + { + if (File.Exists(_savePath) == false) + return string.Empty; + return File.ReadAllText(_savePath, System.Text.Encoding.UTF8); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs.meta b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs.meta new file mode 100644 index 0000000..8c12e40 --- /dev/null +++ b/Assets/YooAsset/Runtime/AssetSystem/Operations/RawFileOperation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e26f14db9addb4c49b4f0f520bf75d9d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: