BundleDownloaderComponent.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 : Component
  21. {
  22. public VersionConfig VersionConfig { get; private set; }
  23. public Queue<string> bundles;
  24. public long TotalSize;
  25. public HashSet<string> downloadedBundles;
  26. public string downloadingBundle;
  27. public UnityWebRequestAsync webRequest;
  28. public TaskCompletionSource<bool> Tcs;
  29. public async Task StartAsync()
  30. {
  31. using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>())
  32. {
  33. string versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
  34. //Log.Debug(versionUrl);
  35. await webRequestAsync.DownloadAsync(versionUrl);
  36. this.VersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
  37. //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
  38. }
  39. VersionConfig localVersionConfig;
  40. // 对比本地的Version.txt
  41. string versionPath = Path.Combine(PathHelper.AppHotfixResPath, "Version.txt");
  42. if (File.Exists(versionPath))
  43. {
  44. localVersionConfig = JsonHelper.FromJson<VersionConfig>(File.ReadAllText(versionPath));
  45. }
  46. else
  47. {
  48. versionPath = Path.Combine(PathHelper.AppResPath, "Version.txt");
  49. using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
  50. {
  51. await request.DownloadAsync(versionPath);
  52. localVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
  53. }
  54. }
  55. // 先删除服务器端没有的ab
  56. foreach (FileVersionInfo fileVersionInfo in localVersionConfig.FileInfoDict.Values)
  57. {
  58. if (this.VersionConfig.FileInfoDict.ContainsKey(fileVersionInfo.File))
  59. {
  60. continue;
  61. }
  62. string abPath = Path.Combine(PathHelper.AppHotfixResPath, fileVersionInfo.File);
  63. File.Delete(abPath);
  64. }
  65. // 再下载
  66. foreach (FileVersionInfo fileVersionInfo in this.VersionConfig.FileInfoDict.Values)
  67. {
  68. FileVersionInfo localVersionInfo;
  69. if (localVersionConfig.FileInfoDict.TryGetValue(fileVersionInfo.File, out localVersionInfo))
  70. {
  71. if (fileVersionInfo.MD5 == localVersionInfo.MD5)
  72. {
  73. continue;
  74. }
  75. }
  76. if (fileVersionInfo.File == "Version.txt")
  77. {
  78. continue;
  79. }
  80. this.bundles.Enqueue(fileVersionInfo.File);
  81. this.TotalSize += fileVersionInfo.Size;
  82. }
  83. if (this.bundles.Count == 0)
  84. {
  85. return;
  86. }
  87. //Log.Debug($"need download bundles: {this.bundles.ToList().ListToString()}");
  88. await this.WaitAsync();
  89. }
  90. private async void UpdateAsync()
  91. {
  92. try
  93. {
  94. while (true)
  95. {
  96. if (this.bundles.Count == 0)
  97. {
  98. break;
  99. }
  100. this.downloadingBundle = this.bundles.Dequeue();
  101. while (true)
  102. {
  103. try
  104. {
  105. using (this.webRequest = ComponentFactory.Create<UnityWebRequestAsync>())
  106. {
  107. await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
  108. byte[] data = this.webRequest.Request.downloadHandler.data;
  109. string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
  110. if (!Directory.Exists(Path.GetDirectoryName(path)))
  111. {
  112. Directory.CreateDirectory(Path.GetDirectoryName(path));
  113. }
  114. using (FileStream fs = new FileStream(path, FileMode.Create))
  115. {
  116. fs.Write(data, 0, data.Length);
  117. }
  118. }
  119. }
  120. catch (Exception e)
  121. {
  122. Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
  123. continue;
  124. }
  125. break;
  126. }
  127. this.downloadedBundles.Add(this.downloadingBundle);
  128. this.downloadingBundle = "";
  129. this.webRequest = null;
  130. }
  131. using (FileStream fs = new FileStream(Path.Combine(PathHelper.AppHotfixResPath, "Version.txt"), FileMode.Create))
  132. using (StreamWriter sw = new StreamWriter(fs))
  133. {
  134. sw.Write(JsonHelper.ToJson(this.VersionConfig));
  135. }
  136. this.Tcs?.SetResult(true);
  137. }
  138. catch (Exception e)
  139. {
  140. Log.Error(e);
  141. }
  142. }
  143. public int Progress
  144. {
  145. get
  146. {
  147. if (this.VersionConfig == null)
  148. {
  149. return 0;
  150. }
  151. if (this.TotalSize == 0)
  152. {
  153. return 0;
  154. }
  155. long alreadyDownloadBytes = 0;
  156. foreach (string downloadedBundle in this.downloadedBundles)
  157. {
  158. long size = this.VersionConfig.FileInfoDict[downloadedBundle].Size;
  159. alreadyDownloadBytes += size;
  160. }
  161. if (this.webRequest != null)
  162. {
  163. alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
  164. }
  165. return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
  166. }
  167. }
  168. private Task<bool> WaitAsync()
  169. {
  170. if (this.bundles.Count == 0 && this.downloadingBundle == "")
  171. {
  172. return Task.FromResult(true);
  173. }
  174. this.Tcs = new TaskCompletionSource<bool>();
  175. UpdateAsync();
  176. return this.Tcs.Task;
  177. }
  178. }
  179. }