UnityWebRequestDownloadAgentHelper.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.Download;
  9. using System;
  10. #if UNITY_5_4_OR_NEWER
  11. using UnityEngine.Networking;
  12. #else
  13. using UnityEngine.Experimental.Networking;
  14. #endif
  15. using Utility = GameFramework.Utility;
  16. namespace UnityGameFramework.Runtime
  17. {
  18. /// <summary>
  19. /// 使用 UnityWebRequest 实现的下载代理辅助器。
  20. /// </summary>
  21. public partial class UnityWebRequestDownloadAgentHelper : DownloadAgentHelperBase, IDisposable
  22. {
  23. private const int CachedBytesLength = 0x1000;
  24. private readonly byte[] m_CachedBytes = new byte[CachedBytesLength];
  25. private UnityWebRequest m_UnityWebRequest = null;
  26. private bool m_Disposed = false;
  27. private EventHandler<DownloadAgentHelperUpdateBytesEventArgs> m_DownloadAgentHelperUpdateBytesEventHandler = null;
  28. private EventHandler<DownloadAgentHelperUpdateLengthEventArgs> m_DownloadAgentHelperUpdateLengthEventHandler = null;
  29. private EventHandler<DownloadAgentHelperCompleteEventArgs> m_DownloadAgentHelperCompleteEventHandler = null;
  30. private EventHandler<DownloadAgentHelperErrorEventArgs> m_DownloadAgentHelperErrorEventHandler = null;
  31. /// <summary>
  32. /// 下载代理辅助器更新数据流事件。
  33. /// </summary>
  34. public override event EventHandler<DownloadAgentHelperUpdateBytesEventArgs> DownloadAgentHelperUpdateBytes
  35. {
  36. add
  37. {
  38. m_DownloadAgentHelperUpdateBytesEventHandler += value;
  39. }
  40. remove
  41. {
  42. m_DownloadAgentHelperUpdateBytesEventHandler -= value;
  43. }
  44. }
  45. /// <summary>
  46. /// 下载代理辅助器更新数据大小事件。
  47. /// </summary>
  48. public override event EventHandler<DownloadAgentHelperUpdateLengthEventArgs> DownloadAgentHelperUpdateLength
  49. {
  50. add
  51. {
  52. m_DownloadAgentHelperUpdateLengthEventHandler += value;
  53. }
  54. remove
  55. {
  56. m_DownloadAgentHelperUpdateLengthEventHandler -= value;
  57. }
  58. }
  59. /// <summary>
  60. /// 下载代理辅助器完成事件。
  61. /// </summary>
  62. public override event EventHandler<DownloadAgentHelperCompleteEventArgs> DownloadAgentHelperComplete
  63. {
  64. add
  65. {
  66. m_DownloadAgentHelperCompleteEventHandler += value;
  67. }
  68. remove
  69. {
  70. m_DownloadAgentHelperCompleteEventHandler -= value;
  71. }
  72. }
  73. /// <summary>
  74. /// 下载代理辅助器错误事件。
  75. /// </summary>
  76. public override event EventHandler<DownloadAgentHelperErrorEventArgs> DownloadAgentHelperError
  77. {
  78. add
  79. {
  80. m_DownloadAgentHelperErrorEventHandler += value;
  81. }
  82. remove
  83. {
  84. m_DownloadAgentHelperErrorEventHandler -= value;
  85. }
  86. }
  87. /// <summary>
  88. /// 通过下载代理辅助器下载指定地址的数据。
  89. /// </summary>
  90. /// <param name="downloadUri">下载地址。</param>
  91. /// <param name="userData">用户自定义数据。</param>
  92. public override void Download(string downloadUri, object userData)
  93. {
  94. if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
  95. {
  96. Log.Fatal("Download agent helper handler is invalid.");
  97. return;
  98. }
  99. m_UnityWebRequest = new UnityWebRequest(downloadUri);
  100. m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
  101. #if UNITY_2017_2_OR_NEWER
  102. m_UnityWebRequest.SendWebRequest();
  103. #else
  104. m_UnityWebRequest.Send();
  105. #endif
  106. }
  107. /// <summary>
  108. /// 通过下载代理辅助器下载指定地址的数据。
  109. /// </summary>
  110. /// <param name="downloadUri">下载地址。</param>
  111. /// <param name="fromPosition">下载数据起始位置。</param>
  112. /// <param name="userData">用户自定义数据。</param>
  113. public override void Download(string downloadUri, long fromPosition, object userData)
  114. {
  115. if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
  116. {
  117. Log.Fatal("Download agent helper handler is invalid.");
  118. return;
  119. }
  120. m_UnityWebRequest = new UnityWebRequest(downloadUri);
  121. m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-", fromPosition.ToString()));
  122. m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
  123. #if UNITY_2017_2_OR_NEWER
  124. m_UnityWebRequest.SendWebRequest();
  125. #else
  126. m_UnityWebRequest.Send();
  127. #endif
  128. }
  129. /// <summary>
  130. /// 通过下载代理辅助器下载指定地址的数据。
  131. /// </summary>
  132. /// <param name="downloadUri">下载地址。</param>
  133. /// <param name="fromPosition">下载数据起始位置。</param>
  134. /// <param name="toPosition">下载数据结束位置。</param>
  135. /// <param name="userData">用户自定义数据。</param>
  136. public override void Download(string downloadUri, long fromPosition, long toPosition, object userData)
  137. {
  138. if (m_DownloadAgentHelperUpdateBytesEventHandler == null || m_DownloadAgentHelperUpdateLengthEventHandler == null || m_DownloadAgentHelperCompleteEventHandler == null || m_DownloadAgentHelperErrorEventHandler == null)
  139. {
  140. Log.Fatal("Download agent helper handler is invalid.");
  141. return;
  142. }
  143. m_UnityWebRequest = new UnityWebRequest(downloadUri);
  144. m_UnityWebRequest.SetRequestHeader("Range", Utility.Text.Format("bytes={0}-{1}", fromPosition.ToString(), toPosition.ToString()));
  145. m_UnityWebRequest.downloadHandler = new DownloadHandler(this);
  146. #if UNITY_2017_2_OR_NEWER
  147. m_UnityWebRequest.SendWebRequest();
  148. #else
  149. m_UnityWebRequest.Send();
  150. #endif
  151. }
  152. /// <summary>
  153. /// 重置下载代理辅助器。
  154. /// </summary>
  155. public override void Reset()
  156. {
  157. if (m_UnityWebRequest != null)
  158. {
  159. m_UnityWebRequest.Abort();
  160. m_UnityWebRequest.Dispose();
  161. m_UnityWebRequest = null;
  162. }
  163. Array.Clear(m_CachedBytes, 0, CachedBytesLength);
  164. }
  165. /// <summary>
  166. /// 释放资源。
  167. /// </summary>
  168. public void Dispose()
  169. {
  170. Dispose(true);
  171. GC.SuppressFinalize(this);
  172. }
  173. /// <summary>
  174. /// 释放资源。
  175. /// </summary>
  176. /// <param name="disposing">释放资源标记。</param>
  177. protected virtual void Dispose(bool disposing)
  178. {
  179. if (m_Disposed)
  180. {
  181. return;
  182. }
  183. if (disposing)
  184. {
  185. if (m_UnityWebRequest != null)
  186. {
  187. m_UnityWebRequest.Dispose();
  188. m_UnityWebRequest = null;
  189. }
  190. }
  191. m_Disposed = true;
  192. }
  193. private void Update()
  194. {
  195. if (m_UnityWebRequest == null)
  196. {
  197. return;
  198. }
  199. if (!m_UnityWebRequest.isDone)
  200. {
  201. return;
  202. }
  203. bool isError = false;
  204. #if UNITY_2020_2_OR_NEWER
  205. isError = m_UnityWebRequest.result != UnityWebRequest.Result.Success;
  206. #elif UNITY_2017_1_OR_NEWER
  207. isError = m_UnityWebRequest.isNetworkError || m_UnityWebRequest.isHttpError;
  208. #else
  209. isError = m_UnityWebRequest.isError;
  210. #endif
  211. if (isError)
  212. {
  213. DownloadAgentHelperErrorEventArgs downloadAgentHelperErrorEventArgs = DownloadAgentHelperErrorEventArgs.Create(m_UnityWebRequest.responseCode == RangeNotSatisfiableErrorCode, m_UnityWebRequest.error);
  214. m_DownloadAgentHelperErrorEventHandler(this, downloadAgentHelperErrorEventArgs);
  215. ReferencePool.Release(downloadAgentHelperErrorEventArgs);
  216. }
  217. else
  218. {
  219. DownloadAgentHelperCompleteEventArgs downloadAgentHelperCompleteEventArgs = DownloadAgentHelperCompleteEventArgs.Create((long)m_UnityWebRequest.downloadedBytes);
  220. m_DownloadAgentHelperCompleteEventHandler(this, downloadAgentHelperCompleteEventArgs);
  221. ReferencePool.Release(downloadAgentHelperCompleteEventArgs);
  222. }
  223. }
  224. }
  225. }