VersionController.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. VEngine.Logger.Loggable = false;
  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) + LauncherConfig.manifest_v);
  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. if (getDownloadSize.totalSize > 0 || updateVersions.changed)
  77. {
  78. string message = $"游戏有新的版本,需要更新{Utility.FormatBytes(getDownloadSize.totalSize)}大小的内容";
  79. yield return Alert.Show(message)
  80. .SetLeftButton(true, "更新", (data) => {
  81. StartDownload(getDownloadSize);
  82. });
  83. yield break;
  84. }
  85. else
  86. {
  87. OnComplete();
  88. }
  89. }
  90. private void StartDownload(GetDownloadSize getDownloadSize)
  91. {
  92. LogServerHelperHttp.SendNodeLog((int)LogNode.StartDownload);
  93. StartCoroutine(Downloading(getDownloadSize));
  94. }
  95. private IEnumerator Downloading(GetDownloadSize getDownloadSize)
  96. {
  97. LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
  98. var downloadAsync = Versions.DownloadAsync(getDownloadSize.result.ToArray());
  99. downloadAsync.updated += downloadAsync =>
  100. {
  101. var current = downloadAsync.downloadedBytes;
  102. var max = downloadAsync.totalSize;
  103. var speed = Download.TotalBandwidth;
  104. LauncherView.Instance.SetDesc($"正在下载资源,速度 {Utility.FormatBytes(speed)}/s", $"{Utility.FormatBytes(current)}/{Utility.FormatBytes(max)}", true);
  105. LauncherView.Instance.SetProgress((int)(downloadAsync.progress * 100));
  106. };
  107. yield return downloadAsync;
  108. if (downloadAsync.status == OperationStatus.Failed)
  109. {
  110. Alert.Show("下载失败!请检查网络状态后重试。")
  111. .SetLeftButton(true, "重试", (data) => {
  112. StartDownload(getDownloadSize);
  113. });
  114. }
  115. else
  116. {
  117. OnComplete();
  118. }
  119. }
  120. private void OnComplete()
  121. {
  122. if(updateVersions != null)
  123. {
  124. updateVersions.Override();
  125. }
  126. LauncherController.OnVersionCompleted();
  127. }
  128. }
  129. }