原生文件加密/解密

pull/326/head
unknown 2024-07-27 18:37:17 +08:00
parent 07d34891ef
commit c6377ce544
7 changed files with 95 additions and 7 deletions

View File

@ -0,0 +1,23 @@

namespace YooAsset.Editor
{
public class TaskEncryption_RFBP : TaskEncryption, IBuildTask
{
/// <summary>
/// 加密文件
/// </summary>
/// <param name="context"></param>
public void Run(BuildContext context)
{
var buildParameters = context.GetContextObject<BuildParametersContext>();
var buildMapContext = context.GetContextObject<BuildMapContext>();
var buildMode = buildParameters.Parameters.BuildMode;
if (buildMode == EBuildMode.ForceRebuild || buildMode == EBuildMode.IncrementalBuild)
{
EncryptingBundleFiles(buildParameters, buildMapContext);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b3e156139dcc25f4c9440ec3d6cb96d2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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(),

View File

@ -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 内部方法

View File

@ -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 内部方法

View File

@ -124,13 +124,14 @@ namespace YooAsset
/// </summary>
/// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">内置文件的根路径</param>
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
/// <param name="remoteServices">远端资源地址查询服务类</param>
/// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">文件系统的根目录</param>
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;
}

View File

@ -34,5 +34,12 @@ namespace YooAsset
/// 注意:加载流对象在资源包对象释放的时候会自动释放
/// </summary>
AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream);
/// <summary>
/// 解密字节数据
/// </summary>
/// <param name="encryptData"></param>
/// <returns></returns>
public byte[] ReadFileData(byte[] encryptData);
}
}