DownInfo.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using MRStore.Util;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. namespace XRTool.Util
  8. {
  9. /// <summary>
  10. /// 当前的状态
  11. /// 0代表准备下载
  12. /// 1代表启动下载,只有当状态为0时才会自动启动下载
  13. /// 2代表暂停下载,主动点击时会暂停下载,需要主动点击解除暂停状态
  14. /// </summary>
  15. public enum DownState
  16. {
  17. /// <summary>
  18. /// 0代表准备下载
  19. /// </summary>
  20. Prepare = 0,
  21. /// <summary>
  22. /// 1代表启动下载,只有当状态为0时才会自动启动下载
  23. /// </summary>
  24. Downing = 1,
  25. /// <summary>
  26. ///2代表暂停下载,主动点击时会暂停下载,需要主动点击解除暂停状态
  27. /// </summary>
  28. Suspend = 2
  29. }
  30. /// <summary>
  31. /// 下载的信息与事件
  32. /// </summary>
  33. public class DownInfo
  34. {
  35. /// <summary>
  36. /// 请求的地址
  37. /// 完整的下载地址
  38. /// </summary>
  39. public string url;
  40. /// <summary>
  41. /// 本地的下载路径
  42. /// </summary>
  43. public string localPath;
  44. /// <summary>
  45. /// 当前的下载状态
  46. /// </summary>
  47. public DownState state { get; private set; }
  48. /// <summary>
  49. /// 获得文件总大小的事件
  50. /// </summary>
  51. public event Action<DownInfo> beginDownload;
  52. /// <summary>
  53. /// 下载进度更新
  54. /// </summary>
  55. public event Action<float> downProcess;
  56. /// <summary>
  57. /// 下载完成后回调下载的路径
  58. /// </summary>
  59. public event Action<DownInfo, string> downComplete;
  60. /// <summary>
  61. /// 下载出错
  62. /// </summary>
  63. public event Action<string> downError;
  64. /// <summary>
  65. /// 下载中的文件后缀
  66. /// </summary>
  67. public const string Extension = ".downing";
  68. /// <summary>
  69. /// 文件名
  70. /// </summary>
  71. public string fileName { get; private set; }
  72. /// <summary>
  73. /// <summary>
  74. /// 本地临时文件名
  75. /// </summary>
  76. public string tmpFilePath { get; private set; }
  77. /// <summary>
  78. /// 本地真实文件名
  79. /// </summary>
  80. public string realFilePath { get; private set; }
  81. /// <summary>
  82. /// 文件总大小
  83. /// </summary>
  84. public long totalSize { get; private set; }
  85. /// <summary>
  86. /// 文件已下载的大小
  87. /// </summary>
  88. public long downloadedFileLen { get; private set; }
  89. private DownInfoHandler handler;
  90. /// <summary>
  91. /// 构造一个下载器
  92. /// </summary>
  93. /// <param name="url"></param>
  94. /// <param name="localPath"></param>
  95. public DownInfo(string url, string localPath, Action<DownInfo, string> downComplete = null,
  96. Action<float> downProcess = null, Action<string> downError = null, Action<DownInfo> beginDownload = null)
  97. {
  98. this.url = url;
  99. this.localPath = localPath;
  100. this.downComplete = downComplete;
  101. this.downProcess = downProcess;
  102. this.downError = downError;
  103. this.beginDownload = beginDownload;
  104. ///单纯的获取文件名+后缀
  105. fileName = Path.GetFileName(url);
  106. ///真实文件名
  107. realFilePath = Path.Combine(localPath, fileName);
  108. ///完整文件名
  109. tmpFilePath = realFilePath + Extension;
  110. }
  111. /// <summary>
  112. /// 判断是否需要下载
  113. /// 如果已存在真实文件,则无需下载
  114. /// </summary>
  115. /// <returns></returns>
  116. public bool IsNeedDown()
  117. {
  118. if (File.Exists(realFilePath))
  119. {
  120. downComplete?.Invoke(this, realFilePath);
  121. return false;
  122. }
  123. return true;
  124. }
  125. /// <summary>
  126. /// 下载失败或者出现错误后
  127. /// </summary>
  128. /// <param name="error"></param>
  129. public void ActiveError(string error)
  130. {
  131. state = DownState.Suspend;
  132. downError?.Invoke(error);
  133. //DataFileUtil.DelFile(tmpFilePath);
  134. }
  135. /// <summary>
  136. /// 获得文件的总大小 = 本地已下载+服务器剩余大小
  137. /// </summary>
  138. /// <param name="urlSize"></param>
  139. public void ActiveTotalLength(long urlSize)
  140. {
  141. totalSize = urlSize;
  142. beginDownload?.Invoke(this);
  143. state = DownState.Downing;
  144. ActiveProcess(downloadedFileLen * 1.0f / totalSize);
  145. ///服务端无新数据,完成下载
  146. if (downloadedFileLen == totalSize)
  147. {
  148. //ActiveComplete();
  149. }
  150. }
  151. /// <summary>
  152. /// 进度更新事件激活
  153. /// 此为文件的总进度 = 已下载的进度+当前的进度*剩余的进度比例
  154. /// </summary>
  155. /// <param name="downSize"></param>
  156. public void ActiveProcess(float p)
  157. {
  158. downProcess?.Invoke(p);
  159. }
  160. /// <summary>
  161. /// 停止下载
  162. /// </summary>
  163. public bool StopDown()
  164. {
  165. if (state == DownState.Downing)
  166. {
  167. if (handler != null && handler.webRequest != null)
  168. {
  169. handler.webRequest.Dispose();
  170. handler.ErrorDispose(null);
  171. }
  172. state = DownState.Suspend;
  173. handler = null;
  174. return true;
  175. }
  176. return false;
  177. }
  178. /// <summary>
  179. /// 取消下载
  180. /// </summary>
  181. public void CancelDown()
  182. {
  183. StopDown();
  184. DataFileUtil.DelFile(tmpFilePath);
  185. DataFileUtil.DelFile(realFilePath);
  186. }
  187. public void SetDownHandler(DownInfoHandler handler)
  188. {
  189. this.handler = handler;
  190. }
  191. /// <summary>
  192. /// 激活下载完成的事件
  193. /// 下载完成后,改名
  194. /// </summary>
  195. public void ActiveComplete()
  196. {
  197. ///下载完成的判断
  198. ///已下载的文件等于总文件大小
  199. if (downloadedFileLen >= totalSize)
  200. {
  201. DataFileUtil.ReNameFile(tmpFilePath, realFilePath);
  202. downComplete?.Invoke(this, realFilePath);
  203. }
  204. }
  205. }
  206. }