DefaultDownloadFileOperation.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3. namespace YooAsset
  4. {
  5. internal abstract class DefaultDownloadFileOperation : FSDownloadFileOperation
  6. {
  7. protected enum ESteps
  8. {
  9. None,
  10. CheckExists,
  11. CreateRequest,
  12. CheckRequest,
  13. VerifyTempFile,
  14. CheckVerifyTempFile,
  15. TryAgain,
  16. Done,
  17. }
  18. // 下载参数
  19. protected readonly DownloadParam Param;
  20. // 请求相关
  21. protected UnityWebRequest _webRequest;
  22. protected string _requestURL;
  23. protected int _requestCount = 0;
  24. // 超时相关
  25. protected bool _isAbort = false;
  26. protected long _latestDownloadBytes;
  27. protected float _latestDownloadRealtime;
  28. protected float _tryAgainTimer;
  29. // 失败相关
  30. protected int FailedTryAgain;
  31. internal DefaultDownloadFileOperation(PackageBundle bundle, DownloadParam param) : base(bundle)
  32. {
  33. Param = param;
  34. FailedTryAgain = param.FailedTryAgain;
  35. }
  36. /// <summary>
  37. /// 获取网络请求地址
  38. /// </summary>
  39. protected string GetRequestURL()
  40. {
  41. // 轮流返回请求地址
  42. _requestCount++;
  43. if (_requestCount % 2 == 0)
  44. return Param.FallbackURL;
  45. else
  46. return Param.MainURL;
  47. }
  48. /// <summary>
  49. /// 重置请求字段
  50. /// </summary>
  51. protected void ResetRequestFiled()
  52. {
  53. // 重置变量
  54. _isAbort = false;
  55. _latestDownloadBytes = 0;
  56. _latestDownloadRealtime = Time.realtimeSinceStartup;
  57. DownloadProgress = 0f;
  58. DownloadedBytes = 0;
  59. // 重置计时器
  60. if (_tryAgainTimer > 0f)
  61. YooLogger.Warning($"Try again download : {_requestURL}");
  62. _tryAgainTimer = 0f;
  63. }
  64. /// <summary>
  65. /// 检测请求超时
  66. /// </summary>
  67. protected void CheckRequestTimeout()
  68. {
  69. // 注意:在连续时间段内无新增下载数据及判定为超时
  70. if (_isAbort == false)
  71. {
  72. if (_latestDownloadBytes != DownloadedBytes)
  73. {
  74. _latestDownloadBytes = DownloadedBytes;
  75. _latestDownloadRealtime = UnityEngine.Time.realtimeSinceStartup;
  76. }
  77. float offset = UnityEngine.Time.realtimeSinceStartup - _latestDownloadRealtime;
  78. if (offset > Param.Timeout)
  79. {
  80. YooLogger.Warning($"Download request timeout : {_requestURL}");
  81. if (_webRequest != null)
  82. _webRequest.Abort();
  83. _isAbort = true;
  84. }
  85. }
  86. }
  87. /// <summary>
  88. /// 检测请求结果
  89. /// </summary>
  90. protected bool CheckRequestResult()
  91. {
  92. HttpCode = _webRequest.responseCode;
  93. #if UNITY_2020_3_OR_NEWER
  94. if (_webRequest.result != UnityWebRequest.Result.Success)
  95. {
  96. Error = _webRequest.error;
  97. return false;
  98. }
  99. else
  100. {
  101. return true;
  102. }
  103. #else
  104. if (_webRequest.isNetworkError || _webRequest.isHttpError)
  105. {
  106. Error = _webRequest.error;
  107. return false;
  108. }
  109. else
  110. {
  111. return true;
  112. }
  113. #endif
  114. }
  115. }
  116. }