From 6da7a8cff27c212bd54b03e30b8c9ef21cf29e77 Mon Sep 17 00:00:00 2001 From: MichaelO Date: Sat, 11 May 2024 20:14:11 +0800 Subject: [PATCH] added RawFile can support encryption --- .../BuildTasks/TaskEncription_RFBP.cs | 23 +++++++++++++++++++ .../BuildTasks/TaskEncription_RFBP.cs.meta | 11 +++++++++ .../RawFileBuildPipeline.cs | 1 + .../ResourceManager/Handle/RawFileHandle.cs | 13 ++++++++--- .../Provider/BundledRawFileProvider.cs | 1 + .../ResourceManager/Provider/ProviderBase.cs | 5 ++++ .../Runtime/ResourceManager/ResourceLoader.cs | 21 +++++++++++++++++ .../Runtime/ResourcePackage/BundleInfo.cs | 7 ++++++ .../Runtime/Services/IDecryptionServices.cs | 7 ++++++ Assets/YooAsset/Runtime/Utility/YooUtility.cs | 12 ++++++++++ 10 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs create mode 100644 Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs.meta diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs new file mode 100644 index 00000000..0a9f366e --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs @@ -0,0 +1,23 @@ +using System; +using System.Linq; +using System.IO; +using System.Collections; +using System.Collections.Generic; + +namespace YooAsset.Editor +{ + public class TaskEncryption_RFBP : TaskEncryption, IBuildTask + { + 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); + } + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs.meta b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs.meta new file mode 100644 index 00000000..5dade931 --- /dev/null +++ b/Assets/YooAsset/Editor/AssetBundleBuilder/BuildPipeline/RawFileBuildPipeline/BuildTasks/TaskEncription_RFBP.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4401997215a8ab040893fbf62435db93 +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 e220a0be..8358e918 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/ResourceManager/Handle/RawFileHandle.cs b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs index 446e2fe3..b6a09454 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Handle/RawFileHandle.cs @@ -64,7 +64,6 @@ namespace YooAsset this.ReleaseInternal(); } - /// /// 获取原生文件的二进制数据 /// @@ -73,7 +72,11 @@ namespace YooAsset if (IsValidWithWarning == false) return null; string filePath = Provider.RawFilePath; - return FileUtility.ReadAllBytes(filePath); + if (Provider.RawFileInfo == null || + Provider.RawFileInfo.Bundle.Encrypted == false) + return FileUtility.ReadAllBytes(filePath); + else + return Provider.RawFileInfo.LoadRawFile(filePath); } /// @@ -84,7 +87,11 @@ namespace YooAsset if (IsValidWithWarning == false) return null; string filePath = Provider.RawFilePath; - return FileUtility.ReadAllText(filePath); + if (Provider.RawFileInfo == null || + Provider.RawFileInfo.Bundle.Encrypted == false) + return FileUtility.ReadAllText(filePath); + else + return FileUtility.BytesToText(Provider.RawFileInfo.LoadRawFile(filePath)); } /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs index 4a104a83..0900fdb1 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/BundledRawFileProvider.cs @@ -45,6 +45,7 @@ namespace YooAsset if (_steps == ESteps.Checking) { RawFilePath = OwnerBundle.FileLoadPath; + RawFileInfo = OwnerBundle.MainBundleInfo; InvokeCompletion(string.Empty, EOperationStatus.Succeed); } } diff --git a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs index 5a54f112..a3211b1f 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/Provider/ProviderBase.cs @@ -57,6 +57,11 @@ namespace YooAsset /// public string RawFilePath { protected set; get; } + /// + /// 原生文件信息 + /// + public BundleInfo RawFileInfo { protected set; get; } + /// /// 引用计数 /// diff --git a/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs b/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs index 029c0b7e..81d6e023 100644 --- a/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs +++ b/Assets/YooAsset/Runtime/ResourceManager/ResourceLoader.cs @@ -14,6 +14,27 @@ namespace YooAsset _delivery = delivery; } + /// + /// 同步加载原生数据对象 + /// + /// + /// + /// + public byte[] LoadRawFile(BundleInfo bundleInfo, string fileLoadPath) + { + if (_decryption == null) + { + YooLogger.Error($"{nameof(IDecryptionServices)} is null ! when load raw file {bundleInfo.Bundle.BundleName}!"); + return null; + } + + DecryptFileInfo fileInfo = new DecryptFileInfo(); + fileInfo.BundleName = bundleInfo.Bundle.BundleName; + fileInfo.FileLoadPath = fileLoadPath; + fileInfo.ConentCRC = bundleInfo.Bundle.UnityCRC; + return _decryption.LoadRawFileData(fileInfo); + } + /// /// 同步加载资源包对象 /// diff --git a/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs b/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs index 5a0dcd68..e56abad6 100644 --- a/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs +++ b/Assets/YooAsset/Runtime/ResourcePackage/BundleInfo.cs @@ -146,6 +146,13 @@ namespace YooAsset } #endregion + #region RawFile + internal byte[] LoadRawFile(string fileLoadPath) + { + return _assist.Loader.LoadRawFile(this, fileLoadPath); + } + #endregion + #region AssetBundle internal AssetBundle LoadAssetBundle(string fileLoadPath, out Stream managedStream) { diff --git a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs index a85877e1..1322a560 100644 --- a/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs +++ b/Assets/YooAsset/Runtime/Services/IDecryptionServices.cs @@ -40,5 +40,12 @@ namespace YooAsset /// 注意:加载流对象在资源包对象释放的时候会自动释放 /// AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); + + /// + /// 同步方式获取解密原生文件的二进制数据 + /// + /// + /// + byte[] LoadRawFileData(DecryptFileInfo fileInfo); } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/Utility/YooUtility.cs b/Assets/YooAsset/Runtime/Utility/YooUtility.cs index 8069b23b..e5f4ad1c 100644 --- a/Assets/YooAsset/Runtime/Utility/YooUtility.cs +++ b/Assets/YooAsset/Runtime/Utility/YooUtility.cs @@ -115,6 +115,18 @@ namespace YooAsset /// internal static class FileUtility { + /// + /// 字节数据转换成文本数据 + /// + /// + /// + public static string BytesToText(byte[] data) + { + UTF8Encoding utf8 = new UTF8Encoding(); + string txt = utf8.GetString(data); + return txt; + } + /// /// 读取文件的文本数据 ///