Merge pull request #326 from absences/decrypt

Assetbundle加载增加解密方法
This commit is contained in:
何冠峰
2024-08-03 17:03:45 +08:00
committed by GitHub
10 changed files with 266 additions and 42 deletions
@@ -2,6 +2,7 @@
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using System.Text;
namespace YooAsset
{
@@ -98,6 +99,11 @@ namespace YooAsset
/// 自定义参数:原生文件构建管线
/// </summary>
public bool RawFileBuildPipeline { private set; get; } = false;
/// <summary>
/// 自定义参数:解密方法类
/// </summary>
public IDecryptionServices DecryptionServices { private set; get; }
#endregion
@@ -181,18 +187,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 +220,11 @@ 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.SetParameter(FileSystemParameters.DECRYPTION_SERVICES, DecryptionServices);
_unpackFileSystem.OnCreate(packageName, null);
}
public virtual void OnUpdate()
@@ -257,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)
{
@@ -268,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 内部方法
@@ -310,7 +342,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);
}
}
/// <summary>
/// 记录文件信息
/// </summary>
@@ -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;
}