RawBundleWebLoader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System.IO;
  2. namespace YooAsset
  3. {
  4. /// <summary>
  5. /// WebGL平台加载器
  6. /// </summary>
  7. internal class RawBundleWebLoader : BundleLoaderBase
  8. {
  9. private enum ESteps
  10. {
  11. None,
  12. Download,
  13. CheckDownload,
  14. Website,
  15. CheckWebsite,
  16. CheckFile,
  17. Done,
  18. }
  19. private ESteps _steps = ESteps.None;
  20. private DownloaderBase _website;
  21. private DownloaderBase _downloader;
  22. public RawBundleWebLoader(AssetSystemImpl impl, BundleInfo bundleInfo) : base(impl, bundleInfo)
  23. {
  24. }
  25. /// <summary>
  26. /// 轮询更新
  27. /// </summary>
  28. public override void Update()
  29. {
  30. if (_steps == ESteps.Done)
  31. return;
  32. if (_steps == ESteps.None)
  33. {
  34. if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromRemote)
  35. {
  36. _steps = ESteps.Download;
  37. FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
  38. }
  39. else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
  40. {
  41. _steps = ESteps.Website;
  42. FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
  43. }
  44. else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromCache)
  45. {
  46. _steps = ESteps.CheckFile;
  47. FileLoadPath = MainBundleInfo.Bundle.CachedDataFilePath;
  48. }
  49. else
  50. {
  51. throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
  52. }
  53. }
  54. // 1. 下载远端文件
  55. if (_steps == ESteps.Download)
  56. {
  57. int failedTryAgain = Impl.DownloadFailedTryAgain;
  58. _downloader = DownloadSystem.BeginDownload(MainBundleInfo, failedTryAgain);
  59. _steps = ESteps.CheckDownload;
  60. }
  61. // 2. 检测下载结果
  62. if (_steps == ESteps.CheckDownload)
  63. {
  64. DownloadProgress = _downloader.DownloadProgress;
  65. DownloadedBytes = _downloader.DownloadedBytes;
  66. if (_downloader.IsDone() == false)
  67. return;
  68. if (_downloader.HasError())
  69. {
  70. _steps = ESteps.Done;
  71. Status = EStatus.Failed;
  72. LastError = _downloader.GetLastError();
  73. }
  74. else
  75. {
  76. _steps = ESteps.CheckFile;
  77. }
  78. }
  79. // 3. 从站点下载
  80. if (_steps == ESteps.Website)
  81. {
  82. int failedTryAgain = Impl.DownloadFailedTryAgain;
  83. var bundleInfo = ManifestTools.ConvertToUnpackInfo(MainBundleInfo.Bundle);
  84. _website = DownloadSystem.BeginDownload(bundleInfo, failedTryAgain);
  85. _steps = ESteps.CheckWebsite;
  86. }
  87. // 4. 检测站点下载
  88. if (_steps == ESteps.CheckWebsite)
  89. {
  90. DownloadProgress = _website.DownloadProgress;
  91. DownloadedBytes = _website.DownloadedBytes;
  92. if (_website.IsDone() == false)
  93. return;
  94. if (_website.HasError())
  95. {
  96. _steps = ESteps.Done;
  97. Status = EStatus.Failed;
  98. LastError = _website.GetLastError();
  99. }
  100. else
  101. {
  102. _steps = ESteps.CheckFile;
  103. }
  104. }
  105. // 5. 检测结果
  106. if (_steps == ESteps.CheckFile)
  107. {
  108. // 设置下载进度
  109. DownloadProgress = 1f;
  110. DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
  111. _steps = ESteps.Done;
  112. if (File.Exists(FileLoadPath))
  113. {
  114. Status = EStatus.Succeed;
  115. }
  116. else
  117. {
  118. Status = EStatus.Failed;
  119. LastError = $"Raw file not found : {FileLoadPath}";
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// 主线程等待异步操作完毕
  125. /// </summary>
  126. public override void WaitForAsyncComplete()
  127. {
  128. if (IsDone() == false)
  129. {
  130. Status = EStatus.Failed;
  131. LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
  132. YooLogger.Error(LastError);
  133. }
  134. }
  135. }
  136. }