feat : add IBundleUnpackPolicy

This commit is contained in:
何冠峰
2026-05-27 12:08:40 +08:00
parent 926f72d129
commit 9e969d34f6
16 changed files with 233 additions and 110 deletions

View File

@@ -1,57 +0,0 @@
using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 淘汰策略执行结果
/// </summary>
internal readonly struct EvictionResult
{
private readonly bool _initialized;
/// <summary>
/// 错误信息
/// </summary>
public string Error { get; }
/// <summary>
/// 需要清理的资源标识符集合
/// </summary>
public IReadOnlyList<string> BundleGuids { get; }
/// <summary>
/// 是否执行成功
/// </summary>
public bool Succeeded
{
get { return _initialized && Error == null; }
}
private EvictionResult(string error, IReadOnlyList<string> bundleGuids)
{
_initialized = true;
Error = error;
BundleGuids = bundleGuids;
}
/// <summary>
/// 创建表示执行成功的淘汰结果
/// </summary>
/// <param name="bundleGuids">需要清理的资源包标识符列表</param>
/// <returns>携带待清理列表的成功结果</returns>
public static EvictionResult CreateSuccess(IReadOnlyList<string> bundleGuids)
{
return new EvictionResult(null, bundleGuids);
}
/// <summary>
/// 创建表示执行失败的淘汰结果
/// </summary>
/// <param name="error">描述失败原因的错误信息</param>
/// <returns>携带错误信息的失败结果</returns>
public static EvictionResult CreateFailure(string error)
{
return new EvictionResult(error, null);
}
}
}

View File

@@ -2,6 +2,59 @@ using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 淘汰策略执行结果
/// </summary>
internal readonly struct EvictionResult
{
private readonly bool _initialized;
/// <summary>
/// 错误信息
/// </summary>
public string Error { get; }
/// <summary>
/// 需要清理的资源标识符集合
/// </summary>
public IReadOnlyList<string> BundleGuids { get; }
/// <summary>
/// 是否执行成功
/// </summary>
public bool Succeeded
{
get { return _initialized && Error == null; }
}
private EvictionResult(string error, IReadOnlyList<string> bundleGuids)
{
_initialized = true;
Error = error;
BundleGuids = bundleGuids;
}
/// <summary>
/// 创建表示执行成功的淘汰结果
/// </summary>
/// <param name="bundleGuids">需要清理的资源包标识符列表</param>
/// <returns>携带待清理列表的成功结果</returns>
public static EvictionResult CreateSuccess(IReadOnlyList<string> bundleGuids)
{
return new EvictionResult(null, bundleGuids);
}
/// <summary>
/// 创建表示执行失败的淘汰结果
/// </summary>
/// <param name="error">描述失败原因的错误信息</param>
/// <returns>携带错误信息的失败结果</returns>
public static EvictionResult CreateFailure(string error)
{
return new EvictionResult(error, null);
}
}
/// <summary>
/// 缓存淘汰策略接口
/// </summary>

View File

@@ -140,5 +140,10 @@ namespace YooAsset
/// WebGL 平台策略 <see cref="IWebPlatformStrategy"/>
/// </summary>
WebPlatformStrategy,
/// <summary>
/// 内置资源包解包策略 <see cref="IBundleUnpackPolicy"/>
/// </summary>
BundleUnpackPolicy,
}
}

View File

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

View File

