refactor : 代码重构

This commit is contained in:
何冠峰
2026-02-25 19:03:42 +08:00
parent 811eb5d82b
commit ba6db2ff4d
245 changed files with 1521 additions and 1207 deletions

View File

@@ -206,8 +206,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var providerTableData = data as ProviderTableData;
if (providerTableData.ProviderInfo.Status == EOperationStatus.Failed.ToString() ||
providerTableData.ProviderInfo.Status == EOperationStatus.Aborted.ToString())
if (providerTableData.ProviderInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
@@ -281,8 +280,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var dependTableData = data as DependTableData;
if (dependTableData.BundleInfo.Status == EOperationStatus.Failed.ToString() ||
dependTableData.BundleInfo.Status == EOperationStatus.Aborted.ToString())
if (dependTableData.BundleInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);

View File

@@ -151,8 +151,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var bundleTableData = data as BundleTableData;
if (bundleTableData.BundleInfo.Status == EOperationStatus.Failed.ToString() ||
bundleTableData.BundleInfo.Status == EOperationStatus.Aborted.ToString())
if (bundleTableData.BundleInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
@@ -268,8 +267,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var usingTableData = data as UsingTableData;
if (usingTableData.ProviderInfo.Status == EOperationStatus.Failed.ToString() ||
usingTableData.ProviderInfo.Status == EOperationStatus.Aborted.ToString())
if (usingTableData.ProviderInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
@@ -343,8 +341,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var feferenceTableData = data as ReferenceTableData;
if (feferenceTableData.BundleInfo.Status == EOperationStatus.Failed.ToString() ||
feferenceTableData.BundleInfo.Status == EOperationStatus.Aborted.ToString())
if (feferenceTableData.BundleInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);

View File

@@ -207,8 +207,7 @@ namespace YooAsset.Editor
{
StyleColor textColor;
var operationTableData = data as OperationTableData;
if (operationTableData.OperationInfo.Status == EOperationStatus.Failed.ToString() ||
operationTableData.OperationInfo.Status == EOperationStatus.Aborted.ToString())
if (operationTableData.OperationInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);
@@ -480,8 +479,7 @@ namespace YooAsset.Editor
// Status
{
StyleColor textColor;
if (operationInfo.Status == EOperationStatus.Failed.ToString() ||
operationInfo.Status == EOperationStatus.Aborted.ToString())
if (operationInfo.Status == EOperationStatus.Failed.ToString())
textColor = new StyleColor(Color.yellow);
else
textColor = new StyleColor(Color.white);

View File

@@ -78,13 +78,13 @@ namespace YooAsset
public float Progress { get; protected set; }
/// <summary>
/// 任务逻辑是否完成Status为Succeeded、Failed或Aborted
/// 任务逻辑是否完成Status为Succeeded、Failed
/// </summary>
public bool IsDone
{
get
{
return Status == EOperationStatus.Succeeded || Status == EOperationStatus.Failed || Status == EOperationStatus.Aborted;
return Status == EOperationStatus.Succeeded || Status == EOperationStatus.Failed;
}
}
@@ -306,7 +306,7 @@ namespace YooAsset
if (IsDone == false)
{
InternalAbort();
Status = EOperationStatus.Aborted;
Status = EOperationStatus.Failed;
Error = "Aborted by user";
YooLogger.Warning($"Async operation {this.GetType().Name} has been aborted.");
}

View File

@@ -25,10 +25,5 @@ namespace YooAsset
/// 已失败
/// </summary>
Failed,
/// <summary>
/// 已中止(用户主动取消)
/// </summary>
Aborted,
}
}

View File

@@ -0,0 +1,48 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 淘汰策略执行结果
/// </summary>
internal readonly struct EvictionResult
{
/// <summary>
/// 错误信息
/// </summary>
public readonly string Error;
/// <summary>
/// 需要清理的资源标识符集合
/// </summary>
public readonly List<string> BundleGUIDs;
/// <summary>
/// 是否成功
/// </summary>
public bool Succeeded
{
get { return Error == null; }
}
public EvictionResult(string error)
{
Error = error;
BundleGUIDs = null;
}
public EvictionResult(List<string> bundleGUIDs)
{
Error = null;
BundleGUIDs = bundleGUIDs;
}
public static EvictionResult Success(List<string> bundleGUIDs)
{
return new EvictionResult(bundleGUIDs);
}
public static EvictionResult Failure(string error)
{
return new EvictionResult(error);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1cfc5979929f8cd43b58e1fcc541eaa8
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 文件缓存系统接口
/// </summary>
internal interface IFileCache : IDisposable
internal interface IBundleCache : IDisposable
{
/// <summary>
/// 包裹名称
@@ -37,27 +36,27 @@ namespace YooAsset
/// <summary>
/// 初始化文件缓存系统
/// </summary>
FCInitializeOperation InitializeAsync();
BCInitializeOperation InitializeAsync();
/// <summary>
/// 写入缓存文件
/// </summary>
FCWriteCacheOperation WriteCacheAsync(FCWriteCacheOptions options);
BCWriteCacheOperation WriteCacheAsync(BCWriteCacheOptions options);
/// <summary>
/// 清理缓存文件
/// </summary>
FCClearCacheOperation ClearCacheAsync(FCClearCacheOptions options);
BCClearCacheOperation ClearCacheAsync(BCClearCacheOptions options);
/// <summary>
/// 验证缓存文件
/// </summary>
FCVerifyCacheOperation VerifyCacheAsync(FCVerifyCacheOptions options);
BCVerifyCacheOperation VerifyCacheAsync(BCVerifyCacheOptions options);
/// <summary>
/// 加载资源包
/// </summary>
FCLoadBundleOperation LoadBundleAsync(FCLoadBundleOptions options);
BCLoadBundleOperation LoadBundleAsync(BCLoadBundleOptions options);
/// <summary>
/// 是否已缓存指定 Bundle

View File

@@ -0,0 +1,15 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 缓存淘汰策略接口
/// </summary>
internal interface ICacheEvictionPolicy
{
/// <summary>
/// 根据策略从缓存条目中选出需要清理的 BundleGUID 列表
/// </summary>
EvictionResult SelectEvictionTargets(IReadOnlyCollection<ICacheEntry> cacheEntries, BCClearCacheOptions options);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c113f2d8eaa9d58408001ddea8d20020
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
namespace YooAsset
{
/// <summary>
/// 清理缓存操作基类
/// </summary>
internal abstract class BCClearCacheOperation : AsyncOperationBase
{
}
/// <summary>
/// 清理缓存完成操作
/// </summary>
internal class BCClearCacheCompleteOperation : BCClearCacheOperation
{
private readonly string _error;
public BCClearCacheCompleteOperation()
{
_error = null;
}
public BCClearCacheCompleteOperation(string error)
{
_error = error;
}
internal override void InternalStart()
{
if (string.IsNullOrEmpty(_error))
{
Status = EOperationStatus.Succeeded;
}
else
{
Status = EOperationStatus.Failed;
Error = _error;
}
}
internal override void InternalUpdate()
{
}
}
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 清理缓存操作选项
/// </summary>
internal struct FCClearCacheOptions
internal struct BCClearCacheOptions
{
/// <summary>
/// 清理模式

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 文件缓存初始化操作基类
/// </summary>
internal abstract class FCInitializeOperation : AsyncOperationBase
internal abstract class BCInitializeOperation : AsyncOperationBase
{
}
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 加载资源包操作基类
/// </summary>
internal abstract class FCLoadBundleOperation : AsyncOperationBase
internal abstract class BCLoadBundleOperation : AsyncOperationBase
{
protected readonly struct LoadResult
{
@@ -45,11 +45,11 @@ namespace YooAsset
/// <summary>
/// 加载资源包失败操作
/// </summary>
internal sealed class FCLoadBundleErrorOperation : FCLoadBundleOperation
internal sealed class BCLoadBundleErrorOperation : BCLoadBundleOperation
{
private readonly string _error;
internal FCLoadBundleErrorOperation(string error)
internal BCLoadBundleErrorOperation(string error)
{
_error = error;
}

View File

@@ -4,14 +4,14 @@ namespace YooAsset
/// <summary>
/// 加载资源包操作选项
/// </summary>
internal readonly struct FCLoadBundleOptions
internal readonly struct BCLoadBundleOptions
{
/// <summary>
/// 资源包
/// </summary>
public readonly PackageBundle Bundle;
public FCLoadBundleOptions(PackageBundle bundle)
public BCLoadBundleOptions(PackageBundle bundle)
{
Bundle = bundle;
}

View File

@@ -4,22 +4,22 @@ namespace YooAsset
/// <summary>
/// 验证缓存操作基类
/// </summary>
internal abstract class FCVerifyCacheOperation : AsyncOperationBase
internal abstract class BCVerifyCacheOperation : AsyncOperationBase
{
}
/// <summary>
/// 验证缓存完成操作
/// </summary>
internal class FCVerifyCacheCompleteOperation : FCVerifyCacheOperation
internal class BCVerifyCacheCompleteOperation : BCVerifyCacheOperation
{
private readonly string _error;
public FCVerifyCacheCompleteOperation()
public BCVerifyCacheCompleteOperation()
{
_error = null;
}
public FCVerifyCacheCompleteOperation(string error)
public BCVerifyCacheCompleteOperation(string error)
{
_error = error;
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 验证缓存操作选项
/// </summary>
internal struct FCVerifyCacheOptions
internal struct BCVerifyCacheOptions
{
/// <summary>
/// 要验证的资源包

View File

@@ -4,22 +4,22 @@ namespace YooAsset
/// <summary>
/// 写入缓存操作基类
/// </summary>
internal abstract class FCWriteCacheOperation : AsyncOperationBase
internal abstract class BCWriteCacheOperation : AsyncOperationBase
{
}
/// <summary>
/// 写入缓存完成操作
/// </summary>
internal class FCWriteCacheCompleteOperation : FCWriteCacheOperation
internal class BCWriteCacheCompleteOperation : BCWriteCacheOperation
{
private readonly string _error;
public FCWriteCacheCompleteOperation()
public BCWriteCacheCompleteOperation()
{
_error = null;
}
public FCWriteCacheCompleteOperation(string error)
public BCWriteCacheCompleteOperation(string error)
{
_error = error;
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 写入缓存操作选项
/// </summary>
internal struct FCWriteCacheOptions
internal struct BCWriteCacheOptions
{
/// <summary>
/// 要缓存的资源包

View File

@@ -69,7 +69,7 @@ namespace YooAsset
// 注意从Web服务器下载数据
if (_downloadBytesRequest == null)
{
string url = DownloadSystemTools.ToLocalUrl(_options.FilePath);
string url = DownloadSystemTools.ToLocalFileUrl(_options.FilePath);
var args = new DownloadDataRequestArgs(url, 60, 0);
_downloadBytesRequest = _options.DownloadBackend.CreateBytesRequest(args);
_downloadBytesRequest.SendRequest();

View File

@@ -6,7 +6,7 @@ namespace YooAsset
/// <summary>
/// 从本地文件加载 AssetBundle 操作
/// </summary>
internal class LoadLocalAssetBundleOperation : FCLoadBundleOperation
internal class LoadLocalAssetBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{

View File

@@ -6,7 +6,7 @@ namespace YooAsset
/// <summary>
/// 从本地加载 RawBundle 操作
/// </summary>
internal class LoadLocalRawBundleOperation : FCLoadBundleOperation
internal class LoadLocalRawBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{

View File

@@ -5,7 +5,7 @@ namespace YooAsset
/// <summary>
/// 从网络加载 AssetBundle 操作的抽象基类
/// </summary>
internal abstract class LoadWebAssetBundleOperation : FCLoadBundleOperation
internal abstract class LoadWebAssetBundleOperation : BCLoadBundleOperation
{
}
@@ -23,8 +23,8 @@ namespace YooAsset
Done,
}
protected readonly LoadWebAssetBundleOptions _options;
private readonly DownloadRetry _downloadRetry;
private readonly LoadWebAssetBundleOptions _options;
private readonly DownloadRetryController _downloadRetryController;
private IDownloadAssetBundleRequest _downloadAssetBundleRequest;
private ESteps _steps = ESteps.None;
@@ -33,7 +33,7 @@ namespace YooAsset
_options = options;
// 注意:网络原因失败后,重新尝试直到成功
_downloadRetry = new DownloadRetry(int.MaxValue, options.RetryPolicy);
_downloadRetryController = new DownloadRetryController(int.MaxValue, options.RetryPolicy);
}
internal override void InternalStart()
{
@@ -61,7 +61,7 @@ namespace YooAsset
if (_downloadAssetBundleRequest.Status == EDownloadRequestStatus.Succeeded)
{
_options.URLPolicy.OnSuccess(_downloadAssetBundleRequest.Url);
_options.URLPolicy.OnRequestSucceeded(_downloadAssetBundleRequest.Url);
var assetBundle = _downloadAssetBundleRequest.Result;
if (assetBundle == null)
{
@@ -81,10 +81,10 @@ namespace YooAsset
string url = _downloadAssetBundleRequest.Url;
long httpCode = _downloadAssetBundleRequest.HttpCode;
string httpError = _downloadAssetBundleRequest.HttpError;
_options.URLPolicy.OnFailure(url, httpCode, httpError);
if (IsWaitForCompletion == false && _downloadRetry.CanRetry(url, httpCode, httpError))
_options.URLPolicy.OnRequestFailed(url, httpCode, httpError);
if (IsWaitForCompletion == false && _downloadRetryController.CanRetryRequest(url, httpCode, httpError))
{
_downloadRetry.BeginWait();
_downloadRetryController.StartRetryDelay();
_steps = ESteps.TryAgain;
}
else
@@ -105,7 +105,7 @@ namespace YooAsset
_downloadAssetBundleRequest = null;
}
if (_downloadRetry.Tick())
if (_downloadRetryController.UpdateRetryDelay())
{
Progress = 0f;
_steps = ESteps.BundleRequest;
@@ -123,7 +123,7 @@ namespace YooAsset
private string GetRequestURL()
{
return _options.URLPolicy.SelectURL(_options.CandidateURLs);
return _options.URLPolicy.SelectUrl(_options.CandidateURLs);
}
}
@@ -144,8 +144,8 @@ namespace YooAsset
Done,
}
protected readonly LoadWebAssetBundleOptions _options;
private readonly DownloadRetry _downloadRetry;
private readonly LoadWebAssetBundleOptions _options;
private readonly DownloadRetryController _downloadRetryController;
private IDownloadBytesRequest _downloadBytesRequest;
private IBundleMemoryDecryptor _decryptor;
private AssetBundleCreateRequest _createRequest;
@@ -156,7 +156,7 @@ namespace YooAsset
_options = options;
// 注意:网络原因失败后,重新尝试直到成功
_downloadRetry = new DownloadRetry(int.MaxValue, options.RetryPolicy);
_downloadRetryController = new DownloadRetryController(int.MaxValue, options.RetryPolicy);
}
internal override void InternalStart()
{
@@ -204,7 +204,7 @@ namespace YooAsset
if (_downloadBytesRequest.Status == EDownloadRequestStatus.Succeeded)
{
_options.URLPolicy.OnSuccess(_downloadBytesRequest.Url);
_options.URLPolicy.OnRequestSucceeded(_downloadBytesRequest.Url);
_steps = ESteps.VerifyData;
}
else
@@ -212,10 +212,10 @@ namespace YooAsset
string url = _downloadBytesRequest.Url;
long httpCode = _downloadBytesRequest.HttpCode;
string httpError = _downloadBytesRequest.HttpError;
_options.URLPolicy.OnFailure(url, httpCode, httpError);
if (IsWaitForCompletion == false && _downloadRetry.CanRetry(url, httpCode, httpError))
_options.URLPolicy.OnRequestFailed(url, httpCode, httpError);
if (IsWaitForCompletion == false && _downloadRetryController.CanRetryRequest(url, httpCode, httpError))
{
_downloadRetry.BeginWait();
_downloadRetryController.StartRetryDelay();
_steps = ESteps.TryAgain;
}
else
@@ -247,9 +247,9 @@ namespace YooAsset
string error = $"[WebBundleVerify] Verify failed. Url:{_downloadBytesRequest.Url} Level: {_options.DownloadVerifyLevel} Result: {verifyResult}";
YooLogger.Warning(error);
if (IsWaitForCompletion == false && _downloadRetry.CanRetry())
if (IsWaitForCompletion == false && _downloadRetryController.HasRetryQuota())
{
_downloadRetry.BeginWait();
_downloadRetryController.StartRetryDelay();
_steps = ESteps.TryAgain;
}
else
@@ -304,7 +304,7 @@ namespace YooAsset
_downloadBytesRequest = null;
}
if (_downloadRetry.Tick())
if (_downloadRetryController.UpdateRetryDelay())
{
Progress = 0f;
_steps = ESteps.DataRequest;
@@ -334,7 +334,7 @@ namespace YooAsset
}
private string GetRequestURL()
{
return _options.URLPolicy.SelectURL(_options.CandidateURLs);
return _options.URLPolicy.SelectUrl(_options.CandidateURLs);
}
}
}

View File

@@ -55,6 +55,6 @@ namespace YooAsset
/// <summary>
/// URL 选择策略
/// </summary>
public IDownloadURLPolicy URLPolicy { get; set; }
public IDownloadUrlPolicy URLPolicy { get; set; }
}
}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 4d3ccbed787d48c4d9051bac3503dacb
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,20 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 清理所有缓存文件
/// </summary>
internal class EvictionAllPolicy : ICacheEvictionPolicy
{
public EvictionResult SelectEvictionTargets(IReadOnlyCollection<ICacheEntry> cacheEntries, BCClearCacheOptions options)
{
var bundleGUIDs = new List<string>(cacheEntries.Count);
foreach (var entry in cacheEntries)
{
bundleGUIDs.Add(entry.BundleGUID);
}
return EvictionResult.Success(bundleGUIDs);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8bd939b4d035fe54fb4adc975a3b7980
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 按资源地址清理缓存文件
/// ClearParam 类型string / string[] / List<string>
/// </summary>
internal class EvictionByLocationsPolicy : ICacheEvictionPolicy
{
public EvictionResult SelectEvictionTargets(IReadOnlyCollection<ICacheEntry> cacheEntries, BCClearCacheOptions options)
{
if (options.Manifest == null)
return EvictionResult.Failure("Active package manifest not found.");
if (options.ClearParam == null)
return EvictionResult.Failure("Clear param is null.");
string[] locations;
if (options.ClearParam is string str)
locations = new string[] { str };
else if (options.ClearParam is List<string> list)
locations = list.ToArray();
else if (options.ClearParam is string[] array)
locations = array;
else
return EvictionResult.Failure($"Invalid clear param: {options.ClearParam.GetType().FullName}");
var bundleGUIDs = new List<string>(locations.Length);
foreach (var location in locations)
{
string assetPath = options.Manifest.TryMappingToAssetPath(location);
if (options.Manifest.TryGetPackageAsset(assetPath, out PackageAsset packageAsset))
{
PackageBundle bundle = options.Manifest.GetMainPackageBundle(packageAsset.BundleID);
bundleGUIDs.Add(bundle.BundleGUID);
}
}
return EvictionResult.Success(bundleGUIDs);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1f8b4ec99aecd94f8da510f81f12e09
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,40 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 按标签清理缓存文件
/// ClearParam 类型string / string[] / List<string>
/// </summary>
internal class EvictionByTagsPolicy : ICacheEvictionPolicy
{
public EvictionResult SelectEvictionTargets(IReadOnlyCollection<ICacheEntry> cacheEntries, BCClearCacheOptions options)
{
if (options.Manifest == null)
return EvictionResult.Failure("Active package manifest not found.");
if (options.ClearParam == null)
return EvictionResult.Failure("Clear param is null.");
string[] tags;
if (options.ClearParam is string str)
tags = new string[] { str };
else if (options.ClearParam is List<string> list)
tags = list.ToArray();
else if (options.ClearParam is string[] array)
tags = array;
else
return EvictionResult.Failure($"Invalid clear param: {options.ClearParam.GetType().FullName}");
var bundleGUIDs = new List<string>(cacheEntries.Count);
foreach (var entry in cacheEntries)
{
if (options.Manifest.TryGetPackageBundleByBundleGUID(entry.BundleGUID, out PackageBundle bundle))
{
if (bundle.HasTag(tags))
bundleGUIDs.Add(bundle.BundleGUID);
}
}
return EvictionResult.Success(bundleGUIDs);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 839c397ca55acea439d217ab3b504565
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 清理 Manifest 中未引用的缓存文件
/// </summary>
internal class EvictionUnusedPolicy : ICacheEvictionPolicy
{
public EvictionResult SelectEvictionTargets(IReadOnlyCollection<ICacheEntry> cacheEntries, BCClearCacheOptions options)
{
if (options.Manifest == null)
return EvictionResult.Failure("Active package manifest not found.");
var bundleGUIDs = new List<string>(cacheEntries.Count);
foreach (var entry in cacheEntries)
{
if (options.Manifest.IsIncludeBundleFile(entry.BundleGUID) == false)
{
bundleGUIDs.Add(entry.BundleGUID);
}
}
return EvictionResult.Success(bundleGUIDs);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b9e404b43b235964c86a0be5373bb450
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -6,7 +6,7 @@ namespace YooAsset
/// <summary>
/// 内置文件缓存系统,用于管理 StreamingAssets 中的资源包
/// </summary>
internal class BuiltinFileCache : IFileCache
internal class BuiltinBundleCache : IBundleCache
{
/// <summary>
/// 内置文件缓存配置
@@ -29,7 +29,7 @@ namespace YooAsset
public IDownloadBackend DownloadBackend { get; set; }
}
private readonly Dictionary<string, BuiltinFileCacheEntry> _cacheEntries = new Dictionary<string, BuiltinFileCacheEntry>(10000);
private readonly Dictionary<string, BuiltinBundleCacheEntry> _cacheEntries = new Dictionary<string, BuiltinBundleCacheEntry>(10000);
/// <summary>
/// 缓存配置
@@ -76,7 +76,7 @@ namespace YooAsset
/// <param name="packageName">包裹名称</param>
/// <param name="rootPath">缓存根目录</param>
/// <param name="config">缓存配置</param>
public BuiltinFileCache(string packageName, string rootPath, CacheConfig config)
public BuiltinBundleCache(string packageName, string rootPath, CacheConfig config)
{
PackageName = packageName;
RootPath = rootPath;
@@ -86,27 +86,27 @@ namespace YooAsset
public void Dispose()
{
}
public virtual FCInitializeOperation InitializeAsync()
public virtual BCInitializeOperation InitializeAsync()
{
var operation = new BFCInitializeOperation(this);
return operation;
}
public virtual FCWriteCacheOperation WriteCacheAsync(FCWriteCacheOptions options)
public virtual BCWriteCacheOperation WriteCacheAsync(BCWriteCacheOptions options)
{
var operation = new FCWriteCacheCompleteOperation($"{nameof(BuiltinFileCache)} is readonly.");
var operation = new BCWriteCacheCompleteOperation($"{nameof(BuiltinBundleCache)} is readonly.");
return operation;
}
public virtual FCClearCacheOperation ClearCacheAsync(FCClearCacheOptions options)
public virtual BCClearCacheOperation ClearCacheAsync(BCClearCacheOptions options)
{
var operation = new FCClearCacheCompleteOperation($"{nameof(BuiltinFileCache)} is readonly.");
var operation = new BCClearCacheCompleteOperation();
return operation;
}
public virtual FCVerifyCacheOperation VerifyCacheAsync(FCVerifyCacheOptions options)
public virtual BCVerifyCacheOperation VerifyCacheAsync(BCVerifyCacheOptions options)
{
var operation = new FCVerifyCacheCompleteOperation();
var operation = new BCVerifyCacheCompleteOperation();
return operation;
}
public virtual FCLoadBundleOperation LoadBundleAsync(FCLoadBundleOptions options)
public virtual BCLoadBundleOperation LoadBundleAsync(BCLoadBundleOptions options)
{
if (options.Bundle.BundleType == (int)EBundleType.AssetBundle)
{
@@ -120,8 +120,8 @@ namespace YooAsset
}
else
{
string error = $"{nameof(BuiltinFileCache)} does not support bundle type: {options.Bundle.BundleType}";
var operation = new FCLoadBundleErrorOperation(error);
string error = $"{nameof(BuiltinBundleCache)} does not support bundle type: {options.Bundle.BundleType}";
var operation = new BCLoadBundleErrorOperation(error);
return operation;
}
}
@@ -134,9 +134,9 @@ namespace YooAsset
/// <summary>
/// 获取指定缓存条目
/// </summary>
internal BuiltinFileCacheEntry GetEntry(string bundleGUID)
internal BuiltinBundleCacheEntry GetEntry(string bundleGUID)
{
if (_cacheEntries.TryGetValue(bundleGUID, out BuiltinFileCacheEntry entry))
if (_cacheEntries.TryGetValue(bundleGUID, out BuiltinBundleCacheEntry entry))
return entry;
else
return null;
@@ -145,7 +145,7 @@ namespace YooAsset
/// <summary>
/// 添加指定缓存条目
/// </summary>
internal void AddEntry(string bundleGUID, BuiltinFileCacheEntry cacheEntry)
internal void AddEntry(string bundleGUID, BuiltinBundleCacheEntry cacheEntry)
{
if (_cacheEntries.ContainsKey(bundleGUID))
throw new YooInternalException($"Cache entry already exists: {bundleGUID}");

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 内置文件缓存条目
/// </summary>
internal class BuiltinFileCacheEntry : ICacheEntry
internal class BuiltinBundleCacheEntry : ICacheEntry
{
/// <summary>
/// 资源包唯一标识
@@ -21,7 +21,7 @@ namespace YooAsset
/// </summary>
/// <param name="bundleGUID">资源包唯一标识</param>
/// <param name="filePath">资源包文件路径</param>
public BuiltinFileCacheEntry(string bundleGUID, string filePath)
public BuiltinBundleCacheEntry(string bundleGUID, string filePath)
{
BundleGUID = bundleGUID;
FilePath = filePath;

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 内置文件缓存初始化操作
/// </summary>
internal class BFCInitializeOperation : FCInitializeOperation
internal class BFCInitializeOperation : BCInitializeOperation
{
private enum ESteps
{
@@ -14,11 +14,11 @@ namespace YooAsset
Done,
}
private readonly BuiltinFileCache _fileCache;
private readonly BuiltinBundleCache _fileCache;
private LoadBuiltinCatalogOperation _loadBuiltinCatalogOp;
private ESteps _steps = ESteps.None;
public BFCInitializeOperation(BuiltinFileCache fileCache)
public BFCInitializeOperation(BuiltinBundleCache fileCache)
{
_fileCache = fileCache;
}
@@ -66,7 +66,7 @@ namespace YooAsset
foreach (var fileEntry in catalog.FileEntries)
{
string filePath = PathUtility.Combine(_fileCache.RootPath, fileEntry.FileName);
var cacheEntry = new BuiltinFileCacheEntry(fileEntry.BundleGUID, filePath);
var cacheEntry = new BuiltinBundleCacheEntry(fileEntry.BundleGUID, filePath);
_fileCache.AddEntry(fileEntry.BundleGUID, cacheEntry);
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 内置文件缓存加载 AssetBundle 操作
/// </summary>
internal class BFCLoadAssetBundleOperation : FCLoadBundleOperation
internal class BFCLoadAssetBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{
@@ -14,13 +14,13 @@ namespace YooAsset
Done,
}
private readonly BuiltinFileCache _fileCache;
private readonly BuiltinBundleCache _fileCache;
private readonly PackageBundle _bundle;
private LoadLocalAssetBundleOperation _loadLocalAssetBundleOp;
private BuiltinFileCacheEntry _cacheEntry;
private BuiltinBundleCacheEntry _cacheEntry;
private ESteps _steps = ESteps.None;
public BFCLoadAssetBundleOperation(BuiltinFileCache fileCache, PackageBundle bundle)
public BFCLoadAssetBundleOperation(BuiltinBundleCache fileCache, PackageBundle bundle)
{
_fileCache = fileCache;
_bundle = bundle;
@@ -96,7 +96,7 @@ namespace YooAsset
/// <summary>
/// 内置文件缓存加载 RawBundle 操作
/// </summary>
internal class BFCLoadRawBundleOperation : FCLoadBundleOperation
internal class BFCLoadRawBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{
@@ -106,13 +106,13 @@ namespace YooAsset
Done,
}
private readonly BuiltinFileCache _fileCache;
private readonly BuiltinBundleCache _fileCache;
private readonly PackageBundle _bundle;
private LoadLocalRawBundleOperation _loadLocalRawBundleOp;
private BuiltinFileCacheEntry _cacheEntry;
private BuiltinBundleCacheEntry _cacheEntry;
private ESteps _steps = ESteps.None;
public BFCLoadRawBundleOperation(BuiltinFileCache fileCache, PackageBundle bundle)
public BFCLoadRawBundleOperation(BuiltinBundleCache fileCache, PackageBundle bundle)
{
_fileCache = fileCache;
_bundle = bundle;

View File

@@ -6,7 +6,7 @@ namespace YooAsset
/// <summary>
/// 编辑器文件缓存系统,用于编辑器模式下的资源模拟加载
/// </summary>
internal class EditorFileCache : IFileCache
internal class EditorBundleCache : IBundleCache
{
/// <summary>
/// 编辑器文件缓存配置
@@ -34,7 +34,7 @@ namespace YooAsset
public int AsyncSimulateMaxFrame { get; set; }
}
private readonly Dictionary<string, EditorFileCacheEntry> _cacheEntries = new Dictionary<string, EditorFileCacheEntry>(10000);
private readonly Dictionary<string, EditorBundleCacheEntry> _cacheEntries = new Dictionary<string, EditorBundleCacheEntry>(10000);
/// <summary>
/// 缓存配置
@@ -81,7 +81,7 @@ namespace YooAsset
/// <param name="packageName">包裹名称</param>
/// <param name="rootPath">缓存根目录</param>
/// <param name="config">缓存配置</param>
public EditorFileCache(string packageName, string rootPath, CacheConfig config)
public EditorBundleCache(string packageName, string rootPath, CacheConfig config)
{
PackageName = packageName;
RootPath = rootPath;
@@ -91,37 +91,60 @@ namespace YooAsset
public void Dispose()
{
}
public virtual FCInitializeOperation InitializeAsync()
public virtual BCInitializeOperation InitializeAsync()
{
var operation = new EFCInitializeOperation(this);
var operation = new EBCInitializeOperation(this);
return operation;
}
public virtual FCWriteCacheOperation WriteCacheAsync(FCWriteCacheOptions options)
public virtual BCWriteCacheOperation WriteCacheAsync(BCWriteCacheOptions options)
{
var operation = new EFCWriteCacheOperation(this, options);
var operation = new EBCWriteCacheOperation(this, options);
return operation;
}
public virtual FCClearCacheOperation ClearCacheAsync(FCClearCacheOptions options)
public virtual BCClearCacheOperation ClearCacheAsync(BCClearCacheOptions options)
{
var operation = new EFCClearCacheOperation(this, options);
ICacheEvictionPolicy policy = CreateEvictionPolicy(options);
if (policy == null)
return new BCClearCacheCompleteOperation($"Invalid clear mode: {options.ClearMode}");
return new EBCClearCacheOperation(this, options, policy);
}
/// <summary>
/// 根据 ClearMode 创建对应的淘汰策略实例
/// </summary>
protected virtual ICacheEvictionPolicy CreateEvictionPolicy(BCClearCacheOptions options)
{
if (options.ClearMode == EFileClearMode.ClearAllBundleFiles.ToString())
return new EvictionAllPolicy();
if (options.ClearMode == EFileClearMode.ClearUnusedBundleFiles.ToString())
return new EvictionUnusedPolicy();
if (options.ClearMode == EFileClearMode.ClearBundleFilesByLocations.ToString())
return new EvictionByLocationsPolicy();
if (options.ClearMode == EFileClearMode.ClearBundleFilesByTags.ToString())
return new EvictionByTagsPolicy();
if (options.ClearParam is ICacheEvictionPolicy customPolicy)
return customPolicy;
return null;
}
public virtual BCVerifyCacheOperation VerifyCacheAsync(BCVerifyCacheOptions options)
{
var operation = new BCVerifyCacheCompleteOperation();
return operation;
}
public virtual FCVerifyCacheOperation VerifyCacheAsync(FCVerifyCacheOptions options)
{
var operation = new FCVerifyCacheCompleteOperation();
return operation;
}
public virtual FCLoadBundleOperation LoadBundleAsync(FCLoadBundleOptions options)
public virtual BCLoadBundleOperation LoadBundleAsync(BCLoadBundleOptions options)
{
if (options.Bundle.BundleType == (int)EBundleType.VirtualBundle)
{
var operation = new EFCLoadBundleOperation(this, options.Bundle);
var operation = new EBCLoadBundleOperation(this, options.Bundle);
return operation;
}
else
{
string error = $"{nameof(EditorFileCache)} does not support bundle type: {options.Bundle.BundleType}";
var operation = new FCLoadBundleErrorOperation(error);
string error = $"{nameof(EditorBundleCache)} does not support bundle type: {options.Bundle.BundleType}";
var operation = new BCLoadBundleErrorOperation(error);
return operation;
}
}
@@ -137,7 +160,7 @@ namespace YooAsset
/// <summary>
/// 获取所有缓存条目
/// </summary>
internal IReadOnlyCollection<EditorFileCacheEntry> GetAllEntries()
internal IReadOnlyCollection<EditorBundleCacheEntry> GetAllEntries()
{
return _cacheEntries.Values;
}
@@ -145,7 +168,7 @@ namespace YooAsset
/// <summary>
/// 添加指定缓存条目
/// </summary>
internal void AddEntry(string bundleGUID, EditorFileCacheEntry cacheEntry)
internal void AddEntry(string bundleGUID, EditorBundleCacheEntry cacheEntry)
{
if (_cacheEntries.ContainsKey(bundleGUID))
throw new YooInternalException($"Cache entry already exists: {bundleGUID}");
@@ -158,7 +181,7 @@ namespace YooAsset
/// </summary>
internal void RemoveEntry(string bundleGUID)
{
if (_cacheEntries.TryGetValue(bundleGUID, out EditorFileCacheEntry entry))
if (_cacheEntries.TryGetValue(bundleGUID, out EditorBundleCacheEntry entry))
{
_cacheEntries.Remove(bundleGUID);
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 编辑器文件缓存条目
/// </summary>
internal class EditorFileCacheEntry : ICacheEntry
internal class EditorBundleCacheEntry : ICacheEntry
{
/// <summary>
/// 资源包唯一标识
@@ -21,7 +21,7 @@ namespace YooAsset
/// </summary>
/// <param name="bundleGUID">资源包唯一标识</param>
/// <param name="filePath">资源包文件路径</param>
public EditorFileCacheEntry(string bundleGUID, string filePath)
public EditorBundleCacheEntry(string bundleGUID, string filePath)
{
BundleGUID = bundleGUID;
FilePath = filePath;

View File

@@ -0,0 +1,72 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 清理编辑器文件缓存操作
/// </summary>
internal sealed class EBCClearCacheOperation : BCClearCacheOperation
{
private enum ESteps
{
None,
GetResult,
ClearCacheFiles,
Done,
}
private readonly EditorBundleCache _fileCache;
private readonly BCClearCacheOptions _options;
private readonly ICacheEvictionPolicy _policy;
private List<string> _bundleGUIDs;
private ESteps _steps = ESteps.None;
internal EBCClearCacheOperation(EditorBundleCache fileCache, BCClearCacheOptions options, ICacheEvictionPolicy policy)
{
_fileCache = fileCache;
_options = options;
_policy = policy;
}
internal override void InternalStart()
{
_steps = ESteps.GetResult;
}
internal override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetResult)
{
var cacheEntries = _fileCache.GetAllEntries();
EvictionResult clearResult = _policy.SelectEvictionTargets(cacheEntries, _options);
if (clearResult.Succeeded == false)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = clearResult.Error;
return;
}
_bundleGUIDs = clearResult.BundleGUIDs;
_steps = ESteps.ClearCacheFiles;
}
if (_steps == ESteps.ClearCacheFiles)
{
foreach (var bundleGUID in _bundleGUIDs)
{
_fileCache.RemoveEntry(bundleGUID);
}
_steps = ESteps.Done;
Status = EOperationStatus.Succeeded;
}
}
internal override void InternalWaitForCompletion()
{
ExecuteBatch();
}
}
}

View File

@@ -4,11 +4,11 @@ namespace YooAsset
/// <summary>
/// 编辑器文件缓存初始化操作
/// </summary>
internal class EFCInitializeOperation : FCInitializeOperation
internal class EBCInitializeOperation : BCInitializeOperation
{
private readonly EditorFileCache _fileCache;
private readonly EditorBundleCache _fileCache;
public EFCInitializeOperation(EditorFileCache cache)
public EBCInitializeOperation(EditorBundleCache cache)
{
_fileCache = cache;
}

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 编辑器文件缓存加载资源包操作
/// </summary>
internal class EFCLoadBundleOperation : FCLoadBundleOperation
internal class EBCLoadBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{
@@ -15,13 +15,13 @@ namespace YooAsset
Done,
}
private readonly EditorFileCache _fileCache;
private readonly EditorBundleCache _fileCache;
private readonly PackageBundle _bundle;
private int _asyncSimulateFrame;
private string _editorFilePath;
private ESteps _steps = ESteps.None;
public EFCLoadBundleOperation(EditorFileCache fileCache, PackageBundle bundle)
public EBCLoadBundleOperation(EditorBundleCache fileCache, PackageBundle bundle)
{
_fileCache = fileCache;
_bundle = bundle;

View File

@@ -4,7 +4,7 @@ namespace YooAsset
/// <summary>
/// 编辑器文件缓存写入操作
/// </summary>
internal class EFCWriteCacheOperation : FCWriteCacheOperation
internal class EBCWriteCacheOperation : BCWriteCacheOperation
{
private enum ESteps
{
@@ -14,11 +14,11 @@ namespace YooAsset
Done,
}
private readonly EditorFileCache _fileCache;
private readonly FCWriteCacheOptions _options;
private readonly EditorBundleCache _fileCache;
private readonly BCWriteCacheOptions _options;
private ESteps _steps = ESteps.None;
public EFCWriteCacheOperation(EditorFileCache cache, FCWriteCacheOptions options)
public EBCWriteCacheOperation(EditorBundleCache cache, BCWriteCacheOptions options)
{
_fileCache = cache;
_options = options;
@@ -48,7 +48,7 @@ namespace YooAsset
if (_steps == ESteps.CacheFile)
{
var cacheEntry = new EditorFileCacheEntry(_options.Bundle.BundleGUID, _options.FilePath);
var cacheEntry = new EditorBundleCacheEntry(_options.Bundle.BundleGUID, _options.FilePath);
_fileCache.AddEntry(_options.Bundle.BundleGUID, cacheEntry);
_steps = ESteps.Done;
Status = EOperationStatus.Succeeded;

View File

@@ -1,4 +1,4 @@

namespace YooAsset
{
/// <summary>
@@ -7,44 +7,54 @@ namespace YooAsset
internal enum EFileVerifyResult
{
/// <summary>
/// 验证异常
/// 信息文件内容无效
/// </summary>
Exception = -8,
InfoFileInvalid = -24,
/// <summary>
/// 数据无效
/// 信息文件头标识不匹配
/// </summary>
BytesDataInvalid = -7,
InfoFileMagicError = -23,
/// <summary>
/// 未找到缓存信息
/// 信息文件版本不匹配
/// </summary>
CacheNotFound = -6,
InfoFileVersionError = -22,
/// <summary>
/// 信息文件不存在
/// </summary>
InfoFileNotExisted = -5,
InfoFileNotExisted = -21,
/// <summary>
/// 数据文件内容无效
/// </summary>
DataFileInvalid = -15,
/// <summary>
/// 数据文件内容不足(小于正常大小)
/// </summary>
DataFileNotComplete = -14,
/// <summary>
/// 数据文件内容溢出(超过正常大小)
/// </summary>
DataFileOverflow = -13,
/// <summary>
/// 数据文件内容不匹配
/// </summary>
DataFileCrcError = -12,
/// <summary>
/// 数据文件不存在
/// </summary>
DataFileNotExisted = -4,
DataFileNotExisted = -11,
/// <summary>
/// 文件内容不足(小于正常大小)
/// 验证异常
/// </summary>
FileNotComplete = -3,
/// <summary>
/// 文件内容溢出(超过正常大小)
/// </summary>
FileOverflow = -2,
/// <summary>
/// 文件内容不匹配
/// </summary>
FileCrcError = -1,
Exception = -1,
/// <summary>
/// 默认状态(校验未完成)

View File

@@ -27,9 +27,9 @@ namespace YooAsset
{
long size = FileUtility.GetFileSize(filePath);
if (size < fileSize)
return EFileVerifyResult.FileNotComplete;
return EFileVerifyResult.DataFileNotComplete;
else if (size > fileSize)
return EFileVerifyResult.FileOverflow;
return EFileVerifyResult.DataFileOverflow;
}
// 可选条件验证文件CRC
@@ -39,7 +39,7 @@ namespace YooAsset
if (crc == fileCRC)
return EFileVerifyResult.Succeed;
else
return EFileVerifyResult.FileCrcError;
return EFileVerifyResult.DataFileCrcError;
}
else
{
@@ -65,16 +65,16 @@ namespace YooAsset
try
{
if (fileData == null || fileData.Length == 0)
return EFileVerifyResult.BytesDataInvalid;
return EFileVerifyResult.DataFileInvalid;
// 可选条件:验证文件大小
if (fileSize > 0)
{
long size = fileData.Length;
if (size < fileSize)
return EFileVerifyResult.FileNotComplete;
return EFileVerifyResult.DataFileNotComplete;
else if (size > fileSize)
return EFileVerifyResult.FileOverflow;
return EFileVerifyResult.DataFileOverflow;
}
// 可选条件验证文件CRC
@@ -84,7 +84,7 @@ namespace YooAsset
if (crc == fileCRC)
return EFileVerifyResult.Succeed;
else
return EFileVerifyResult.FileCrcError;
return EFileVerifyResult.DataFileCrcError;
}
else
{

Some files were not shown because too many files have changed in this diff Show More