NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/HotUpdate/GameLauncher.cs

222 lines
6.9 KiB
C#
Raw Normal View History

2024-02-22 14:53:23 +08:00
using Code.Scripts.HotUpdate.UI;
2024-02-22 13:24:26 +08:00
using Cysharp.Threading.Tasks;
2024-02-21 16:24:29 +08:00
using PhxhSDK.AOT;
2024-02-22 12:41:18 +08:00
using PhxhSDK.AOT.SimpleLoader;
2024-02-22 14:53:23 +08:00
using PhxhSDK.AOT.VersionUpdate;
2024-07-24 12:43:10 +08:00
using System;
2024-08-22 12:45:38 +08:00
using PhxhSDK.AOT.PreConfig;
2023-10-19 15:00:19 +08:00
using UnityEngine;
2024-07-17 12:51:08 +08:00
using UnityEngine.SceneManagement;
2024-09-26 19:16:39 +08:00
using Assets.PhxhSDK.AOT.BI;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
2023-10-19 15:00:19 +08:00
public class GameLauncher : MonoBehaviour
{
const string START_SCENE_NAME = "Assets/Scenes/StartScene.unity";
2024-07-16 14:00:58 +08:00
2024-01-30 19:42:52 +08:00
//最大能容忍的下载卡住时长,一旦超过则重新开始下载
2024-01-31 18:33:47 +08:00
// private const float DOWNLOAD_STUCK_TIME_MAX = 10;
2024-01-30 19:42:52 +08:00
2024-07-16 14:00:58 +08:00
#if USE_HCLR
2024-02-22 12:41:18 +08:00
private HybridCLRHelper _hybridCLRHelper;
2024-07-16 14:00:58 +08:00
#endif
2024-02-22 14:53:23 +08:00
private VersionUpdateHandle _versionUpdateHandle;
private ISimpleLoader _simpleLoader;
2024-08-01 15:46:36 +08:00
private IVersionUpdateUI versionUpdateUI;
2023-10-19 15:00:19 +08:00
public bool enableHotUpdate = true;
private void Start()
{
versionUpdateUI = new TempUIHandle();
var agreement = PlayerPrefs.GetInt("PrivacyAgreement", 0) == 1;
if (!agreement)
{
TempUpdateUI.Instance.PrivacyAgreeCallback = () =>
{
TempUpdateUI.Instance.m_ProgressObj.SetActive(true);
Launch();
};
TempUpdateUI.Instance.m_PrivacyDialogObj.SetActive(true);
}
else
{
TempUpdateUI.Instance.m_ProgressObj.SetActive(true);
Launch();
}
2023-10-19 15:00:19 +08:00
}
2024-02-22 14:53:23 +08:00
private async void Launch()
2023-10-19 15:00:19 +08:00
{
#if UNITY_EDITOR
enableHotUpdate = false;
#endif
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
2024-02-21 16:24:29 +08:00
#endif
2024-08-23 13:21:34 +08:00
versionUpdateUI.UpdateProgress(0f);
versionUpdateUI.UpdateProgressText("尝试网络连接中...");
_versionUpdateHandle = new VersionUpdateHandle(versionUpdateUI);
VersionUpdateInfo versionUpdateInfo = _versionUpdateHandle.GetVersionInfo();
2024-09-11 19:32:40 +08:00
PackDataInst.CreateInst();
UpdateLocalInfo localInfo = PackDataInst.inst.localInfo;
versionUpdateUI.ShowVersion(Application.version, localInfo.workSpace, localInfo.localFullVersion, "获取中...", "获取中...");
2024-09-26 19:16:39 +08:00
#if SDK_TALKINGDATA
2024-11-28 16:59:49 +08:00
BIToolinAOT.Instance.Init(TalkingDataInitTool.appID, TalkingDataInitTool.channel);//TalkingData初始化
2024-12-06 15:47:36 +08:00
//BIToolinAOT.Instance.TalkingDataStart();
BIToolinAOT.Instance.TrackEventInAOT("app_open", null);
2024-09-26 19:16:39 +08:00
#endif
#region 更新AppConfig
2024-08-23 12:44:50 +08:00
bool downloadResult;
do
2024-08-22 12:45:38 +08:00
{
2024-08-23 13:21:34 +08:00
try
{
2024-08-29 15:03:47 +08:00
versionUpdateUI.UpdateProgressText("开始请求APPConfig");
2024-09-26 19:16:39 +08:00
BIToolinAOT.Instance.TrackEventInAOT("app_config_request", null);
2024-08-23 13:21:34 +08:00
downloadResult = await PreConfig.instance.DownloadConfig();
2024-09-26 19:16:39 +08:00
2024-08-29 15:03:47 +08:00
versionUpdateUI.UpdateProgressText("请求APPConfig结束");
versionUpdateUI.ShowVersion(Application.version, localInfo.workSpace, localInfo.localFullVersion, "获取中...", PreConfig.instance.ServerName);
2024-09-26 19:16:39 +08:00
BIToolinAOT.Instance.TrackEventInAOT("app_config_response", new System.Collections.Generic.Dictionary<string, object>(){
{"result", downloadResult? 0 : 1 }
});
2024-08-23 13:21:34 +08:00
}
catch (Exception)
{
downloadResult = false;
}
2024-10-12 18:29:13 +08:00
}
while (!downloadResult && await versionUpdateUI.ShowDialog("提示", "下载配置文件失败,是否重试?"));
2024-09-30 16:04:37 +08:00
2024-08-23 12:44:50 +08:00
if (!downloadResult)
TempUpdateUI.Instance.OnClickCancel();
#endregion
2024-08-22 12:45:38 +08:00
if (!PreConfig.instance.isConfigValid)
{
// 配置错误,没有读取到ResUrl
// TODO: 这种情况也应该有个弹出框,提示用户配置文件错误
Debug.LogError("配置文件错误");
2024-08-23 13:21:34 +08:00
await versionUpdateUI.ShowDialog("提示", "配置文件错误");
2024-08-23 12:44:50 +08:00
TempUpdateUI.Instance.OnClickCancel();
2024-08-22 12:45:38 +08:00
return;
}
2024-07-16 14:00:58 +08:00
2024-02-21 16:24:29 +08:00
#if USE_YOO
2024-02-22 13:24:26 +08:00
await YooAssetHelper.Init();
2024-02-22 14:53:23 +08:00
_simpleLoader = new SimpleLoader_YooAsset();
#else
_simpleLoader = new SimpleLoader_Addressables();
2023-10-19 15:00:19 +08:00
#endif
Debug.Log($"Launch Game! enableHotUpdate:{enableHotUpdate}");
2024-07-16 14:00:58 +08:00
2024-09-26 19:16:39 +08:00
bool result = await _versionUpdateHandle.CheckUpdate();//这里会打点资源检测更新事件
2024-02-22 14:53:23 +08:00
if (!result)
{
Debug.LogError("更新失败!,无法进入游戏");
return;
}
2024-07-16 14:00:58 +08:00
2023-10-19 15:00:19 +08:00
if (enableHotUpdate)
2024-02-22 12:41:18 +08:00
{
2024-07-16 14:00:58 +08:00
#if USE_HCLR
2024-02-22 12:41:18 +08:00
Debug.Log("需要热更新,开始加载程序集");
_hybridCLRHelper = new HybridCLRHelper();
_hybridCLRHelper.SetDllList(AOTGenericReferences.PatchedAOTAssemblyList);
2024-02-22 14:53:23 +08:00
_hybridCLRHelper.RegistLoader(_simpleLoader);
2024-07-16 14:19:27 +08:00
2024-02-22 13:24:26 +08:00
await _hybridCLRHelper.LoadAssemblies();
2024-02-22 12:41:18 +08:00
Debug.Log("加载程序集结束!");
2024-07-16 14:00:58 +08:00
#endif
2024-02-22 12:41:18 +08:00
}
2024-08-27 18:25:16 +08:00
await _simpleLoader.WarmUpShader();
2024-07-16 14:00:58 +08:00
2024-02-22 13:24:26 +08:00
await EnterGame();
2023-10-19 15:00:19 +08:00
}
2024-02-22 14:53:23 +08:00
private async UniTask EnterGame()
2023-10-19 15:00:19 +08:00
{
2024-02-22 14:53:23 +08:00
Debug.Log("开始加载游戏场景");
2024-08-01 15:46:36 +08:00
SimulationProgress(versionUpdateUI.GetCurProgress());
2024-07-24 12:43:10 +08:00
try
2024-07-16 14:00:58 +08:00
{
2024-07-24 12:43:10 +08:00
Scene scene = await _simpleLoader.LoadSceneAsync(START_SCENE_NAME, LoadSceneMode.Additive);
foreach (GameObject go in scene.GetRootGameObjects())
2024-07-16 14:00:58 +08:00
{
2024-07-24 12:43:10 +08:00
if ("EventSystem".Equals(go.name))
{
Destroy(go);
break;
}
2024-07-16 14:00:58 +08:00
}
}
2024-07-24 12:43:10 +08:00
catch (Exception e)
{
Debug.LogError(e);
}
finally
{
await LoadOver();
Debug.Log("加载游戏场景结束");
}
2023-10-19 15:00:19 +08:00
}
2024-07-30 18:51:27 +08:00
private async void SimulationProgress(float startProgress)
2024-07-16 14:00:58 +08:00
{
2024-08-01 15:46:36 +08:00
versionUpdateUI.UpdateProgressText("初始化游戏...");
versionUpdateUI.UpdateProgress(startProgress);
2024-02-22 12:41:18 +08:00
2024-07-30 18:51:27 +08:00
float step = (1f - startProgress) * 0.02f;
2024-08-01 15:46:36 +08:00
while (startProgress + step <= 0.99f)
2024-07-30 18:51:27 +08:00
{
await UniTask.Delay(20);
startProgress += step;
2024-08-01 15:46:36 +08:00
versionUpdateUI.UpdateProgress(startProgress);
2024-07-30 18:51:27 +08:00
}
2024-07-16 14:00:58 +08:00
}
private async UniTask LoadOver()
{
while (true)
{
await UniTask.Yield();
2024-08-08 13:00:58 +08:00
// Debug.Log($"校验是否加载完:{IVersionUpdateUI.IsLoadOver}");
2024-07-16 14:19:27 +08:00
if (IVersionUpdateUI.IsLoadOver)
2024-07-16 14:00:58 +08:00
{
2024-08-01 15:46:36 +08:00
versionUpdateUI.UpdateProgress(1f);
versionUpdateUI.UpdateProgressText("完成");
2024-07-16 14:00:58 +08:00
await UniTask.Delay(200);
2024-08-01 15:46:36 +08:00
versionUpdateUI.CloseAll();
2024-07-16 14:00:58 +08:00
return;
2024-07-16 14:19:27 +08:00
}
2024-07-16 14:00:58 +08:00
}
}
2024-12-13 15:15:19 +08:00
DownloadTest downloadTest;
/// <summary>
/// 下载测试
/// </summary>
public void DownloadTest()
{
if (null == downloadTest)
downloadTest = new();
if (downloadTest.IsBeginDownload)
return;
downloadTest.BeginDownload();
}
2023-11-08 19:04:23 +08:00
}