VersionController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using GFGGame.Launcher;
  2. using VEngine;
  3. using System.Collections;
  4. using UnityEngine;
  5. using System.Reflection;
  6. using System;
  7. namespace GFGGame
  8. {
  9. public class VersionController : SingletonMonoBase<VersionController>
  10. {
  11. private UpdateVersions updateVersions;
  12. public void Init()
  13. {
  14. StartCoroutine(InitVersion());
  15. }
  16. public IEnumerator InitVersion()
  17. {
  18. LogServerHelperHttp.SendNodeLog((int)LogNode.StartCheckVersion);
  19. Versions.DownloadURL = LauncherConfig.CDN_ROOT;
  20. var operation = Versions.InitializeAsync(LauncherConfig.CDN_ROOT);
  21. yield return operation;
  22. VEngine.Logger.I("Initialize: {0}", operation.status);
  23. VEngine.Logger.I("API Version: {0}", Versions.APIVersion);
  24. VEngine.Logger.I("AppVersion: {0}", Versions.Manifest.appVersion);
  25. VEngine.Logger.I("Manifests Version: {0}", Versions.ManifestsVersion);
  26. VEngine.Logger.I("PlayerDataPath: {0}", Versions.PlayerDataPath);
  27. VEngine.Logger.I("DownloadDataPath: {0}", Versions.DownloadDataPath);
  28. VEngine.Logger.I("DownloadURL: {0}", Versions.DownloadURL);
  29. StartUpdateManifest();
  30. }
  31. private void CheckApkVersion()
  32. {
  33. var versionTarget = Versions.Manifest.appVersion;
  34. var version = Application.version;
  35. if(VersionUtil.compare(version, versionTarget))
  36. {
  37. DownloadApk();
  38. }
  39. else
  40. {
  41. DownloadRes();
  42. }
  43. }
  44. private void StartUpdateManifest()
  45. {
  46. StartCoroutine(CheckManifestVersion());
  47. }
  48. private IEnumerator CheckManifestVersion()
  49. {
  50. if (!Versions.OfflineMode)
  51. {
  52. // TODO:生产环境这里的清单名字应该使用带 hash 的版本
  53. updateVersions = Versions.UpdateAsync(nameof(Manifest));
  54. while(!updateVersions.isDone)
  55. {
  56. if(updateVersions.asset != null && updateVersions.asset.assetVersion != null)
  57. {
  58. var max = updateVersions.asset.assetVersion.size;
  59. LauncherView.Instance.SetDesc($"获取版本文件...", $"{Utility.FormatBytes(max)}");
  60. LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
  61. }
  62. yield return updateVersions;
  63. };
  64. if (updateVersions.status == OperationStatus.Failed)
  65. {
  66. yield return Alert.Show("更新版本信息失败,请检测网络链接后重试。")
  67. .SetLeftButton(true, "重试", (data) => { StartUpdateManifest(); });
  68. yield break;
  69. }
  70. CheckApkVersion();
  71. yield break;
  72. }
  73. OnComplete();
  74. }
  75. private void DownloadApk()
  76. {
  77. Alert.Show("需要安装新的安装包,请联系研发获取。")
  78. .SetLeftButton(true, "知道了", (data) => {
  79. Application.Quit();
  80. });
  81. }
  82. private void DownloadRes()
  83. {
  84. StartCoroutine(GetDownloadSize());
  85. }
  86. private IEnumerator GetDownloadSize()
  87. {
  88. LogServerHelperHttp.SendNodeLog((int)LogNode.StartDownload);
  89. Debug.Log("VersionController GetDownloadSize");
  90. var getDownloadSize = Versions.GetDownloadSizeAsync(updateVersions);
  91. var totalCount = getDownloadSize.bundles.Count;
  92. while(!getDownloadSize.isDone)
  93. {
  94. var remainCount = getDownloadSize.bundles.Count;
  95. LauncherView.Instance.SetDesc($"正在计算更新内容大小...", $"{ totalCount - remainCount }/{totalCount}");
  96. yield return getDownloadSize;
  97. }
  98. Debug.Log("VersionController GetDownloadSize tips");
  99. if (getDownloadSize.totalSize > 0 || updateVersions.changed)
  100. {
  101. string message = $"游戏有新的版本,需要更新{Utility.FormatBytes(getDownloadSize.totalSize)}大小的内容";
  102. yield return Alert.Show(message)
  103. .SetLeftButton(true, "更新", (data) => {
  104. StartDownload(getDownloadSize);
  105. });
  106. yield break;
  107. }
  108. OnComplete();
  109. }
  110. private void StartDownload(GetDownloadSize getDownloadSize)
  111. {
  112. StartCoroutine(Downloading(getDownloadSize));
  113. }
  114. private IEnumerator Downloading(GetDownloadSize getDownloadSize)
  115. {
  116. LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
  117. var downloadAsync = Versions.DownloadAsync(getDownloadSize.result.ToArray());
  118. downloadAsync.updated += downloadAsync =>
  119. {
  120. var current = downloadAsync.downloadedBytes;
  121. var max = downloadAsync.totalSize;
  122. var speed = Download.TotalBandwidth;
  123. LauncherView.Instance.SetDesc($"正在下载资源,速度 {Utility.FormatBytes(speed)}/s", $"{Utility.FormatBytes(current)}/{Utility.FormatBytes(max)}");
  124. LauncherView.Instance.SetProgress((int)(downloadAsync.progress * 100));
  125. };
  126. yield return downloadAsync;
  127. if (downloadAsync.status == OperationStatus.Failed)
  128. {
  129. yield return Alert.Show("下载失败!请检查网络状态后重试。")
  130. .SetLeftButton(true, "重试", (data) => {
  131. StartDownload(getDownloadSize);
  132. });
  133. yield break;
  134. }
  135. OnComplete();
  136. }
  137. private void OnComplete()
  138. {
  139. if(updateVersions != null)
  140. {
  141. updateVersions.Override();
  142. }
  143. LauncherView.Instance.SetDesc($"正在启动游戏...");
  144. LauncherView.Instance.SetProgress(100, () =>
  145. {
  146. HotUpdateCodeLoader.Instance.StartLoad();
  147. });
  148. }
  149. }
  150. }