BundleDownloaderComponent.cs 5.4 KB

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