From b0ce14dc0ea56e95f969f461ad5c82ddc5357589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Mon, 8 Jul 2024 17:36:25 +0800 Subject: [PATCH] update file system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IFileSystem新增ReadFileData方法 --- .../DefaultBuildinFileSystem.cs | 23 ++++++++++ .../Operation/DBFSLoadBundleOperation.cs | 26 +++--------- .../DefaultCacheFileSystem.cs | 19 ++++++++- .../Operation/DCFSLoadBundleOperation.cs | 42 ++++++------------- .../DefaultEditorFileSystem.cs | 9 ++++ .../DefaultWebFileSystem.cs | 9 ++++ .../FileSystem/Interface/IFileSystem.cs | 15 ++++++- .../ResourceManager/Handle/RawFileHandle.cs | 8 ++-- .../Loader/LoadBundleFileOperation.cs | 2 +- .../Provider/BundledRawFileProvider.cs | 4 +- .../Provider/DatabaseRawFileProvider.cs | 2 +- .../Provider/ProviderOperation.cs | 10 ++--- .../Runtime/ResourceManager/RawBundle.cs | 36 ++++++++++++++++ .../Runtime/ResourceManager/RawBundle.cs.meta | 11 +++++ Assets/YooAsset/Runtime/Utility/YooUtility.cs | 2 +- 15 files changed, 150 insertions(+), 68 deletions(-) create mode 100644 Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs create mode 100644 Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs.meta diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs index a710c77e..dc896f0b 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs @@ -254,6 +254,29 @@ namespace YooAsset return false; } + public virtual byte[] ReadFileData(PackageBundle bundle) + { + if (NeedUnpack(bundle)) + return _unpackFileSystem.ReadFileData(bundle); + + if (Exists(bundle) == false) + return null; + + string filePath = GetBuildinFileLoadPath(bundle); + return FileUtility.ReadAllBytes(filePath); + } + public virtual string ReadFileText(PackageBundle bundle) + { + if (NeedUnpack(bundle)) + return _unpackFileSystem.ReadFileText(bundle); + + if (Exists(bundle) == false) + return null; + + string filePath = GetBuildinFileLoadPath(bundle); + return FileUtility.ReadAllText(filePath); + } + #region 内部方法 protected string GetDefaultRoot() { diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs index e5f52fc5..498e18a7 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs @@ -111,7 +111,6 @@ namespace YooAsset { None, LoadBuildinRawBundle, - CheckLoadBuildinResult, Done, } @@ -139,32 +138,17 @@ namespace YooAsset if (_steps == ESteps.LoadBuildinRawBundle) { string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle); - Result = filePath; - _steps = ESteps.CheckLoadBuildinResult; - } - - if (_steps == ESteps.CheckLoadBuildinResult) - { - if (Result != null) + if (File.Exists(filePath)) { - string filePath = Result as string; - if (File.Exists(filePath)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Can not found buildin raw bundle file : {filePath}"; - } + _steps = ESteps.Done; + Result = new RawBundle(_fileSystem, _bundle, filePath); + Status = EOperationStatus.Succeed; } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = $"Failed to load buildin raw bundle file : {_bundle.BundleName}"; + Error = $"Can not found buildin raw bundle file : {filePath}"; } } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index 09a88d7d..b084dfa0 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -303,6 +303,23 @@ namespace YooAsset return Exists(bundle) == false; } + public virtual byte[] ReadFileData(PackageBundle bundle) + { + if (Exists(bundle) == false) + return null; + + string filePath = GetCacheFileLoadPath(bundle); + return FileUtility.ReadAllBytes(filePath); + } + public virtual string ReadFileText(PackageBundle bundle) + { + if (Exists(bundle) == false) + return null; + + string filePath = GetCacheFileLoadPath(bundle); + return FileUtility.ReadAllText(filePath); + } + #region 内部方法 private readonly BufferWriter _sharedBuffer = new BufferWriter(1024); public void WriteInfoFile(string filePath, string dataFileCRC, long dataFileSize) @@ -368,7 +385,7 @@ namespace YooAsset } return filePath; } - public string GetFileLoadPath(PackageBundle bundle) + public string GetCacheFileLoadPath(PackageBundle bundle) { return GetDataFilePath(bundle); } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs index 46460aab..0f4238df 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs @@ -78,7 +78,7 @@ namespace YooAsset if (_steps == ESteps.LoadAssetBundle) { - string filePath = _fileSystem.GetFileLoadPath(_bundle); + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); if (_isWaitForAsyncComplete) { Result = AssetBundle.LoadFromFile(filePath); @@ -122,7 +122,7 @@ namespace YooAsset { // 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。 // 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发! - string filePath = _fileSystem.GetFileLoadPath(_bundle); + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); byte[] fileData = FileUtility.ReadAllBytes(filePath); if (fileData != null && fileData.Length > 0) { @@ -165,7 +165,7 @@ namespace YooAsset while (true) { - if(_downloadFileOp != null) + if (_downloadFileOp != null) _downloadFileOp.WaitForAsyncComplete(); if (ExecuteWhileDone()) @@ -195,8 +195,7 @@ namespace YooAsset None, CheckExist, DownloadFile, - LoadRawBundle, - CheckResult, + LoadCacheRawBundle, Done, } @@ -226,7 +225,7 @@ namespace YooAsset { DownloadProgress = 1f; DownloadedBytes = _bundle.FileSize; - _steps = ESteps.LoadRawBundle; + _steps = ESteps.LoadCacheRawBundle; } else { @@ -249,7 +248,7 @@ namespace YooAsset if (_downloadFileOp.Status == EOperationStatus.Succeed) { - _steps = ESteps.LoadRawBundle; + _steps = ESteps.LoadCacheRawBundle; } else { @@ -259,35 +258,20 @@ namespace YooAsset } } - if (_steps == ESteps.LoadRawBundle) + if (_steps == ESteps.LoadCacheRawBundle) { - string filePath = _fileSystem.GetFileLoadPath(_bundle); - Result = filePath; - _steps = ESteps.CheckResult; - } - - if (_steps == ESteps.CheckResult) - { - if (Result != null) + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); + if (File.Exists(filePath)) { - string filePath = Result as string; - if (File.Exists(filePath)) - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Can not found cache raw bundle file : {filePath}"; - } + _steps = ESteps.Done; + Result = new RawBundle(_fileSystem, _bundle, filePath); + Status = EOperationStatus.Succeed; } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; - Error = $"Failed to load cache raw bundle file : {_bundle.BundleName}"; + Error = $"Can not found cache raw bundle file : {filePath}"; } } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/DefaultEditorFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/DefaultEditorFileSystem.cs index 1ca92b48..f2e4a169 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/DefaultEditorFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultEditorFileSystem/DefaultEditorFileSystem.cs @@ -135,6 +135,15 @@ namespace YooAsset return false; } + public virtual byte[] ReadFileData(PackageBundle bundle) + { + throw new System.NotImplementedException(); + } + public virtual string ReadFileText(PackageBundle bundle) + { + throw new System.NotImplementedException(); + } + #region 内部方法 protected string GetDefaultRoot() { diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs index 8981ecfb..5bbf61db 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs @@ -157,6 +157,15 @@ namespace YooAsset return false; } + public virtual byte[] ReadFileData(PackageBundle bundle) + { + throw new System.NotImplementedException(); + } + public virtual string ReadFileText(PackageBundle bundle) + { + throw new System.NotImplementedException(); + } + #region 内部方法 protected string GetDefaultWebRoot() { diff --git a/Assets/YooAsset/Runtime/FileSystem/Interface/IFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/Interface/IFileSystem.cs index 88e3d33a..a7b52b28 100644 --- a/Assets/YooAsset/Runtime/FileSystem/Interface/IFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/Interface/IFileSystem.cs @@ -38,7 +38,7 @@ namespace YooAsset /// 清空所有的文件 /// FSClearAllBundleFilesOperation ClearAllBundleFilesAsync(); - + /// /// 清空未使用的文件 /// @@ -95,10 +95,21 @@ namespace YooAsset /// 是否需要解压 /// bool NeedUnpack(PackageBundle bundle); - + /// /// 是否需要导入 /// bool NeedImport(PackageBundle bundle); + + + /// + /// 读取文件二进制数据 + /// + byte[] ReadFileData(PackageBundle bundle); + + /// + /// 读取文件文本数据 + /// + string ReadFileText(PackageBundle bundle); } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs index da890a80..19a73be8 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs @@ -72,8 +72,7 @@ namespace YooAsset { if (IsValidWithWarning == false) return null; - string filePath = Provider.RawFilePath; - return FileUtility.ReadAllBytes(filePath); + return Provider.RawBundleObject.ReadFileData(); } /// @@ -83,8 +82,7 @@ namespace YooAsset { if (IsValidWithWarning == false) return null; - string filePath = Provider.RawFilePath; - return FileUtility.ReadAllText(filePath); + return Provider.RawBundleObject.ReadFileText(); } /// @@ -94,7 +92,7 @@ namespace YooAsset { if (IsValidWithWarning == false) return string.Empty; - return Provider.RawFilePath; + return Provider.RawBundleObject.GetFilePath(); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs index 7274af83..5694b264 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Loader/LoadBundleFileOperation.cs @@ -45,7 +45,7 @@ namespace YooAsset public long DownloadedBytes { set; get; } = 0; /// - /// 下载结果 + /// 加载结果 /// public object Result { set; get; } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs index 4d56b3f7..a64acbf9 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs @@ -32,7 +32,7 @@ namespace YooAsset return; } - if (LoadBundleFileOp.Result is string == false) + if (LoadBundleFileOp.Result is RawBundle == false) { string error = "Try load AssetBundle file using load raw file method !"; InvokeCompletion(error, EOperationStatus.Failed); @@ -45,7 +45,7 @@ namespace YooAsset // 2. 检测加载结果 if (_steps == ESteps.Checking) { - RawFilePath = LoadBundleFileOp.Result as string; + RawBundleObject = LoadBundleFileOp.Result as RawBundle; InvokeCompletion(string.Empty, EOperationStatus.Succeed); } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs index ec0cf7a9..830d0088 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/DatabaseRawFileProvider.cs @@ -53,7 +53,7 @@ namespace YooAsset // 2. 检测加载结果 if (_steps == ESteps.Checking) { - RawFilePath = MainAssetInfo.AssetPath; + RawBundleObject = new RawBundle(null, null, MainAssetInfo.AssetPath); InvokeCompletion(string.Empty, EOperationStatus.Succeed); } #endif diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs index fe0543be..33a1efbd 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderOperation.cs @@ -46,16 +46,16 @@ namespace YooAsset /// public UnityEngine.SceneManagement.Scene SceneObject { protected set; get; } + /// + /// 获取的原生对象 + /// + public RawBundle RawBundleObject { protected set; get; } + /// /// 加载的场景名称 /// public string SceneName { protected set; get; } - /// - /// 原生文件路径 - /// - public string RawFilePath { protected set; get; } - /// /// 引用计数 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs b/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs new file mode 100644 index 00000000..5fdfc5b7 --- /dev/null +++ b/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs @@ -0,0 +1,36 @@ + +namespace YooAsset +{ + internal class RawBundle + { + private readonly IFileSystem _fileSystem; + private readonly PackageBundle _packageBundle; + private readonly string _filePath; + + internal RawBundle(IFileSystem fileSystem, PackageBundle packageBundle, string filePath) + { + _fileSystem = fileSystem; + _packageBundle = packageBundle; + _filePath = filePath; + } + + public string GetFilePath() + { + return _filePath; + } + public byte[] ReadFileData() + { + if (_fileSystem != null) + return _fileSystem.ReadFileData(_packageBundle); + else + return FileUtility.ReadAllBytes(_filePath); + } + public string ReadFileText() + { + if (_fileSystem != null) + return _fileSystem.ReadFileText(_packageBundle); + else + return FileUtility.ReadAllText(_filePath); + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs.meta b/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs.meta new file mode 100644 index 00000000..1abf950e --- /dev/null +++ b/Assets/YooAsset/Runtime/ResourceManager/RawBundle.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c6e71c986d2a8c74d981deeed7b5a8ef +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index 8069b23b..0f64cc4a 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -121,7 +121,7 @@ namespace YooAsset public static string ReadAllText(string filePath) { if (File.Exists(filePath) == false) - return string.Empty; + return null; return File.ReadAllText(filePath, Encoding.UTF8); }