refactor : 重构网络下载模块

新增通用下载接口,扩展了默认的Unity引擎下载器
This commit is contained in:
何冠峰
2026-01-05 19:44:10 +08:00
parent 1884fab0c2
commit c87efdb509
89 changed files with 2597 additions and 1641 deletions
@@ -16,19 +16,21 @@ namespace YooAsset
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private readonly IWebDecryptionServices _decryptionServices;
private UnityWebDataRequestOperation _unityWebDataRequestOp;
private readonly IDownloadBackend _downloadBackend;
private IDownloadBytesRequest _unityWebDataRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer = 0;
private int _failedTryAgain;
private ESteps _steps = ESteps.None;
internal LoadWebEncryptAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, IWebDecryptionServices decryptionServices)
internal LoadWebEncryptAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, IWebDecryptionServices decryptionServices, IDownloadBackend downloadBackend)
{
_bundle = bundle;
_options = options;
_failedTryAgain = options.FailedTryAgain;
_decryptionServices = decryptionServices;
_downloadBackend = downloadBackend;
}
internal override void InternalStart()
{
@@ -52,24 +54,23 @@ namespace YooAsset
}
string url = GetRequestURL();
_unityWebDataRequestOp = new UnityWebDataRequestOperation(url, 0);
_unityWebDataRequestOp.StartOperation();
AddChildOperation(_unityWebDataRequestOp);
var args = new DownloadDataRequestArgs(url, 0, 0);
_unityWebDataRequestOp = _downloadBackend.CreateBytesRequest(args);
_unityWebDataRequestOp.SendRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
_unityWebDataRequestOp.UpdateOperation();
Progress = _unityWebDataRequestOp.Progress;
Progress = _unityWebDataRequestOp.DownloadProgress;
DownloadProgress = _unityWebDataRequestOp.DownloadProgress;
DownloadedBytes = _unityWebDataRequestOp.DownloadedBytes;
if (_unityWebDataRequestOp.IsDone == false)
return;
// 检查网络错误
if (_unityWebDataRequestOp.Status == EOperationStatus.Succeed)
if (_unityWebDataRequestOp.Status == EDownloadRequestStatus.Succeed)
{
AssetBundle assetBundle = LoadEncryptedAssetBundle(_unityWebDataRequestOp.Result);
if (assetBundle == null)
@@ -16,7 +16,8 @@ namespace YooAsset
private readonly PackageBundle _bundle;
private readonly DownloadFileOptions _options;
private readonly bool _disableUnityWebCache;
private UnityAssetBundleRequestOperation _unityAssetBundleRequestOp;
private readonly IDownloadBackend _downloadBackend;
private IDownloadAssetBundleRequest _unityAssetBundleRequestOp;
private int _requestCount = 0;
private float _tryAgainTimer = 0;
@@ -24,12 +25,13 @@ namespace YooAsset
private ESteps _steps = ESteps.None;
internal LoadWebNormalAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, bool disableUnityWebCache)
internal LoadWebNormalAssetBundleOperation(PackageBundle bundle, DownloadFileOptions options, bool disableUnityWebCache, IDownloadBackend downloadBackend)
{
_bundle = bundle;
_options = options;
_failedTryAgain = options.FailedTryAgain;
_disableUnityWebCache = disableUnityWebCache;
_downloadBackend = downloadBackend;
}
internal override void InternalStart()
{
@@ -44,23 +46,22 @@ namespace YooAsset
if (_steps == ESteps.CreateRequest)
{
string url = GetRequestURL();
_unityAssetBundleRequestOp = new UnityAssetBundleRequestOperation(_bundle, _disableUnityWebCache, url);
_unityAssetBundleRequestOp.StartOperation();
AddChildOperation(_unityAssetBundleRequestOp);
var args = new DownloadAssetBundleRequestArgs(url, 0, 0, _disableUnityWebCache, _bundle.FileHash, _bundle.UnityCRC);
_unityAssetBundleRequestOp = _downloadBackend.CreateAssetBundleRequest(args);
_unityAssetBundleRequestOp.SendRequest();
_steps = ESteps.CheckRequest;
}
// 检测下载结果
if (_steps == ESteps.CheckRequest)
{
_unityAssetBundleRequestOp.UpdateOperation();
Progress = _unityAssetBundleRequestOp.Progress;
Progress = _unityAssetBundleRequestOp.DownloadProgress;
DownloadedBytes = _unityAssetBundleRequestOp.DownloadedBytes;
DownloadProgress = _unityAssetBundleRequestOp.DownloadProgress;
if (_unityAssetBundleRequestOp.IsDone == false)
return;
if (_unityAssetBundleRequestOp.Status == EOperationStatus.Succeed)
if (_unityAssetBundleRequestOp.Status == EDownloadRequestStatus.Succeed)
{
_steps = ESteps.Done;
Status = EOperationStatus.Succeed;
@@ -13,11 +13,12 @@ internal class LoadWebPackageManifestOperation : AsyncOperationBase
private readonly IManifestRestoreServices _manifestServices;
private readonly IRemoteServices _remoteServices;
private readonly IDownloadBackend _downloadBackend;
private readonly string _packageName;
private readonly string _packageVersion;
private readonly string _packageHash;
private readonly int _timeout;
private UnityWebDataRequestOperation _webDataRequestOp;
private IDownloadBytesRequest _webDataRequestOp;
private DeserializeManifestOperation _deserializer;
private int _requestCount = 0;
private ESteps _steps = ESteps.None;
@@ -28,11 +29,12 @@ internal class LoadWebPackageManifestOperation : AsyncOperationBase
public PackageManifest Manifest { private set; get; }
internal LoadWebPackageManifestOperation(IManifestRestoreServices manifestServices, IRemoteServices remoteServices,
internal LoadWebPackageManifestOperation(IManifestRestoreServices manifestServices, IRemoteServices remoteServices, IDownloadBackend downloadBackend,
string packageName, string packageVersion, string packageHash, int timeout)
{
_manifestServices = manifestServices;
_remoteServices = remoteServices;
_downloadBackend = downloadBackend;
_packageName = packageName;
_packageVersion = packageVersion;
_packageHash = packageHash;
@@ -54,17 +56,16 @@ internal class LoadWebPackageManifestOperation : AsyncOperationBase
{
string fileName = YooAssetSettingsData.GetManifestBinaryFileName(_packageName, _packageVersion);
string url = GetRequestURL(fileName);
_webDataRequestOp = new UnityWebDataRequestOperation(url, _timeout);
_webDataRequestOp.StartOperation();
AddChildOperation(_webDataRequestOp);
var args = new DownloadDataRequestArgs(url, _timeout, 0);
_webDataRequestOp = _downloadBackend.CreateBytesRequest(args);
_webDataRequestOp.SendRequest();
}
_webDataRequestOp.UpdateOperation();
Progress = _webDataRequestOp.Progress;
Progress = _webDataRequestOp.DownloadProgress;
if (_webDataRequestOp.IsDone == false)
return;
if (_webDataRequestOp.Status == EOperationStatus.Succeed)
if (_webDataRequestOp.Status == EDownloadRequestStatus.Succeed)
{
_steps = ESteps.VerifyFileData;
}
@@ -10,10 +10,11 @@ internal class RequestWebPackageHashOperation : AsyncOperationBase
}
private readonly IRemoteServices _remoteServices;
private readonly IDownloadBackend _downloadBackend;
private readonly string _packageName;
private readonly string _packageVersion;
private readonly int _timeout;
private UnityWebTextRequestOperation _webTextRequestOp;
private IDownloadTextRequest _webTextRequestOp;
private int _requestCount = 0;
private ESteps _steps = ESteps.None;
@@ -23,9 +24,10 @@ internal class RequestWebPackageHashOperation : AsyncOperationBase
public string PackageHash { private set; get; }
public RequestWebPackageHashOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout)
public RequestWebPackageHashOperation(IRemoteServices remoteServices, IDownloadBackend downloadBackend, string packageName, string packageVersion, int timeout)
{
_remoteServices = remoteServices;
_downloadBackend = downloadBackend;
_packageName = packageName;
_packageVersion = packageVersion;
_timeout = timeout;
@@ -46,17 +48,16 @@ internal class RequestWebPackageHashOperation : AsyncOperationBase
{
string fileName = YooAssetSettingsData.GetPackageHashFileName(_packageName, _packageVersion);
string url = GetRequestURL(fileName);
_webTextRequestOp = new UnityWebTextRequestOperation(url, _timeout);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
var args = new DownloadDataRequestArgs(url, _timeout, 0);
_webTextRequestOp = _downloadBackend.CreateTextRequest(args);
_webTextRequestOp.SendRequest();
}
_webTextRequestOp.UpdateOperation();
Progress = _webTextRequestOp.Progress;
Progress = _webTextRequestOp.DownloadProgress;
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
if (_webTextRequestOp.Status == EDownloadRequestStatus.Succeed)
{
PackageHash = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageHash))
@@ -10,10 +10,11 @@ internal class RequestWebPackageVersionOperation : AsyncOperationBase
}
private readonly IRemoteServices _remoteServices;
private readonly IDownloadBackend _downloadBackend;
private readonly string _packageName;
private readonly bool _appendTimeTicks;
private readonly int _timeout;
private UnityWebTextRequestOperation _webTextRequestOp;
private IDownloadTextRequest _webTextRequestOp;
private int _requestCount = 0;
private ESteps _steps = ESteps.None;
@@ -23,9 +24,10 @@ internal class RequestWebPackageVersionOperation : AsyncOperationBase
public string PackageVersion { private set; get; }
public RequestWebPackageVersionOperation(IRemoteServices remoteServices, string packageName, bool appendTimeTicks, int timeout)
public RequestWebPackageVersionOperation(IRemoteServices remoteServices, IDownloadBackend downloadBackend, string packageName, bool appendTimeTicks, int timeout)
{
_remoteServices = remoteServices;
_downloadBackend = downloadBackend;
_packageName = packageName;
_appendTimeTicks = appendTimeTicks;
_timeout = timeout;
@@ -46,17 +48,16 @@ internal class RequestWebPackageVersionOperation : AsyncOperationBase
{
string fileName = YooAssetSettingsData.GetPackageVersionFileName(_packageName);
string url = GetRequestURL(fileName);
_webTextRequestOp = new UnityWebTextRequestOperation(url, _timeout);
_webTextRequestOp.StartOperation();
AddChildOperation(_webTextRequestOp);
var args = new DownloadDataRequestArgs(url, _timeout, 0);
_webTextRequestOp = _downloadBackend.CreateTextRequest(args);
_webTextRequestOp.SendRequest();
}
_webTextRequestOp.UpdateOperation();
Progress = _webTextRequestOp.Progress;
Progress = _webTextRequestOp.DownloadProgress;
if (_webTextRequestOp.IsDone == false)
return;
if (_webTextRequestOp.Status == EOperationStatus.Succeed)
if (_webTextRequestOp.Status == EDownloadRequestStatus.Succeed)
{
PackageVersion = _webTextRequestOp.Result;
if (string.IsNullOrEmpty(PackageVersion))