BundleDownloaderComponent.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading.Tasks;
  5. namespace ET
  6. {
  7. public class BundleDownloaderComponentAwakeSystem : AwakeSystem<BundleDownloaderComponent>
  8. {
  9. public override void Awake(BundleDownloaderComponent self)
  10. {
  11. self.bundles = new Queue<string>();
  12. self.downloadedBundles = new HashSet<string>();
  13. self.downloadingBundle = "";
  14. }
  15. }
  16. /// <summary>
  17. /// 用来对比web端的资源,比较md5,对比下载资源
  18. /// </summary>
  19. public class BundleDownloaderComponent : Entity
  20. {
  21. private VersionConfig remoteVersionConfig;
  22. public Queue<string> bundles;
  23. public long TotalSize;
  24. public HashSet<string> downloadedBundles;
  25. public string downloadingBundle;
  26. public UnityWebRequestAsync webRequest;
  27. public override void Dispose()
  28. {
  29. if (this.IsDisposed)
  30. {
  31. return;
  32. }
  33. if (this.Parent.IsDisposed)
  34. {
  35. return;
  36. }
  37. base.Dispose();
  38. this.remoteVersionConfig = null;
  39. this.TotalSize = 0;
  40. this.bundles = null;
  41. this.downloadedBundles = null;
  42. this.downloadingBundle = null;
  43. this.webRequest?.Dispose();
  44. this.Parent.RemoveComponent<BundleDownloaderComponent>();
  45. }
  46. public async ETTask StartAsync(string url)
  47. {
  48. // 获取远程的Version.txt
  49. string versionUrl = "";
  50. try
  51. {
  52. using (UnityWebRequestAsync webRequestAsync = EntityFactory.Create<UnityWebRequestAsync>(this.Domain))
  53. {
  54. versionUrl = url + "StreamingAssets/" + "Version.txt";
  55. //Log.Debug(versionUrl);
  56. await webRequestAsync.DownloadAsync(versionUrl);
  57. remoteVersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
  58. //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
  59. }
  60. }
  61. catch (Exception e)
  62. {
  63. throw new Exception($"url: {versionUrl}", e);
  64. }
  65. // 获取streaming目录的Version.txt
  66. VersionConfig streamingVersionConfig;
  67. string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
  68. using (UnityWebRequestAsync request = EntityFactory.Create<UnityWebRequestAsync>(this.Domain))
  69. {
  70. await request.DownloadAsync(versionPath);
  71. streamingVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
  72. }
  73. // 删掉远程不存在的文件
  74. DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath);
  75. if (directoryInfo.Exists)
  76. {
  77. FileInfo[] fileInfos = directoryInfo.GetFiles();
  78. foreach (FileInfo fileInfo in fileInfos)
  79. {
  80. if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
  81. {
  82. continue;
  83. }
  84. if (fileInfo.Name == "Version.txt")
  85. {
  86. continue;
  87. }
  88. fileInfo.Delete();
  89. }
  90. }
  91. else
  92. {
  93. directoryInfo.Create();
  94. }
  95. // 对比MD5
  96. foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
  97. {
  98. // 对比md5
  99. string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
  100. if (fileVersionInfo.MD5 == localFileMD5)
  101. {
  102. continue;
  103. }
  104. this.bundles.Enqueue(fileVersionInfo.File);
  105. this.TotalSize += fileVersionInfo.Size;
  106. }
  107. }
  108. public int Progress
  109. {
  110. get
  111. {
  112. if (this.TotalSize == 0)
  113. {
  114. return 0;
  115. }
  116. long alreadyDownloadBytes = 0;
  117. foreach (string downloadedBundle in this.downloadedBundles)
  118. {
  119. long size = this.remoteVersionConfig.FileInfoDict[downloadedBundle].Size;
  120. alreadyDownloadBytes += size;
  121. }
  122. if (this.webRequest != null)
  123. {
  124. alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
  125. }
  126. return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
  127. }
  128. }
  129. public async ETTask DownloadAsync(string url)
  130. {
  131. if (this.bundles.Count == 0 && this.downloadingBundle == "")
  132. {
  133. return;
  134. }
  135. try
  136. {
  137. while (true)
  138. {
  139. if (this.bundles.Count == 0)
  140. {
  141. break;
  142. }
  143. this.downloadingBundle = this.bundles.Dequeue();
  144. while (true)
  145. {
  146. try
  147. {
  148. using (this.webRequest = EntityFactory.Create<UnityWebRequestAsync>(this.Domain))
  149. {
  150. await this.webRequest.DownloadAsync(url + "StreamingAssets/" + this.downloadingBundle);
  151. byte[] data = this.webRequest.Request.downloadHandler.data;
  152. string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
  153. using (FileStream fs = new FileStream(path, FileMode.Create))
  154. {
  155. fs.Write(data, 0, data.Length);
  156. }
  157. }
  158. }
  159. catch (Exception e)
  160. {
  161. Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
  162. continue;
  163. }
  164. break;
  165. }
  166. this.downloadedBundles.Add(this.downloadingBundle);
  167. this.downloadingBundle = "";
  168. this.webRequest = null;
  169. }
  170. }
  171. catch (Exception e)
  172. {
  173. Log.Error(e);
  174. }
  175. }
  176. }
  177. }