using GFGGame.Launcher; //using VEngine; using System.Collections; using System.Collections.Generic; using UnityEngine; using YooAsset; //using dnlib.PE; //using VEngine; //using UnityEngine.Events; namespace GFGGame { public class VersionController : SingletonMonoBase { //private UpdateVersions updateVersions; public const string DefaultPackage = "GameLogic"; /// /// 包裹的版本信息 /// public string PackageVersion { set; get; } public void Init() { // 初始化资源系统 YooAssets.Initialize(); 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; yield return InitDefaultPackage(); yield return GetStaticVersion(DefaultPackage); yield return UpdateManifest(DefaultPackage); CreateDownloader(DefaultPackage); } private IEnumerator InitDefaultPackage() { // 创建默认的资源包 string packageName = DefaultPackage; var package = YooAssets.TryGetPackage(packageName); if (package == null) { package = YooAssets.CreatePackage(packageName); YooAssets.SetDefaultPackage(package); } var playMode = GameLauncher.Instance.PlayMode; InitializationOperation initializationOperation = null; if (playMode == EPlayMode.EditorSimulateMode) { // 编辑器下的模拟模式 var createParameters = new EditorSimulateModeParameters(); createParameters.SimulateManifestFilePath = EditorSimulateModeHelper.SimulateBuild(packageName); initializationOperation = package.InitializeAsync(createParameters); } else if (playMode == EPlayMode.HostPlayMode) { // 联机运行模式 string defaultHostServer = GetHostServerURL(DefaultPackage); string fallbackHostServer = defaultHostServer; var createParameters = new HostPlayModeParameters(); //createParameters.DecryptionServices = new GameDecryptionServices(); createParameters.QueryServices = new GameQueryServices(); createParameters.RemoteServices = new RemoteServices(defaultHostServer, fallbackHostServer); initializationOperation = package.InitializeAsync(createParameters); } yield return initializationOperation; if (initializationOperation.Status != EOperationStatus.Succeed) { Debug.LogWarning($"{initializationOperation.Error}"); } } private IEnumerator GetStaticVersion(string packageName) { var package = YooAssets.GetPackage(packageName); var operation = package.UpdatePackageVersionAsync(); yield return operation; if (operation.Status == EOperationStatus.Succeed) { VersionController.Instance.PackageVersion = operation.PackageVersion; Debug.Log($"远端最新版本为: {operation.PackageVersion}"); } else { Debug.LogWarning(operation.Error); } } private IEnumerator UpdateManifest(string packageName) { bool savePackageVersion = true; var package = YooAssets.GetPackage(packageName); var operation = package.UpdatePackageManifestAsync(VersionController.Instance.PackageVersion, savePackageVersion); yield return operation; if (operation.Status != EOperationStatus.Succeed) { Debug.LogWarning(operation.Error); Alert.Show("更新版本信息失败,请检测网络链接后重试。") .SetLeftButton(true, "重试", (data) => { StartCoroutine(UpdateManifest(packageName)); }); } } private void CreateDownloader(string packageName) { int downloadingMaxNum = 10; int failedTryAgain = 3; ResourcePackage package = YooAssets.GetPackage(packageName); var downloaderOperation = package.CreateResourceDownloader(new string[] { "preload", "dynamic" }, downloadingMaxNum, failedTryAgain); if (downloaderOperation.TotalDownloadCount == 0) { LauncherController.OnVersionCompleted(); } else { //A total of 10 files were found that need to be downloaded Debug.Log($"Found total {downloaderOperation.TotalDownloadCount} files that need download !"); // 发现新更新文件后,挂起流程系统 // 注意:开发者需要在下载前检测磁盘空间不足 int totalDownloadCount = downloaderOperation.TotalDownloadCount; long totalDownloadBytes = downloaderOperation.TotalDownloadBytes; float sizeMB = totalDownloadBytes / 1048576f; sizeMB = Mathf.Clamp(sizeMB, 0.1f, float.MaxValue); string totalSizeMB = sizeMB.ToString("f1"); string message = $"游戏有新的内容,{packageName}需要更新{totalSizeMB}MB大小的内容"; Alert.Show(message) .SetLeftButton(true, "更新", (data) => { StartCoroutine(BeginDownload(downloaderOperation, packageName)); }); } } private IEnumerator BeginDownload(ResourceDownloaderOperation downloaderOperation, string packageName) { // 注册下载回调 downloaderOperation.OnDownloadErrorCallback = (fileName, error) => { Debug.LogError($"加载{fileName}失败 {error}"); }; downloaderOperation.OnDownloadProgressCallback = (totalDownloadCount, currentDownloadCount, totalDownloadSizeBytes, currentDownloadSizeBytes) => { string currentSizeMB = (currentDownloadSizeBytes / 1048576f).ToString("f1"); string totalSizeMB = (totalDownloadSizeBytes / 1048576f).ToString("f1"); var progress = currentDownloadSizeBytes / totalDownloadSizeBytes; LauncherView.Instance.SetDesc($"正在下载资源,{currentDownloadCount}/{totalDownloadCount}", $"{currentSizeMB}MB/{totalSizeMB}MB", true); LauncherView.Instance.SetProgress((int)(progress * 100)); }; downloaderOperation.BeginDownload(); yield return downloaderOperation; // 检测下载结果 if (downloaderOperation.Status != EOperationStatus.Succeed) { Alert.Show("下载失败!请检查网络状态后重试。") .SetLeftButton(true, "重试", (data) => { StartCoroutine(BeginDownload(downloaderOperation, packageName)); }); yield break; } LauncherController.OnVersionCompleted(); } /// /// 获取资源服务器地址 /// private string GetHostServerURL(string packageName) { string hostServerIP = "http://10.108.64.127"; #if UNITY_EDITOR if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.Android) return $"{hostServerIP}/CDN/Android/{packageName}"; else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.iOS) return $"{hostServerIP}/CDN/IPhone/{packageName}"; else if (UnityEditor.EditorUserBuildSettings.activeBuildTarget == UnityEditor.BuildTarget.WebGL) return $"{hostServerIP}/CDN/WebGL/{packageName}"; else return $"{hostServerIP}/CDN/PC/{packageName}"; #else if (Application.platform == RuntimePlatform.Android) return $"{hostServerIP}/CDN/Android/{packageName}"; else if (Application.platform == RuntimePlatform.IPhonePlayer) return $"{hostServerIP}/CDN/IPhone/{packageName}"; else if (Application.platform == RuntimePlatform.WebGLPlayer) return $"{hostServerIP}/CDN/WebGL/{packageName}"; else return $"{hostServerIP}/CDN/PC/{packageName}"; #endif } /// /// 远端资源地址查询服务类 /// private class RemoteServices : IRemoteServices { private readonly string _defaultHostServer; private readonly string _fallbackHostServer; public RemoteServices(string defaultHostServer, string fallbackHostServer) { _defaultHostServer = defaultHostServer; _fallbackHostServer = fallbackHostServer; } string IRemoteServices.GetRemoteMainURL(string fileName) { return $"{_defaultHostServer}/{fileName}"; } string IRemoteServices.GetRemoteFallbackURL(string fileName) { return $"{_fallbackHostServer}/{fileName}"; } } //private void StartCheckManifestVersion() //{ // StartCoroutine(CheckManifestVersion()); //} //private IEnumerator CheckManifestVersion() //{ // if (GameLauncher.Instance.PlayMode == EPlayMode.HostPlayMode) // { // 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(); // } //} } }