BundleDownloaderComponent.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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 async Task StartAsync()
  29. {
  30. // 获取远程的Version.txt
  31. string versionUrl = "";
  32. try
  33. {
  34. using (UnityWebRequestAsync webRequestAsync = ComponentFactory.Create<UnityWebRequestAsync>())
  35. {
  36. versionUrl = GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + "Version.txt";
  37. //Log.Debug(versionUrl);
  38. await webRequestAsync.DownloadAsync(versionUrl);
  39. remoteVersionConfig = JsonHelper.FromJson<VersionConfig>(webRequestAsync.Request.downloadHandler.text);
  40. //Log.Debug(JsonHelper.ToJson(this.VersionConfig));
  41. }
  42. }
  43. catch (Exception e)
  44. {
  45. throw new Exception($"url: {versionUrl}", e);
  46. }
  47. // 获取streaming目录的Version.txt
  48. VersionConfig streamingVersionConfig;
  49. string versionPath = Path.Combine(PathHelper.AppResPath4Web, "Version.txt");
  50. using (UnityWebRequestAsync request = ComponentFactory.Create<UnityWebRequestAsync>())
  51. {
  52. await request.DownloadAsync(versionPath);
  53. streamingVersionConfig = JsonHelper.FromJson<VersionConfig>(request.Request.downloadHandler.text);
  54. }
  55. // 删掉远程不存在的文件
  56. DirectoryInfo directoryInfo = new DirectoryInfo(PathHelper.AppHotfixResPath);
  57. if (directoryInfo.Exists)
  58. {
  59. FileInfo[] fileInfos = directoryInfo.GetFiles();
  60. foreach (FileInfo fileInfo in fileInfos)
  61. {
  62. if (remoteVersionConfig.FileInfoDict.ContainsKey(fileInfo.Name))
  63. {
  64. continue;
  65. }
  66. if (fileInfo.Name == "Version.txt")
  67. {
  68. continue;
  69. }
  70. fileInfo.Delete();
  71. }
  72. }
  73. else
  74. {
  75. directoryInfo.Create();
  76. }
  77. // 对比MD5
  78. foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
  79. {
  80. // 对比md5
  81. string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
  82. if (fileVersionInfo.MD5 == localFileMD5)
  83. {
  84. continue;
  85. }
  86. this.bundles.Enqueue(fileVersionInfo.File);
  87. this.TotalSize += fileVersionInfo.Size;
  88. }
  89. }
  90. public int Progress
  91. {
  92. get
  93. {
  94. if (this.TotalSize == 0)
  95. {
  96. return 0;
  97. }
  98. long alreadyDownloadBytes = 0;
  99. foreach (string downloadedBundle in this.downloadedBundles)
  100. {
  101. long size = this.remoteVersionConfig.FileInfoDict[downloadedBundle].Size;
  102. alreadyDownloadBytes += size;
  103. }
  104. if (this.webRequest != null)
  105. {
  106. alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
  107. }
  108. return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
  109. }
  110. }
  111. public async Task DownloadAsync()
  112. {
  113. if (this.bundles.Count == 0 && this.downloadingBundle == "")
  114. {
  115. return;
  116. }
  117. try
  118. {
  119. while (true)
  120. {
  121. if (this.bundles.Count == 0)
  122. {
  123. break;
  124. }
  125. this.downloadingBundle = this.bundles.Dequeue();
  126. while (true)
  127. {
  128. try
  129. {
  130. using (this.webRequest = ComponentFactory.Create<UnityWebRequestAsync>())
  131. {
  132. await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
  133. byte[] data = this.webRequest.Request.downloadHandler.data;
  134. string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
  135. using (FileStream fs = new FileStream(path, FileMode.Create))
  136. {
  137. fs.Write(data, 0, data.Length);
  138. }
  139. }
  140. }
  141. catch (Exception e)
  142. {
  143. Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
  144. continue;
  145. }
  146. break;
  147. }
  148. this.downloadedBundles.Add(this.downloadingBundle);
  149. this.downloadingBundle = "";
  150. this.webRequest = null;
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. Log.Error(e);
  156. }
  157. }
  158. }
  159. }