AkiraPixelWind/Assets/Scripts/Main/Procedure/ProcedureCheckVersion.cs
2022-12-29 18:20:40 +08:00

254 lines
8.6 KiB
C#
Raw Permalink 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 Axibug;
using Axibug.Event;
using Axibug.Resources;
using Axibug.Runtime;
using System;
using System.IO;
using UnityEngine;
using static Axibug.Utility;
using ProcedureOwner = Axibug.Fsm.IFsm<Axibug.Procedure.IProcedureManager>;
namespace Game
{
//版本检测
public class ProcedureCheckVersion : ProcedureBase
{
private bool _checkVersionComplete = false; //版本检测是否完成
private bool _needUpdateVersion = false; //是否需要更新
private int _fileCount = 0; //请求文件个数本脚本为2个本地和服务器version.txt
NativeUpdateUI _updateUI;
float delayTime = 0;
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
_checkVersionComplete = false;
_needUpdateVersion = false;
_fileCount = 0;
_updateUI = AppEntry.OpenNativeUI<NativeUpdateUI>("NativeUpdateUI");
_updateUI.Show("检查游戏版本...");
AppEntry.Event.Subscribe(WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess);
AppEntry.Event.Subscribe(WebRequestFailureEventArgs.EventId, OnWebRequestFailure);
//初始化数据 和 基础版本号
HotData d = new HotData();
d.Init();
d.filename = HotPlatform.versionTxt;
d.version_local = UnityEngine.Application.version;
HotData.JsonList.Add(HotPlatform.versionTxt, d);
HotData.curHot = d;//当前正在热更的数据
HotData.xml.Init();
RequestRemoteVersion();
RequestLocalVersion();
}
// 向服务器请求版本信息
private void RequestRemoteVersion()
{
string url = Utility.Path.GetRemoteVersionFilePath();
Log.Debug($"remote url = {url}");
AppEntry.WebRequest.AddWebRequest(url, true); //true代表远程文件
}
private void RequestLocalVersion()
{
string path = Utility.Path.GetLocalVersionFilePath();
//Log.Debug($"local url = {path}");
//如果没有本地文件直接加1
if (!File.Exists(path))
{
_fileCount++;
Log.Debug("本地文件不存在...");
return;
}
path = Text.Format("{0}{1}", "file:///", path);
//Log.Debug($"AddWebRequest local url = {path}");
AppEntry.WebRequest.AddWebRequest(path, false); //false代表本地文件
}
private void LoadLocalFile(FileList arr)
{
Log.Debug("本地文件下载完成");
foreach (var d in arr.listinfo)
{
if (d.filename == HotPlatform.versionTxt)
{
HotData.curHot.version_local = d.hash;
continue;
}
HotData.curHot.local.Add(d.filename, d);
}
HotData.curHot.load_ab_fail = 0;
}
private void LoadRemoteFile(FileList arr)
{
Log.Debug("远程文件下载完成");
foreach (var d in arr.listinfo)
{
//0 读取送检版本
if (d.filename == HotPlatform.versionCheck)
{
HotData.xml.versionCheck = d.hash;
continue;
}
else if (d.filename == HotPlatform.versionTxt)
{
HotData.curHot.version_remote = d.hash;
HotData.curHot.totalBytes = d.length;
continue;
}
else if (d.filename == HotPlatform.loginServer)
{
string s = HotPlatform.ToDecrypt("1981", d.hash);
string[] arr2 = s.Split(',');
int size = arr2.Length;
if (size < 4)
continue;
for (int i = 0; i < size; i++)
{
if (i == 0)
HotData.xml.xml_head = arr2[0];
else if (i == 1)
Int32.TryParse(arr2[1], out HotData.xml.xml_code);
else if (i == 2)
Int32.TryParse(arr2[2], out HotData.xml.xml_version);
else if (i == 3)
HotData.xml.xml_testcode = arr2[3];
else
{
string[] arr3 = arr2[i].Split(':');
if (arr3.Length == 2)
{
LoginServerData login = new LoginServerData();
login.ip = arr3[0];
Int32.TryParse(arr3[1], out login.port);
HotData.xml.loginserver.Add(login);
}
}
}
continue;
}
HotData.curHot.remote.Add(d.filename, d);
}
}
private void OnWebRequestSuccess(object sender, LogicEventArgs e)
{
WebRequestSuccessEventArgs ne = (WebRequestSuccessEventArgs)e;
byte[] versionInfoBytes = ne.GetWebResponseBytes();
string versionInfoString = Converter.GetString(versionInfoBytes);
FileList arr = Json.ToObject<FileList>(versionInfoString);
_fileCount++;
try
{
if ((bool)ne.UserData == true)
{
LoadRemoteFile(arr);
HotData.curHot.bytes = versionInfoBytes;
HotData.curHot.load_ab_fail = 0;
}
else
{
LoadLocalFile(arr);
}
}
catch (Exception ex)
{
Log.Error($"err:{ex.Message}", LogLevel.Error);
return;
}
if (_fileCount != 2)
return;
//读取本地文件
_needUpdateVersion = AppEntry.HotUpdate.CheckVersionList();
_checkVersionComplete = true;
}
private void OnWebRequestFailure(object sender, LogicEventArgs e)
{
WebRequestFailureEventArgs ne = (WebRequestFailureEventArgs)e;
//网络错误,提示是否重新下载
if ((bool)ne.UserData == true)
{
//HotfixEntry.UI.ShowMessageBox("系统提示", "服务器版本文件下载失败,是否重试", "重试", "退出",
AppEntry.ShowMessageBox("系统提示", "服务器版本文件下载失败,是否重试", "重试", "退出",
() => { RequestRemoteVersion(); } ,
() => { AppEntry.QuitGame(); });
}
else
{
//HotfixEntry.UI.ShowMessageBox("系统提示", "本地版本文件打开失败,是否重试", "重试", "退出",
AppEntry.ShowMessageBox("系统提示", "本地版本文件打开失败,是否重试", "重试", "退出",
() => { RequestLocalVersion(); },
() => { AppEntry.QuitGame(); });
}
}
protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
{
base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
_updateUI.RefreshPregress("正在比对文件...", AppEntry.HotUpdate.ProcessValue);
if (!_checkVersionComplete)
return;
if (_needUpdateVersion)
{
if (AppEntry.confirmUIGo != null)
return;
AppEntry.ShowMessageBox("更新提示", $"最新版本有{AppEntry.HotUpdate.GetByteLengthString(HotData.curHot.totalBytes)}资源需要更新(建议使用Wifi网络).\n是否开始下载?", "开始下载", "暂不下载",
() =>
{
ChangeState<ProcedureUpdateVersion>(procedureOwner);
},
() => { AppEntry.QuitGame(); });
}
else
{
delayTime += elapseSeconds;
if (delayTime < 1)
return;
ChangeState<ProcedureInitSystem>(procedureOwner);
}
}
protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
{
AppEntry.Event.Unsubscribe(WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess);
AppEntry.Event.Unsubscribe(WebRequestFailureEventArgs.EventId, OnWebRequestFailure);
AppEntry.CloseNativeUI("NativeUpdateUI");
_updateUI = null;
base.OnLeave(procedureOwner, isShutdown);
}
}
}