Files
Main/Assets/Launcher/UpdateFormDll/UIForm/UIUpdateForm.cs
2025-01-25 04:38:09 +08:00

533 lines
20 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Collections;
using Thousandto.Cfg.UpdateData;
using Thousandto.CoreSDK;
using Thousandto.Launcher.Form;
using Thousandto.Update.Enum;
using Thousandto.Update.Flow;
using Thousandto.Update.Manager;
using Thousandto.UpdateForm.Center;
using UnityEngine;
using UnityEngine.Gonbest.MagicCube;
using EventManager = UnityEngine.Gonbest.MagicCube.EventManager;
using PreLoadEventDefine = UnityEngine.Gonbest.MagicCube.PreLoadEventDefine;
namespace Thousandto.UpdateForm.Form
{
/// <summary>
/// 更新界面
/// </summary>
public class UIUpdateForm : MonoBehaviour
{
#region //UI控件
UILauncherMainForm _mainForm;
#endregion
#region //私有成员
private PlatformType _pType = PlatformType.Windows;
#endregion
#region //继承MonoBehaviour函数
void Awake()
{
FindAllComponents();
InitPath();
}
void Start()
{
RegEvents();
CheckNetWork(StartUpdate);
}
void Update()
{
UpdateManager.Instance.Update();
if (UpdateManager.Instance.Running() && _mainForm != null && _mainForm.IsInitliazed)
{
if (UpdateManager.Instance.CurrentFlow == null) return;
string url = "";
int total = 0;
int progressValue = 0;
bool useDownload = false;
UpdateManager.Instance.GetDownloadInfo(out url, out total, out progressValue, out useDownload);
_mainForm.Downloading.TotalSize = total;
_mainForm.Downloading.DownSize = progressValue;
_mainForm.UseLoadStyle = useDownload ? LoadStyleCode.Download : LoadStyleCode.Real;
}
}
void OnDestroy()
{
UnRegEvents();
Resources.UnloadUnusedAssets();
UnityEngine.Debug.Log("OnDestroy : UIUpdateForm");
}
#endregion
#region //事件注册
private void RegEvents()
{
EventManager.SharedInstance.RegFixEventHandle(PreLoadEventDefine.EID_STEP_START, OnStepStart);
}
private void UnRegEvents()
{
EventManager.SharedInstance.UnRegFixEventHandle(PreLoadEventDefine.EID_STEP_START, OnStepStart);
}
private void OnStepStart(object dat, object sender)
{
_mainForm.FakeLoading.SetStep((int)dat);
}
#endregion
#region //私有功能函数
//初始化路径信息
private void InitPath()
{
_pType = PlatformType.Windows;
switch (LauncherUpdate.RumtimePlatform)
{
case MyRuntimePlatform.AndroidEditor:
case MyRuntimePlatform.IOSEditor:
case MyRuntimePlatform.StandaloneEditor:
Thousandto.Update.Trans.TransAndroidResource.ForTest = true;
break;
case MyRuntimePlatform.Android:
_pType = PlatformType.Android;
break;
case MyRuntimePlatform.IOS:
_pType = PlatformType.IOS;
break;
case MyRuntimePlatform.Standalone:
_pType = PlatformType.Windows;
break;
default:
#if UNITY_EDITOR
//正式接入的时候要去掉这个
Thousandto.Update.Trans.TransAndroidResource.ForTest = true;
#elif UNITY_ANDROID
_pType = PlatformType.Android;
#elif UNITY_IPHONE
_pType = PlatformType.IOS;
#else
_pType = PlatformType.Windows;
#endif
break;
}
//IOS设置Document下路径不做云备份
#if UNITY_IPHONE
//ios设置nobackup
string iosStreamingPath = PathUtils.GetReleaseResRootPath();
if (!System.IO.Directory.Exists(iosStreamingPath))
{
System.IO.Directory.CreateDirectory(iosStreamingPath);
}
UnityEngine.iOS.Device.SetNoBackupFlag(iosStreamingPath);
UnityEngine.Debug.Log("---SetNoBackupFlag---设置文件路径不备份到iCloud" + iosStreamingPath);
#endif
}
//退出游戏
private void ExitGame()
{
//没初始化成功,直接退出游戏
if (!FuncellSDK.Instance.IsSDKInitFinish())
{
Application.Quit();
return;
}
if (!FuncellSDK.Instance.ExitGame())
{
Application.Quit();
}
}
//判断网络
private void CheckNetWork(EventDelegate.Callback callback)
{
//判断网络状态
if (LauncherUpdate.IsNetworkEnable())
{
//if (LauncherUpdate.Is4G())
//{
// UIMsgBoxForm.Open(UpdateCfgData.C_UIUpdateForm_text_167_0, UpdateCfgData.C_UIUpdateForm_text_167_1, UpdateCfgData.C_UIUpdateForm_text_167_2, callback, ExitGame);
//}
//else
{
callback();
}
}
else
{
_mainForm.ShowMessage(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_176_0),
UpdateCfgData.C_UIUpdateForm_text_176_1, UpdateCfgData.C_UIUpdateForm_text_474_0,
x =>
{
if (x == 2)
{
callback();
}
else
{
ExitGame();
}
});
}
}
/// <summary>
/// 查找UI上各个控件
/// </summary>
private void FindAllComponents()
{
_mainForm = gameObject.GetComponent<UILauncherMainForm>();
if (_mainForm == null)
_mainForm = gameObject.AddComponent<UILauncherMainForm>();
_mainForm.Initialize();
_mainForm.HideAll();
_mainForm.ShowLauncher();
_mainForm.FakeLoading.StepNames = UpdateStringUtils.GetEnterGameStepNames();
_mainForm.FakeLoading.SetMaxStep(6);
_mainForm.FakeLoading.Start(true);
_mainForm.Downloading.OnRunningHandler = UpdateManager.Instance.Running;
}
#region //更新流程的回调处理
/// <summary>
/// 每个流程开始时调用
/// </summary>
/// <param name="obj"></param>
private void onFlowBeginCallback(object obj)
{
if (UpdateManager.Instance.CurrentFlow == null)
{
_mainForm.UseLoadStyle = LoadStyleCode.Fake;
return;
}
else
{
string appVers = "";
string resVers = "";
UpdateManager.Instance.GetVersionInfo(out appVers, out resVers);
_mainForm.VerPanel.SetData(appVers, resVers);
string url = "";
int total = 0;
int progressValue = 0;
bool useDownload = false;
UpdateManager.Instance.GetDownloadInfo(out url, out total, out progressValue, out useDownload);
_mainForm.Downloading.DownSize = progressValue;
_mainForm.Downloading.TotalSize = total;
_mainForm.Downloading.StepName = UpdateStringUtils.GetFlowName(UpdateManager.Instance.GetCurFlowType());
}
}
/// <summary>
/// 更新结束
/// </summary>
/// <param name="result">true:成功false失败</param>
/// <param name="ret">返回码</param>
private void FinishCallback(bool result, int ret)
{
string msg = UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_451_0) + " result=" +
result + " ret = " + ret;
Debug.Log(msg);
//再调一次收尾,最后显示的进度
string url = "";
int total = 0;
int progressValue = 0;
bool useDownload = false;
if (UpdateManager.Instance.CurrentFlow != null)
{
UpdateManager.Instance.GetDownloadInfo(out url, out total, out progressValue, out useDownload);
_mainForm.Downloading.DownSize = progressValue;
_mainForm.Downloading.TotalSize = total;
_mainForm.Downloading.StepName = UpdateStringUtils.GetFlowName(UpdateManager.Instance.GetCurFlowType());
_mainForm.Downloading.Refresh();
}
if (result)
{
if (ret == CodeDefine.RET_SKIP_BY_DOWNLOAD_APP)
{
return;
}
_mainForm.UseLoadStyle = LoadStyleCode.Fake;
//初始化进度
_mainForm.FakeLoading.Start(true);
EventManager.SharedInstance.PushFixEvent(PreLoadEventDefine.EID_STEP_START, 0);
////////////不判断sdk是否初始化完了状态并不稳定/////////////
#if !UNITY_EDITOR
if (FuncellSDK.Instance.IsSDKInitFinish())
#endif
{
StartCoroutine(AssemblyLoader.EnterGame());
}
#if !UNITY_EDITOR
else
{
//在这里等待sdk初始化完成
StartCoroutine(WaitSDKStatus(true));
}
#endif
}
else
{
//更新失败的话弹出错误
msg = string.Format(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_469_0),
UILauncherLangScript.ConvertLanText(UpdateStringUtils.GetErrorMsg(ret)));
if (!LauncherUpdate.IsNetworkEnable())
{
msg = UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_472_0);
}
//这里第二次检测网络有问题了,直接退出游戏
_mainForm.ShowMessage(msg, UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_474_1),
null,
x =>
{
Application.Quit();
//CheckNetWork(UpdateManager.Instance.Restart);
});
}
}
//等待sdk初始化如果到这里都没有初始化成功则判定失败不能进游戏
IEnumerator WaitSDKStatus(bool needReInitSDK)
{
yield return null;
float timer = 0;
while (!CoreSDK.FuncellSDK.Instance.IsSDKInitFinish())
{
yield return null;
if (needReInitSDK)
{
CoreSDK.FuncellSDK.Instance.ReInitSDK();
needReInitSDK = false;
UnityEngine.Debug.Log("Do Reinit SDK !!!");
}
timer += Time.deltaTime;
if (timer >= 30)
{
_mainForm.FakeLoading.Stop();
_mainForm.ShowMessage(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_Net_Error),
UpdateCfgData.C_UIUpdateForm_text_474_1,
UpdateCfgData.C_UIUpdateForm_Wait_More,
x =>
{
if (x == 2)
{
_mainForm.FakeLoading.Start(false);
StartCoroutine(WaitSDKStatus(false));
}
else
{
Application.Quit();
}
});
yield break;
}
}
yield return new WaitForSeconds(1);
yield return AssemblyLoader.EnterGame();
}
//转移成功后清理一下资源
private void OnTransResourceFinish(bool success)
{
Caching.ClearCache();
}
/// <summary>
/// 客户端下载完成
/// Android直接安装
/// IOS跳转到url
/// PC不处理
/// </summary>
/// <param name="success"></param>
private void OnClientDownloadFinish(bool success)
{
return;
//string msg = UpdateCfgData.C_UIUpdateForm_text_609_0 + " success=" + success;
if (UpdateManager.Instance.GetPlatformType() == PlatformType.IOS)
{
string clientUrl = UpdateManager.Instance.GetClientUrl();
Debug.Log("ios平台跳转到url: " + clientUrl);
//Application.OpenURL(clientUrl);
CoreSDK.FuncellSDK.Instance.InstallApp(clientUrl);
}
else if (UpdateManager.Instance.GetPlatformType() == PlatformType.Android)
{
//CoreSDK.FuncellSDK.Instance.InstallApp(UpdateManager.Instance.GetApkPath());
CoreSDK.FuncellSDK.Instance.InstallApp(UpdateManager.Instance.GetClientUrl());
}
}
private int OnDownClientFunc(string url, string storeDir)
{
Application.OpenURL(UpdateManager.Instance.GetClientUrl());
//CoreSDK.FuncellSDK.Instance.InstallApp(UpdateManager.Instance.GetClientUrl());
return CodeDefine.RET_SKIP_BY_DOWNLOAD_APP;
}
/// <summary>
/// 下载前的提示比如弹出下载提示框如果不需要直接Resume即可
/// </summary>
/// <param name="size"></param>
private void OnDownloadNotice(int size)
{
string msg = null;
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_646_0));
sb.Append("\n");
if (size < 1024 * 1024)
{
msg = string.Format(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_646_KB),
size / 1024 + "KB"); //KB
}
else
msg = string.Format(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_646_KB),
(int)size / 1024 / 1024 + "MB"); //MB
sb.Append(msg);
sb.Append("\n");
//需要本地存储空间,+500MB
sb.Append(string.Format(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_text_NeedSpace),
(int)((size + 512 * 1024 * 1024) / 1024 / 1024)));
sb.Append("\n");
sb.Append(UILauncherLangScript.ConvertLanText(UpdateCfgData.C_UIUpdateForm_SUGGEST_WIFI));
_mainForm.ShowMessage(sb.ToString(), UpdateCfgData.C_UIUpdateForm_text_648_1,
UpdateCfgData.C_UIUpdateForm_text_648_0,
x =>
{
if (x == 2)
{
UpdateManager.Instance.CurrentFlow.Resume(true);
}
else
{
Debug.LogError("退出游戏");
Application.Quit();
}
}
);
}
#endregion
#endregion
#region //公有功能函数
public void StartUpdate()
{
#if UNITY_WEBGL
Debug.Log("Skip Update!!");
//如果当前是WebGL则直接跳过更新
FinishCallback(true,1);
#else
Debug.Log("StartUpdate++++");
if (UpdateManager.Instance.Running())
{
Debug.Log("running!!! return");
return;
}
//IOS需要读取包内xml的地址
string inAppLocalXmlPath = PathUtils.GetAppConfigFilePath("LocalVersion.xml");
//释放目录的xml
string localXmlPath = PathUtils.GetConfigFilePath("LocalVersion.xml");
//释放目录的根目录
string storeDir = PathUtils.GetReleaseResRootPath();
//进入测试流程用到的数据
string imeiOrMacOrIdfa = MachineUUID.Value;
UnityEngine.Debug.Log("imeiOrMacOrIdfa : " + imeiOrMacOrIdfa);
//打包的时候写在代码里面的版本
string inAppClientVersion = "1.0.0.0";
if (AppManager.Instance.LocalVersionData.ContainsKey("local_app_version"))
{
inAppClientVersion = AppManager.Instance.LocalVersionData["local_app_version"];
}
//打包的时候写在代码里面的分段版本
string inAppBaseVersion = "1";
if (AppManager.Instance.LocalVersionData.ContainsKey("local_base_res_version"))
{
inAppBaseVersion = AppManager.Instance.LocalVersionData["local_base_res_version"];
}
//apk或者ipa所在路径安装路径。Unity中通过Application.streamingAssetsPath获取
string installedPath = AppManager.Instance.AppPath;
Debug.Log(string.Format("localXmlPath={0} \ninAppLocalXmlPath={1} \nstoreDir={2}", localXmlPath,
inAppLocalXmlPath, storeDir));
Debug.Log(string.Format("AppVer = {0} BaseResVer = {1}", inAppClientVersion, inAppBaseVersion));
string[] backupCdnArray = null;
int cpuCoreCount = SystemInfo.processorCount;
//因为下载对游戏运行的影响太多,所以把下载线程数调整为2个.
cpuCoreCount = 2;
//注册日志回调
UpdateManager.Instance.RegisterLog(Debug.Log, Debug.LogWarning, Debug.LogError);
UpdateManager.Instance.Initialize(localXmlPath, inAppLocalXmlPath, installedPath, storeDir, _pType, true,
cpuCoreCount);
//设置进入测试流程的条件
UpdateManager.Instance.SetImeiOrMacOrIdfa(imeiOrMacOrIdfa);
//设置资源转移的路径和版本信息
UpdateManager.Instance.SetTransData(inAppClientVersion, inAppBaseVersion, installedPath,
OnTransResourceFinish);
//设置备份的cdn主要是台湾版本要用
UpdateManager.Instance.SetBackupCdn(backupCdnArray);
//设置客户端下载完成的回调
UpdateManager.Instance.SetClientDownClientFunc(OnClientDownloadFinish, OnDownClientFunc);
//设置下载提示的回调
UpdateManager.Instance.SetDownloadNoticeFunc(OnDownloadNotice);
//设置每个流程的回调
UpdateManager.Instance.SetOnFlowBeginCallback(onFlowBeginCallback);
//设置更新流程结束的回调函数
UpdateManager.Instance.SetFinishCallback(FinishCallback);
UpdateManager.Instance.SetSceneReferenceResConfigPath("Assets/GameAssets/Resources/SceneConfig");
//开始更新流程
UpdateManager.Instance.StartUpdate();
Debug.Log("StartUpdate---");
#endif
}
#endregion
}
}