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

View File

@@ -35,30 +35,28 @@ namespace YooAsset
if (_steps == ESteps.CheckInitStatus) if (_steps == ESteps.CheckInitStatus)
{ {
if (_resourcePackage.InitializeStatus == EOperationStatus.None) switch (_resourcePackage.InitializeStatus)
{ {
_steps = ESteps.DestroyPackage; case EOperationStatus.None:
} case EOperationStatus.Failed:
else if (_resourcePackage.InitializeStatus == EOperationStatus.Processing)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "The Package is initializing. Please try to destroy the package again later.";
}
else if (_resourcePackage.InitializeStatus == EOperationStatus.Failed)
{
_steps = ESteps.DestroyPackage;
}
else if (_resourcePackage.InitializeStatus == EOperationStatus.Succeed)
{
if (_resourcePackage.PackageValid)
_steps = ESteps.UnloadAllAssets;
else
_steps = ESteps.DestroyPackage; _steps = ESteps.DestroyPackage;
} break;
else
{ case EOperationStatus.Processing:
throw new System.NotImplementedException(_resourcePackage.InitializeStatus.ToString()); _steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "The Package is initializing. Please try to destroy the package again later.";
break;
case EOperationStatus.Succeed:
if (_resourcePackage.PackageValid)
_steps = ESteps.UnloadAllAssets;
else
_steps = ESteps.DestroyPackage;
break;
default:
throw new System.NotImplementedException(_resourcePackage.InitializeStatus.ToString());
} }
} }

View File

@@ -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>

View File

@@ -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>

View File

@@ -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,29 +157,28 @@ 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)
{ {
return StringUtility.Format("{0}{1}", fileHash, fileExtension); case EFileNameStyle.HashName:
} 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); }
} else
else {
{ 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); }
}
} default:
else throw new NotImplementedException($"Invalid name style : {nameStyle}");
{
throw new NotImplementedException($"Invalid name style : {nameStyle}");
} }
} }
} }

View File

@@ -889,18 +889,17 @@ namespace YooAsset
{ {
if (InitializeStatus != EOperationStatus.Succeed) if (InitializeStatus != EOperationStatus.Succeed)
{ {
if (InitializeStatus == EOperationStatus.None) switch (InitializeStatus)
{ {
throw new YooPackageException(PackageName, "Resource package not initialized."); case EOperationStatus.None:
} 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}");
} }
} }