VersionController.cs 6.1 KB

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