Files
YooAsset/Assets/YooAsset/Runtime/BundleCache/Services/WebServerBundleCache/Operations/WSBCLoadAssetBundleOperation.cs
2026-05-26 19:18:39 +08:00

124 lines
4.9 KiB
C#

namespace YooAsset
{
/// <summary>
/// Web服务器文件缓存加载 AssetBundle 操作
/// </summary>
internal sealed class WSBCLoadAssetBundleOperation : 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;
/// <summary>
/// 创建 WSBCLoadAssetBundleOperation 实例
/// </summary>
/// <param name="fileCache">Web 服务器文件缓存系统</param>
/// <param name="options">加载资源包操作选项</param>
public WSBCLoadAssetBundleOperation(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);
if (_options.Bundle.IsEncrypted)
{
var encryptedOptions = new LoadWebEncryptedAssetBundleOptions(
cacheName: _fileCache.GetType().Name,
bundle: _options.Bundle,
candidateUrls: new[] { url },
assetBundleDecryptor: _fileCache.Config.AssetBundleDecryptor,
downloadBackend: _fileCache.Config.DownloadBackend,
downloadVerifyLevel: _fileCache.Config.DownloadVerifyLevel,
watchdogTimeout: _fileCache.Config.WatchdogTimeout,
downloadRetryPolicy: _fileCache.Config.DownloadRetryPolicy,
downloadUrlPolicy: _fileCache.Config.DownloadUrlPolicy);
_loadBundleOp = new LoadWebEncryptedAssetBundleOperation(encryptedOptions);
}
else
{
var platformOptions = new LoadWebPlatformAssetBundleOptions(
bundle: _options.Bundle,
candidateUrls: new[] { url },
platformStrategy: _fileCache.Config.PlatformStrategy,
downloadBackend: _fileCache.Config.DownloadBackend,
watchdogTimeout: _fileCache.Config.WatchdogTimeout,
disableUnityWebCache: _fileCache.Config.DisableUnityWebCache,
downloadRetryPolicy: _fileCache.Config.DownloadRetryPolicy,
downloadUrlPolicy: _fileCache.Config.DownloadUrlPolicy);
_loadBundleOp = new LoadWebPlatformAssetBundleOperation(platformOptions);
}
_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);
}
}
}
}