From ddce031ee524b149dfc987384acd546e1c03f559 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jul 2024 15:25:21 +0800 Subject: [PATCH 1/4] =?UTF-8?q?Assetbundle=E5=8A=A0=E8=BD=BD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultBuildinFileSystem.cs | 78 +++++++++++++++++-- .../Operation/DBFSLoadBundleOperation.cs | 5 +- .../DefaultCacheFileSystem.cs | 75 ++++++++++++++++-- .../Operation/DCFSLoadBundleOperation.cs | 6 +- .../DefaultWebFileSystem.cs | 2 +- .../YooAsset/Runtime/InitializeParameters.cs | 37 +++++---- 6 files changed, 169 insertions(+), 34 deletions(-) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs index 1dcfb6c..5f94d88 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs @@ -98,6 +98,11 @@ namespace YooAsset /// 自定义参数:原生文件构建管线 /// public bool RawFileBuildPipeline { private set; get; } = false; + + /// + /// 自定义参数:解密方法类 + /// + public IDecryptionServices DecryptionServices { private set; get; } #endregion @@ -181,18 +186,22 @@ namespace YooAsset public virtual void SetParameter(string name, object value) { - if (name == "FILE_VERIFY_LEVEL") + if (name == FileSystemParameters.FILE_VERIFY_LEVEL) { FileVerifyLevel = (EFileVerifyLevel)value; } - else if (name == "APPEND_FILE_EXTENSION") + else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) { AppendFileExtension = (bool)value; } - else if (name == "RAW_FILE_BUILD_PIPELINE") + else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) { RawFileBuildPipeline = (bool)value; } + else if (name == FileSystemParameters.DECRYPTION_SERVICES) + { + DecryptionServices = (IDecryptionServices)value; + } else { YooLogger.Warning($"Invalid parameter : {name}"); @@ -210,10 +219,10 @@ namespace YooAsset // 创建解压文件系统 var remoteServices = new UnpackRemoteServices(_packageRoot); _unpackFileSystem = new DefaultUnpackFileSystem(); - _unpackFileSystem.SetParameter("REMOTE_SERVICES", remoteServices); - _unpackFileSystem.SetParameter("FILE_VERIFY_LEVEL", FileVerifyLevel); - _unpackFileSystem.SetParameter("APPEND_FILE_EXTENSION", AppendFileExtension); - _unpackFileSystem.SetParameter("RAW_FILE_BUILD_PIPELINE", RawFileBuildPipeline); + _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.OnCreate(packageName, null); } public virtual void OnUpdate() @@ -310,7 +319,60 @@ 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); + } + } + /// /// 记录文件信息 /// diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs index 498e18a..2276f96 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/Operation/DBFSLoadBundleOperation.cs @@ -41,14 +41,13 @@ namespace YooAsset if (_steps == ESteps.LoadBuidlinAssetBundle) { - string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle); if (_isWaitForAsyncComplete) { - Result = AssetBundle.LoadFromFile(filePath); + Result = _fileSystem.LoadAssetBundle(_bundle); } else { - _createRequest = AssetBundle.LoadFromFileAsync(filePath); + _createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); } _steps = ESteps.CheckLoadBuildinResult; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index b084dfa..08d3984 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -97,6 +97,11 @@ namespace YooAsset /// 自定义参数:断点续传下载器关注的错误码 /// public List ResumeDownloadResponseCodes { private set; get; } = null; + + /// + /// 自定义参数:解密方法类 + /// + public IDecryptionServices DecryptionServices { private set; get; } #endregion @@ -208,30 +213,34 @@ namespace YooAsset public virtual void SetParameter(string name, object value) { - if (name == "REMOTE_SERVICES") + if (name == FileSystemParameters.REMOTE_SERVICES) { RemoteServices = (IRemoteServices)value; } - else if (name == "FILE_VERIFY_LEVEL") + else if (name == FileSystemParameters.FILE_VERIFY_LEVEL) { FileVerifyLevel = (EFileVerifyLevel)value; } - else if (name == "APPEND_FILE_EXTENSION") + else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) { AppendFileExtension = (bool)value; } - else if (name == "RAW_FILE_BUILD_PIPELINE") + else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) { RawFileBuildPipeline = (bool)value; } - else if (name == "RESUME_DOWNLOAD_MINMUM_SIZE") + else if (name == FileSystemParameters.RESUME_DOWNLOAD_MINMUM_SIZE) { ResumeDownloadMinimumSize = (long)value; } - else if (name == "RESUME_DOWNLOAD_RESPONSE_CODES") + else if (name == FileSystemParameters.RESUME_DOWNLOAD_RESPONSE_CODES) { ResumeDownloadResponseCodes = (List)value; } + else if (name == FileSystemParameters.DECRYPTION_SERVICES) + { + DecryptionServices = (IDecryptionServices)value; + } else { YooLogger.Warning($"Invalid parameter : {name}"); @@ -528,6 +537,60 @@ namespace YooAsset { return _wrappers.Keys.ToList(); } + + internal AssetBundle LoadAssetBundle(PackageBundle bundle) + { + string filePath = GetCacheFileLoadPath(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); + } + } + + internal AssetBundleCreateRequest LoadAssetBundleAsync(PackageBundle bundle) + { + string filePath = GetCacheFileLoadPath(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); + } + } #endregion } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs index 0f4238d..6b61010 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/Operation/DCFSLoadBundleOperation.cs @@ -78,15 +78,15 @@ namespace YooAsset if (_steps == ESteps.LoadAssetBundle) { - string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); if (_isWaitForAsyncComplete) { - Result = AssetBundle.LoadFromFile(filePath); + Result = _fileSystem.LoadAssetBundle(_bundle); } else { - _createRequest = AssetBundle.LoadFromFileAsync(filePath); + _createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); } + _steps = ESteps.CheckResult; } diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultWebFileSystem/DefaultWebFileSystem.cs index 684984a..eedbf66 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 == "DISABLE_UNITY_WEB_CACHE") + if (name == FileSystemParameters.DISABLE_UNITY_WEB_CACHE) { DisableUnityWebCache = (bool)value; } diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index 25076f7..4fed436 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -55,6 +55,15 @@ 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(); /// @@ -101,11 +110,12 @@ namespace YooAsset /// /// 缓存文件的校验等级 /// 内置文件的根路径 - public static FileSystemParameters CreateDefaultBuildinFileSystemParameters(EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) + 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(FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); return fileSystemParams; } @@ -118,9 +128,9 @@ namespace YooAsset { 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(FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); + fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); return fileSystemParams; } @@ -130,12 +140,13 @@ namespace YooAsset /// 远端资源地址查询服务类 /// 缓存文件的校验等级 /// 文件系统的根目录 - public static FileSystemParameters CreateDefaultCacheFileSystemParameters(IRemoteServices remoteServices, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) + 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(REMOTE_SERVICES, remoteServices); + fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); return fileSystemParams; } @@ -149,10 +160,10 @@ namespace YooAsset { 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(REMOTE_SERVICES, remoteServices); + fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); + fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); + fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); return fileSystemParams; } @@ -164,7 +175,7 @@ namespace YooAsset { string fileSystemClass = typeof(DefaultWebFileSystem).FullName; var fileSystemParams = new FileSystemParameters(fileSystemClass, null); - fileSystemParams.AddParameter("DISABLE_UNITY_WEB_CACHE", disableUnityWebCache); + fileSystemParams.AddParameter(DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); return fileSystemParams; } } From 07d34891efc0f189113c9f6d14c7c39c815e3d43 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jul 2024 17:23:26 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Assetbundle=E5=8A=A0=E8=BD=BD=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E8=A7=A3=E5=AF=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs index 5f94d88..ca0a632 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs @@ -223,6 +223,7 @@ namespace YooAsset _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.OnCreate(packageName, null); } public virtual void OnUpdate() From c6377ce544339c8ffb79fd95d3ce8b1741aa6219 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jul 2024 18:37:17 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=8E=9F=E7=94=9F=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8A=A0=E5=AF=86/=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BuildTasks/TaskEncryption_RFBP.cs | 23 +++++++++++++++ .../BuildTasks/TaskEncryption_RFBP.cs.meta | 11 ++++++++ .../RawFileBuildPipeline.cs | 1 + .../DefaultBuildinFileSystem.cs | 26 +++++++++++++++-- .../DefaultCacheFileSystem.cs | 28 +++++++++++++++++-- .../YooAsset/Runtime/InitializeParameters.cs | 6 ++-- .../Runtime/Services/IDecryptionServices.cs | 7 +++++ 7 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs new file mode 100644 index 0000000..cf4f27c --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs @@ -0,0 +1,23 @@ + +namespace YooAsset.Editor +{ + public class TaskEncryption_RFBP : TaskEncryption, IBuildTask + { + /// + /// 加密文件 + /// + /// + public void 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); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs.meta b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs.meta new file mode 100644 index 0000000..af54524 --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncryption_RFBP.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3e156139dcc25f4c9440ec3d6cb96d2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/RawFileBuildPipeline.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/RawFileBuildPipeline.cs index e220a0b..8358e91 100644 --- a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/RawFileBuildPipeline.cs +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/RawFileBuildPipeline.cs @@ -32,6 +32,7 @@ namespace YooAsset.Editor new TaskPrepare_RFBP(), new TaskGetBuildMap_RFBP(), new TaskBuilding_RFBP(), + new TaskEncryption_RFBP(), new TaskUpdateBundleInfo_RFBP(), new TaskCreateManifest_RFBP(), new TaskCreateReport_RFBP(), diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs index ca0a632..676f28e 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/DefaultBuildinFileSystem.cs @@ -2,6 +2,7 @@ using System.IO; using System.Collections.Generic; using UnityEngine; +using System.Text; namespace YooAsset { @@ -267,7 +268,17 @@ namespace YooAsset return null; string filePath = GetBuildinFileLoadPath(bundle); - return FileUtility.ReadAllBytes(filePath); + var data = FileUtility.ReadAllBytes(filePath); + if (bundle.Encrypted) + { + if (DecryptionServices == null) + { + YooLogger.Error($"DecryptionServices is Null!"); + return null; + } + return DecryptionServices.ReadFileData(data); + } + return data; } public virtual string ReadFileText(PackageBundle bundle) { @@ -278,7 +289,18 @@ namespace YooAsset return null; string filePath = GetBuildinFileLoadPath(bundle); - return FileUtility.ReadAllText(filePath); + var data = FileUtility.ReadAllBytes(filePath); + + if (bundle.Encrypted) + { + if (DecryptionServices == null) + { + YooLogger.Error($"DecryptionServices is Null!"); + return null; + } + data = DecryptionServices.ReadFileData(data); + } + return Encoding.UTF8.GetString(data); } #region 内部方法 diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs index 08d3984..a9c5878 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultCacheFileSystem/DefaultCacheFileSystem.cs @@ -3,6 +3,7 @@ using System.IO; using System.Collections.Generic; using System.Linq; using UnityEngine; +using System.Text; namespace YooAsset { @@ -316,9 +317,19 @@ namespace YooAsset { if (Exists(bundle) == false) return null; - string filePath = GetCacheFileLoadPath(bundle); - return FileUtility.ReadAllBytes(filePath); + var data = FileUtility.ReadAllBytes(filePath); + + if (bundle.Encrypted) + { + if (DecryptionServices == null) + { + YooLogger.Error($"DecryptionServices is Null!"); + return null; + } + return DecryptionServices.ReadFileData(data); + } + return data; } public virtual string ReadFileText(PackageBundle bundle) { @@ -326,7 +337,18 @@ namespace YooAsset return null; string filePath = GetCacheFileLoadPath(bundle); - return FileUtility.ReadAllText(filePath); + var data = FileUtility.ReadAllBytes(filePath); + + if (bundle.Encrypted) + { + if (DecryptionServices == null) + { + YooLogger.Error($"DecryptionServices is Null!"); + return null; + } + data = DecryptionServices.ReadFileData(data); + } + return Encoding.UTF8.GetString(data); } #region 内部方法 diff --git a/Assets/YooAsset/Runtime/InitializeParameters.cs b/Assets/YooAsset/Runtime/InitializeParameters.cs index 4fed436..8ae2684 100644 --- a/Assets/YooAsset/Runtime/InitializeParameters.cs +++ b/Assets/YooAsset/Runtime/InitializeParameters.cs @@ -124,13 +124,14 @@ namespace YooAsset /// /// 缓存文件的校验等级 /// 内置文件的根路径 - public static FileSystemParameters CreateDefaultBuildinRawFileSystemParameters(EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) + 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); return fileSystemParams; } @@ -156,7 +157,7 @@ namespace YooAsset /// 远端资源地址查询服务类 /// 缓存文件的校验等级 /// 文件系统的根目录 - public static FileSystemParameters CreateDefaultCacheRawFileSystemParameters(IRemoteServices remoteServices, EFileVerifyLevel verifyLevel = EFileVerifyLevel.Middle, string rootDirectory = null) + 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); @@ -164,6 +165,7 @@ namespace YooAsset fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); + fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); return fileSystemParams; } diff --git a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs index 9f7f6f5..614625e 100644 --- a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs @@ -34,5 +34,12 @@ namespace YooAsset /// 注意:加载流对象在资源包对象释放的时候会自动释放 /// AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); + + /// + /// 解密字节数据 + /// + /// + /// + public byte[] ReadFileData(byte[] encryptData); } } \ No newline at end of file From 0d5558f29f1bfb9ea84caa6f6f255dec88cdaece Mon Sep 17 00:00:00 2001 From: absences <49706424+absences@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:34:21 +0800 Subject: [PATCH 4/4] Update IDecryptionServices.cs --- Assets/YooAsset/Runtime/Services/IDecryptionServices.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs index 614625e..0fa1c48 100644 --- a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs @@ -40,6 +40,6 @@ namespace YooAsset /// /// /// - public byte[] ReadFileData(byte[] encryptData); + byte[] ReadFileData(byte[] encryptData); } -} \ No newline at end of file +}