feat : the bundle file decryption

资源文件解密
pull/342/head
hevinci 2024-08-03 18:43:12 +08:00
parent 6b56275f87
commit d017688416
12 changed files with 319 additions and 226 deletions

View File

@ -1,9 +1,4 @@
using System; 
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
public class TaskEncryption_BBP : TaskEncryption, IBuildTask public class TaskEncryption_BBP : TaskEncryption, IBuildTask

View File

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

View File

@ -1,9 +1,4 @@
using System; 
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace YooAsset.Editor namespace YooAsset.Editor
{ {
public class TaskEncryption_SBP : TaskEncryption, IBuildTask public class TaskEncryption_SBP : TaskEncryption, IBuildTask

View File

@ -2,7 +2,6 @@
using System.IO; using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using System.Text;
namespace YooAsset namespace YooAsset
{ {
@ -178,8 +177,11 @@ namespace YooAsset
if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream)) if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream))
{ {
managedStream.Close(); if (managedStream != null)
managedStream.Dispose(); {
managedStream.Close();
managedStream.Dispose();
}
_loadedStream.Remove(bundle.BundleGUID); _loadedStream.Remove(bundle.BundleGUID);
} }
} }
@ -187,19 +189,19 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParameters.FILE_VERIFY_LEVEL) if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL)
{ {
FileVerifyLevel = (EFileVerifyLevel)value; FileVerifyLevel = (EFileVerifyLevel)value;
} }
else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION)
{ {
AppendFileExtension = (bool)value; AppendFileExtension = (bool)value;
} }
else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) else if (name == FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE)
{ {
RawFileBuildPipeline = (bool)value; RawFileBuildPipeline = (bool)value;
} }
else if (name == FileSystemParameters.DECRYPTION_SERVICES) else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{ {
DecryptionServices = (IDecryptionServices)value; DecryptionServices = (IDecryptionServices)value;
} }
@ -220,11 +222,11 @@ namespace YooAsset
// 创建解压文件系统 // 创建解压文件系统
var remoteServices = new UnpackRemoteServices(_packageRoot); var remoteServices = new UnpackRemoteServices(_packageRoot);
_unpackFileSystem = new DefaultUnpackFileSystem(); _unpackFileSystem = new DefaultUnpackFileSystem();
_unpackFileSystem.SetParameter(FileSystemParameters.REMOTE_SERVICES, remoteServices); _unpackFileSystem.SetParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
_unpackFileSystem.SetParameter(FileSystemParameters.FILE_VERIFY_LEVEL, FileVerifyLevel); _unpackFileSystem.SetParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, FileVerifyLevel);
_unpackFileSystem.SetParameter(FileSystemParameters.APPEND_FILE_EXTENSION, AppendFileExtension); _unpackFileSystem.SetParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, AppendFileExtension);
_unpackFileSystem.SetParameter(FileSystemParameters.RAW_FILE_BUILD_PIPELINE, RawFileBuildPipeline); _unpackFileSystem.SetParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, RawFileBuildPipeline);
_unpackFileSystem.SetParameter(FileSystemParameters.DECRYPTION_SERVICES, DecryptionServices); _unpackFileSystem.SetParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, DecryptionServices);
_unpackFileSystem.OnCreate(packageName, null); _unpackFileSystem.OnCreate(packageName, null);
} }
public virtual void OnUpdate() public virtual void OnUpdate()
@ -267,18 +269,28 @@ namespace YooAsset
if (Exists(bundle) == false) if (Exists(bundle) == false)
return null; return null;
string filePath = GetBuildinFileLoadPath(bundle);
var data = FileUtility.ReadAllBytes(filePath);
if (bundle.Encrypted) if (bundle.Encrypted)
{ {
if (DecryptionServices == null) if (DecryptionServices == null)
{ {
YooLogger.Error($"DecryptionServices is Null!"); YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null; return null;
} }
return DecryptionServices.ReadFileData(data);
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileData(fileInfo);
}
else
{
string filePath = GetBuildinFileLoadPath(bundle);
return FileUtility.ReadAllBytes(filePath);
} }
return data;
} }
public virtual string ReadFileText(PackageBundle bundle) public virtual string ReadFileText(PackageBundle bundle)
{ {
@ -288,19 +300,28 @@ namespace YooAsset
if (Exists(bundle) == false) if (Exists(bundle) == false)
return null; return null;
string filePath = GetBuildinFileLoadPath(bundle);
var data = FileUtility.ReadAllBytes(filePath);
if (bundle.Encrypted) if (bundle.Encrypted)
{ {
if (DecryptionServices == null) if (DecryptionServices == null)
{ {
YooLogger.Error($"DecryptionServices is Null!"); YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null; return null;
} }
data = DecryptionServices.ReadFileData(data);
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileText(fileInfo);
}
else
{
string filePath = GetBuildinFileLoadPath(bundle);
return FileUtility.ReadAllText(filePath);
} }
return Encoding.UTF8.GetString(data);
} }
#region 内部方法 #region 内部方法
@ -342,59 +363,6 @@ namespace YooAsset
string rootPath = PathUtility.Combine(Application.dataPath, "StreamingAssets", YooAssetSettingsData.Setting.DefaultYooFolderName); string rootPath = PathUtility.Combine(Application.dataPath, "StreamingAssets", YooAssetSettingsData.Setting.DefaultYooFolderName);
return PathUtility.Combine(rootPath, PackageName); 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>
/// 记录文件信息 /// 记录文件信息
@ -418,6 +386,42 @@ namespace YooAsset
{ {
return _unpackFileSystem.InitializeFileSystemAsync(); return _unpackFileSystem.InitializeFileSystemAsync();
} }
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
var assetBundle = DecryptionServices.LoadAssetBundle(fileInfo, out var managedStream);
_loadedStream.Add(bundle.BundleGUID, managedStream);
return assetBundle;
}
/// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundleCreateRequest LoadEncryptedAssetBundleAsync(PackageBundle bundle)
{
string filePath = GetBuildinFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
var createRequest = DecryptionServices.LoadAssetBundleAsync(fileInfo, out var managedStream);
_loadedStream.Add(bundle.BundleGUID, managedStream);
return createRequest;
}
#endregion #endregion
} }
} }

