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