refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-16 10:28:01 +08:00
parent 2b6e7856e7
commit b0e85017d2
100 changed files with 1042 additions and 1582 deletions
@@ -10,8 +10,7 @@ namespace YooAsset
{
private InitializePackageOperation _initializeOp;
internal ResourceManager _resourceManager;
internal IBundleQuery _bundleQuery;
internal IPlayMode _playModeImpl;
internal FileSystemHost _fileSystemHost;
internal EPlayMode _playMode;
/// <summary>
@@ -39,9 +38,9 @@ namespace YooAsset
{
get
{
if (_playModeImpl == null)
if (_fileSystemHost == null)
return false;
return _playModeImpl.ActiveManifest != null;
return _fileSystemHost.ActiveManifest != null;
}
}
@@ -66,7 +65,6 @@ namespace YooAsset
internal void InternalDestroy()
{
_initializeOp = null;
_bundleQuery = null;
// 销毁资源管理器
if (_resourceManager != null)
@@ -75,11 +73,11 @@ namespace YooAsset
_resourceManager = null;
}
// 销毁文件系统
if (_playModeImpl != null)
// 销毁文件系统中枢
if (_fileSystemHost != null)
{
_playModeImpl.DestroyFileSystem();
_playModeImpl = null;
_fileSystemHost.Destroy();
_fileSystemHost = null;
}
}
@@ -135,7 +133,7 @@ namespace YooAsset
public RequestPackageVersionOperation RequestPackageVersionAsync(RequestPackageVersionOptions options)
{
DebugCheckInitialize(false);
var operation = _playModeImpl.RequestPackageVersionAsync(options);
var operation = new RequestPackageVersionOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@@ -153,7 +151,7 @@ namespace YooAsset
YooLogger.Warning($"Found loaded bundle before update manifest ! Recommended to call the {nameof(UnloadAllAssetsAsync)} method to release loaded bundle !");
}
var operation = _playModeImpl.LoadPackageManifestAsync(options);
var operation = new LoadPackageManifestOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@@ -164,7 +162,7 @@ namespace YooAsset
public PreDownloadContentOperation PreDownloadContentAsync(PreDownloadContentOptions options)
{
DebugCheckInitialize(false);
var operation = _playModeImpl.PreDownloadContentAsync(options);
var operation = new PreDownloadContentOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@@ -175,7 +173,8 @@ namespace YooAsset
public ClearCacheFilesOperation ClearCacheFilesAsync(ClearCacheFilesOptions options)
{
DebugCheckInitialize(false);
var operation = _playModeImpl.ClearCacheFilesAsync(options);
options.Manifest = _fileSystemHost.ActiveManifest;
var operation = new ClearCacheFilesOperation(_fileSystemHost, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@@ -188,7 +187,7 @@ namespace YooAsset
public string GetPackageVersion()
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.PackageVersion;
return _fileSystemHost.ActiveManifest.PackageVersion;
}
/// <summary>
@@ -197,7 +196,7 @@ namespace YooAsset
public string GetPackageNote()
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.PackageNote;
return _fileSystemHost.ActiveManifest.PackageNote;
}
/// <summary>
@@ -206,7 +205,7 @@ namespace YooAsset
public PackageDetails GetPackageDetails()
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.GetPackageDetails();
return _fileSystemHost.ActiveManifest.GetPackageDetails();
}
#endregion
@@ -216,7 +215,7 @@ namespace YooAsset
/// </summary>
public UnloadAllAssetsOperation UnloadAllAssetsAsync()
{
var options = new UnloadAllAssetsOptions();
var options = new UnloadAllAssetsOptions(true, true);
return UnloadAllAssetsAsync(options);
}
@@ -235,12 +234,22 @@ namespace YooAsset
/// <summary>
/// 回收不再使用的资源
/// 说明:卸载引用计数为零的资源
/// 说明:默认循环10次
/// </summary>
/// <param name="loopCount">循环迭代次数</param>
public UnloadUnusedAssetsOperation UnloadUnusedAssetsAsync(int loopCount = 10)
public UnloadUnusedAssetsOperation UnloadUnusedAssetsAsync()
{
var options = new UnloadUnusedAssetsOptions(10);
return UnloadUnusedAssetsAsync(options);
}
/// <summary>
/// 回收不再使用的资源
/// 说明:卸载引用计数为零的资源
/// </summary>
public UnloadUnusedAssetsOperation UnloadUnusedAssetsAsync(UnloadUnusedAssetsOptions options)
{
DebugCheckInitialize();
var operation = new UnloadUnusedAssetsOperation(_resourceManager, loopCount);
var operation = new UnloadUnusedAssetsOperation(_resourceManager, options);
OperationSystem.StartOperation(PackageName, operation);
return operation;
}
@@ -295,7 +304,7 @@ namespace YooAsset
public AssetInfo[] GetAllAssetInfos()
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.GetAllAssetInfos();
return _fileSystemHost.ActiveManifest.GetAllAssetInfos();
}
/// <summary>
@@ -306,7 +315,7 @@ namespace YooAsset
{
DebugCheckInitialize();
string[] tags = new string[] { tag };
return _playModeImpl.ActiveManifest.GetAssetInfosByTags(tags);
return _fileSystemHost.ActiveManifest.GetAssetInfosByTags(tags);
}
/// <summary>
@@ -316,7 +325,7 @@ namespace YooAsset
public AssetInfo[] GetAssetInfos(string[] tags)
{
DebugCheckInitialize();
return _playModeImpl.ActiveManifest.GetAssetInfosByTags(tags);
return _fileSystemHost.ActiveManifest.GetAssetInfosByTags(tags);
}
/// <summary>
@@ -368,7 +377,7 @@ namespace YooAsset
public bool CheckLocationValid(string location)
{
DebugCheckInitialize();
string assetPath = _playModeImpl.ActiveManifest.TryMappingToAssetPath(location);
string assetPath = _fileSystemHost.ActiveManifest.TryMappingToAssetPath(location);
return string.IsNullOrEmpty(assetPath) == false;
}
@@ -380,11 +389,11 @@ namespace YooAsset
return false;
}
BundleInfo bundleInfo = _bundleQuery.GetMainBundleInfo(assetInfo);
BundleInfo bundleInfo = _fileSystemHost.GetMainBundleInfo(assetInfo);
if (bundleInfo.IsNeedDownloadFromRemote())
return true;
List<BundleInfo> depends = _bundleQuery.GetDependBundleInfos(assetInfo);
List<BundleInfo> depends = _fileSystemHost.GetDependBundleInfos(assetInfo);
foreach (var depend in depends)
{
if (depend.IsNeedDownloadFromRemote())
@@ -849,192 +858,55 @@ namespace YooAsset
#endregion
#region 资源下载
/// <summary>
/// 创建资源下载器,用于下载当前资源版本所有的资源包文件
/// </summary>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateResourceDownloader(int downloadingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByAll(downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源标签关联的资源包文件
/// </summary>
/// <param name="tag">资源标签</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateResourceDownloader(string tag, int downloadingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByTags(new string[] { tag }, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源标签列表关联的资源包文件
/// </summary>
/// <param name="tags">资源标签列表</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateResourceDownloader(string[] tags, int downloadingMaxNumber, int failedTryAgain)
public ResourceDownloaderOperation CreateResourceDownloader(ResourceDownloaderOptions options)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByTags(tags, downloadingMaxNumber, failedTryAgain);
return _fileSystemHost.CreateResourceDownloader(options);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源依赖的资源包文件
/// 创建资源下载器,用于下载指定的资源信息列表依赖的资源包文件
/// </summary>
/// <param name="location">资源的定位地址</param>
/// <param name="recursiveDownload">下载资源对象所属资源包内所有资源对象依赖的资源包</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateBundleDownloader(string location, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain)
public ResourceDownloaderOperation CreateBundleDownloader(BundleDownloaderOptions options)
{
DebugCheckInitialize();
var assetInfo = ConvertLocationToAssetInfo(location, null);
AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(string location, int downloadingMaxNumber, int failedTryAgain)
{
return CreateBundleDownloader(location, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="locations">资源的定位地址列表</param>
/// <param name="recursiveDownload">下载资源对象所属资源包内所有资源对象依赖的资源包</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
List<AssetInfo> assetInfos = new List<AssetInfo>(locations.Length);
foreach (var location in locations)
{
var assetInfo = ConvertLocationToAssetInfo(location, null);
assetInfos.Add(assetInfo);
}
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos.ToArray(), recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(string[] locations, int downloadingMaxNumber, int failedTryAgain)
{
return CreateBundleDownloader(locations, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源依赖的资源包文件
/// </summary>
/// <param name="assetInfo">资源信息</param>
/// <param name="recursiveDownload">下载资源对象所属资源包内所有资源对象依赖的资源包</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
AssetInfo[] assetInfos = new AssetInfo[] { assetInfo };
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo assetInfo, int downloadingMaxNumber, int failedTryAgain)
{
return CreateBundleDownloader(assetInfo, false, downloadingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源下载器,用于下载指定的资源列表依赖的资源包文件
/// </summary>
/// <param name="assetInfos">资源信息列表</param>
/// <param name="recursiveDownload">下载资源对象所属资源包内所有资源对象依赖的资源包</param>
/// <param name="downloadingMaxNumber">同时下载的最大文件数</param>
/// <param name="failedTryAgain">下载失败的重试次数</param>
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, bool recursiveDownload, int downloadingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceDownloaderByPaths(assetInfos, recursiveDownload, downloadingMaxNumber, failedTryAgain);
}
public ResourceDownloaderOperation CreateBundleDownloader(AssetInfo[] assetInfos, int downloadingMaxNumber, int failedTryAgain)
{
return CreateBundleDownloader(assetInfos, false, downloadingMaxNumber, failedTryAgain);
return _fileSystemHost.CreateResourceDownloader(options);
}
#endregion
#region 资源解压
/// <summary>
/// 创建内置资源解压器,用于解压当前资源版本所有的资源包文件
/// </summary>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public ResourceUnpackerOperation CreateResourceUnpacker(int unpackingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByAll(unpackingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建内置资源解压器,用于解压指定的资源标签关联的资源包文件
/// </summary>
/// <param name="tag">资源标签</param>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public ResourceUnpackerOperation CreateResourceUnpacker(string tag, int unpackingMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByTags(new string[] { tag }, unpackingMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建内置资源解压器,用于解压指定的资源标签列表关联的资源包文件
/// </summary>
/// <param name="tags">资源标签列表</param>
/// <param name="unpackingMaxNumber">同时解压的最大文件数</param>
/// <param name="failedTryAgain">解压失败的重试次数</param>
public ResourceUnpackerOperation CreateResourceUnpacker(string[] tags, int unpackingMaxNumber, int failedTryAgain)
public ResourceUnpackerOperation CreateResourceUnpacker(ResourceUnpackerOptions options)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceUnpackerByTags(tags, unpackingMaxNumber, failedTryAgain);
return _fileSystemHost.CreateResourceUnpacker(options);
}
#endregion
#region 资源导入
/// <summary>
/// 创建资源导入器
/// 注意:资源文件名称必须和资源服务器部署的文件名称一致!
/// </summary>
/// <param name="filePaths">资源路径列表</param>
/// <param name="importerMaxNumber">同时导入的最大文件数</param>
/// <param name="failedTryAgain">导入失败的重试次数</param>
public ResourceImporterOperation CreateResourceImporter(string[] filePaths, int importerMaxNumber, int failedTryAgain)
public ResourceImporterOperation CreateResourceImporter(BundleImporterOptions options)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceImporterByFilePaths(filePaths, importerMaxNumber, failedTryAgain);
}
/// <summary>
/// 创建资源导入器
/// 注意:资源信息里需要指定BundleName或BundleGUID!
/// </summary>
/// <param name="fileInfos">资源信息列表</param>
/// <param name="importerMaxNumber">同时导入的最大文件数</param>
/// <param name="failedTryAgain">导入失败的重试次数</param>
public ResourceImporterOperation CreateResourceImporter(ImportFileInfo[] fileInfos, int importerMaxNumber, int failedTryAgain)
{
DebugCheckInitialize();
return _playModeImpl.CreateResourceImporterByFileInfos(fileInfos, importerMaxNumber, failedTryAgain);
return _fileSystemHost.CreateResourceImporter(options);
}
#endregion
#region 内部方法
private AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
internal AssetInfo ConvertLocationToAssetInfo(string location, System.Type assetType)
{
return _playModeImpl.ActiveManifest.ConvertLocationToAssetInfo(location, assetType);
return _fileSystemHost.ActiveManifest.ConvertLocationToAssetInfo(location, assetType);
}
private AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType)
internal AssetInfo ConvertAssetGUIDToAssetInfo(string assetGUID, System.Type assetType)
{
return _playModeImpl.ActiveManifest.ConvertAssetGUIDToAssetInfo(assetGUID, assetType);
return _fileSystemHost.ActiveManifest.ConvertAssetGUIDToAssetInfo(assetGUID, assetType);
}
#endregion
@@ -1060,7 +932,7 @@ namespace YooAsset
if (checkActiveManifest)
{
if (_playModeImpl.ActiveManifest == null)
if (_fileSystemHost.ActiveManifest == null)
throw new YooPackageException(PackageName, "Can not found active package manifest !");
}
}