VersionController.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using GFGGame.Launcher;
  2. using VEngine;
  3. using System.Collections;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class VersionController : SingletonMonoBase<VersionController>
  8. {
  9. private UpdateVersions updateVersions;
  10. public void Init()
  11. {
  12. StartCoroutine(InitVersion());
  13. }
  14. public IEnumerator InitVersion()
  15. {
  16. LauncherView.Instance.SetDesc("正在校验资源版本...");
  17. EncryptHelper.resKey = LauncherConfig.resKey;
  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. StartCheckManifestVersion();
  29. }
  30. private void StartCheckManifestVersion()
  31. {
  32. StartCoroutine(CheckManifestVersion());
  33. }
  34. private IEnumerator CheckManifestVersion()
  35. {
  36. if (!Versions.OfflineMode)
  37. {
  38. LogServerHelperHttp.SendNodeLog((int)LogNode.StartCheckVersion);
  39. // TODO:生产环境这里的清单名字应该使用带 hash 的版本
  40. updateVersions = Versions.UpdateAsync(nameof(Manifest));
  41. while(!updateVersions.isDone)
  42. {
  43. if(updateVersions.asset != null && updateVersions.asset.assetVersion != null)
  44. {
  45. var max = updateVersions.asset.assetVersion.size;
  46. LauncherView.Instance.SetDesc($"正在校验资源版本...", $"{Utility.FormatBytes(max)}");
  47. LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
  48. }
  49. yield return updateVersions;
  50. };
  51. if (updateVersions.status == OperationStatus.Failed)
  52. {
  53. Alert.Show("更新版本信息失败,请检测网络链接后重试。")
  54. .SetLeftButton(true, "重试", (data) => { StartCheckManifestVersion(); });
  55. }
  56. else
  57. {
  58. yield return GetDownloadSize();
  59. }
  60. }
  61. else
  62. {
  63. OnComplete();
  64. }
  65. }
  66. private IEnumerator GetDownloadSize()
  67. {
  68. var getDownloadSize = Versions.GetDownloadSizeAsync(updateVersions);
  69. var totalCount = getDownloadSize.bundles.Count;
  70. while(!getDownloadSize.isDone)
  71. {
  72. var remainCount = getDownloadSize.bundles.Count;
  73. LauncherView.Instance.SetDesc($"正在计算更新内容大小...", $"{ totalCount - remainCount }/{totalCount}");
  74. yield return getDownloadSize;
  75. }
  76. Debug.Log("VersionController GetDownloadSize tips");
  77. if (getDownloadSize.totalSize > 0 || updateVersions.changed)
  78. {
  79. string message = $"游戏有新的版本,需要更新{Utility.FormatBytes(getDownloadSize.totalSize)}大小的内容";
  80. yield return Alert.Show(message)
  81. .SetLeftButton(true, "更新", (data) => {
  82. StartDownload(getDownloadSize);
  83. });
  84. yield break;
  85. }
  86. else
  87. {
  88. OnComplete();
  89. }
  90. }
  91. private void StartDownload(GetDownloadSize getDownloadSize)
  92. {
  93. LogServerHelperHttp.SendNodeLog((int)LogNode.StartDownload);
  94. StartCoroutine(Downloading(getDownloadSize));
  95. }
  96. private IEnumerator Downloading(GetDownloadSize getDownloadSize)
  97. {
  98. LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
  99. var downloadAsync = Versions.DownloadAsync(getDownloadSize.result.ToArray());
  100. downloadAsync.updated += downloadAsync =>
  101. {
  102. var current = downloadAsync.downloadedBytes;
  103. var max = downloadAsync.totalSize;
  104. var speed = Download.TotalBandwidth;
  105. LauncherView.Instance.SetDesc($"正在下载资源,速度 {Utility.FormatBytes(speed)}/s", $"{Utility.FormatBytes(current)}/{Utility.FormatBytes(max)}");
  106. LauncherView.Instance.SetProgress((int)(downloadAsync.progress * 100));
  107. };
  108. yield return downloadAsync;
  109. if (downloadAsync.status == OperationStatus.Failed)
  110. {
  111. Alert.Show("下载失败!请检查网络状态后重试。")
  112. .SetLeftButton(true, "重试", (data) => {
  113. StartDownload(getDownloadSize);
  114. });
  115. }
  116. else
  117. {
  118. OnComplete();
  119. }
  120. }
  121. private void OnComplete()
  122. {
  123. if(updateVersions != null)
  124. {
  125. updateVersions.Override();
  126. }
  127. LauncherView.Instance.SetDesc($"正在启动游戏...");
  128. LauncherView.Instance.SetProgress(100, () =>
  129. {
  130. HotUpdateCodeLoader.Instance.StartLoad();
  131. });
  132. }
  133. }
  134. }