DownloaderBase.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 
  2. namespace YooAsset
  3. {
  4. internal abstract class DownloaderBase
  5. {
  6. protected enum ESteps
  7. {
  8. None,
  9. CheckTempFile,
  10. WaitingCheckTempFile,
  11. PrepareDownload,
  12. CreateResumeDownloader,
  13. CreateGeneralDownloader,
  14. CheckDownload,
  15. VerifyTempFile,
  16. WaitingVerifyTempFile,
  17. CachingFile,
  18. TryAgain,
  19. Succeed,
  20. Failed,
  21. }
  22. protected readonly BundleInfo _bundleInfo;
  23. protected ESteps _steps = ESteps.None;
  24. protected int _timeout;
  25. protected int _failedTryAgain;
  26. protected int _requestCount;
  27. protected string _requestURL;
  28. protected string _lastError = string.Empty;
  29. protected long _lastCode = 0;
  30. protected float _downloadProgress = 0f;
  31. protected ulong _downloadedBytes = 0;
  32. /// <summary>
  33. /// 是否等待异步结束
  34. /// 警告:只能用于解压APP内部资源
  35. /// </summary>
  36. public bool WaitForAsyncComplete = false;
  37. /// <summary>
  38. /// 下载进度(0f~1f)
  39. /// </summary>
  40. public float DownloadProgress
  41. {
  42. get { return _downloadProgress; }
  43. }
  44. /// <summary>
  45. /// 已经下载的总字节数
  46. /// </summary>
  47. public ulong DownloadedBytes
  48. {
  49. get { return _downloadedBytes; }
  50. }
  51. public DownloaderBase(BundleInfo bundleInfo)
  52. {
  53. _bundleInfo = bundleInfo;
  54. }
  55. public void SendRequest(int failedTryAgain, int timeout)
  56. {
  57. if (_steps == ESteps.None)
  58. {
  59. _failedTryAgain = failedTryAgain;
  60. _timeout = timeout;
  61. _steps = ESteps.CheckTempFile;
  62. }
  63. }
  64. public abstract void Update();
  65. public abstract void Abort();
  66. /// <summary>
  67. /// 获取网络请求地址
  68. /// </summary>
  69. protected string GetRequestURL()
  70. {
  71. // 轮流返回请求地址
  72. _requestCount++;
  73. if (_requestCount % 2 == 0)
  74. return _bundleInfo.RemoteFallbackURL;
  75. else
  76. return _bundleInfo.RemoteMainURL;
  77. }
  78. /// <summary>
  79. /// 获取资源包信息
  80. /// </summary>
  81. public BundleInfo GetBundleInfo()
  82. {
  83. return _bundleInfo;
  84. }
  85. /// <summary>
  86. /// 检测下载器是否已经完成(无论成功或失败)
  87. /// </summary>
  88. public bool IsDone()
  89. {
  90. return _steps == ESteps.Succeed || _steps == ESteps.Failed;
  91. }
  92. /// <summary>
  93. /// 下载过程是否发生错误
  94. /// </summary>
  95. public bool HasError()
  96. {
  97. return _steps == ESteps.Failed;
  98. }
  99. /// <summary>
  100. /// 按照错误级别打印错误
  101. /// </summary>
  102. public void ReportError()
  103. {
  104. YooLogger.Error(GetLastError());
  105. }
  106. /// <summary>
  107. /// 按照警告级别打印错误
  108. /// </summary>
  109. public void ReportWarning()
  110. {
  111. YooLogger.Warning(GetLastError());
  112. }
  113. /// <summary>
  114. /// 获取最近发生的错误信息
  115. /// </summary>
  116. public string GetLastError()
  117. {
  118. return $"Failed to download : {_requestURL} Error : {_lastError} Code : {_lastCode}";
  119. }
  120. }
  121. }