RawBundleWebLoader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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(ResourceManager 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.CachedDataFilePath;
  38. }
  39. else if (MainBundleInfo.LoadMode == BundleInfo.ELoadMode.LoadFromStreaming)
  40. {
  41. _steps = ESteps.Website;
  42. FileLoadPath = MainBundleInfo.CachedDataFilePath;
  43. }
  44. else
  45. {
  46. throw new System.NotImplementedException(MainBundleInfo.LoadMode.ToString());
  47. }
  48. }
  49. // 1. 下载远端文件
  50. if (_steps == ESteps.Download)
  51. {
  52. _downloader = MainBundleInfo.CreateDownloader(int.MaxValue);
  53. _downloader.SendRequest();
  54. _steps = ESteps.CheckDownload;
  55. }
  56. // 2. 检测下载结果
  57. if (_steps == ESteps.CheckDownload)
  58. {
  59. DownloadProgress = _downloader.DownloadProgress;
  60. DownloadedBytes = _downloader.DownloadedBytes;
  61. if (_downloader.IsDone() == false)
  62. return;
  63. if (_downloader.HasError())
  64. {
  65. _steps = ESteps.Done;
  66. Status = EStatus.Failed;
  67. LastError = _downloader.GetLastError();
  68. }
  69. else
  70. {
  71. _steps = ESteps.CheckFile;
  72. }
  73. }
  74. // 3. 从站点下载
  75. if (_steps == ESteps.Website)
  76. {
  77. _website = MainBundleInfo.CreateUnpacker(int.MaxValue);
  78. _website.SendRequest();
  79. _steps = ESteps.CheckWebsite;
  80. }
  81. // 4. 检测站点下载
  82. if (_steps == ESteps.CheckWebsite)
  83. {
  84. DownloadProgress = _website.DownloadProgress;
  85. DownloadedBytes = _website.DownloadedBytes;
  86. if (_website.IsDone() == false)
  87. return;
  88. if (_website.HasError())
  89. {
  90. _steps = ESteps.Done;
  91. Status = EStatus.Failed;
  92. LastError = _website.GetLastError();
  93. }
  94. else
  95. {
  96. _steps = ESteps.CheckFile;
  97. }
  98. }
  99. // 5. 检测结果
  100. if (_steps == ESteps.CheckFile)
  101. {
  102. // 设置下载进度
  103. DownloadProgress = 1f;
  104. DownloadedBytes = (ulong)MainBundleInfo.Bundle.FileSize;
  105. _steps = ESteps.Done;
  106. if (File.Exists(FileLoadPath))
  107. {
  108. Status = EStatus.Succeed;
  109. }
  110. else
  111. {
  112. Status = EStatus.Failed;
  113. LastError = $"Raw file not found : {FileLoadPath}";
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 主线程等待异步操作完毕
  119. /// </summary>
  120. public override void WaitForAsyncComplete()
  121. {
  122. if (IsDone() == false)
  123. {
  124. Status = EStatus.Failed;
  125. LastError = $"{nameof(WaitForAsyncComplete)} failed ! WebGL platform not support sync load method !";
  126. YooLogger.Error(LastError);
  127. }
  128. }
  129. }
  130. }