refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-17 16:12:03 +08:00
parent 3e3661ad95
commit da222c6179
9 changed files with 269 additions and 315 deletions
@@ -12,7 +12,7 @@ namespace YooAsset
/// <summary>
/// 当前激活的清单
/// </summary>
public PackageManifest ActiveManifest { private set; get; }
public PackageManifest ActiveManifest { get; private set; }
public FileSystemHost(string packageName)
@@ -75,8 +75,10 @@ namespace YooAsset
/// <summary>
/// 获取主文件系统
/// 说明:文件系统列表里,最后一个属于主文件系统
/// </summary>
/// <remarks>
/// 文件系统列表里,最后一个属于主文件系统
/// </remarks>
public IFileSystem GetMainFileSystem()
{
int count = FileSystems.Count;
@@ -123,7 +125,7 @@ namespace YooAsset
if (assetInfo == null || assetInfo.IsInvalid)
throw new YooInternalException();
// 注意:如果清单里未找到资源包会抛出异常!
// 注意:如果清单里未找到资源包会抛出异常
var packageBundle = ActiveManifest.GetMainPackageBundle(assetInfo.Asset);
return CreateBundleInfo(packageBundle);
}
@@ -161,6 +163,8 @@ namespace YooAsset
#endregion
#region 下载器相关
private const int DefaultBundleInfoCapacity = 1000;
public bool IsNeedDownloadFromRemoteInternal(AssetInfo assetInfo)
{
if (assetInfo.IsInvalid)
@@ -183,6 +187,20 @@ namespace YooAsset
return false;
}
private delegate bool NeedPredicate(IFileSystem fileSystem, PackageBundle bundle);
private bool NeedDownload(IFileSystem fileSystem, PackageBundle bundle)
{
return fileSystem.NeedDownload(bundle);
}
private bool NeedUnpack(IFileSystem fileSystem, PackageBundle bundle)
{
return fileSystem.NeedUnpack(bundle);
}
private bool NeedImport(IFileSystem fileSystem, PackageBundle bundle)
{
return fileSystem.NeedImport(bundle);
}
public ResourceDownloaderOperation CreateResourceDownloader(ResourceDownloaderOptions options)
{
return CreateResourceDownloader(ActiveManifest, options);
@@ -191,9 +209,9 @@ namespace YooAsset
{
List<BundleInfo> downloadList;
if (options.Tags == null)
downloadList = GetDownloadListByAll(manifest);
downloadList = GetBundleListByAll(manifest, NeedDownload);
else
downloadList = GetDownloadListByTags(manifest, options.Tags);
downloadList = GetBundleListByTags(manifest, options.Tags, NeedDownload);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, options.MaximumConcurrency, options.FailedTryAgain);
return operation;
@@ -207,9 +225,9 @@ namespace YooAsset
{
List<BundleInfo> downloadList;
if (options.AssetInfos == null)
downloadList = GetDownloadListByAll(manifest);
downloadList = GetBundleListByAll(manifest, NeedDownload);
else
downloadList = GetDownloadListByAssetInfos(manifest, options.AssetInfos, options.DownloadBundleDependencies);
downloadList = GetBundleListByAssetInfos(manifest, options.AssetInfos, options.DownloadBundleDependencies, NeedDownload);
var operation = new ResourceDownloaderOperation(PackageName, downloadList, options.MaximumConcurrency, options.FailedTryAgain);
return operation;
@@ -221,13 +239,13 @@ namespace YooAsset
}
public ResourceUnpackerOperation CreateResourceUnpacker(PackageManifest manifest, ResourceUnpackerOptions options)
{
List<BundleInfo> unpcakList;
List<BundleInfo> unpackList;
if (options.Tags == null)
unpcakList = GetUnpackListByAll(manifest);
unpackList = GetBundleListByAll(manifest, NeedUnpack);
else
unpcakList = GetUnpackListByTags(manifest, options.Tags);
unpackList = GetBundleListByTags(manifest, options.Tags, NeedUnpack);
var operation = new ResourceUnpackerOperation(PackageName, unpcakList, options.MaximumConcurrency, options.FailedTryAgain);
var operation = new ResourceUnpackerOperation(PackageName, unpackList, options.MaximumConcurrency, options.FailedTryAgain);
return operation;
}
@@ -237,24 +255,24 @@ namespace YooAsset
}
public ResourceImporterOperation CreateResourceImporter(PackageManifest manifest, BundleImporterOptions options)
{
List<BundleInfo> importerList = GetImporterListByBundleInfos(manifest, options.BundleInfos);
List<BundleInfo> importerList = GetBundleListByBundleInfos(manifest, options.BundleInfos, NeedImport);
var operation = new ResourceImporterOperation(PackageName, importerList, options.MaximumConcurrency, options.FailedTryAgain);
return operation;
}
private List<BundleInfo> GetDownloadListByAll(PackageManifest manifest)
private List<BundleInfo> GetBundleListByAll(PackageManifest manifest, Func<IFileSystem, PackageBundle, bool> predicate)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>(1000);
List<BundleInfo> result = new List<BundleInfo>(DefaultBundleInfoCapacity);
foreach (var packageBundle in manifest.BundleList)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
continue;
if (fileSystem.NeedDownload(packageBundle))
if (predicate(fileSystem, packageBundle))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
result.Add(bundleInfo);
@@ -262,21 +280,21 @@ namespace YooAsset
}
return result;
}
private List<BundleInfo> GetDownloadListByTags(PackageManifest manifest, string[] tags)
private List<BundleInfo> GetBundleListByTags(PackageManifest manifest, string[] tags, Func<IFileSystem, PackageBundle, bool> predicate)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>(1000);
List<BundleInfo> result = new List<BundleInfo>(DefaultBundleInfoCapacity);
foreach (var packageBundle in manifest.BundleList)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
continue;
if (fileSystem.NeedDownload(packageBundle))
if (predicate(fileSystem, packageBundle))
{
// 如果未带任何标记,则统一下载
// 注意:如果未带任何标记,则统一处理
if (packageBundle.HasAnyTags() == false)
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
@@ -284,7 +302,6 @@ namespace YooAsset
}
else
{
// 查询DLC资源
if (packageBundle.HasTag(tags))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
@@ -295,7 +312,7 @@ namespace YooAsset
}
return result;
}
private List<BundleInfo> GetDownloadListByAssetInfos(PackageManifest manifest, AssetInfo[] assetInfos, bool recursiveDownload)
private List<BundleInfo> GetBundleListByAssetInfos(PackageManifest manifest, AssetInfo[] assetInfos, bool recursiveDepend, Func<IFileSystem, PackageBundle, bool> predicate)
{
if (manifest == null)
return new List<BundleInfo>();
@@ -331,7 +348,7 @@ namespace YooAsset
}
// 下载主资源包内所有资源对象依赖的资源包
if (recursiveDownload)
if (recursiveDepend)
{
foreach (var otherMainAsset in mainBundle.IncludeMainAssets)
{
@@ -355,14 +372,14 @@ namespace YooAsset
}
}
List<BundleInfo> result = new List<BundleInfo>(1000);
List<BundleInfo> result = new List<BundleInfo>(DefaultBundleInfoCapacity);
foreach (var packageBundle in checkList)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
continue;
if (fileSystem.NeedDownload(packageBundle))
if (predicate(fileSystem, packageBundle))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
result.Add(bundleInfo);
@@ -370,62 +387,19 @@ namespace YooAsset
}
return result;
}
private List<BundleInfo> GetUnpackListByAll(PackageManifest manifest)
private List<BundleInfo> GetBundleListByBundleInfos(PackageManifest manifest, ImportBundleInfo[] fileInfos, Func<IFileSystem, PackageBundle, bool> predicate)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>(1000);
foreach (var packageBundle in manifest.BundleList)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
continue;
if (fileSystem.NeedUnpack(packageBundle))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
result.Add(bundleInfo);
}
}
return result;
}
private List<BundleInfo> GetUnpackListByTags(PackageManifest manifest, string[] tags)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>(1000);
foreach (var packageBundle in manifest.BundleList)
{
var fileSystem = GetBelongFileSystem(packageBundle);
if (fileSystem == null)
continue;
if (fileSystem.NeedUnpack(packageBundle))
{
if (packageBundle.HasTag(tags))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle);
result.Add(bundleInfo);
}
}
}
return result;
}
private List<BundleInfo> GetImporterListByBundleInfos(PackageManifest manifest, ImportBundleInfo[] fileInfos)
{
if (manifest == null)
return new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>();
List<BundleInfo> result = new List<BundleInfo>(DefaultBundleInfoCapacity);
foreach (var fileInfo in fileInfos)
{
string filePath = fileInfo.FilePath;
if (string.IsNullOrEmpty(filePath))
continue;
PackageBundle packageBundle = null;
PackageBundle packageBundle;
if (string.IsNullOrEmpty(fileInfo.BundleName) == false)
{
if (manifest.TryGetPackageBundleByBundleName(fileInfo.BundleName, out packageBundle) == false)
@@ -458,7 +432,7 @@ namespace YooAsset
if (fileSystem == null)
continue;
if (fileSystem.NeedImport(packageBundle))
if (predicate(fileSystem, packageBundle))
{
var bundleInfo = new BundleInfo(fileSystem, packageBundle, filePath);
result.Add(bundleInfo);