View File

@ -11,8 +11,8 @@ namespace YooAsset
private enum ESteps private enum ESteps
{ {
None, None,
LoadBuidlinAssetBundle, LoadAssetBundle,
CheckLoadBuildinResult, CheckResult,
Done, Done,
} }
@ -32,27 +32,56 @@ namespace YooAsset
{ {
DownloadProgress = 1f; DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize; DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadBuidlinAssetBundle; _steps = ESteps.LoadAssetBundle;
} }
internal override void InternalOnUpdate() internal override void InternalOnUpdate()
{ {
if (_steps == ESteps.None || _steps == ESteps.Done) if (_steps == ESteps.None || _steps == ESteps.Done)
return; return;
if (_steps == ESteps.LoadBuidlinAssetBundle) if (_steps == ESteps.LoadAssetBundle)
{ {
if (_bundle.Encrypted)
{
if (_fileSystem.DecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
}
if (_isWaitForAsyncComplete) if (_isWaitForAsyncComplete)
{ {
Result = _fileSystem.LoadAssetBundle(_bundle); if (_bundle.Encrypted)
{
Result = _fileSystem.LoadEncryptedAssetBundle(_bundle);
}
else
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
Result = AssetBundle.LoadFromFile(filePath);
}
} }
else else
{ {
_createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); if (_bundle.Encrypted)
{
_createRequest = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
}
else
{
string filePath = _fileSystem.GetBuildinFileLoadPath(_bundle);
_createRequest = AssetBundle.LoadFromFileAsync(filePath);
}
} }
_steps = ESteps.CheckLoadBuildinResult;
_steps = ESteps.CheckResult;
} }
if (_steps == ESteps.CheckLoadBuildinResult) if (_steps == ESteps.CheckResult)
{ {
if (_createRequest != null) if (_createRequest != null)
{ {
@ -74,12 +103,22 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
return;
}
if (_bundle.Encrypted)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"Failed to load encrypted buildin asset bundle file : {_bundle.BundleName}";
YooLogger.Error(Error);
} }
else else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}"; Error = $"Failed to load buildin asset bundle file : {_bundle.BundleName}";
YooLogger.Error(Error);
} }
} }
} }
@ -148,6 +187,7 @@ namespace YooAsset
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Can not found buildin raw bundle file : {filePath}"; Error = $"Can not found buildin raw bundle file : {filePath}";
YooLogger.Error(Error);
} }
} }
} }

