com.alicizax.unity.tuyoogam.../RuntimeExtension/ExtensionFileSystem/WechatFileSystem/Operation/WXFSLoadBundleOperation.cs

96 lines
3.3 KiB
C#
Raw Normal View History

2025-01-09 11:31:04 +08:00
#if UNITY_WEBGL && WEIXINMINIGAME
using YooAsset;
internal class WXFSLoadBundleOperation : FSLoadBundleOperation
{
private enum ESteps
{
None,
2025-02-28 16:11:01 +08:00
DownloadAssetBundle,
2025-01-09 11:31:04 +08:00
Done,
}
private readonly WechatFileSystem _fileSystem;
private readonly PackageBundle _bundle;
2025-02-28 16:11:01 +08:00
private DownloadAssetBundleOperation _downloadAssetBundleOp;
2025-01-09 11:31:04 +08:00
private ESteps _steps = ESteps.None;
2025-02-28 16:11:01 +08:00
2025-01-09 11:31:04 +08:00
internal WXFSLoadBundleOperation(WechatFileSystem 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-02-28 16:11:01 +08:00
_steps = ESteps.DownloadAssetBundle;
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-02-28 16:11:01 +08:00
if (_steps == ESteps.DownloadAssetBundle)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
if (_downloadAssetBundleOp == null)
2025-01-09 11:31:04 +08:00
{
2025-05-13 10:40:30 +08:00
DownloadFileOptions options = new DownloadFileOptions(int.MaxValue, 60);
options.MainURL = _fileSystem.RemoteServices.GetRemoteMainURL(_bundle.FileName); ;
options.FallbackURL = _fileSystem.RemoteServices.GetRemoteFallbackURL(_bundle.FileName);
2025-02-28 16:11:01 +08:00
if (_bundle.Encrypted)
{
2025-05-13 10:40:30 +08:00
_downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(false, _fileSystem.DecryptionServices, _bundle, options);
2025-02-28 16:11:01 +08:00
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
}
else
{
2025-05-13 10:40:30 +08:00
_downloadAssetBundleOp = new DownloadWechatAssetBundleOperation(_bundle, options);
2025-02-28 16:11:01 +08:00
_downloadAssetBundleOp.StartOperation();
AddChildOperation(_downloadAssetBundleOp);
}
2025-01-09 11:31:04 +08:00
}
2025-02-28 16:11:01 +08:00
_downloadAssetBundleOp.UpdateOperation();
DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
DownloadedBytes = (long)_downloadAssetBundleOp.DownloadedBytes;
2025-01-09 11:31:04 +08:00
Progress = DownloadProgress;
2025-02-28 16:11:01 +08:00
if (_downloadAssetBundleOp.IsDone == false)
2025-01-09 11:31:04 +08:00
return;
2025-02-28 16:11:01 +08:00
if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
2025-01-09 11:31:04 +08:00
{
2025-02-28 16:11:01 +08:00
var assetBundle = _downloadAssetBundleOp.Result;
2025-01-09 11:31:04 +08:00
if (assetBundle == null)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-02-28 16:11:01 +08:00
Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
2025-01-09 11:31:04 +08:00
}
else
{
_steps = ESteps.Done;
Result = new WXAssetBundleResult(_fileSystem, _bundle, assetBundle);
Status = EOperationStatus.Succeed;
}
}
else
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-02-28 16:11:01 +08:00
Error = _downloadAssetBundleOp.Error;
2025-01-09 11:31:04 +08:00
}
}
}
internal override void InternalWaitForAsyncComplete()
{
if (_steps != ESteps.Done)
{
_steps = ESteps.Done;
Status = EOperationStatus.Failed;
2025-02-28 16:11:01 +08:00
Error = "WebGL platform not support sync load method !";
2025-01-09 11:31:04 +08:00
UnityEngine.Debug.LogError(Error);
}
}
}
#endif