feat : web server file system support raw bundle

This commit is contained in:
何冠峰
2026-05-21 22:07:44 +08:00
parent 7842235af6
commit 687dc1d9e9
4 changed files with 135 additions and 2 deletions

View File

@@ -0,0 +1,101 @@
namespace YooAsset
{
/// <summary>
/// Web服务器文件缓存加载 RawBundle 操作
/// </summary>
internal sealed class WSBCLoadRawBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{
None,
GetEntry,
LoadBundle,
Done,
}
private readonly WebServerBundleCache _fileCache;
private readonly BCLoadBundleOptions _options;
private BCLoadBundleOperation _loadBundleOp;
private WebServerBundleCacheEntry _cacheEntry;
private ESteps _steps = ESteps.None;
public WSBCLoadRawBundleOperation(WebServerBundleCache fileCache, BCLoadBundleOptions options)
{
_fileCache = fileCache;
_options = options;
}
protected override void InternalStart()
{
_steps = ESteps.GetEntry;
}
protected override void InternalUpdate()
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
if (_steps == ESteps.GetEntry)
{
_cacheEntry = _fileCache.GetEntry(_options.Bundle.BundleGuid);
if (_cacheEntry == null)
{
_steps = ESteps.Done;
SetError($"File cache entry not found: '{_options.Bundle.BundleGuid}'.");
}
else
{
_steps = ESteps.LoadBundle;
}
}
if (_steps == ESteps.LoadBundle)
{
if (_loadBundleOp == null)
{
string url = DownloadUrlHelper.ToLocalFileUrl(_cacheEntry.FilePath);
var options = new LoadWebRawBundleOptions(
cacheName: _fileCache.GetType().Name,
bundle: _options.Bundle,
candidateUrls: new[] { url },
rawBundleDecryptor: _fileCache.Config.RawBundleDecryptor,
downloadBackend: _fileCache.Config.DownloadBackend,
downloadVerifyLevel: _fileCache.Config.DownloadVerifyLevel,
watchdogTimeout: _fileCache.Config.WatchdogTimeout,
downloadRetryPolicy: _fileCache.Config.DownloadRetryPolicy,
downloadUrlPolicy: _fileCache.Config.DownloadUrlPolicy);
_loadBundleOp = new LoadWebRawBundleOperation(options);
_loadBundleOp.StartOperation();
AddChildOperation(_loadBundleOp);
}
_loadBundleOp.UpdateOperation();
Progress = _loadBundleOp.Progress;
if (_loadBundleOp.IsDone == false)
return;
if (_loadBundleOp.Status == EOperationStatus.Succeeded)
{
if (_loadBundleOp.BundleHandle == null)
throw new YooInternalException("Loaded bundle handle is null.");
_steps = ESteps.Done;
SetResult();
BundleHandle = _loadBundleOp.BundleHandle;
}
else
{
_steps = ESteps.Done;
SetError(_loadBundleOp.Error);
}
}
}
protected override void InternalWaitForCompletion()
{
if (_steps != ESteps.Done)
{
_steps = ESteps.Done;
SetError("WebGL platform does not support synchronous loading.");
YooLogger.LogError(Error);
}
}
}
}

View File

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

View File

@@ -30,6 +30,11 @@ namespace YooAsset
/// </summary>
public IBundleDecryptor AssetBundleDecryptor { get; }
/// <summary>
/// RawBundle 解密器
/// </summary>
public IBundleDecryptor RawBundleDecryptor { get; }
/// <summary>
/// 下载后台
/// </summary>
@@ -45,14 +50,15 @@ namespace YooAsset
/// </summary>
public IDownloadUrlPolicy DownloadUrlPolicy { get; }
public Configuration(int watchdogTimeout, bool disableUnityWebCache,
EFileVerifyLevel downloadVerifyLevel, IBundleDecryptor assetBundleDecryptor,
public Configuration(int watchdogTimeout, bool disableUnityWebCache,
EFileVerifyLevel downloadVerifyLevel, IBundleDecryptor assetBundleDecryptor, IBundleDecryptor rawBundleDecryptor,
IDownloadBackend downloadBackend, IDownloadRetryPolicy downloadRetryPolicy, IDownloadUrlPolicy downloadUrlPolicy)
{
WatchdogTimeout = watchdogTimeout;
DisableUnityWebCache = disableUnityWebCache;
DownloadVerifyLevel = downloadVerifyLevel;
AssetBundleDecryptor = assetBundleDecryptor;
RawBundleDecryptor = rawBundleDecryptor;
DownloadBackend = downloadBackend;
DownloadRetryPolicy = downloadRetryPolicy;
DownloadUrlPolicy = downloadUrlPolicy;
@@ -139,6 +145,11 @@ namespace YooAsset
var operation = new WSBCLoadAssetBundleOperation(this, options);
return operation;
}
else if (options.Bundle.GetBundleType() == (int)EBundleType.RawBundle)
{
var operation = new WSBCLoadRawBundleOperation(this, options);
return operation;
}
else
{
string error = $"{nameof(WebServerBundleCache)} does not support bundle type: {options.Bundle.GetBundleType()}.";

View File

@@ -61,6 +61,11 @@ namespace YooAsset
/// </summary>
public IBundleDecryptor AssetBundleDecryptor { get; private set; }
/// <summary>
/// 自定义参数RawBundle 解密器
/// </summary>
public IBundleDecryptor RawBundleDecryptor { get; private set; }
/// <summary>
/// 自定义参数:资源清单解密器
/// </summary>
@@ -154,6 +159,10 @@ namespace YooAsset
{
AssetBundleDecryptor = FileSystemHelper.CastParameter<IBundleDecryptor>(paramName, value);
}
else if (paramName == nameof(EFileSystemParameter.RawbundleDecryptor))
{
RawBundleDecryptor = FileSystemHelper.CastParameter<IBundleDecryptor>(paramName, value);
}
else if (paramName == nameof(EFileSystemParameter.ManifestDecryptor))
{
ManifestDecryptor = FileSystemHelper.CastParameter<IManifestDecryptor>(paramName, value);
@@ -199,6 +208,7 @@ namespace YooAsset
disableUnityWebCache: DisableUnityWebCache,
downloadVerifyLevel: DownloadVerifyLevel,
assetBundleDecryptor: AssetBundleDecryptor,
rawBundleDecryptor: RawBundleDecryptor,
downloadBackend: DownloadBackend,
downloadRetryPolicy: DownloadRetryPolicy,
downloadUrlPolicy: DownloadUrlPolicy);