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 (_resourcePackage.InitializeStatus == EOperationStatus.None)
switch (_resourcePackage.InitializeStatus)
{
_steps = ESteps.DestroyPackage;
}
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
case EOperationStatus.None:
case EOperationStatus.Failed:
_steps = ESteps.DestroyPackage;
}
else
{
throw new System.NotImplementedException(_resourcePackage.InitializeStatus.ToString());
break;
case EOperationStatus.Processing:
_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;
_lastDownloadCount = _cachedDownloadCount;
if (TotalDownloadBytes == 0)
Progress = (float)_lastDownloadCount / TotalDownloadCount;
else
Progress = (float)_lastDownloadBytes / TotalDownloadBytes;
Progress = CalculateProgress();
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>

View File

@@ -162,10 +162,7 @@ namespace YooAsset
/// </summary>
public bool HasAnyTags()
{
if (Tags != null && Tags.Length > 0)
return true;
else
return false;
return Tags != null && Tags.Length > 0;
}
/// <summary>

View File

@@ -20,17 +20,14 @@ namespace YooAsset
if (string.IsNullOrEmpty(hashValue))
return false;
// 注意兼容种验证方式
// 注意:兼容种验证方式
string fileHash;
if (hashValue.Length == MD5HashLength)
fileHash = HashUtility.BytesMD5(fileData);
else
fileHash = HashUtility.BytesCRC32(fileData);
if (fileHash == hashValue)
return true;
else
return false;
return fileHash == hashValue;
}
/// <summary>
@@ -160,29 +157,28 @@ namespace YooAsset
/// </summary>
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);
}
else if (nameStyle == (int)EFileNameStyle.BundleName)
{
return bundleName;
}
else if (nameStyle == (int)EFileNameStyle.BundleName_HashName)
{
if (string.IsNullOrEmpty(fileExtension))
{
return StringUtility.Format("{0}_{1}", bundleName, fileHash);
}
else
{
string fileName = bundleName.Remove(bundleName.LastIndexOf('.'));
return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension);
}
}
else
{
throw new NotImplementedException($"Invalid name style : {nameStyle}");
case EFileNameStyle.HashName:
return StringUtility.Format("{0}{1}", fileHash, fileExtension);
case EFileNameStyle.BundleName:
return bundleName;
case EFileNameStyle.BundleName_HashName:
if (string.IsNullOrEmpty(fileExtension))
{
return StringUtility.Format("{0}_{1}", bundleName, fileHash);
}
else
{
string fileName = bundleName.Remove(bundleName.LastIndexOf('.'));
return StringUtility.Format("{0}_{1}{2}", fileName, fileHash, fileExtension);
}
default:
throw new NotImplementedException($"Invalid name style : {nameStyle}");
}
}
}

View File

@@ -889,18 +889,17 @@ namespace YooAsset
{
if (InitializeStatus != EOperationStatus.Succeed)
{
if (InitializeStatus == EOperationStatus.None)
switch (InitializeStatus)
{
throw new YooPackageException(PackageName, "Resource package not initialized.");
}
else if (InitializeStatus == EOperationStatus.Processing)
{
throw new YooPackageException(PackageName, "Resource package initialization not completed.");
}
else if (InitializeStatus == EOperationStatus.Failed)
{
string error = _initializeOp == null ? string.Empty : _initializeOp.Error;
throw new YooPackageException(PackageName, $"Resource package initialization failed. Error: {error}");
case EOperationStatus.None:
throw new YooPackageException(PackageName, "Resource package not initialized.");
case EOperationStatus.Processing:
throw new YooPackageException(PackageName, "Resource package initialization not completed.");
case EOperationStatus.Failed:
string error = _initializeOp == null ? string.Empty : _initializeOp.Error;
throw new YooPackageException(PackageName, $"Resource package initialization failed. Error: {error}");
}
}