VersionController.cs 5.7 KB

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