DWSFSLoadBundleOperation.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 
  2. namespace YooAsset
  3. {
  4. internal class DWSFSLoadAssetBundleOperation : FSLoadBundleOperation
  5. {
  6. private enum ESteps
  7. {
  8. None,
  9. DownloadAssetBundle,
  10. Done,
  11. }
  12. private readonly DefaultWebServerFileSystem _fileSystem;
  13. private readonly PackageBundle _bundle;
  14. private DownloadAssetBundleOperation _downloadAssetBundleOp;
  15. private ESteps _steps = ESteps.None;
  16. internal DWSFSLoadAssetBundleOperation(DefaultWebServerFileSystem fileSystem, PackageBundle bundle)
  17. {
  18. _fileSystem = fileSystem;
  19. _bundle = bundle;
  20. }
  21. internal override void InternalStart()
  22. {
  23. _steps = ESteps.DownloadAssetBundle;
  24. }
  25. internal override void InternalUpdate()
  26. {
  27. if (_steps == ESteps.None || _steps == ESteps.Done)
  28. return;
  29. if (_steps == ESteps.DownloadAssetBundle)
  30. {
  31. if (_downloadAssetBundleOp == null)
  32. {
  33. DownloadParam downloadParam = new DownloadParam(int.MaxValue, 60);
  34. string fileLoadPath = _fileSystem.GetWebFileLoadPath(_bundle);
  35. downloadParam.MainURL = DownloadSystemHelper.ConvertToWWWPath(fileLoadPath);
  36. downloadParam.FallbackURL = downloadParam.MainURL;
  37. if (_bundle.Encrypted)
  38. {
  39. _downloadAssetBundleOp = new DownloadWebEncryptAssetBundleOperation(true, _fileSystem.DecryptionServices, _bundle, downloadParam);
  40. _downloadAssetBundleOp.StartOperation();
  41. AddChildOperation(_downloadAssetBundleOp);
  42. }
  43. else
  44. {
  45. _downloadAssetBundleOp = new DownloadWebNormalAssetBundleOperation(_fileSystem.DisableUnityWebCache, _bundle, downloadParam);
  46. _downloadAssetBundleOp.StartOperation();
  47. AddChildOperation(_downloadAssetBundleOp);
  48. }
  49. }
  50. _downloadAssetBundleOp.UpdateOperation();
  51. DownloadProgress = _downloadAssetBundleOp.DownloadProgress;
  52. DownloadedBytes = _downloadAssetBundleOp.DownloadedBytes;
  53. Progress = _downloadAssetBundleOp.Progress;
  54. if (_downloadAssetBundleOp.IsDone == false)
  55. return;
  56. if (_downloadAssetBundleOp.Status == EOperationStatus.Succeed)
  57. {
  58. var assetBundle = _downloadAssetBundleOp.Result;
  59. if (assetBundle == null)
  60. {
  61. _steps = ESteps.Done;
  62. Status = EOperationStatus.Failed;
  63. Error = $"{nameof(DownloadAssetBundleOperation)} loaded asset bundle is null !";
  64. }
  65. else
  66. {
  67. _steps = ESteps.Done;
  68. Result = new AssetBundleResult(_fileSystem, _bundle, assetBundle, null);
  69. Status = EOperationStatus.Succeed;
  70. }
  71. }
  72. else
  73. {
  74. _steps = ESteps.Done;
  75. Status = EOperationStatus.Failed;
  76. Error = _downloadAssetBundleOp.Error;
  77. }
  78. }
  79. }
  80. internal override void InternalWaitForAsyncComplete()
  81. {
  82. if (_steps != ESteps.Done)
  83. {
  84. _steps = ESteps.Done;
  85. Status = EOperationStatus.Failed;
  86. Error = "WebGL platform not support sync load method !";
  87. UnityEngine.Debug.LogError(Error);
  88. }
  89. }
  90. }
  91. }