View File

@ -3,7 +3,6 @@ using System.IO;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using UnityEngine; using UnityEngine;
using System.Text;
namespace YooAsset namespace YooAsset
{ {
@ -206,39 +205,42 @@ namespace YooAsset
if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream)) if (_loadedStream.TryGetValue(bundle.BundleGUID, out Stream managedStream))
{ {
managedStream.Close(); if (managedStream != null)
managedStream.Dispose(); {
managedStream.Close();
managedStream.Dispose();
}
_loadedStream.Remove(bundle.BundleGUID); _loadedStream.Remove(bundle.BundleGUID);
} }
} }
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParameters.REMOTE_SERVICES) if (name == FileSystemParametersDefine.REMOTE_SERVICES)
{ {
RemoteServices = (IRemoteServices)value; RemoteServices = (IRemoteServices)value;
} }
else if (name == FileSystemParameters.FILE_VERIFY_LEVEL) else if (name == FileSystemParametersDefine.FILE_VERIFY_LEVEL)
{ {
FileVerifyLevel = (EFileVerifyLevel)value; FileVerifyLevel = (EFileVerifyLevel)value;
} }
else if (name == FileSystemParameters.APPEND_FILE_EXTENSION) else if (name == FileSystemParametersDefine.APPEND_FILE_EXTENSION)
{ {
AppendFileExtension = (bool)value; AppendFileExtension = (bool)value;
} }
else if (name == FileSystemParameters.RAW_FILE_BUILD_PIPELINE) else if (name == FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE)
{ {
RawFileBuildPipeline = (bool)value; RawFileBuildPipeline = (bool)value;
} }
else if (name == FileSystemParameters.RESUME_DOWNLOAD_MINMUM_SIZE) else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_MINMUM_SIZE)
{ {
ResumeDownloadMinimumSize = (long)value; ResumeDownloadMinimumSize = (long)value;
} }
else if (name == FileSystemParameters.RESUME_DOWNLOAD_RESPONSE_CODES) else if (name == FileSystemParametersDefine.RESUME_DOWNLOAD_RESPONSE_CODES)
{ {
ResumeDownloadResponseCodes = (List<long>)value; ResumeDownloadResponseCodes = (List<long>)value;
} }
else if (name == FileSystemParameters.DECRYPTION_SERVICES) else if (name == FileSystemParametersDefine.DECRYPTION_SERVICES)
{ {
DecryptionServices = (IDecryptionServices)value; DecryptionServices = (IDecryptionServices)value;
} }
@ -317,38 +319,57 @@ namespace YooAsset
{ {
if (Exists(bundle) == false) if (Exists(bundle) == false)
return null; return null;
string filePath = GetCacheFileLoadPath(bundle);
var data = FileUtility.ReadAllBytes(filePath);
if (bundle.Encrypted) if (bundle.Encrypted)
{ {
if (DecryptionServices == null) if (DecryptionServices == null)
{ {
YooLogger.Error($"DecryptionServices is Null!"); YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null; return null;
} }
return DecryptionServices.ReadFileData(data);
string filePath = GetCacheFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileData(fileInfo);
}
else
{
string filePath = GetCacheFileLoadPath(bundle);
return FileUtility.ReadAllBytes(filePath);
} }
return data;
} }
public virtual string ReadFileText(PackageBundle bundle) public virtual string ReadFileText(PackageBundle bundle)
{ {
if (Exists(bundle) == false) if (Exists(bundle) == false)
return null; return null;
string filePath = GetCacheFileLoadPath(bundle);
var data = FileUtility.ReadAllBytes(filePath);
if (bundle.Encrypted) if (bundle.Encrypted)
{ {
if (DecryptionServices == null) if (DecryptionServices == null)
{ {
YooLogger.Error($"DecryptionServices is Null!"); YooLogger.Error($"The {nameof(IDecryptionServices)} is null !");
return null; return null;
} }
data = DecryptionServices.ReadFileData(data);
string filePath = GetCacheFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
return DecryptionServices.ReadFileText(fileInfo);
}
else
{
string filePath = GetCacheFileLoadPath(bundle);
return FileUtility.ReadAllText(filePath);
} }
return Encoding.UTF8.GetString(data);
} }
#region 内部方法 #region 内部方法
@ -560,58 +581,40 @@ namespace YooAsset
return _wrappers.Keys.ToList(); return _wrappers.Keys.ToList();
} }
internal AssetBundle LoadAssetBundle(PackageBundle bundle) /// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundle LoadEncryptedAssetBundle(PackageBundle bundle)
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
if (bundle.Encrypted) var assetBundle = DecryptionServices.LoadAssetBundle(fileInfo, out var managedStream);
{ _loadedStream.Add(bundle.BundleGUID, managedStream);
if (DecryptionServices == null) return assetBundle;
{
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) /// <summary>
/// 加载加密资源文件
/// </summary>
public AssetBundleCreateRequest LoadEncryptedAssetBundleAsync(PackageBundle bundle)
{ {
string filePath = GetCacheFileLoadPath(bundle); string filePath = GetCacheFileLoadPath(bundle);
var fileInfo = new DecryptFileInfo()
{
BundleName = bundle.BundleName,
FileLoadCRC = bundle.UnityCRC,
FileLoadPath = filePath,
};
if (bundle.Encrypted) var createRequest = DecryptionServices.LoadAssetBundleAsync(fileInfo, out var managedStream);
{ _loadedStream.Add(bundle.BundleGUID, managedStream);
if (DecryptionServices == null) return createRequest;
{
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 #endregion
} }

View File

@ -78,13 +78,41 @@ namespace YooAsset
if (_steps == ESteps.LoadAssetBundle) if (_steps == ESteps.LoadAssetBundle)
{ {
if (_bundle.Encrypted)
{
if (_fileSystem.DecryptionServices == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = $"The {nameof(IDecryptionServices)} is null !";
YooLogger.Error(Error);
return;
}
}
if (_isWaitForAsyncComplete) if (_isWaitForAsyncComplete)
{ {
Result = _fileSystem.LoadAssetBundle(_bundle); if (_bundle.Encrypted)
{
Result = _fileSystem.LoadEncryptedAssetBundle(_bundle);
}
else
{
string filePath = _fileSystem.GetCacheFileLoadPath(_bundle);
Result = AssetBundle.LoadFromFile(filePath);
}
} }
else else
{ {
_createRequest = _fileSystem.LoadAssetBundleAsync(_bundle); if (_bundle.Encrypted)
{
_createRequest = _fileSystem.LoadEncryptedAssetBundleAsync(_bundle);
}
else
{
string filePath = _fileSystem.GetCacheFileLoadPath(_bundle);
_createRequest = AssetBundle.LoadFromFileAsync(filePath);
}
} }
_steps = ESteps.CheckResult; _steps = ESteps.CheckResult;
@ -112,51 +140,59 @@ namespace YooAsset
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Succeed; Status = EOperationStatus.Succeed;
return;
} }
else
// 注意当缓存文件的校验等级为Low的时候并不能保证缓存文件的完整性。
// 说明在AssetBundle文件加载失败的情况下我们需要重新验证文件的完整性
EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle);
if (verifyResult == EFileVerifyResult.Succeed)
{ {
// 注意当缓存文件的校验等级为Low的时候并不能保证缓存文件的完整性。 if (_bundle.Encrypted)
// 说明在AssetBundle文件加载失败的情况下我们需要重新验证文件的完整性
EFileVerifyResult verifyResult = _fileSystem.VerifyCacheFile(_bundle);
if (verifyResult == EFileVerifyResult.Succeed)
{ {
// 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。 _steps = ESteps.Done;
// 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发! Status = EOperationStatus.Failed;
string filePath = _fileSystem.GetCacheFileLoadPath(_bundle); Error = $"Failed to load encrypted asset bundle file : {_bundle.BundleName}";
byte[] fileData = FileUtility.ReadAllBytes(filePath); YooLogger.Error(Error);
if (fileData != null && fileData.Length > 0) return;
}
// 注意:在安卓移动平台,华为和三星真机上有极小概率加载资源包失败。
// 说明:大多数情况在首次安装下载资源到沙盒内,游戏过程中切换到后台再回到游戏内有很大概率触发!
string filePath = _fileSystem.GetCacheFileLoadPath(_bundle);
byte[] fileData = FileUtility.ReadAllBytes(filePath);
if (fileData != null && fileData.Length > 0)
{
Result = AssetBundle.LoadFromMemory(fileData);
if (Result == null)
{ {
Result = AssetBundle.LoadFromMemory(fileData); _steps = ESteps.Done;
if (Result == null) Status = EOperationStatus.Failed;
{ Error = $"Failed to load asset bundle from memory : {_bundle.BundleName}";
_steps = ESteps.Done; YooLogger.Error(Error);
Status = EOperationStatus.Failed;
Error = $"Failed to load assetBundle from memory : {_bundle.BundleName}";
YooLogger.Error(Error);
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
}
} }
else else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Succeed;
Error = $"Failed to read assetBundle file bytes : {_bundle.BundleName}";
YooLogger.Error(Error);
} }
} }
else else
{ {
_steps = ESteps.Done; _steps = ESteps.Done;
_fileSystem.DeleteCacheFile(_bundle.BundleGUID);
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Find corrupted file and delete the file : {_bundle.BundleName}"; Error = $"Failed to read asset bundle file bytes : {_bundle.BundleName}";
YooLogger.Error(Error); YooLogger.Error(Error);
} }
} }
else
{
_steps = ESteps.Done;
_fileSystem.DeleteCacheFile(_bundle.BundleGUID);
Status = EOperationStatus.Failed;
Error = $"Find corrupted asset bundle file and delete : {_bundle.BundleName}";
YooLogger.Error(Error);
}
} }
} }
internal override void InternalWaitForAsyncComplete() internal override void InternalWaitForAsyncComplete()
@ -272,6 +308,7 @@ namespace YooAsset
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = $"Can not found cache raw bundle file : {filePath}"; Error = $"Can not found cache raw bundle file : {filePath}";
YooLogger.Error(Error);
} }
} }
} }

