2025-01-09 11:31:04 +08:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace YooAsset
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
internal class DownloadWebEncryptAssetBundleOperation : DownloadAssetBundleOperation
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
private readonly bool _checkTimeout;
|
|
|
|
|
private readonly IWebDecryptionServices _decryptionServices;
|
|
|
|
|
private DownloadHandlerBuffer _downloadhandler;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
private ESteps _steps = ESteps.None;
|
|
|
|
|
|
2025-05-13 10:40:30 +08:00
|
|
|
|
internal DownloadWebEncryptAssetBundleOperation(bool checkTimeout, IWebDecryptionServices decryptionServices, PackageBundle bundle, DownloadFileOptions options) : base(bundle, options)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
_checkTimeout = checkTimeout;
|
|
|
|
|
_decryptionServices = decryptionServices;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
internal override void InternalStart()
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.CreateRequest;
|
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
internal override void InternalUpdate()
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
if (_steps == ESteps.None || _steps == ESteps.Done)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// 创建下载器
|
|
|
|
|
if (_steps == ESteps.CreateRequest)
|
|
|
|
|
{
|
|
|
|
|
// 获取请求地址
|
|
|
|
|
_requestURL = GetRequestURL();
|
|
|
|
|
|
|
|
|
|
// 重置变量
|
|
|
|
|
ResetRequestFiled();
|
|
|
|
|
|
|
|
|
|
// 创建下载器
|
|
|
|
|
CreateWebRequest();
|
|
|
|
|
|
|
|
|
|
_steps = ESteps.CheckRequest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检测下载结果
|
|
|
|
|
if (_steps == ESteps.CheckRequest)
|
|
|
|
|
{
|
|
|
|
|
DownloadProgress = _webRequest.downloadProgress;
|
|
|
|
|
DownloadedBytes = (long)_webRequest.downloadedBytes;
|
|
|
|
|
Progress = DownloadProgress;
|
|
|
|
|
if (_webRequest.isDone == false)
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (_checkTimeout)
|
|
|
|
|
CheckRequestTimeout();
|
2025-01-09 11:31:04 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查网络错误
|
|
|
|
|
if (CheckRequestResult())
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
if (_decryptionServices == null)
|
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
Status = EOperationStatus.Failed;
|
|
|
|
|
Error = $"The {nameof(IWebDecryptionServices)} is null !";
|
|
|
|
|
YooLogger.Error(Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var fileData = _downloadhandler.data;
|
|
|
|
|
if (fileData == null || fileData.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
Status = EOperationStatus.Failed;
|
|
|
|
|
Error = $"The download handler data is null or empty !";
|
|
|
|
|
YooLogger.Error(Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssetBundle assetBundle = LoadEncryptedAssetBundle(fileData);
|
2025-01-09 11:31:04 +08:00
|
|
|
|
if (assetBundle == null)
|
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
Status = EOperationStatus.Failed;
|
2025-02-28 16:11:01 +08:00
|
|
|
|
Error = "Download handler asset bundle object is null !";
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
Result = assetBundle;
|
|
|
|
|
Status = EOperationStatus.Succeed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.TryAgain;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注意:最终释放请求器
|
|
|
|
|
DisposeWebRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重新尝试下载
|
|
|
|
|
if (_steps == ESteps.TryAgain)
|
|
|
|
|
{
|
|
|
|
|
if (FailedTryAgain <= 0)
|
|
|
|
|
{
|
|
|
|
|
Status = EOperationStatus.Failed;
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
YooLogger.Error(Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_tryAgainTimer += Time.unscaledDeltaTime;
|
|
|
|
|
if (_tryAgainTimer > 1f)
|
|
|
|
|
{
|
|
|
|
|
FailedTryAgain--;
|
|
|
|
|
_steps = ESteps.CreateRequest;
|
|
|
|
|
YooLogger.Warning(Error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
internal override void InternalAbort()
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
|
|
|
|
_steps = ESteps.Done;
|
|
|
|
|
DisposeWebRequest();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateWebRequest()
|
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
_downloadhandler = new DownloadHandlerBuffer();
|
2025-01-09 11:31:04 +08:00
|
|
|
|
_webRequest = DownloadSystemHelper.NewUnityWebRequestGet(_requestURL);
|
|
|
|
|
_webRequest.downloadHandler = _downloadhandler;
|
|
|
|
|
_webRequest.disposeDownloadHandlerOnDispose = true;
|
|
|
|
|
_webRequest.SendWebRequest();
|
|
|
|
|
}
|
|
|
|
|
private void DisposeWebRequest()
|
|
|
|
|
{
|
|
|
|
|
if (_webRequest != null)
|
|
|
|
|
{
|
|
|
|
|
//注意:引擎底层会自动调用Abort方法
|
|
|
|
|
_webRequest.Dispose();
|
|
|
|
|
_webRequest = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-02-28 16:11:01 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 加载加密资源文件
|
|
|
|
|
/// </summary>
|
|
|
|
|
private AssetBundle LoadEncryptedAssetBundle(byte[] fileData)
|
2025-01-09 11:31:04 +08:00
|
|
|
|
{
|
2025-02-28 16:11:01 +08:00
|
|
|
|
var fileInfo = new WebDecryptFileInfo();
|
|
|
|
|
fileInfo.BundleName = Bundle.BundleName;
|
|
|
|
|
fileInfo.FileLoadCRC = Bundle.UnityCRC;
|
|
|
|
|
fileInfo.FileData = fileData;
|
|
|
|
|
var decryptResult = _decryptionServices.LoadAssetBundle(fileInfo);
|
|
|
|
|
return decryptResult.Result;
|
2025-01-09 11:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|