FM/Assets/Scripts/Base/MonoBehaviour/PatchWindow.cs

260 lines
11 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using ZGame;
namespace HK
{
public class PatchWindow : MonoBehaviour
{
[Header("下载")] public Slider slider;
public TMP_Text txt_Slider;
private void Awake()
{
EventManager.Instance.Subscribe(InitializeFailedEventArgs.EventId, InitializeFailedEvent);
EventManager.Instance.Subscribe(PatchStatesChangeEventArgs.EventId, PatchStatesChangeEvent);
EventManager.Instance.Subscribe(FoundUpdateFilesEventArgs.EventId, FoundUpdateFilesEvent);
EventManager.Instance.Subscribe(DownloadProgressUpdateEventArgs.EventId, DownloadProgressUpdateEvent);
EventManager.Instance.Subscribe(PackageVersionUpdateFailedEventArgs.EventId,
PackageVersionUpdateFailedEvent);
EventManager.Instance.Subscribe(PatchManifestUpdateFailedEventArgs.EventId, PatchManifestUpdateFailedEvent);
EventManager.Instance.Subscribe(WebFileDownloadFailedEventArgs.EventId, WebFileDownloadFailedEvent);
EventManager.Instance.Subscribe(UpdaterDoneEventArgs.EventId, UpdaterDoneEvent);
this.btn_Yes.onClick.AddListener(ClickBtnYes);
this.btn_No.onClick.AddListener(ClickBtnNo);
this.btnSingle.onClick.AddListener(this.ClickBtnSingle);
this.btnQuit.onClick.AddListener(this.ClickBtnQuit);
}
private void OnDestroy()
{
EventManager.Instance.Unsubscribe(InitializeFailedEventArgs.EventId, InitializeFailedEvent);
EventManager.Instance.Unsubscribe(PatchStatesChangeEventArgs.EventId, PatchStatesChangeEvent);
EventManager.Instance.Unsubscribe(FoundUpdateFilesEventArgs.EventId, FoundUpdateFilesEvent);
EventManager.Instance.Unsubscribe(DownloadProgressUpdateEventArgs.EventId, DownloadProgressUpdateEvent);
EventManager.Instance.Unsubscribe(PackageVersionUpdateFailedEventArgs.EventId,
PackageVersionUpdateFailedEvent);
EventManager.Instance.Unsubscribe(PatchManifestUpdateFailedEventArgs.EventId,
PatchManifestUpdateFailedEvent);
EventManager.Instance.Unsubscribe(WebFileDownloadFailedEventArgs.EventId, WebFileDownloadFailedEvent);
EventManager.Instance.Unsubscribe(UpdaterDoneEventArgs.EventId, UpdaterDoneEvent);
this.btn_Yes.onClick.RemoveListener(ClickBtnYes);
this.btn_No.onClick.RemoveListener(ClickBtnNo);
this.btnSingle.onClick.RemoveListener(this.ClickBtnSingle);
this.btnQuit.onClick.RemoveListener(this.ClickBtnQuit);
}
[Header("下载提示")] public GameObject downloadTips;
public TMP_Text txtDownloadTips;
public Button btn_Yes;
public Button btn_No;
private void ShowDownloadTips(int count, string size)
{
this.downloadTips.SetActive(true);
txtDownloadTips.text = $"Found update patch files, Total count {count} Total szie {size}MB";
}
[Header("提示")] public GameObject tips;
public TMP_Text txt_Tips;
IEnumerator ShowTips(string str)
{
this.tips.SetActive(true);
this.txt_Tips.text = str;
yield return new WaitForSeconds(1);
this.tips.SetActive(false);
}
private void ClickBtnNo()
{
Debug.Log("取消更新就退出,或者进入单机模型");
this.downloadTips.SetActive(false);
Application.Quit();
}
private void ClickBtnYes()
{
EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId,
new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.));
this.downloadTips.SetActive(false);
}
[Header("错误提示")] public GameObject errorTips;
public TMP_Text txtErrorTips;
public Button btnSingle;
public Button btnQuit;
void SetTipsContent(string str)
{
this.txtErrorTips.text = str;
this.errorTips.SetActive(true);
}
private void ClickBtnSingle()
{
this.errorTips.SetActive(false);
EventManager.Instance.FireNow(EnterSingleModeEventArgs.EventId, new EnterSingleModeEventArgs());
}
private void ClickBtnQuit()
{
Debug.Log("取消更新就退出,或者进入单机模型");
this.errorTips.SetActive(false);
Application.Quit();
}
#region Event
private int count;
private void InitializeFailedEvent(object sender, GameEventArgs e)
{
var args = e as InitializeFailedEventArgs;
string str = "初始化失败";
StartCoroutine(ShowTips(str));
EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId,
new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.));
}
private void PatchStatesChangeEvent(object sender, GameEventArgs e)
{
var args = e as PatchStatesChangeEventArgs;
string str = $"补丁流程步骤改变: {args.Tips}";
StartCoroutine(ShowTips(str));
}
private void FoundUpdateFilesEvent(object sender, GameEventArgs e)
{
var args = e as FoundUpdateFilesEventArgs;
string str = $"发现更新文件: {args.TotalCount}, {args.TotalSizeBytes}";
StartCoroutine(ShowTips(str));
float sizeMB = args.TotalSizeBytes / 1048576f;
sizeMB = Mathf.Clamp(sizeMB, 0.1f, float.MaxValue);
string totalSizeMB = sizeMB.ToString("f1");
// 弹出对话框,让用户选择是否更新。
Debug.Log($"Found update patch files, Total count {args.TotalCount} Total szie {totalSizeMB}MB");
ShowDownloadTips(args.TotalCount, totalSizeMB);
}
private void DownloadProgressUpdateEvent(object sender, GameEventArgs e)
{
var args = e as DownloadProgressUpdateEventArgs;
slider.value = (float)args.CurrentDownloadCount / args.TotalDownloadCount;
string currentSizeMB = (args.CurrentDownloadSizeBytes / 1048576f).ToString("f1");
string totalSizeMB = (args.TotalDownloadSizeBytes / 1048576f).ToString("f1");
this.txt_Slider.text =
$"下载中:{args.CurrentDownloadCount}/{args.TotalDownloadCount} {currentSizeMB}MB/{totalSizeMB}MB";
}
private void PackageVersionUpdateFailedEvent(object sender, GameEventArgs e)
{
var args = e as PackageVersionUpdateFailedEventArgs;
Debug.Log($"Failed to update static version, please check the network status.");
this.count++;
if (this.count <= 3)
{
string str = $"Failed to update static version, please check the network status.";
StartCoroutine(ShowTips(str));
// 用户尝试再次更新静态版本,此步回调资源更新中的更新版本步骤
EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId,
new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.));
}
else
{
string str = "获取最新版本失败,请检查服务器是否未开启!";
SetTipsContent(str);
}
}
private void PatchManifestUpdateFailedEvent(object sender, GameEventArgs e)
{
var args = e as PatchManifestUpdateFailedEventArgs;
Debug.Log("Failed to update patch manifest, please check the network status.");
string str = $"Failed to update patch manifest, please check the network status.";
StartCoroutine(ShowTips(str));
// 用户尝试再次更新补丁清单
EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId,
new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.));
}
private void WebFileDownloadFailedEvent(object sender, GameEventArgs e)
{
var args = e as WebFileDownloadFailedEventArgs;
Debug.Log($"Failed to download file : {args.FileName}");
string str = $"Failed to download file : {args.FileName}";
StartCoroutine(ShowTips(str));
// 用户尝试再次下载网络文件
EventManager.Instance.FireNow(UpdatePackageCallbackEventArgs.EventId,
new UpdatePackageCallbackEventArgs(UpdatePackageCallbackType.));
}
private void UpdaterDoneEvent(object sender, GameEventArgs e)
{
var args = e as UpdaterDoneEventArgs;
Debug.Log("资源更新完成");
string str = $"资源更新完成";
slider.value = 1;
StartCoroutine(ShowTips(str));
GlobalInitFinishEvent(this, null);
}
private void GlobalInitFinishEvent(object sender, GameEventArgs e)
{
Destroy(this.gameObject);
}
#endregion
// [Header("提示")]
// public GameObject tipsGo;
// public TMP_Text txtTipsTitle;
// public TMP_Text txtTipsContent;
// public TMP_Text txtTipsOk;
// public TMP_Text txtTipsCancel;
// public Button btnTipsOk;
// public Button btnTipsCancel;
//
// void OpenTips(string title, string content, string ok = "OK", string cancel = "Cancel",
// Action okCallback = null, Action cancelCallback = null)
// {
// tipsGo.SetActive(true);
// txtTipsTitle.text = title;
// txtTipsContent.text = content;
// txtTipsOk.text = ok;
// txtTipsCancel.text = cancel;
// btnTipsOk.onClick.RemoveAllListeners();
// btnTipsCancel.onClick.RemoveAllListeners();
// btnTipsOk.onClick.AddListener(() => { okCallback?.Invoke(); });
// btnTipsCancel.onClick.AddListener(() => { cancelCallback?.Invoke(); });
// }
//
// async UniTask OpenTips(string title, string content, float maxTime)
// {
// tipsGo.SetActive(true);
// txtTipsTitle.text = title;
// txtTipsContent.text = content;
// if (btnTipsOk.gameObject.activeSelf)
// btnTipsOk.gameObject.SetActive(false);
// if (btnTipsCancel.gameObject.activeSelf)
// btnTipsCancel.gameObject.SetActive(false);
// float time = 0;
// while (time < maxTime)
// {
// await UniTask.Yield();
// time += Time.deltaTime;
// }
// tipsGo.SetActive(false);
// }
}
}