@@ -0,0 +1,21 @@
namespace YooAsset
{
internal sealed class DefaultBundleUnpackPolicy : IBundleUnpackPolicy
{
public bool IsUnpackBundle(BundleUnpackInfo unpackInfo)
{
#if UNITY_ANDROID || UNITY_OPENHARMONY
if (unpackInfo.IsEncrypted)
return true;
if (unpackInfo.BundleType == (int)EBundleType.RawBundle)
return true;
if (unpackInfo.BundleType == (int)EBundleType.ArchiveBundle)
return true;
return false;
#else
return false;
#endif
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 1cfc5979929f8cd43b58e1fcc541eaa8
guid: 76f39715fe286bd43b87ebab22ff1c9f
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -60,9 +60,7 @@ namespace YooAsset
/// </summary>
public IDownloadBackend DownloadBackend { get; private set; }
/// <summary>
/// 包裹名称
/// </summary>
/// <inheritdoc />
public string PackageName { get; private set; }
#region
@@ -142,6 +140,11 @@ namespace YooAsset
/// 自定义参数:资源清单解密器
/// </summary>
public IManifestDecryptor ManifestDecryptor { get; private set; }
/// <summary>
/// 自定义参数:内置资源包解包策略
/// </summary>
internal IBundleUnpackPolicy BundleUnpackPolicy { get; private set; }
#endregion
/// <summary>
@@ -286,6 +289,10 @@ namespace YooAsset
{
ManifestDecryptor = FileSystemHelper.CastParameter<IManifestDecryptor>(paramName, value);
}
else if (paramName == nameof(EFileSystemParameter.BundleUnpackPolicy))
{
BundleUnpackPolicy = FileSystemHelper.CastParameter<IBundleUnpackPolicy>(paramName, value);
}
else
{
throw new ArgumentException($"Unrecognized parameter name: '{paramName}'.", nameof(paramName));
@@ -311,6 +318,10 @@ namespace YooAsset
_unpackBundleFilesRoot = PathUtility.Combine(unpackRoot, BuiltinFileSystemConsts.UnpackBundleFilesFolderName);
_tempFilesRoot = PathUtility.Combine(unpackRoot, BuiltinFileSystemConsts.UnpackTempFilesFolderName);
// 创建默认的解包策略
if (BundleUnpackPolicy == null)
BundleUnpackPolicy = new DefaultBundleUnpackPolicy();
// 创建默认的下载后台接口
if (DownloadBackend == null)
DownloadBackend = new UnityWebRequestBackend(WebRequestCreator);
@@ -376,14 +387,13 @@ namespace YooAsset
/// <inheritdoc />
public bool IsUnpackRequired(PackageBundle bundle)
{
if (IsUnpackBundleFile(bundle))
{
return UnpackBundleCache.IsCached(bundle.BundleGuid) == false;
}
else
{
if (CanAcceptBundle(bundle) == false)
return false;
}
if (IsUnpackBundle(bundle) == false)
return false;
return UnpackBundleCache.IsCached(bundle.BundleGuid) == false;
}
/// <inheritdoc />
public bool IsImportRequired(PackageBundle bundle)
@@ -391,31 +401,16 @@ namespace YooAsset
return false;
}
#region
/// <summary>
/// 是否属于解压资源包文件
/// 通过策略判定指定资源包是否为需要解包的类型
/// </summary>
public bool IsUnpackBundleFile(PackageBundle bundle)
internal bool IsUnpackBundle(PackageBundle bundle)
{
if (CanAcceptBundle(bundle) == false)
return false;
#if UNITY_ANDROID || UNITY_OPENHARMONY
if (bundle.IsEncrypted)
return true;
if (bundle.GetBundleType() == (int)EBundleType.RawBundle)
return true;
if (bundle.GetBundleType() == (int)EBundleType.ArchiveBundle)
return true;
return false;
#else
return false;
#endif
var unpackInfo = new BundleUnpackInfo(bundle);
return BundleUnpackPolicy.IsUnpackBundle(unpackInfo);
}
#region
/// <summary>
/// 获取默认的内置包裹根目录
/// </summary>

View File

@@ -33,7 +33,7 @@ namespace YooAsset
if (_steps == ESteps.CheckUnpack)
{
if (_fileSystem.IsUnpackBundleFile(_options.Bundle))
if (_fileSystem.IsUnpackBundle(_options.Bundle))
{
if (_fileSystem.UnpackBundleCache.IsCached(_options.Bundle.BundleGuid))
{

View File

@@ -40,7 +40,7 @@ namespace YooAsset
if (_steps == ESteps.Prepare)
{
if (_fileSystem.IsUnpackBundleFile(_options.Bundle))
if (_fileSystem.IsUnpackBundle(_options.Bundle))
{
if (_fileSystem.UnpackBundleCache.IsCached(_options.Bundle.BundleGuid))
_steps = ESteps.LoadUnpackBundle;

View File

@@ -28,9 +28,7 @@ namespace YooAsset
/// </summary>
public IDownloadBackend DownloadBackend { get; private set; }
/// <summary>
/// 包裹名称
/// </summary>
/// <inheritdoc />
public string PackageName { get; private set; }
#region

View File

@@ -50,9 +50,7 @@ namespace YooAsset
/// </summary>
public IDownloadBackend DownloadBackend { get; private set; }
/// <summary>
/// 包裹名称
/// </summary>
/// <inheritdoc />
public string PackageName { get; private set; }
#region

View File

@@ -8,11 +8,6 @@ namespace YooAsset
/// </summary>
internal class WebNetworkFileSystem : IFileSystem
{
/// <summary>
/// 包裹名称
/// </summary>
public string PackageName { get; private set; }
/// <summary>
/// Web 文件缓存系统
/// </summary>
@@ -28,6 +23,9 @@ namespace YooAsset
/// </summary>
public IWebPlatformStrategy PlatformStrategy { get; private set; }
/// <inheritdoc />
public string PackageName { get; private set; }
#region
/// <summary>
/// 自定义参数UnityWebRequest 创建委托

View File

@@ -35,9 +35,7 @@ namespace YooAsset
/// </summary>
public IWebPlatformStrategy PlatformStrategy { get; private set; }
/// <summary>
/// 包裹名称
/// </summary>
/// <inheritdoc />
public string PackageName { get; private set; }
#region

View File

@@ -0,0 +1,82 @@
namespace YooAsset
{
/// <summary>
/// 资源包解包判定信息
/// </summary>
public readonly struct BundleUnpackInfo
{
private readonly PackageBundle _bundle;
/// <summary>
/// 资源包名称
/// </summary>
public string BundleName => _bundle.BundleName;
/// <summary>
/// 资源包文件名称
/// </summary>
public string FileName => _bundle.GetFileName();
/// <summary>
/// 资源包类型
/// </summary>
public int BundleType => _bundle.GetBundleType();
/// <summary>
/// 是否为加密资源包
/// </summary>
public bool IsEncrypted => _bundle.IsEncrypted;
/// <summary>
/// 分类标签数量
/// </summary>
public int TagCount
{
get
{
return _bundle.Tags == null ? 0 : _bundle.Tags.Length;
}
}
internal BundleUnpackInfo(PackageBundle bundle)
{
_bundle = bundle;
}
/// <summary>
/// 获取指定索引的分类标签
/// </summary>
public string GetTag(int index)
{
return _bundle.Tags[index];
}
/// <summary>
/// 是否包含指定的单个标签
/// </summary>
public bool HasTag(string tag)
{
return _bundle.HasTag(tag);
}
/// <summary>
/// 是否包含指定标签数组中的任意一个
/// </summary>
public bool HasAnyTag(string[] tags)
{
return _bundle.HasAnyTag(tags);
}
}
/// <summary>
/// 内置资源包解包策略接口
/// </summary>
public interface IBundleUnpackPolicy
{
/// <summary>
/// 判定指定资源包是否为需要解包的类型
/// </summary>
bool IsUnpackBundle(BundleUnpackInfo unpackInfo);
}
}

View File

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

View File

@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Collections.Generic;
namespace YooAsset
@@ -142,10 +141,24 @@ namespace YooAsset
}
/// <summary>
/// 是否包含指定的标签
/// 是否包含指定的单个标签
/// </summary>
public bool HasTag(string tag)
{
if (Tags == null || Tags.Length == 0)
return false;
for (int i = 0; i < Tags.Length; i++)
{
if (Tags[i] == tag)
return true;
}
return false;
}
/// <summary>
/// 是否包含指定标签数组中的任意一个
/// </summary>
/// <param name="tags">要检查的标签数组</param>
/// <returns>如果包含任意一个标签返回true否则返回false。</returns>
public bool HasAnyTag(string[] tags)
{
if (tags == null || tags.Length == 0)
@@ -153,9 +166,9 @@ namespace YooAsset
if (Tags == null || Tags.Length == 0)
return false;
foreach (var tag in tags)
for (int i = 0; i < tags.Length; i++)
{
if (Tags.Contains(tag))
if (HasTag(tags[i]))
return true;
}
return false;