View File

@ -114,7 +114,7 @@ namespace YooAsset
public virtual void SetParameter(string name, object value) public virtual void SetParameter(string name, object value)
{ {
if (name == FileSystemParameters.DISABLE_UNITY_WEB_CACHE) if (name == FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE)
{ {
DisableUnityWebCache = (bool)value; DisableUnityWebCache = (bool)value;
} }

View File

@ -0,0 +1,15 @@

namespace YooAsset
{
internal class FileSystemParametersDefine
{
public const string FILE_VERIFY_LEVEL = "FILE_VERIFY_LEVEL";
public const string REMOTE_SERVICES = "REMOTE_SERVICES";
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 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";
}
}

View File

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

View File

@ -55,15 +55,6 @@ namespace YooAsset
/// </summary> /// </summary>
public class FileSystemParameters 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<string, object> CreateParameters = new Dictionary<string, object>(); internal Dictionary<string, object> CreateParameters = new Dictionary<string, object>();
/// <summary> /// <summary>
@ -108,30 +99,32 @@ namespace YooAsset
/// <summary> /// <summary>
/// 创建默认的内置文件系统参数 /// 创建默认的内置文件系统参数
/// </summary> /// </summary>
/// <param name="decryptionServices">加密文件解密服务类</param>
/// <param name="verifyLevel">缓存文件的校验等级</param> /// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">内置文件的根路径</param> /// <param name="rootDirectory">内置文件的根路径</param>
public static FileSystemParameters CreateDefaultBuildinFileSystemParameters(IDecryptionServices decryptionServices = null, 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; string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel);
return fileSystemParams; return fileSystemParams;
} }
/// <summary> /// <summary>
/// 创建默认的内置文件系统参数(原生文件) /// 创建默认的内置文件系统参数(原生文件)
/// </summary> /// </summary>
/// <param name="decryptionServices">加密文件解密服务类</param>
/// <param name="verifyLevel">缓存文件的校验等级</param> /// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">内置文件的根路径</param> /// <param name="rootDirectory">内置文件的根路径</param>
public static FileSystemParameters CreateDefaultBuildinRawFileSystemParameters(IDecryptionServices decryptionServices = null, 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; string fileSystemClass = typeof(DefaultBuildinFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel);
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); fileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); fileSystemParams.AddParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, true);
return fileSystemParams; return fileSystemParams;
} }
@ -139,15 +132,16 @@ namespace YooAsset
/// 创建默认的缓存文件系统参数 /// 创建默认的缓存文件系统参数
/// </summary> /// </summary>
/// <param name="remoteServices">远端资源地址查询服务类</param> /// <param name="remoteServices">远端资源地址查询服务类</param>
/// <param name="decryptionServices">加密文件解密服务类</param>
/// <param name="verifyLevel">缓存文件的校验等级</param> /// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">文件系统的根目录</param> /// <param name="rootDirectory">文件系统的根目录</param>
public static FileSystemParameters CreateDefaultCacheFileSystemParameters(IRemoteServices remoteServices, IDecryptionServices decryptionServices = null, 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; string fileSystemClass = typeof(DefaultCacheFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
fileSystemParams.AddParameter(REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel);
return fileSystemParams; return fileSystemParams;
} }
@ -155,17 +149,18 @@ namespace YooAsset
/// 创建默认的缓存文件系统参数(原生文件) /// 创建默认的缓存文件系统参数(原生文件)
/// </summary> /// </summary>
/// <param name="remoteServices">远端资源地址查询服务类</param> /// <param name="remoteServices">远端资源地址查询服务类</param>
/// <param name="decryptionServices">加密文件解密服务类</param>
/// <param name="verifyLevel">缓存文件的校验等级</param> /// <param name="verifyLevel">缓存文件的校验等级</param>
/// <param name="rootDirectory">文件系统的根目录</param> /// <param name="rootDirectory">文件系统的根目录</param>
public static FileSystemParameters CreateDefaultCacheRawFileSystemParameters(IRemoteServices remoteServices, IDecryptionServices decryptionServices = null, 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; string fileSystemClass = typeof(DefaultCacheFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory); var fileSystemParams = new FileSystemParameters(fileSystemClass, rootDirectory);
fileSystemParams.AddParameter(REMOTE_SERVICES, remoteServices); fileSystemParams.AddParameter(FileSystemParametersDefine.REMOTE_SERVICES, remoteServices);
fileSystemParams.AddParameter(FILE_VERIFY_LEVEL, verifyLevel); fileSystemParams.AddParameter(FileSystemParametersDefine.DECRYPTION_SERVICES, decryptionServices);
fileSystemParams.AddParameter(APPEND_FILE_EXTENSION, true); fileSystemParams.AddParameter(FileSystemParametersDefine.FILE_VERIFY_LEVEL, verifyLevel);
fileSystemParams.AddParameter(RAW_FILE_BUILD_PIPELINE, true); fileSystemParams.AddParameter(FileSystemParametersDefine.APPEND_FILE_EXTENSION, true);
fileSystemParams.AddParameter(DECRYPTION_SERVICES, decryptionServices); fileSystemParams.AddParameter(FileSystemParametersDefine.RAW_FILE_BUILD_PIPELINE, true);
return fileSystemParams; return fileSystemParams;
} }
@ -177,7 +172,7 @@ namespace YooAsset
{ {
string fileSystemClass = typeof(DefaultWebFileSystem).FullName; string fileSystemClass = typeof(DefaultWebFileSystem).FullName;
var fileSystemParams = new FileSystemParameters(fileSystemClass, null); var fileSystemParams = new FileSystemParameters(fileSystemClass, null);
fileSystemParams.AddParameter(DISABLE_UNITY_WEB_CACHE, disableUnityWebCache); fileSystemParams.AddParameter(FileSystemParametersDefine.DISABLE_UNITY_WEB_CACHE, disableUnityWebCache);
return fileSystemParams; return fileSystemParams;
} }
} }

View File

@ -36,10 +36,13 @@ namespace YooAsset
AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream); AssetBundleCreateRequest LoadAssetBundleAsync(DecryptFileInfo fileInfo, out Stream managedStream);
/// <summary> /// <summary>
/// 解密字节数据 /// 获取解密字节数据
/// </summary> /// </summary>
/// <param name="encryptData"></param> byte[] ReadFileData(DecryptFileInfo fileInfo);
/// <returns></returns>
byte[] ReadFileData(byte[] encryptData); /// <summary>
/// 获取解密的文本数据
/// </summary>
string ReadFileText(DecryptFileInfo fileInfo);
} }
} }