refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-17 17:06:57 +08:00
parent da222c6179
commit e10888de93
13 changed files with 42 additions and 43 deletions

View File

@@ -76,7 +76,7 @@ namespace YooAsset
else else
{ {
if (string.IsNullOrEmpty(_args.FileHash)) if (string.IsNullOrEmpty(_args.FileHash))
throw new YooInternalException("File hash is null or empty !"); throw new YooInternalException("File hash is null or empty!");
// 使用 Unity 缓存 // 使用 Unity 缓存
// 说明The file hash defining the version of the asset bundle. // 说明The file hash defining the version of the asset bundle.

View File

@@ -99,7 +99,7 @@ namespace YooAsset
if (_webRequest == null) if (_webRequest == null)
{ {
Status = EDownloadRequestStatus.Failed; Status = EDownloadRequestStatus.Failed;
Error = $"[{GetType().Name}] Created web request is null."; Error = $"[{GetType().Name}] Created web request is null!";
} }
else else
{ {
@@ -109,7 +109,7 @@ namespace YooAsset
catch (Exception ex) catch (Exception ex)
{ {
Status = EDownloadRequestStatus.Failed; Status = EDownloadRequestStatus.Failed;
Error = $"[{GetType().Name}] Failed to create web request : {ex.Message}"; Error = $"[{GetType().Name}] Failed to create web request: {ex.Message}";
} }
} }
} }
@@ -144,7 +144,7 @@ namespace YooAsset
else else
{ {
Status = EDownloadRequestStatus.Failed; Status = EDownloadRequestStatus.Failed;
Error = $"[{GetType().Name}] URL: {URL} - 错误: {_webRequest.error}"; Error = $"[{GetType().Name}] URL: {URL} - Error: {_webRequest.error}";
OnRequestFailed(); OnRequestFailed();
} }
@@ -226,7 +226,7 @@ namespace YooAsset
protected void ApplyRequestOptions(int timeout, int watchdogTime, Dictionary<string, string> headers) protected void ApplyRequestOptions(int timeout, int watchdogTime, Dictionary<string, string> headers)
{ {
if (_webRequest == null) if (_webRequest == null)
throw new YooInternalException("Web request is null !"); throw new YooInternalException("Web request is null!");
// 设置看门狗超时时间 // 设置看门狗超时时间
_watchdogTime = watchdogTime; _watchdogTime = watchdogTime;

View File

@@ -57,7 +57,7 @@ namespace YooAsset
#elif UNITY_STANDALONE_LINUX #elif UNITY_STANDALONE_LINUX
url = StringUtility.Format("file:///root/{0}", path); url = StringUtility.Format("file:///root/{0}", path);
#else #else
throw new System.NotSupportedException($"[{nameof(DownloadSystemHelper.ConvertToWWWPath)}] not implemented platform: {UnityEngine.Application.platform}"); throw new System.NotSupportedException($"[{nameof(DownloadSystemHelper.ConvertToWWWPath)}] Platform '{UnityEngine.Application.platform}' is not supported!");
#endif #endif
// For some special cases when users have special characters in their devices, url paths can not be identified correctly. // For some special cases when users have special characters in their devices, url paths can not be identified correctly.

View File

@@ -408,8 +408,6 @@ namespace YooAsset
} }
#region #region
private const int MaxCycleCheckDepth = 4096; // 循环检测最大深度
/// <summary> /// <summary>
/// 开始的时间 /// 开始的时间
/// </summary> /// </summary>
@@ -466,6 +464,7 @@ namespace YooAsset
/// </summary> /// </summary>
private bool WouldCreateCycle(AsyncOperationBase child) private bool WouldCreateCycle(AsyncOperationBase child)
{ {
const int MaxCycleCheckDepth = 4096; // 循环检测最大深度
var stack = new Stack<AsyncOperationBase>(); var stack = new Stack<AsyncOperationBase>();
var visited = new HashSet<AsyncOperationBase>(); var visited = new HashSet<AsyncOperationBase>();
stack.Push(child); stack.Push(child);

View File

@@ -19,17 +19,17 @@ namespace YooAsset
/// <summary> /// <summary>
/// 所属包裹 /// 所属包裹
/// </summary> /// </summary>
public string PackageName { private set; get; } public string PackageName { get; private set; }
/// <summary> /// <summary>
/// 资源类型 /// 资源类型
/// </summary> /// </summary>
public System.Type AssetType { private set; get; } public System.Type AssetType { get; private set; }
/// <summary> /// <summary>
/// 错误信息 /// 错误信息
/// </summary> /// </summary>
public string Error { private set; get; } public string Error { get; private set; }
/// <summary> /// <summary>
/// 加载方法 /// 加载方法

View File

@@ -6,17 +6,17 @@ namespace YooAsset
/// <summary> /// <summary>
/// 清理模式 /// 清理模式
/// </summary> /// </summary>
public string ClearMode { set; get; } public string ClearMode { get; set; }
/// <summary> /// <summary>
/// 附加参数 /// 附加参数
/// </summary> /// </summary>
public object ClearParam { set; get; } public object ClearParam { get; set; }
/// <summary> /// <summary>
/// 资源清单 /// 资源清单
/// </summary> /// </summary>
internal PackageManifest Manifest { set; get; } internal PackageManifest Manifest { get; set; }
public ClearCacheFilesOptions(EFileClearMode clearMode) public ClearCacheFilesOptions(EFileClearMode clearMode)
{ {

View File

@@ -35,12 +35,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 统计的下载文件总数量 /// 统计的下载文件总数量
/// </summary> /// </summary>
public int TotalDownloadCount { private set; get; } public int TotalDownloadCount { get; private set; }
/// <summary> /// <summary>
/// 统计的下载文件的总大小 /// 统计的下载文件的总大小
/// </summary> /// </summary>
public long TotalDownloadBytes { private set; get; } public long TotalDownloadBytes { get; private set; }
/// <summary> /// <summary>
/// 当前已经完成的下载总数量 /// 当前已经完成的下载总数量
@@ -61,29 +61,29 @@ namespace YooAsset
/// <summary> /// <summary>
/// 下载完成事件委托(无论成功或失败) /// 下载完成事件委托(无论成功或失败)
/// </summary> /// </summary>
public DownloadFinishedEventHandler DownloadFinishedHandler { set; get; } public DownloadFinishedEventHandler DownloadFinishedHandler { get; set; }
/// <summary> /// <summary>
/// 下载进度更新事件委托 /// 下载进度更新事件委托
/// </summary> /// </summary>
public DownloadProgressChangedEventHandler DownloadProgressChangedHandler { set; get; } public DownloadProgressChangedEventHandler DownloadProgressChangedHandler { get; set; }
/// <summary> /// <summary>
/// 下载错误事件委托 /// 下载错误事件委托
/// </summary> /// </summary>
public DownloadErrorEventHandler DownloadErrorHandler { set; get; } public DownloadErrorEventHandler DownloadErrorHandler { get; set; }
/// <summary> /// <summary>
/// 开始下载单个文件事件委托 /// 开始下载单个文件事件委托
/// </summary> /// </summary>
public DownloadFileStartedEventHandler DownloadFileStartedHandler { set; get; } public DownloadFileStartedEventHandler DownloadFileStartedHandler { get; set; }
internal DownloaderOperation(string packageName, List<BundleInfo> downloadList, int maximumConcurrency, int failedTryAgain) internal DownloaderOperation(string packageName, List<BundleInfo> downloadList, int maximumConcurrency, int failedTryAgain)
{ {
_packageName = packageName; _packageName = packageName;
_bundleInfoList = downloadList; _bundleInfoList = downloadList;
_maximumConcurrency = UnityEngine.Mathf.Clamp(maximumConcurrency, 1, MAX_LOADER_COUNT); ; _maximumConcurrency = UnityEngine.Mathf.Clamp(maximumConcurrency, 1, MAX_LOADER_COUNT);
_failedTryAgain = failedTryAgain; _failedTryAgain = failedTryAgain;
// 统计下载信息 // 统计下载信息
@@ -91,7 +91,7 @@ namespace YooAsset
} }
internal override void InternalStart() internal override void InternalStart()
{ {
YooLogger.Log($"Begine to download {TotalDownloadCount} files and {TotalDownloadBytes} bytes"); YooLogger.Log($"Begin to download {TotalDownloadCount} files and {TotalDownloadBytes} bytes");
_steps = ESteps.Check; _steps = ESteps.Check;
} }
internal override void InternalUpdate() internal override void InternalUpdate()

View File

@@ -9,23 +9,23 @@ namespace YooAsset
/// <summary> /// <summary>
/// 最大并发数量 /// 最大并发数量
/// </summary> /// </summary>
public int MaximumConcurrency { set; get; } public int MaximumConcurrency { get; set; }
/// <summary> /// <summary>
/// 失败后的重试次数 /// 失败后的重试次数
/// </summary> /// </summary>
public int FailedTryAgain { set; get; } public int FailedTryAgain { get; set; }
/// <summary> /// <summary>
/// 下载资源对象所属资源包内所有资源对象依赖的资源包 /// 下载资源对象所属资源包内所有资源对象依赖的资源包
/// </summary> /// </summary>
public bool DownloadBundleDependencies { set; get; } public bool DownloadBundleDependencies { get; set; }
/// <summary> /// <summary>
/// 资源信息列表 /// 资源信息列表
/// 说明如果列表为NULL则下载所有资产 /// 说明如果列表为NULL则下载所有资产
/// </summary> /// </summary>
public AssetInfo[] AssetInfos { set; get; } public AssetInfo[] AssetInfos { get; set; }
public BundleDownloaderOptions(AssetInfo assetInfo, bool downloadDependencies, int maximumConcurrency, int failedTryAgain) public BundleDownloaderOptions(AssetInfo assetInfo, bool downloadDependencies, int maximumConcurrency, int failedTryAgain)
{ {
@@ -51,18 +51,18 @@ namespace YooAsset
/// <summary> /// <summary>
/// 最大并发数量 /// 最大并发数量
/// </summary> /// </summary>
public int MaximumConcurrency { set; get; } public int MaximumConcurrency { get; set; }
/// <summary> /// <summary>
/// 失败后的重试次数 /// 失败后的重试次数
/// </summary> /// </summary>
public int FailedTryAgain { set; get; } public int FailedTryAgain { get; set; }
/// <summary> /// <summary>
/// 资源标签列表 /// 资源标签列表
/// 说明如果列表为NULL则下载所有资产 /// 说明如果列表为NULL则下载所有资产
/// </summary> /// </summary>
public string[] Tags { set; get; } public string[] Tags { get; set; }
public ResourceDownloaderOptions(int maximumConcurrency, int failedTryAgain) public ResourceDownloaderOptions(int maximumConcurrency, int failedTryAgain)
{ {
@@ -92,18 +92,18 @@ namespace YooAsset
/// <summary> /// <summary>
/// 最大并发数量 /// 最大并发数量
/// </summary> /// </summary>
public int MaximumConcurrency { set; get; } public int MaximumConcurrency { get; set; }
/// <summary> /// <summary>
/// 失败后的重试次数 /// 失败后的重试次数
/// </summary> /// </summary>
public int FailedTryAgain { set; get; } public int FailedTryAgain { get; set; }
/// <summary> /// <summary>
/// 资源标签列表 /// 资源标签列表
/// 说明如果列表为NULL则解压所有资产 /// 说明如果列表为NULL则解压所有资产
/// </summary> /// </summary>
public string[] Tags { set; get; } public string[] Tags { get; set; }
public ResourceUnpackerOptions(int maximumConcurrency, int failedTryAgain) public ResourceUnpackerOptions(int maximumConcurrency, int failedTryAgain)
{ {
@@ -133,17 +133,17 @@ namespace YooAsset
/// <summary> /// <summary>
/// 最大并发数量 /// 最大并发数量
/// </summary> /// </summary>
public int MaximumConcurrency { set; get; } public int MaximumConcurrency { get; set; }
/// <summary> /// <summary>
/// 失败后的重试次数 /// 失败后的重试次数
/// </summary> /// </summary>
public int FailedTryAgain { set; get; } public int FailedTryAgain { get; set; }
/// <summary> /// <summary>
/// 资源包信息列表 /// 资源包信息列表
/// </summary> /// </summary>
public ImportBundleInfo[] BundleInfos { set; get; } public ImportBundleInfo[] BundleInfos { get; set; }
public BundleImporterOptions(ImportBundleInfo[] bundleInfos, int maximumConcurrency, int failedTryAgain) public BundleImporterOptions(ImportBundleInfo[] bundleInfos, int maximumConcurrency, int failedTryAgain)
{ {

View File

@@ -6,12 +6,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 包裹版本 /// 包裹版本
/// </summary> /// </summary>
public string PackageVersion { set; get; } public string PackageVersion { get; set; }
/// <summary> /// <summary>
/// 超时时间 /// 超时时间
/// </summary> /// </summary>
public int Timeout { set; get; } public int Timeout { get; set; }
public LoadPackageManifestOptions(string packageVersion, int timeout) public LoadPackageManifestOptions(string packageVersion, int timeout)
{ {

View File

@@ -6,12 +6,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 预下载的包裹版本 /// 预下载的包裹版本
/// </summary> /// </summary>
public string PackageVersion { set; get; } public string PackageVersion { get; set; }
/// <summary> /// <summary>
/// 资源清单请求超时时间 /// 资源清单请求超时时间
/// </summary> /// </summary>
public int Timeout { set; get; } public int Timeout { get; set; }
public PreDownloadContentOptions(string packageVersion, int timeout) public PreDownloadContentOptions(string packageVersion, int timeout)
{ {

View File

@@ -18,7 +18,7 @@ namespace YooAsset
/// <summary> /// <summary>
/// 当前最新的包裹版本 /// 当前最新的包裹版本
/// </summary> /// </summary>
public string PackageVersion { private set; get; } public string PackageVersion { get; private set; }
internal RequestPackageVersionOperation(FileSystemHost host, RequestPackageVersionOptions options) internal RequestPackageVersionOperation(FileSystemHost host, RequestPackageVersionOptions options)

View File

@@ -6,12 +6,12 @@ namespace YooAsset
/// <summary> /// <summary>
/// 在URL末尾添加时间戳 /// 在URL末尾添加时间戳
/// </summary> /// </summary>
public bool AppendTimeTicks { set; get; } public bool AppendTimeTicks { get; set; }
/// <summary> /// <summary>
/// 超时时间 /// 超时时间
/// </summary> /// </summary>
public int Timeout { set; get; } public int Timeout { get; set; }
public RequestPackageVersionOptions(bool appendTimeTicks, int timeout) public RequestPackageVersionOptions(bool appendTimeTicks, int timeout)
{ {

View File

@@ -900,7 +900,7 @@ namespace YooAsset
else if (InitializeStatus == EOperationStatus.Failed) else if (InitializeStatus == EOperationStatus.Failed)
{ {
string error = _initializeOp == null ? string.Empty : _initializeOp.Error; string error = _initializeOp == null ? string.Empty : _initializeOp.Error;
throw new YooPackageException(PackageName, $"Resource package initialization failed! Error : {error}"); throw new YooPackageException(PackageName, $"Resource package initialization failed! Error: {error}");
} }
} }