Files
YooAsset/Assets/YooAsset/Runtime/BundleCache/Services/WebServerBundleCache/Operations/WSBCLoadBundleOperation.cs
2026-03-20 19:51:28 +08:00

107 lines
4.0 KiB
C#

namespace YooAsset
{
/// <summary>
/// Web服务器文件缓存加载 AssetBundle 操作
/// </summary>
internal class WSBCLoadAssetBundleOperation : BCLoadBundleOperation
{
private enum ESteps
{
None,
GetEntry,
LoadBundle,
Done,
}
private readonly WebServerBundleCache _fileCache;
private readonly BCLoadBundleOptions _options;
private LoadWebAssetBundleOperation _loadWebAssetBundleOp;
private WebServerBundleCacheEntry _cacheEntry;
private ESteps _steps = ESteps.None;
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 (_loadWebAssetBundleOp == null)
{
string url = DownloadUrlHelper.ToLocalFileUrl(_cacheEntry.FilePath);
var options = new LoadWebAssetBundleOptions();
options.CacheName = _fileCache.GetType().Name;
options.Bundle = _options.Bundle;
options.CandidateUrls = new[] { url };
options.AssetBundleDecryptor = _fileCache.Config.AssetBundleDecryptor;
options.DownloadBackend = _fileCache.Config.DownloadBackend;
options.DownloadVerifyLevel = _fileCache.Config.DownloadVerifyLevel;
options.WatchdogTimeout = _fileCache.Config.WatchdogTimeout;
options.DisableUnityWebCache = _fileCache.Config.DisableUnityWebCache;
options.DownloadRetryPolicy = _fileCache.Config.DownloadRetryPolicy;
options.DownloadUrlPolicy = _fileCache.Config.DownloadUrlPolicy;
if (_options.Bundle.IsEncrypted)
_loadWebAssetBundleOp = new LoadWebEncryptedAssetBundleOperation(options);
else
_loadWebAssetBundleOp = new LoadWebNormalAssetBundleOperation(options);
_loadWebAssetBundleOp.StartOperation();
AddChildOperation(_loadWebAssetBundleOp);
}
_loadWebAssetBundleOp.UpdateOperation();
if (_loadWebAssetBundleOp.IsDone == false)
return;
if (_loadWebAssetBundleOp.Status == EOperationStatus.Succeeded)
{
if (_loadWebAssetBundleOp.BundleHandle == null)
throw new YooInternalException("Loaded bundle handle is null.");
_steps = ESteps.Done;
SetResult();
BundleHandle = _loadWebAssetBundleOp.BundleHandle;
}
else
{
_steps = ESteps.Done;
SetError(_loadWebAssetBundleOp.Error);
}
}
}
protected override void InternalWaitForCompletion()
{
if (_steps != ESteps.Done)
{
_steps = ESteps.Done;
SetError($"{nameof(WebServerBundleCache)} does not support synchronous asset bundle loading.");
YooLogger.Error(Error);
}
}
}
}