BundleDownloaderComponent.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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}");
  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. fileInfo.Delete();
  67. }
  68. }
  69. else
  70. {
  71. directoryInfo.Create();
  72. }
  73. // 对比MD5
  74. foreach (FileVersionInfo fileVersionInfo in remoteVersionConfig.FileInfoDict.Values)
  75. {
  76. // 对比md5
  77. string localFileMD5 = BundleHelper.GetBundleMD5(streamingVersionConfig, fileVersionInfo.File);
  78. if (fileVersionInfo.MD5 == localFileMD5)
  79. {
  80. continue;
  81. }
  82. this.bundles.Enqueue(fileVersionInfo.File);
  83. this.TotalSize += fileVersionInfo.Size;
  84. }
  85. }
  86. public int Progress
  87. {
  88. get
  89. {
  90. if (this.TotalSize == 0)
  91. {
  92. return 0;
  93. }
  94. long alreadyDownloadBytes = 0;
  95. foreach (string downloadedBundle in this.downloadedBundles)
  96. {
  97. long size = this.remoteVersionConfig.FileInfoDict[downloadedBundle].Size;
  98. alreadyDownloadBytes += size;
  99. }
  100. if (this.webRequest != null)
  101. {
  102. alreadyDownloadBytes += (long)this.webRequest.Request.downloadedBytes;
  103. }
  104. return (int)(alreadyDownloadBytes * 100f / this.TotalSize);
  105. }
  106. }
  107. public async Task DownloadAsync()
  108. {
  109. if (this.bundles.Count == 0 && this.downloadingBundle == "")
  110. {
  111. return;
  112. }
  113. try
  114. {
  115. while (true)
  116. {
  117. if (this.bundles.Count == 0)
  118. {
  119. break;
  120. }
  121. this.downloadingBundle = this.bundles.Dequeue();
  122. while (true)
  123. {
  124. try
  125. {
  126. using (this.webRequest = ComponentFactory.Create<UnityWebRequestAsync>())
  127. {
  128. await this.webRequest.DownloadAsync(GlobalConfigComponent.Instance.GlobalProto.GetUrl() + "StreamingAssets/" + this.downloadingBundle);
  129. byte[] data = this.webRequest.Request.downloadHandler.data;
  130. string path = Path.Combine(PathHelper.AppHotfixResPath, this.downloadingBundle);
  131. using (FileStream fs = new FileStream(path, FileMode.Create))
  132. {
  133. fs.Write(data, 0, data.Length);
  134. }
  135. }
  136. }
  137. catch (Exception e)
  138. {
  139. Log.Error($"download bundle error: {this.downloadingBundle}\n{e}");
  140. continue;
  141. }
  142. break;
  143. }
  144. this.downloadedBundles.Add(this.downloadingBundle);
  145. this.downloadingBundle = "";
  146. this.webRequest = null;
  147. }
  148. }
  149. catch (Exception e)
  150. {
  151. Log.Error(e);
  152. }
  153. }
  154. }
  155. }