DownInfoHandler.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.IO;
  3. using UnityEngine.Networking;
  4. using XRTool.Util;
  5. namespace MRStore.Util
  6. {
  7. /// <summary>
  8. /// 断点下载的辅助器
  9. /// </summary>
  10. public class DownInfoHandler : DownloadHandlerScript
  11. {
  12. /// <summary>
  13. /// 文件流
  14. /// </summary>
  15. private FileStream stream;
  16. /// <summary>
  17. /// 下载资源的结构体
  18. /// </summary>
  19. public DownInfo info { get; private set; }
  20. public UnityWebRequest webRequest { get; private set; }
  21. public void SetWebRequest(UnityWebRequest req, long contentLength)
  22. {
  23. webRequest = req;
  24. ReadLocalData();
  25. if (info != null)
  26. {
  27. info.ActiveTotalLength((long)contentLength);
  28. }
  29. }
  30. /// <summary>
  31. /// 读取本地的数据
  32. /// 读取临时数据缓存区tmppath
  33. /// </summary>
  34. private void ReadLocalData()
  35. {
  36. if (stream == null)
  37. {
  38. try
  39. {
  40. FileMode model = FileMode.Append;
  41. if (!File.Exists(info.tmpFilePath))
  42. {
  43. model = FileMode.OpenOrCreate;
  44. }
  45. stream = new FileStream(info.tmpFilePath, model, FileAccess.Write, FileShare.Write);
  46. }
  47. catch (Exception ex)
  48. {
  49. UnityLog.LogError(info.tmpFilePath + "File Stream Error" + ex.ToString());
  50. if (stream != null)
  51. {
  52. stream.Dispose();
  53. }
  54. stream = null;
  55. }
  56. }
  57. }
  58. /// <summary>
  59. /// 构造函数
  60. /// </summary>
  61. /// <param name="info"></param>
  62. public DownInfoHandler(DownInfo info) : base(new byte[1024*256])
  63. {
  64. this.info = info;
  65. this.info.SetDownHandler(this);
  66. }
  67. /// <summary>
  68. /// 服务器返回此文件的剩余大小
  69. /// </summary>
  70. /// <param name="contentLength"></param>
  71. protected override void ReceiveContentLengthHeader(ulong contentLength)
  72. {
  73. if (contentLength == 0)
  74. {
  75. CompleteContent();
  76. }
  77. base.ReceiveContentLengthHeader(contentLength);
  78. }
  79. /// <summary>
  80. /// 从服务器接收到数据
  81. /// </summary>
  82. /// <param name="data"></param>
  83. /// <param name="dataLength"></param>
  84. /// <returns></returns>
  85. protected override bool ReceiveData(byte[] data, int dataLength)
  86. {
  87. WriteFile(data, dataLength);
  88. return base.ReceiveData(data, dataLength);
  89. }
  90. /// <summary>
  91. /// 下载,写入文件
  92. /// </summary>
  93. /// <param name="data"></param>
  94. /// <param name="dataLength"></param>
  95. public void WriteFile(byte[] data, int dataLength)
  96. {
  97. if (data != null && data.Length > 0)
  98. {
  99. if (stream != null)
  100. {
  101. stream.Write(data, 0, dataLength);
  102. info.ActiveProcess(GetProgress());
  103. }
  104. }
  105. }
  106. protected override float GetProgress()
  107. {
  108. return info.downloadedFileLen*1.0f / info.totalSize;
  109. }
  110. /// <summary>
  111. /// 下载完成
  112. /// 网络停止时也有可能会调用此方法
  113. /// </summary>
  114. protected override void CompleteContent()
  115. {
  116. if (stream != null)
  117. {
  118. stream.Flush();
  119. stream.Dispose();
  120. }
  121. stream = null;
  122. info.ActiveComplete();
  123. base.CompleteContent();
  124. Dispose();
  125. }
  126. /// <summary>
  127. /// 下载错误,程序异常退出,网络停止时等异常情况下,停止此资源
  128. /// </summary>
  129. public void ErrorDispose(string error)
  130. {
  131. if (stream != null)
  132. {
  133. stream.Flush();
  134. stream.Dispose();
  135. }
  136. if (info != null && !string.IsNullOrEmpty(error))
  137. {
  138. info.ActiveError(error);
  139. }
  140. stream = null;
  141. webRequest = null;
  142. Dispose();
  143. }
  144. }
  145. }