com.alicizax.unity.tuyoogam.../Runtime/FileSystem/DefaultEditorFileSystem/Operation/DEFSLoadBundleOperation.cs

126 lines
3.9 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00

namespace YooAsset
{
internal class DEFSLoadBundleOperation : FSLoadBundleOperation
{
protected enum ESteps
{
None,
2025-09-23 12:03:44 +08:00
CheckExist,
2025-01-09 11:31:04 +08:00
DownloadFile,
LoadAssetBundle,
CheckResult,
Done,
}
private readonly DefaultEditorFileSystem _fileSystem;
private readonly PackageBundle _bundle;
2025-09-23 12:03:44 +08:00
protected FSDownloadFileOperation _downloadFileOp;
2025-01-09 11:31:04 +08:00
private int _asyncSimulateFrame;
private ESteps _steps = ESteps.None;
internal DEFSLoadBundleOperation(DefaultEditorFileSystem fileSystem, PackageBundle bundle)
{
_fileSystem = fileSystem;
_bundle = bundle;
}
2025-02-28 16:11:01 +08:00
internal override void InternalStart()
2025-01-09 11:31:04 +08:00
{
2025-09-23 12:03:44 +08:00
_steps = ESteps.CheckExist;
_asyncSimulateFrame = _fileSystem.GetAsyncSimulateFrame();
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
internal override void InternalUpdate()
2025-01-09 11:31:04 +08:00
{
if (_steps == ESteps.None || _steps == ESteps.Done)
return;
2025-09-23 12:03:44 +08:00
if (_steps == ESteps.CheckExist)
{
if (_fileSystem.Exists(_bundle))
{
DownloadProgress = 1f;
DownloadedBytes = _bundle.FileSize;
_steps = ESteps.LoadAssetBundle;
}
else
{
_steps = ESteps.DownloadFile;
}
}
2025-01-09 11:31:04 +08:00
if (_steps == ESteps.DownloadFile)
{
2025-09-23 12:03:44 +08:00
if (_downloadFileOp == null)
{
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue);
_downloadFileOp = _fileSystem.DownloadFileAsync(_bundle, options);
_downloadFileOp.StartOperation();
AddChildOperation(_downloadFileOp);
}
if (IsWaitForAsyncComplete)
_downloadFileOp.WaitForAsyncComplete();
_downloadFileOp.UpdateOperation();
DownloadProgress = _downloadFileOp.DownloadProgress;
DownloadedBytes = _downloadFileOp.DownloadedBytes;
if (_downloadFileOp.IsDone == false)
return;
if (_downloadFileOp.Status == EOperationStatus.Succeed)
{
_steps = ESteps.LoadAssetBundle;
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = _downloadFileOp.Error;
}
2025-01-09 11:31:04 +08:00
}
if (_steps == ESteps.LoadAssetBundle)
{
if (IsWaitForAsyncComplete)
{
2025-09-23 12:03:44 +08:00
if (_fileSystem.VirtualWebGLMode)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
Error = "Virtual WebGL Mode only support asyn load method !";
YooLogger.Error(Error);
}
else
{
_steps = ESteps.CheckResult;
}
2025-01-09 11:31:04 +08:00
}
else
{
if (_asyncSimulateFrame <= 0)
_steps = ESteps.CheckResult;
else
_asyncSimulateFrame--;
}
}
if (_steps == ESteps.CheckResult)
{
_steps = ESteps.Done;
Result = new VirtualBundleResult(_fileSystem, _bundle);
Status = EOperationStatus.Succeed;
}
}
internal override void InternalWaitForAsyncComplete()
{
while (true)
{
if (ExecuteWhileDone())
{
_steps = ESteps.Done;
break;
}
}
}
}
}