From c6377ce544339c8ffb79fd95d3ce8b1741aa6219 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 27 Jul 2024 18:37:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=9F=E7=94=9F=E6=96=87=E4=BB=B6=E5=8A=A0?= =?UTF-8?q?=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