DownloaderBase.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Networking;
  4. namespace YooAsset
  5. {
  6. internal abstract class DownloaderBase
  7. {
  8. public enum EStatus
  9. {
  10. None = 0,
  11. Succeed,
  12. Failed
  13. }
  14. protected readonly BundleInfo _bundleInfo;
  15. protected readonly System.Type _requesterType;
  16. protected readonly int _timeout;
  17. protected int _failedTryAgain;
  18. protected IWebRequester _requester;
  19. protected EStatus _status = EStatus.None;
  20. protected string _lastestNetError = string.Empty;
  21. protected long _lastestHttpCode = 0;
  22. // 请求次数
  23. protected int _requestCount = 0;
  24. protected string _requestURL;
  25. // 超时相关
  26. protected bool _isAbort = false;
  27. protected ulong _latestDownloadBytes;
  28. protected float _latestDownloadRealtime;
  29. protected float _tryAgainTimer;
  30. /// <summary>
  31. /// 是否等待异步结束
  32. /// 警告:只能用于解压APP内部资源
  33. /// </summary>
  34. public bool WaitForAsyncComplete = false;
  35. /// <summary>
  36. /// 下载进度(0f~1f)
  37. /// </summary>
  38. public float DownloadProgress { protected set; get; }
  39. /// <summary>
  40. /// 已经下载的总字节数
  41. /// </summary>
  42. public ulong DownloadedBytes { protected set; get; }
  43. /// <summary>
  44. /// 引用计数
  45. /// </summary>
  46. public int RefCount { private set; get; }
  47. public DownloaderBase(BundleInfo bundleInfo, System.Type requesterType, int failedTryAgain, int timeout)
  48. {
  49. _bundleInfo = bundleInfo;
  50. _requesterType = requesterType;
  51. _failedTryAgain = failedTryAgain;
  52. _timeout = timeout;
  53. }
  54. public abstract void SendRequest(params object[] args);
  55. public abstract void Update();
  56. public abstract void Abort();
  57. public abstract AssetBundle GetAssetBundle();
  58. /// <summary>
  59. /// 引用(引用计数递加)
  60. /// </summary>
  61. public void Reference()
  62. {
  63. RefCount++;
  64. }
  65. /// <summary>
  66. /// 释放(引用计数递减)
  67. /// </summary>
  68. public void Release()
  69. {
  70. RefCount--;
  71. }
  72. /// <summary>
  73. /// 检测下载器是否已经完成(无论成功或失败)
  74. /// </summary>
  75. public bool IsDone()
  76. {
  77. return _status == EStatus.Succeed || _status == EStatus.Failed;
  78. }
  79. /// <summary>
  80. /// 下载过程是否发生错误
  81. /// </summary>
  82. public bool HasError()
  83. {
  84. return _status == EStatus.Failed;
  85. }
  86. /// <summary>
  87. /// 按照错误级别打印错误
  88. /// </summary>
  89. public void ReportError()
  90. {
  91. YooLogger.Error(GetLastError());
  92. }
  93. /// <summary>
  94. /// 按照警告级别打印错误
  95. /// </summary>
  96. public void ReportWarning()
  97. {
  98. YooLogger.Warning(GetLastError());
  99. }
  100. /// <summary>
  101. /// 获取最近发生的错误信息
  102. /// </summary>
  103. public string GetLastError()
  104. {
  105. return $"Failed to download : {_requestURL} Error : {_lastestNetError} Code : {_lastestHttpCode}";
  106. }
  107. /// <summary>
  108. /// 获取下载文件的大小
  109. /// </summary>
  110. /// <returns></returns>
  111. public long GetDownloadFileSize()
  112. {
  113. return _bundleInfo.Bundle.FileSize;
  114. }
  115. /// <summary>
  116. /// 获取下载的资源包名称
  117. /// </summary>
  118. public string GetDownloadBundleName()
  119. {
  120. return _bundleInfo.Bundle.BundleName;
  121. }
  122. /// <summary>
  123. /// 获取网络请求地址
  124. /// </summary>
  125. protected string GetRequestURL()
  126. {
  127. // 轮流返回请求地址
  128. _requestCount++;
  129. if (_requestCount % 2 == 0)
  130. return _bundleInfo.RemoteFallbackURL;
  131. else
  132. return _bundleInfo.RemoteMainURL;
  133. }
  134. /// <summary>
  135. /// 超时判定方法
  136. /// </summary>
  137. protected void CheckTimeout()
  138. {
  139. // 注意:在连续时间段内无新增下载数据及判定为超时
  140. if (_isAbort == false)
  141. {
  142. if (_latestDownloadBytes != DownloadedBytes)
  143. {
  144. _latestDownloadBytes = DownloadedBytes;
  145. _latestDownloadRealtime = Time.realtimeSinceStartup;
  146. }
  147. float offset = Time.realtimeSinceStartup - _latestDownloadRealtime;
  148. if (offset > _timeout)
  149. {
  150. YooLogger.Warning($"Web file request timeout : {_requestURL}");
  151. if (_requester != null)
  152. _requester.Abort();
  153. _isAbort = true;
  154. }
  155. }
  156. }
  157. }
  158. }