refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-19 16:22:07 +08:00
parent 0a1c81faf1
commit b0678906af
5 changed files with 63 additions and 68 deletions
@@ -35,29 +35,27 @@ namespace YooAsset
if (_steps == ESteps.CheckInitStatus) if (_steps == ESteps.CheckInitStatus)
{ {
if (_resourcePackage.InitializeStatus == EOperationStatus.None) switch (_resourcePackage.InitializeStatus)
{ {
case EOperationStatus.None:
case EOperationStatus.Failed:
_steps = ESteps.DestroyPackage; _steps = ESteps.DestroyPackage;
} break;
else if (_resourcePackage.InitializeStatus == EOperationStatus.Processing)
{ case EOperationStatus.Processing:
_steps = ESteps.Done; _steps = ESteps.Done;
Status = EOperationStatus.Failed; Status = EOperationStatus.Failed;
Error = "The Package is initializing. Please try to destroy the package again later."; Error = "The Package is initializing. Please try to destroy the package again later.";
} break;
else if (_resourcePackage.InitializeStatus == EOperationStatus.Failed)
{ case EOperationStatus.Succeed:
_steps = ESteps.DestroyPackage;
}
else if (_resourcePackage.InitializeStatus == EOperationStatus.Succeed)
{
if (_resourcePackage.PackageValid) if (_resourcePackage.PackageValid)
_steps = ESteps.UnloadAllAssets; _steps = ESteps.UnloadAllAssets;
else else
_steps = ESteps.DestroyPackage; _steps = ESteps.DestroyPackage;
} break;
else
{ default:
throw new System.NotImplementedException(_resourcePackage.InitializeStatus.ToString()); throw new System.NotImplementedException(_resourcePackage.InitializeStatus.ToString());
} }
} }
@@ -168,10 +168,7 @@ namespace YooAsset
{ {
_lastDownloadBytes = downloadBytes; _lastDownloadBytes = downloadBytes;
_lastDownloadCount = _cachedDownloadCount; _lastDownloadCount = _cachedDownloadCount;
if (TotalDownloadBytes == 0) Progress = CalculateProgress();
Progress = (float)_lastDownloadCount / TotalDownloadCount;
else
Progress = (float)_lastDownloadBytes / TotalDownloadBytes;
if (DownloadProgressChangedHandler != null) if (DownloadProgressChangedHandler != null)
{ {
@@ -280,6 +277,14 @@ namespace YooAsset
} }
} }
private float CalculateProgress()
{
if (TotalDownloadBytes == 0)
return (float)_lastDownloadCount / TotalDownloadCount;
else
return (float)_lastDownloadBytes / TotalDownloadBytes;
}
/// <summary> /// <summary>
/// 合并其它下载器 /// 合并其它下载器
/// </summary> /// </summary>
@@ -162,10 +162,7 @@ namespace YooAsset
/// </summary> /// </summary>
public bool HasAnyTags() public bool HasAnyTags()
{ {
if (Tags != null && Tags.Length > 0) return Tags != null && Tags.Length > 0;
return true;
else
return false;
} }
/// <summary> /// <summary>
@@ -20,17 +20,14 @@ namespace YooAsset
if (string.IsNullOrEmpty(hashValue)) if (string.IsNullOrEmpty(hashValue))
return false; return false;
// 注意:兼容俩种验证方式 // 注意:兼容两种验证方式
string fileHash; string fileHash;
if (hashValue.Length == MD5HashLength) if (hashValue.Length == MD5HashLength)
fileHash = HashUtility.BytesMD5(fileData); fileHash = HashUtility.BytesMD5(fileData);
else else
fileHash = HashUtility.BytesCRC32(fileData); fileHash = HashUtility.BytesCRC32(fileData);
if (fileHash == hashValue) return fileHash == hashValue;
return true;
else
return false;
} }
/// <summary> /// <summary>
@@ -160,16 +157,16 @@ namespace YooAsset
/// </summary> /// </summary>
public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash) public static string GetRemoteBundleFileName(int nameStyle, string bundleName, string fileExtension, string fileHash)
{ {
if (nameStyle == (int)EFileNameStyle.HashName) EFileNameStyle style = (EFileNameStyle)nameStyle;
switch (style)
{ {
case EFileNameStyle.HashName:
return StringUtility.Format("{0}{1}", fileHash, fileExtension); return StringUtility.Format("{0}{1}", fileHash, fileExtension);
}
else if (nameStyle == (int)EFileNameStyle.BundleName) case EFileNameStyle.BundleName:
{
return bundleName; return bundleName;
}
else if (nameStyle == (int)EFileNameStyle.BundleName_HashName) case EFileNameStyle.BundleName_HashName:
{
if (string.IsNullOrEmpty(fileExtension)) if (string.IsNullOrEmpty(fileExtension))
{ {
return StringUtility.Format("{0}_{1}", bundleName, fileHash); return StringUtility.Format("{0}_{1}", bundleName, fileHash);
@@ -179,9 +176,8 @@ namespace YooAsset
string fileName = bundleName.Remove(bundleName.LastIndexOf('.')); string fileName = bundleName.Remove(bundleName.LastIndexOf('.'));
return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension); return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension);
} }
}
else default:
{
throw new NotImplementedException($"Invalid name style : {nameStyle}"); throw new NotImplementedException($"Invalid name style : {nameStyle}");
} }
} }
@@ -889,16 +889,15 @@ namespace YooAsset
{ {
if (InitializeStatus != EOperationStatus.Succeed) if (InitializeStatus != EOperationStatus.Succeed)
{ {
if (InitializeStatus == EOperationStatus.None) switch (InitializeStatus)
{ {
case EOperationStatus.None:
throw new YooPackageException(PackageName, "Resource package not initialized."); throw new YooPackageException(PackageName, "Resource package not initialized.");
}
else if (InitializeStatus == EOperationStatus.Processing) case EOperationStatus.Processing:
{
throw new YooPackageException(PackageName, "Resource package initialization not completed."); throw new YooPackageException(PackageName, "Resource package initialization not completed.");
}
else if (InitializeStatus == EOperationStatus.Failed) case 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}");
} }