VersionController.cs 6.3 KB

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