AkiraPixelWind/Assets/Scripts/Main/Procedure/ProcedureUpdateVersion.cs

201 lines
6.7 KiB
C#
Raw Normal View History

2022-12-29 18:20:40 +08:00
using Axibug;
using Axibug.Event;
using Axibug.Procedure;
using Axibug.Resources;
using Axibug.Runtime;
using System.IO;
using UnityEngine;
using static Axibug.Utility;
using ProcedureOwner = Axibug.Fsm.IFsm<Axibug.Procedure.IProcedureManager>;
namespace Game
{
public class ProcedureUpdateVersion : ProcedureBase
{
private int _downloadCount = 0; //<2F><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>
private int _needDownloadFileCount; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
private int _downloadFileCount; //<2F>Ѵ<EFBFBD><D1B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
NativeUpdateUI _updateUI;
float delayTime = 0;
private void downloadFiles()
{
_needDownloadFileCount = HotData.curHot.diff.Count;
Log.Debug($"<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>{_needDownloadFileCount}");
_downloadFileCount = 0;
for (int i = 0; i < HotData.curHot.diff.Count; i++)
{
var fd = HotData.curHot.diff[i];
string path = Utility.Path.GetRemoteFilePath(HotData.curHot.version_remote, fd.filename);
Log.Debug($"<22><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>{path}");
AppEntry.WebRequest.AddWebRequest(path, fd);
}
_downloadCount++;
}
protected override void OnEnter(ProcedureOwner procedureOwner)
{
base.OnEnter(procedureOwner);
Log.Debug($"<22><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>...<2E><><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD>·<EFBFBD><C2B7>Ϊ<EFBFBD><CEAA>{Application.persistentDataPath}");
_updateUI = AppEntry.OpenNativeUI<NativeUpdateUI>("NativeUpdateUI");
if (_updateUI == null)
throw new GameException("<22><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>update<74><65><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>");
_downloadCount = 0;
AppEntry.Event.Subscribe(WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess);
AppEntry.Event.Subscribe(WebRequestFailureEventArgs.EventId, OnWebRequestFailure);
downloadFiles();
}
private void OnWebRequestSuccess(object sender, LogicEventArgs e)
{
WebRequestSuccessEventArgs ne = (WebRequestSuccessEventArgs)e;
FileData fd = (FileData)ne.UserData;
Log.Debug("<22><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ɹ<EFBFBD><C9B9><EFBFBD>" + fd.toString());
byte[] bytes = ne.GetWebResponseBytes();
string fileString = Converter.GetString(bytes);
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>С
HotData.curHot.curBytes += bytes.Length;
string mes = $"<22><><EFBFBD>ڸ<EFBFBD><DAB8>£<EFBFBD>{AppEntry.HotUpdate.GetByteLengthString(HotData.curHot.curBytes)} / {AppEntry.HotUpdate.GetByteLengthString(HotData.curHot.totalBytes)} " +
$"<22>ļ<EFBFBD>: {_downloadFileCount + 1} / {_needDownloadFileCount}";
float v = (float)HotData.curHot.curBytes / HotData.curHot.totalBytes;
//<2F><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
if (_updateUI != null)
_updateUI.RefreshPregress(mes, v);
//д<><D0B4><EFBFBD>ȸ<EFBFBD><C8B8>ļ<EFBFBD>
WriteUpdateFile(fd.filename, bytes);
//<2F>Ƴ<EFBFBD><C6B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
HotData.curHot.diff.Remove((FileData)ne.UserData);
_downloadFileCount++;
}
private void OnWebRequestFailure(object sender, LogicEventArgs e)
{
Log.Debug("failed");
_downloadFileCount++;
}
protected override void OnUpdate(ProcedureOwner procedureOwner, float elapseSeconds, float realElapseSeconds)
{
base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
//һ<><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (_needDownloadFileCount == _downloadFileCount)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>û<EFBFBD><C3BB><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD>
if (HotData.curHot.diff.Count > 0)
{
//<2F><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>5<EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (_downloadCount >= 5)
{
//HotfixEntry.UI.ShowMessageBox("ϵͳ<CFB5><CDB3>ʾ", "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>", "<22><><EFBFBD><EFBFBD>", "<22>˳<EFBFBD>",
AppEntry.ShowMessageBox("ϵͳ<CFB5><CDB3>ʾ", "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><ECB3A3><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>", "<22><><EFBFBD><EFBFBD>", "<22>˳<EFBFBD>",
() =>
{
_downloadCount = 0;
downloadFiles();
},
() => { AppEntry.QuitGame(); });
}
else
{
//û<><C3BB><EFBFBD><EFBFBD>5<EFBFBD>Σ<EFBFBD><CEA3>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
downloadFiles();
}
return;
}
if (_updateUI != null)
_updateUI.RefreshPregress("<22>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD>ڽ<EFBFBD>ѹ...", 1);
delayTime += elapseSeconds;
if (delayTime < 1)
return;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ
WriteVersion();
ChangeState<ProcedureInitSystem>(procedureOwner);
}
}
protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown)
{
AppEntry.Event.Unsubscribe(WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess);
AppEntry.Event.Unsubscribe(WebRequestFailureEventArgs.EventId, OnWebRequestFailure);
//ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD>
//HotfixEntry.UI.CloseUI<NativeUpdateUI>();
AppEntry.CloseNativeUI("NativeUpdateUI");
_updateUI = null;
base.OnLeave(procedureOwner, isShutdown);
}
void WriteUpdateFile(string file, byte[] bytes)
{
string path = string.Empty;
//<2F>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
string[] tmp = file.Split('/');
string tmpDir = string.Empty;
//<2F><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < tmp.Length - 1; i++)
{
tmpDir += tmp[i];
path = Utility.Path.GetUpdateWriteFilePath(tmpDir);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
tmpDir += '/';
}
path = Utility.Path.GetUpdateWriteFilePath(file);
Stream stream = File.Create(path, bytes.Length);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
//Log.Debug($"д<><D0B4><EFBFBD>ļ<EFBFBD><C4BC>ɹ<EFBFBD><C9B9><EFBFBD> path={path}");
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɺ<EFBFBD><C9BA><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>ļ<EFBFBD>
void WriteVersion()
{
string path = Utility.Path.GetLocalVersionFilePath();
if (File.Exists(path))
File.Delete(path);
try
{
Stream stream = File.Create(path, HotData.curHot.bytes.Length);
stream.Write(HotData.curHot.bytes, 0, HotData.curHot.bytes.Length);
stream.Close();
stream.Dispose();
Log.Debug($"д<><D0B4><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD> path={path}");
}
catch (System.Exception ex)
{
Log.Error(ex.Message);
}
}
}
}