BundleDownloaderComponent.cs 4.9 KB

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