VersionController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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) + LauncherConfig.manifest_v);
  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. 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. LauncherController.AfterVersion();
  128. }
  129. }
  130. }