From d017688416fdfe6539fda2614a56b0360551e1bb Mon Sep 17 00:00:00 2001 From: hevinci Date: Sat, 3 Aug 2024 18:43:12 +0800 Subject: [PATCH] feat : the bundle file decryption MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 资源文件解密 --- .../BuildTasks/TaskEncryption_BBP.cs | 7 +- .../BuildTasks/TaskEncryption_RFBP.cs | 7 +- .../BuildTasks/TaskEncryption_SBP.cs | 7 +- .../DefaultBuildinFileSystem.cs | 156 +++++++++--------- .../Operation/DBFSLoadBundleOperation.cs | 56 ++++++- .../DefaultCacheFileSystem.cs | 133 +++++++-------- .../Operation/DCFSLoadBundleOperation.cs | 97 +++++++---- .../DefaultWebFileSystem.cs | 2 +- .../FileSystem/FileSystemParametersDefine.cs | 15 ++ .../FileSystemParametersDefine.cs.meta | 11 ++ .../YooAsset/Runtime/InitializeParameters.cs | 43 +++-- .../Runtime/Services/IDecryptionServices.cs | 11 +- 12 files changed, 319 insertions(+), 226 deletions(-) create mode 100644 Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs create mode 100644 Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BuiltinBuildPipeline/BuildTasks/TaskEncryption_BBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BuiltinBuildPipeline/BuildTasks/TaskEncryption_BBP.cs index 0248499..ff2cf5b 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BuiltinBuildPipeline/BuildTasks/TaskEncryption_BBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/BuiltinBuildPipeline/BuildTasks/TaskEncryption_BBP.cs @@ -1,9 +1,4 @@ -using System; -using System.Linq; -using System.IO; -using System.Collections; -using System.Collections.Generic; - + namespace YooAsset.Editor { public class TaskEncryption_BBP : TaskEncryption, IBuildTask diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs index cf4f27c..6e40676 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs @@ -3,17 +3,12 @@ namespace YooAsset.Editor { public class TaskEncryption_RFBP : TaskEncryption, IBuildTask { - /// - /// 加密文件 - /// - /// - public void Run(BuildContext context) + void IBuildTask.Run(BuildContext context) { var buildParameters = context.GetContextObject(); var buildMapContext = context.GetContextObject(); var buildMode = buildParameters.Parameters.BuildMode; - if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild) { EncryptingBundleFiles(buildParameters, buildMapContext); diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/ScriptableBuildPipeline/BuildTasks/TaskEncryption_SBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/ScriptableBuildPipeline/BuildTasks/TaskEncryption_SBP.cs index 8b1ffae..27ef511 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/ScriptableBuildPipeline/BuildTasks/TaskEncryption_SBP.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/ScriptableBuildPipeline/BuildTasks/TaskEncryption_SBP.cs @@ -1,9 +1,4 @@ -using System; -using System.Linq; -using System.IO; -using System.Collections; -using System.Collections.Generic; - + namespace YooAsset.Editor { public class TaskEncryption_SBP : TaskEncryption, IBuildTask diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs index 676f28e..2db0e01 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs @@ -2,7 +2,6 @@ using System.IO; using System.Collections.Generic; using UnityEngine; -using System.Text; namespace YooAsset { @@ -178,8 +177,11 @@ namespace YooAsset if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream)) { - managedStream.Close(); - managedStream.Dispose(); + if (managedStream != null) + { + managedStream.Close(); + managedStream.Dispose(); + } _loadedStream.Remove(bundle.BundleGUID); } } @@ -187,19 +189,19 @@ namespace YooAsset public virtual void SetParameter(string name, object value) { - if (name == FileSystemParameters.FILE_VERIFY_LEVEL) + if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL) { FileVerifyLevel = (EFileVerifyLevel)value; } - else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) + else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION) { AppendFileExtension = (bool)value; } - else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) + else if (name == FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE) { RawFileBuildPipeline = (bool)value; } - else if (name == FileSystemParameters.DECRYPTION_SERVICES) + else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES) { DecryptionServices = (IDecryptionServices)value; } @@ -220,11 +222,11 @@ namespace YooAsset // 创建解压文件系统 var remoteServices = new UnpackRemoteServices(_packageRoot); _unpackFileSystem = new DefaultUnpackFileSystem(); - _unpackFileSystem.SetParameter(FileSystemParameters.REMOTE_SERVICES, remoteServices); - _unpackFileSystem.SetParameter(FileSystemParameters.FILE_VERIFY_LEVEL, FileVerifyLevel); - _unpackFileSystem.SetParameter(FileSystemParameters.APPEND_FILE_EXTENSION, AppendFileExtension); - _unpackFileSystem.SetParameter(FileSystemParameters.RAW_FILE_BUILD_PIPELINE, RawFileBuildPipeline); - _unpackFileSystem.SetParameter(FileSystemParameters.DECRYPTION_SERVICES, DecryptionServices); + _unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); + _unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel); + _unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension); + _unpackFileSystem.SetParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, RawFileBuildPipeline); + _unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices); _unpackFileSystem.OnCreate(packageName, null); } public virtual void OnUpdate() @@ -267,18 +269,28 @@ namespace YooAsset if (Exists(bundle) == false) return null; - string filePath = GetBuildinFileLoadPath(bundle); - var data = FileUtility.ReadAllBytes(filePath); if (bundle.Encrypted) { if (DecryptionServices == null) { - YooLogger.Error($"DecryptionServices is Null!"); + YooLogger.Error($"The {nameof(IDecryptionServices)} is null !"); return null; } - return DecryptionServices.ReadFileData(data); + + string filePath = GetBuildinFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + return DecryptionServices.ReadFileData(fileInfo); + } + else + { + string filePath = GetBuildinFileLoadPath(bundle); + return FileUtility.ReadAllBytes(filePath); } - return data; } public virtual string ReadFileText(PackageBundle bundle) { @@ -288,19 +300,28 @@ namespace YooAsset if (Exists(bundle) == false) return null; - string filePath = GetBuildinFileLoadPath(bundle); - var data = FileUtility.ReadAllBytes(filePath); - if (bundle.Encrypted) { if (DecryptionServices == null) { - YooLogger.Error($"DecryptionServices is Null!"); + YooLogger.Error($"The {nameof(IDecryptionServices)} is null !"); return null; } - data = DecryptionServices.ReadFileData(data); + + string filePath = GetBuildinFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + return DecryptionServices.ReadFileText(fileInfo); + } + else + { + string filePath = GetBuildinFileLoadPath(bundle); + return FileUtility.ReadAllText(filePath); } - return Encoding.UTF8.GetString(data); } #region 内部方法 @@ -342,59 +363,6 @@ namespace YooAsset string rootPath = PathUtility.Combine(Application.dataPath, "StreamingAssets", YooAssetSettingsData.Setting.DefaultYooFolderName); return PathUtility.Combine(rootPath, PackageName); } - public AssetBundle LoadAssetBundle(PackageBundle bundle) - { - string filePath = GetBuildinFileLoadPath(bundle); - - if (bundle.Encrypted) - { - if (DecryptionServices == null) - { - YooLogger.Error($"DecryptionServices is Null!"); - return null; - } - else - { - return DecryptionServices.LoadAssetBundle(new DecryptFileInfo() - { - BundleName = bundle.BundleName, - FileLoadCRC = bundle.UnityCRC, - FileLoadPath = filePath, - }, out _); - } - } - else - { - return AssetBundle.LoadFromFile(filePath); - } - } - - public AssetBundleCreateRequest LoadAssetBundleAsync(PackageBundle bundle) - { - string filePath = GetBuildinFileLoadPath(bundle); - - if (bundle.Encrypted) - { - if (DecryptionServices == null) - { - YooLogger.Error($"DecryptionServices is Empty!"); - return null; - } - else - { - return DecryptionServices.LoadAssetBundleAsync(new DecryptFileInfo() - { - BundleName = bundle.BundleName, - FileLoadCRC = bundle.UnityCRC, - FileLoadPath = filePath, - }, out _); - } - } - else - { - return AssetBundle.LoadFromFileAsync(filePath); - } - } /// /// 记录文件信息 @@ -418,6 +386,42 @@ namespace YooAsset { return _unpackFileSystem.InitializeFileSystemAsync(); } + + /// + /// 加载加密资源文件 + /// + public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle) + { + string filePath = GetBuildinFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + + var assetBundle = DecryptionServices.LoadAssetBundle(fileInfo, out var managedStream); + _loadedStream.Add(bundle.BundleGUID, managedStream); + return assetBundle; + } + + /// + /// 加载加密资源文件 + /// + public AssetBundleCreateRequest LoadEncryptedAssetBundleAsync(PackageBundle bundle) + { + string filePath = GetBuildinFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + + var createRequest = DecryptionServices.LoadAssetBundleAsync(fileInfo, out var managedStream); + _loadedStream.Add(bundle.BundleGUID, managedStream); + return createRequest; + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs index 2276f96..7acac55 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs @@ -11,8 +11,8 @@ namespace YooAsset private enum ESteps { None, - LoadBuidlinAssetBundle, - CheckLoadBuildinResult, + LoadAssetBundle, + CheckResult, Done, } @@ -32,27 +32,56 @@ namespace YooAsset { DownloadProgress = 1f; DownloadedBytes = _bundle.FileSize; - _steps = ESteps.LoadBuidlinAssetBundle; + _steps = ESteps.LoadAssetBundle; } internal override void InternalOnUpdate() { if (_steps == ESteps.None || _steps == ESteps.Done) return; - if (_steps == ESteps.LoadBuidlinAssetBundle) + if (_steps == ESteps.LoadAssetBundle) { + if (_bundle.Encrypted) + { + if (_fileSystem.DecryptionServices == null) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"The {nameof(IDecryptionServices)} is null !"; + YooLogger.Error(Error); + return; + } + } + if (_isWaitForAsyncComplete) { - Result = _fileSystem.LoadAssetBundle(_bundle); + if (_bundle.Encrypted) + { + Result = _fileSystem.LoadEncryptedAssetBundle(_bundle); + } + else + { + string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle); + Result = AssetBundle.LoadFromFile(filePath); + } } else { - _createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); + if (_bundle.Encrypted) + { + _createRequest = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle); + } + else + { + string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle); + _createRequest = AssetBundle.LoadFromFileAsync(filePath); + } } - _steps = ESteps.CheckLoadBuildinResult; + + _steps = ESteps.CheckResult; } - if (_steps == ESteps.CheckLoadBuildinResult) + if (_steps == ESteps.CheckResult) { if (_createRequest != null) { @@ -74,12 +103,22 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Succeed; + return; + } + + if (_bundle.Encrypted) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed to load encrypted buildin asset bundle file : {_bundle.BundleName}"; + YooLogger.Error(Error); } else { _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}"; + YooLogger.Error(Error); } } } @@ -148,6 +187,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = $"Can not found buildin raw bundle file : {filePath}"; + YooLogger.Error(Error); } } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index a9c5878..9bc520f 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -3,7 +3,6 @@ using System.IO; using System.Collections.Generic; using System.Linq; using UnityEngine; -using System.Text; namespace YooAsset { @@ -206,39 +205,42 @@ namespace YooAsset if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream)) { - managedStream.Close(); - managedStream.Dispose(); + if (managedStream != null) + { + managedStream.Close(); + managedStream.Dispose(); + } _loadedStream.Remove(bundle.BundleGUID); } } public virtual void SetParameter(string name, object value) { - if (name == FileSystemParameters.REMOTE_SERVICES) + if (name == FileSystemParametersDefine.REMOTE_SERVICES) { RemoteServices = (IRemoteServices)value; } - else if (name == FileSystemParameters.FILE_VERIFY_LEVEL) + else if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL) { FileVerifyLevel = (EFileVerifyLevel)value; } - else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) + else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION) { AppendFileExtension = (bool)value; } - else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) + else if (name == FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE) { RawFileBuildPipeline = (bool)value; } - else if (name == FileSystemParameters.RESUME_DOWNLOAD_MINMUM_SIZE) + else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_MINMUM_SIZE) { ResumeDownloadMinimumSize = (long)value; } - else if (name == FileSystemParameters.RESUME_DOWNLOAD_RESPONSE_CODES) + else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_RESPONSE_CODES) { ResumeDownloadResponseCodes = (List)value; } - else if (name == FileSystemParameters.DECRYPTION_SERVICES) + else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES) { DecryptionServices = (IDecryptionServices)value; } @@ -317,38 +319,57 @@ namespace YooAsset { if (Exists(bundle) == false) return null; - string filePath = GetCacheFileLoadPath(bundle); - var data = FileUtility.ReadAllBytes(filePath); if (bundle.Encrypted) { if (DecryptionServices == null) { - YooLogger.Error($"DecryptionServices is Null!"); + YooLogger.Error($"The {nameof(IDecryptionServices)} is null !"); return null; } - return DecryptionServices.ReadFileData(data); + + string filePath = GetCacheFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + return DecryptionServices.ReadFileData(fileInfo); + } + else + { + string filePath = GetCacheFileLoadPath(bundle); + return FileUtility.ReadAllBytes(filePath); } - return data; } public virtual string ReadFileText(PackageBundle bundle) { if (Exists(bundle) == false) return null; - string filePath = GetCacheFileLoadPath(bundle); - var data = FileUtility.ReadAllBytes(filePath); - if (bundle.Encrypted) { if (DecryptionServices == null) { - YooLogger.Error($"DecryptionServices is Null!"); + YooLogger.Error($"The {nameof(IDecryptionServices)} is null !"); return null; } - data = DecryptionServices.ReadFileData(data); + + string filePath = GetCacheFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; + return DecryptionServices.ReadFileText(fileInfo); + } + else + { + string filePath = GetCacheFileLoadPath(bundle); + return FileUtility.ReadAllText(filePath); } - return Encoding.UTF8.GetString(data); } #region 内部方法 @@ -560,58 +581,40 @@ namespace YooAsset return _wrappers.Keys.ToList(); } - internal AssetBundle LoadAssetBundle(PackageBundle bundle) + /// + /// 加载加密资源文件 + /// + public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; - if (bundle.Encrypted) - { - if (DecryptionServices == null) - { - YooLogger.Error($"DecryptionServices is Null!"); - return null; - } - else - { - return DecryptionServices.LoadAssetBundle(new DecryptFileInfo() - { - BundleName = bundle.BundleName, - FileLoadCRC = bundle.UnityCRC, - FileLoadPath = filePath, - }, out _); - } - } - else - { - return AssetBundle.LoadFromFile(filePath); - } + var assetBundle = DecryptionServices.LoadAssetBundle(fileInfo, out var managedStream); + _loadedStream.Add(bundle.BundleGUID, managedStream); + return assetBundle; } - internal AssetBundleCreateRequest LoadAssetBundleAsync(PackageBundle bundle) + /// + /// 加载加密资源文件 + /// + public AssetBundleCreateRequest LoadEncryptedAssetBundleAsync(PackageBundle bundle) { string filePath = GetCacheFileLoadPath(bundle); + var fileInfo = new DecryptFileInfo() + { + BundleName = bundle.BundleName, + FileLoadCRC = bundle.UnityCRC, + FileLoadPath = filePath, + }; - if (bundle.Encrypted) - { - if (DecryptionServices == null) - { - YooLogger.Error($"DecryptionServices is Empty!"); - return null; - } - else - { - return DecryptionServices.LoadAssetBundleAsync(new DecryptFileInfo() - { - BundleName = bundle.BundleName, - FileLoadCRC = bundle.UnityCRC, - FileLoadPath = filePath, - }, out _); - } - } - else - { - return AssetBundle.LoadFromFileAsync(filePath); - } + var createRequest = DecryptionServices.LoadAssetBundleAsync(fileInfo, out var managedStream); + _loadedStream.Add(bundle.BundleGUID, managedStream); + return createRequest; } #endregion } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs index 6b61010..9124ab7 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs @@ -78,13 +78,41 @@ namespace YooAsset if (_steps == ESteps.LoadAssetBundle) { + if (_bundle.Encrypted) + { + if (_fileSystem.DecryptionServices == null) + { + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"The {nameof(IDecryptionServices)} is null !"; + YooLogger.Error(Error); + return; + } + } + if (_isWaitForAsyncComplete) { - Result = _fileSystem.LoadAssetBundle(_bundle); + if (_bundle.Encrypted) + { + Result = _fileSystem.LoadEncryptedAssetBundle(_bundle); + } + else + { + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); + Result = AssetBundle.LoadFromFile(filePath); + } } else { - _createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); + if (_bundle.Encrypted) + { + _createRequest = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle); + } + else + { + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); + _createRequest = AssetBundle.LoadFromFileAsync(filePath); + } } _steps = ESteps.CheckResult; @@ -112,51 +140,59 @@ namespace YooAsset { _steps = ESteps.Done; Status = EOperationStatus.Succeed; + return; } - else + + // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。 + // 说明:在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性! + EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle); + if (verifyResult == EFileVerifyResult.Succeed) { - // 注意:当缓存文件的校验等级为Low的时候,并不能保证缓存文件的完整性。 - // 说明:在AssetBundle文件加载失败的情况下,我们需要重新验证文件的完整性! - EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle); - if (verifyResult == EFileVerifyResult.Succeed) + if (_bundle.Encrypted) { - // 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。 - // 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发! - string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); - byte[] fileData = FileUtility.ReadAllBytes(filePath); - if (fileData != null && fileData.Length > 0) + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed to load encrypted asset bundle file : {_bundle.BundleName}"; + YooLogger.Error(Error); + return; + } + + // 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。 + // 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发! + string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); + byte[] fileData = FileUtility.ReadAllBytes(filePath); + if (fileData != null && fileData.Length > 0) + { + Result = AssetBundle.LoadFromMemory(fileData); + if (Result == null) { - Result = AssetBundle.LoadFromMemory(fileData); - if (Result == null) - { - _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Failed to load assetBundle from memory : {_bundle.BundleName}"; - YooLogger.Error(Error); - } - else - { - _steps = ESteps.Done; - Status = EOperationStatus.Succeed; - } + _steps = ESteps.Done; + Status = EOperationStatus.Failed; + Error = $"Failed to load asset bundle from memory : {_bundle.BundleName}"; + YooLogger.Error(Error); } else { _steps = ESteps.Done; - Status = EOperationStatus.Failed; - Error = $"Failed to read assetBundle file bytes : {_bundle.BundleName}"; - YooLogger.Error(Error); + Status = EOperationStatus.Succeed; } } else { _steps = ESteps.Done; - _fileSystem.DeleteCacheFile(_bundle.BundleGUID); Status = EOperationStatus.Failed; - Error = $"Find corrupted file and delete the file : {_bundle.BundleName}"; + Error = $"Failed to read asset bundle file bytes : {_bundle.BundleName}"; YooLogger.Error(Error); } } + else + { + _steps = ESteps.Done; + _fileSystem.DeleteCacheFile(_bundle.BundleGUID); + Status = EOperationStatus.Failed; + Error = $"Find corrupted asset bundle file and delete : {_bundle.BundleName}"; + YooLogger.Error(Error); + } } } internal override void InternalWaitForAsyncComplete() @@ -272,6 +308,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = $"Can not found cache raw bundle file : {filePath}"; + YooLogger.Error(Error); } } } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs index eedbf66..f2da0eb 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs @@ -114,7 +114,7 @@ namespace YooAsset public virtual void SetParameter(string name, object value) { - if (name == FileSystemParameters.DISABLE_UNITY_WEB_CACHE) + if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE) { DisableUnityWebCache = (bool)value; } diff --git a/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs new file mode 100644 index 0000000..e197bc3 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs @@ -0,0 +1,15 @@ + +namespace YooAsset +{ + internal class FileSystemParametersDefine + { + public const string FILE_VERIFY_LEVEL = "FILE_VERIFY_LEVEL"; + public const string REMOTE_SERVICES = "REMOTE_SERVICES"; + public const string DECRYPTION_SERVICES = "DECRYPTION_SERVICES"; + public const string APPEND_FILE_EXTENSION = "APPEND_FILE_EXTENSION"; + public const string RAW_FILE_BUILD_PIPELINE = "RAW_FILE_BUILD_PIPELINE"; + public const string DISABLE_UNITY_WEB_CACHE = "DISABLE_UNITY_WEB_CACHE"; + public const string RESUME_DOWNLOAD_MINMUM_SIZE = "RESUME_DOWNLOAD_MINMUM_SIZE"; + public const string RESUME_DOWNLOAD_RESPONSE_CODES = "RESUME_DOWNLOAD_RESPONSE_CODES"; + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs.meta b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs.meta new file mode 100644 index 0000000..e2b7b06 --- /dev/null +++ b/Assets/YooAsset/Runtime/FileSystem/FileSystemParametersDefine.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6b83a734e8baddb46aa731ee31fe988d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index 8ae2684..7490073 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -55,15 +55,6 @@ namespace YooAsset /// public class FileSystemParameters { - public const string FILE_VERIFY_LEVEL = "FILE_VERIFY_LEVEL"; - public const string DECRYPTION_SERVICES = "DECRYPTION_SERVICES"; - public const string APPEND_FILE_EXTENSION = "APPEND_FILE_EXTENSION"; - public const string RAW_FILE_BUILD_PIPELINE = "RAW_FILE_BUILD_PIPELINE"; - public const string REMOTE_SERVICES = "REMOTE_SERVICES"; - public const string DISABLE_UNITY_WEB_CACHE = "DISABLE_UNITY_WEB_CACHE"; - public const string RESUME_DOWNLOAD_MINMUM_SIZE = "RESUME_DOWNLOAD_MINMUM_SIZE"; - public const string RESUME_DOWNLOAD_RESPONSE_CODES = "RESUME_DOWNLOAD_RESPONSE_CODES"; - internal Dictionary CreateParameters = new Dictionary(); /// @@ -108,30 +99,32 @@ namespace YooAsset /// /// 创建默认的内置文件系统参数 /// + /// 加密文件解密服务类 /// 缓存文件的校验等级 /// 内置文件的根路径 public static FileSystemParameters CreateDefaultBuildinFileSystemParameters(IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) { string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); - fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); - fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel); return fileSystemParams; } /// /// 创建默认的内置文件系统参数(原生文件) /// + /// 加密文件解密服务类 /// 缓存文件的校验等级 /// 内置文件的根路径 public static FileSystemParameters CreateDefaultBuildinRawFileSystemParameters(IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) { string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); - fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); - fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); - fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); - fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true); + fileSystemParams.AddParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, true); return fileSystemParams; } @@ -139,15 +132,16 @@ namespace YooAsset /// 创建默认的缓存文件系统参数 /// /// 远端资源地址查询服务类 + /// 加密文件解密服务类 /// 缓存文件的校验等级 /// 文件系统的根目录 public static FileSystemParameters CreateDefaultCacheFileSystemParameters(IRemoteServices remoteServices, IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) { string fileSystemClass = typeof(DefaultCacheFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); - fileSystemParams.AddParameter(REMOTE_SERVICES, remoteServices); - fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); - fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel); return fileSystemParams; } @@ -155,17 +149,18 @@ namespace YooAsset /// 创建默认的缓存文件系统参数(原生文件) /// /// 远端资源地址查询服务类 + /// 加密文件解密服务类 /// 缓存文件的校验等级 /// 文件系统的根目录 public static FileSystemParameters CreateDefaultCacheRawFileSystemParameters(IRemoteServices remoteServices, IDecryptionServices decryptionServices = null, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) { string fileSystemClass = typeof(DefaultCacheFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); - fileSystemParams.AddParameter(REMOTE_SERVICES, remoteServices); - fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); - fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); - fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); - fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices); + fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true); + fileSystemParams.AddParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, true); return fileSystemParams; } @@ -177,7 +172,7 @@ namespace YooAsset { string fileSystemClass = typeof(DefaultWebFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, null); - fileSystemParams.AddParameter(DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); + fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); return fileSystemParams; } } diff --git a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs index 0fa1c48..bf58ecf 100644 --- a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs @@ -36,10 +36,13 @@ namespace YooAsset AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); /// - /// 解密字节数据 + /// 获取解密的字节数据 /// - /// - /// - byte[] ReadFileData(byte[] encryptData); + byte[] ReadFileData(DecryptFileInfo fileInfo); + + /// + /// 获取解密的文本数据 + /// + string ReadFileText(DecryptFileInfo fileInfo); } }