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; namespace Game { public class ProcedureUpdateVersion : ProcedureBase { private int _downloadCount = 0; //下载次数 private int _needDownloadFileCount; //本次需要处理下载的文件数量 private int _downloadFileCount; //已处理下载的文件数量 NativeUpdateUI _updateUI; float delayTime = 0; private void downloadFiles() { _needDownloadFileCount = HotData.curHot.diff.Count; Log.Debug($"本次需下载文件数量:{_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($"请求路径:{path}"); AppEntry.WebRequest.AddWebRequest(path, fd); } _downloadCount++; } protected override void OnEnter(ProcedureOwner procedureOwner) { base.OnEnter(procedureOwner); Log.Debug($"开始更新...你的更新路径为:{Application.persistentDataPath}"); _updateUI = AppEntry.OpenNativeUI("NativeUpdateUI"); if (_updateUI == null) throw new GameException("获取内置update界面失败"); _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("下载文件成功:" + fd.toString()); byte[] bytes = ne.GetWebResponseBytes(); string fileString = Converter.GetString(bytes); //加上文件大小 HotData.curHot.curBytes += bytes.Length; string mes = $"正在更新:{AppEntry.HotUpdate.GetByteLengthString(HotData.curHot.curBytes)} / {AppEntry.HotUpdate.GetByteLengthString(HotData.curHot.totalBytes)} " + $"文件: {_downloadFileCount + 1} / {_needDownloadFileCount}"; float v = (float)HotData.curHot.curBytes / HotData.curHot.totalBytes; //显示进程 if (_updateUI != null) _updateUI.RefreshPregress(mes, v); //写入热更文件 WriteUpdateFile(fd.filename, bytes); //移除差异文件 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); //一次下载完 if (_needDownloadFileCount == _downloadFileCount) { //如果还有文件没下载,重下 if (HotData.curHot.diff.Count > 0) { //下载次数超过5次,还是失败,即提醒 if (_downloadCount >= 5) { //HotfixEntry.UI.ShowMessageBox("系统提示", "网络连接异常,是否重试", "重试", "退出", AppEntry.ShowMessageBox("系统提示", "网络连接异常,是否重试", "重试", "退出", () => { _downloadCount = 0; downloadFiles(); }, () => { AppEntry.QuitGame(); }); } else { //没超过5次,自动重下 downloadFiles(); } return; } if (_updateUI != null) _updateUI.RefreshPregress("文件下载完成,正在解压...", 1); delayTime += elapseSeconds; if (delayTime < 1) return; //进入游戏 WriteVersion(); ChangeState(procedureOwner); } } protected override void OnLeave(ProcedureOwner procedureOwner, bool isShutdown) { AppEntry.Event.Unsubscribe(WebRequestSuccessEventArgs.EventId, OnWebRequestSuccess); AppEntry.Event.Unsubscribe(WebRequestFailureEventArgs.EventId, OnWebRequestFailure); //删除更新界面框 //HotfixEntry.UI.CloseUI(); AppEntry.CloseNativeUI("NativeUpdateUI"); _updateUI = null; base.OnLeave(procedureOwner, isShutdown); } void WriteUpdateFile(string file, byte[] bytes) { string path = string.Empty; //先创建文件夹 string[] tmp = file.Split('/'); string tmpDir = string.Empty; //最后文件名的不创建 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($"写入文件成功, path={path}"); } //更新完成后,写入版本文件 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($"写入版本文件, path={path}"); } catch (System.Exception ex) { Log.Error(ex.Message); } } } }