123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using GFGGame.Launcher;
- using VEngine;
- using System.Collections;
- using UnityEngine;
- namespace GFGGame
- {
- public class VersionController : SingletonMonoBase<VersionController>
- {
- private UpdateVersions updateVersions;
-
- public void Init()
- {
- StartCoroutine(InitVersion());
- }
- public IEnumerator InitVersion()
- {
- VEngine.Logger.Loggable = false;
- EncryptHelper.resKey = LauncherConfig.resKey;
- Versions.DownloadURL = LauncherConfig.CDN_ROOT;
- var operation = Versions.InitializeAsync(LauncherConfig.CDN_ROOT);
- yield return operation;
- //VEngine.Logger.I("Initialize: {0}", operation.status);
- //VEngine.Logger.I("API Version: {0}", Versions.APIVersion);
- //VEngine.Logger.I("AppVersion: {0}", Versions.Manifest.appVersion);
- //VEngine.Logger.I("Manifests Version: {0}", Versions.ManifestsVersion);
- //VEngine.Logger.I("PlayerDataPath: {0}", Versions.PlayerDataPath);
- //VEngine.Logger.I("DownloadDataPath: {0}", Versions.DownloadDataPath);
- //VEngine.Logger.I("DownloadURL: {0}", Versions.DownloadURL);
- StartCheckManifestVersion();
- }
- private void StartCheckManifestVersion()
- {
- StartCoroutine(CheckManifestVersion());
- }
- private IEnumerator CheckManifestVersion()
- {
- if (!Versions.OfflineMode)
- {
- LogServerHelperHttp.SendNodeLog((int)LogNode.StartCheckVersion);
- // TODO:生产环境这里的清单名字应该使用带 hash 的版本
- updateVersions = Versions.UpdateAsync(nameof(Manifest) + LauncherConfig.manifest_v);
- while(!updateVersions.isDone)
- {
- if(updateVersions.asset != null && updateVersions.asset.assetVersion != null)
- {
-
- var max = updateVersions.asset.assetVersion.size;
- LauncherView.Instance.SetDesc($"正在校验资源版本...", $"{Utility.FormatBytes(max)}");
- LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
- }
- yield return updateVersions;
- };
- if (updateVersions.status == OperationStatus.Failed)
- {
- Alert.Show("更新版本信息失败,请检测网络链接后重试。")
- .SetLeftButton(true, "重试", (data) => { StartCheckManifestVersion(); });
- }
- else
- {
- yield return GetDownloadSize();
- }
- }
- else
- {
- OnComplete();
- }
- }
- private IEnumerator GetDownloadSize()
- {
- var getDownloadSize = Versions.GetDownloadSizeAsync(updateVersions);
- var totalCount = getDownloadSize.bundles.Count;
- while(!getDownloadSize.isDone)
- {
- var remainCount = getDownloadSize.bundles.Count;
- LauncherView.Instance.SetDesc($"正在计算更新内容大小...", $"{ totalCount - remainCount }/{totalCount}");
- yield return getDownloadSize;
- }
- if (getDownloadSize.totalSize > 0 || updateVersions.changed)
- {
- string message = $"游戏有新的版本,需要更新{Utility.FormatBytes(getDownloadSize.totalSize)}大小的内容";
- yield return Alert.Show(message)
- .SetLeftButton(true, "更新", (data) => {
- StartDownload(getDownloadSize);
- });
- yield break;
- }
- else
- {
- OnComplete();
- }
- }
- private void StartDownload(GetDownloadSize getDownloadSize)
- {
- LogServerHelperHttp.SendNodeLog((int)LogNode.StartDownload);
- StartCoroutine(Downloading(getDownloadSize));
- }
- private IEnumerator Downloading(GetDownloadSize getDownloadSize)
- {
- LauncherView.Instance.SetProgress((int)(updateVersions.asset.progress * 100));
- var downloadAsync = Versions.DownloadAsync(getDownloadSize.result.ToArray());
- downloadAsync.updated += downloadAsync =>
- {
- var current = downloadAsync.downloadedBytes;
- var max = downloadAsync.totalSize;
- var speed = Download.TotalBandwidth;
- LauncherView.Instance.SetDesc($"正在下载资源,速度 {Utility.FormatBytes(speed)}/s", $"{Utility.FormatBytes(current)}/{Utility.FormatBytes(max)}", true);
- LauncherView.Instance.SetProgress((int)(downloadAsync.progress * 100));
- };
- yield return downloadAsync;
- if (downloadAsync.status == OperationStatus.Failed)
- {
- Alert.Show("下载失败!请检查网络状态后重试。")
- .SetLeftButton(true, "重试", (data) => {
- StartDownload(getDownloadSize);
- });
- }
- else
- {
- OnComplete();
- }
- }
- private void OnComplete()
- {
- if(updateVersions != null)
- {
- updateVersions.Override();
- }
- LauncherController.OnVersionCompleted();
- }
- }
- }
|