using UnityEngine; using Thousandto.Code.Center; using Thousandto.Plugins.Common; using Thousandto.Code.Global; using Thousandto.Update.Manager; using Thousandto.Core.Base; using Thousandto.Core.Asset; using Thousandto.Cfg.Data; using EventManager = UnityEngine.Gonbest.MagicCube.EventManager; using EventSystemHandler = UnityEngine.Gonbest.MagicCube.EventSystemHandler; using EventMessage = UnityEngine.Gonbest.MagicCube.EventMessage; using EventConstDefine = UnityEngine.Gonbest.MagicCube.EventConstDefine; using CoreEventDefine = UnityEngine.Gonbest.MagicCube.CoreEventDefine; using PathUtils = UnityEngine.Gonbest.MagicCube.PathUtils; using CoroutinePool = UnityEngine.Gonbest.MagicCube.CoroutinePool; using StringUtils = UnityEngine.Gonbest.MagicCube.StringUtils; namespace Thousandto.Code.Logic { /// /// 更新管理系统,对接更新模块,转接接口 /// public class UpdateSystem :BaseSystem { //是否初始化 private static bool _initialize = false; private static bool _isFirstCallBackDownload = true; public bool HideForm = false; private static int SPEED_HIGH = 0; private static int SPEED_MIDDLE = 1; private static int SPEED_LOW = 2; private int _curSpeedStatus = SPEED_HIGH; private float _lastSetSpeedTime = 0; //是否已经领取奖励 public bool IsGetedAward = false; //地图顺序 private static int[] _mapSequnce = new int[] { 13 , }; public UpdateSystem() { UpdateManager.Instance.RegisterLog(DebugUpdateLog, WarnUpdateLog, ErrorUpdateLog); } public void Initialize() { EventManager.SharedInstance.RegFixEventHandle(CoreEventDefine.EID_CORE_CHECK_FILE_PATH_REQ, GetResource); EventManager.SharedInstance.RegFixEventHandle(CoreEventDefine.EID_CORE_CHECK_FILE_IS_VALID_SYN_REQ, OnCheckFileIsValid); EventManager.SharedInstance.RegFixEventHandle((int)LogicEventDefine.EID_EVENT_UPDATE_RECORDER_STEP, OnUpdateStep); EventManager.SharedInstance.RegFixEventHandle((int)LogicEventDefine.EID_EVENT_PLAYER_BASE_ATTR_CHANGED, OnLevelChange); } public void Uninitialize() { HideForm = true; EventManager.SharedInstance.UnRegFixEventHandle(CoreEventDefine.EID_CORE_CHECK_FILE_PATH_REQ, GetResource); EventManager.SharedInstance.UnRegFixEventHandle(CoreEventDefine.EID_CORE_CHECK_FILE_IS_VALID_SYN_REQ, OnCheckFileIsValid); EventManager.SharedInstance.UnRegFixEventHandle((int)LogicEventDefine.EID_EVENT_UPDATE_RECORDER_STEP, OnUpdateStep); EventManager.SharedInstance.UnRegFixEventHandle((int)LogicEventDefine.EID_EVENT_PLAYER_BASE_ATTR_CHANGED, OnLevelChange); } public void OnUpdateStep(object obj, object sender = null) { var arr = obj as object[]; if (arr != null && arr.Length > 0) { var stepType = (Thousandto.Update.Recorder.StepType)arr[0]; if (arr.Length == 1) { Thousandto.Update.Recorder.StepRecorder.AddStep(stepType); } else if (arr.Length == 2) { var resultCode = (int)arr[1]; Thousandto.Update.Recorder.StepRecorder.AddStep(stepType, resultCode); } else if (arr.Length == 3) { var resultCode = (int)arr[1]; var resultInfo = arr[2] as string; Thousandto.Update.Recorder.StepRecorder.AddStep(stepType, resultCode, resultInfo); } } } public void SetIsGetedAward(bool isGetedAward) { IsGetedAward = isGetedAward; } //等级改变的消息 public void OnLevelChange(object obj, object sender = null) { if (Application.platform == RuntimePlatform.WebGLPlayer) {//WebGL的话就不要静默下载了 return; } else { BaseProperty pro = obj as BaseProperty; if (pro.CurrentChangeBasePropType == RoleBaseAttribute.Level) { if (!_initialize) { int lv = (int)pro.Level; //角色大于一定等级的话就开始静默下载 int startLv = int.Parse(DeclareGlobal.Get(1801).Params); if (lv >= startLv) { InitialBackDownload(); GameCenter.SceneBackLoadSystem.BackDown(); } } } } } /// /// 通过后台更新获取资源 /// /// public void GetResource(object obj,object sender=null) { FileClasses fc = obj as FileClasses; if (Application.platform == RuntimePlatform.WebGLPlayer) { string md; int size; if (UpdateManager.Instance.TryGetInAppData(fc.GameAssetsPath, out md, out size)) { OnDownloadCallback(fc.AssetsPath, 1, fc); } } else { UpdateManager.Instance.DownloadResource(fc.AssetsPath, OnDownloadCallback, fc); } } /// /// 通过后台更新获取资源 /// /// private void OnCheckFileIsValid(object obj, object sender = null) { ResFileInfo fi = obj as ResFileInfo; if (fi != null) { bool isExist = false; if (Application.platform == RuntimePlatform.WebGLPlayer) { string md; int size; if (UpdateManager.Instance.TryGetInAppData(fi.GameAssetsPath, out md, out size)) { fi.IsValid = true; fi.IsExist = true; } else { fi.IsValid = false; fi.IsExist = false; } } else { fi.IsValid = UpdateManager.Instance.IsFileValid(fi.FullAssetPath, out isExist); fi.IsExist = isExist; } } } /// /// 下载资源完成后回调,通知加载资源 /// /// /// /// private void OnDownloadCallback(string path, int result, object obj) { FileClasses fc = obj as FileClasses; fc.AssetsPath = path; fc.ErrorCode = result; if (result == Thousandto.Update.Flow.CodeDefine.RET_FAIL_FULL_DISK) EventManager.SharedInstance.PushFixEvent(Thousandto.Update.Flow.CodeDefine.RET_FAIL_FULL_DISK); EventManager.SharedInstance.PushFixEvent(CoreEventDefine.EID_CORE_CHECK_FILE_PATH_RESP, fc); } private void DebugUpdateLog(string msg) { UnityEngine.Debug.Log(msg); } private void WarnUpdateLog(string msg) { UnityEngine.Debug.LogWarning(msg); } private void ErrorUpdateLog(string msg) { UnityEngine.Debug.LogError(msg); } /// /// 下载当前场景资源 /// /// public void DownloadCurSceneRes(int mapID) { UpdateManager.Instance.BackDownloadSceneData(mapID, true); } /// /// 预下载下个场景的资源 /// /// public void DownloadNextSceneRes(int mapID) { int index = _mapSequnce.Length; for(int i = 0; i < _mapSequnce.Length; ++i) { if (_mapSequnce[i] == mapID) { index = i + 1; break; } } if(index < _mapSequnce.Length) UpdateManager.Instance.BackDownloadSceneData(_mapSequnce[index], false); } /// /// 暂停静默下载 /// public void PauseBaseResDownload() { UpdateManager.Instance.PauseBaseDownload(); } public void PauseAll() { UpdateManager.Instance.PauseAll(); } public void ResumeAll() { UpdateManager.Instance.ResumeAll(); } /// /// 恢复静默下载 /// public void ResumeBaseResDownload() { UpdateManager.Instance.ResumeBaseDownload(); } /// /// 暂停当前场景资源下载 /// public void PauseCurSceneDownload() { UpdateManager.Instance.PauseCurSceneDownload(); } /// /// 恢复当前场景资源下载 /// public void ResumeCurSceneDownload() { UpdateManager.Instance.ResumeCurSceneDownload(); } /// /// 是否暂停中 /// /// public bool IsPaused() { return UpdateManager.Instance.IsPaused(); } /// /// 是否暂停了静默更新(总资源) /// /// public bool IsBaseResPaused() { return UpdateManager.Instance.IsBaseResPaused(); } /// /// 当前场景的资源是否下载完成了 /// /// public bool IsCurSceneDownloadFinish() { return UpdateManager.Instance.IsCurSceneDownloadFinish(); } /// /// 是否静默更新下载完了 /// /// public bool IsBaseResDownloadFinish() { return UpdateManager.Instance.IsBaseResDownloadFinish(); } /// /// 是否暂停了当前场景更新 /// /// public bool IsCurScenePaused() { return UpdateManager.Instance.IsCurScenePaused(); } /// /// 获取当前正在下载的场景资源信息 /// /// /// public void GetCurSceneTotalResSize(out long total, out long downloaded) { UpdateManager.Instance.GetCurSceneTotalResSize(out total, out downloaded); } /// /// 总的静默更新资源大小 /// /// public int GetBaseResTotalSize() { int totalSize = UpdateManager.Instance.GetTotalBaseResSize(); return totalSize; } /// /// 总的已下载资源大小 /// /// public int GetTotalDownloadedSize() { return UpdateManager.Instance.GetTotalDownloadedSize(); } /// /// 下载速度 kb/s /// /// public int GetDownloadSpeed() { var speed = UpdateManager.Instance.GetDownloadSpeed(); if (_lastSetSpeedTime == 0) _lastSetSpeedTime = Time.realtimeSinceStartup; //超过5秒才做判断 if((Time.realtimeSinceStartup - _lastSetSpeedTime) > 5) { _lastSetSpeedTime = Time.realtimeSinceStartup; var ping = GameCenter.HeartSystem.NetPingValue; if(ping > 600 && _curSpeedStatus != SPEED_LOW) { if(_curSpeedStatus == SPEED_HIGH) UpdateManager.Instance.SetDownloadSpeed(speed / 4); else UpdateManager.Instance.SetDownloadSpeed(speed / 2); _curSpeedStatus = SPEED_LOW; } else if (ping > 200 && _curSpeedStatus != SPEED_MIDDLE) { if(_curSpeedStatus == SPEED_HIGH) UpdateManager.Instance.SetDownloadSpeed(speed / 2); else UpdateManager.Instance.SetDownloadSpeed(speed * 2); _curSpeedStatus = SPEED_MIDDLE; } else if(_curSpeedStatus != SPEED_HIGH) { _curSpeedStatus = SPEED_HIGH; //10Mb UpdateManager.Instance.SetDownloadSpeed(10 * 1024 * 1024); } } return speed; } /// /// 初始化后台更新 /// public void InitialBackDownload() { if (_initialize) { int total = GameCenter.UpdateSystem.GetBaseResTotalSize(); int downloaded = GameCenter.UpdateSystem.GetTotalDownloadedSize(); if (total > 0 && downloaded < total) { //这里新做了一个下载的界面在主界面,老的那个就屏蔽掉[丁华强 2020 - 6 - 9] //GameCenter.UpdateSystem.OpenUI(null); } UnityEngine.Debug.Log(string.Format("BackDownload: total={0} downloaded={1}", total, downloaded)); return; } //#if UNITY_ANDROID //wifi网络,安卓环境不弹出提示框 if (IsNetworkEnable() && !Is4G() && !IsPaused()) { //最先下载第一个场景的资源 //DownloadCurSceneRes(_mapSequnce[0]); //DownloadNextSceneRes(_mapSequnce[0]); UpdateManager.Instance.BackDownloadTotalData(); //这里新做了一个下载的界面在主界面,老的那个就屏蔽掉[丁华强 2020-6-9] //GameCenter.UpdateSystem.OpenUI(null); _initialize = true; return; } else { UnityEngine.Debug.Log("当前安卓非wifi或者暂停中"); } if(_isFirstCallBackDownload) { //#endif //暂停后台下载 PauseAll(); //将总资源放入下载队列,这时不会下载 UpdateManager.Instance.BackDownloadTotalData(); } //4G网络下,东南亚首次启动后台更新直接返回,等下次(过图)的时候再判断后台更新 if (Is4G() && GameCenter.SDKSystem.IsDongNanYaSDK() && _isFirstCallBackDownload) { _isFirstCallBackDownload = false; return; } _isFirstCallBackDownload = false; ShowBackDownloadNoticeBox(); _initialize = true; } public void ShowBackDownloadNoticeBox() { int totalSize = GetBaseResTotalSize(); if (totalSize > 0) { string msg = string.Format(DeclareMessageString.Get(DeclareMessageString.C_UPDATESYSTEM_BACKDOWNLOAD_TIPS), (float)totalSize / 1024 / 1024); if (Is4G()) { msg += DeclareMessageString.Get(DeclareMessageString.C_UPDATESYSTEM_NETSTATE_DATA); } //提示网络 GameCenter.MsgPromptSystem.ShowMsgBox(msg, DeclareMessageString.Get(DeclareMessageString.C_UPDATE_CONFIRM_DOWNLOAD), DeclareMessageString.Get(DeclareMessageString.C_UPDATE_CONTINUE), (_msgCode) => { //这里不管是否同意都需要去恢复下载资源,只是提示下玩家当前不是wifi if (_msgCode == MsgBoxResultCode.Button1) { //最先下载第一个场景的资源 //DownloadCurSceneRes(_mapSequnce[0]); //DownloadNextSceneRes(_mapSequnce[0]); ResumeAll(); //这里新做了一个下载的界面在主界面,老的那个就屏蔽掉[丁华强 2020-6-9] //打开后台更新UI //GameCenter.UpdateSystem.OpenUI(null); // notice the mainform show "download pro btn" and update download progress rate GameCenter.PushFixEvent(LogicEventDefine.EID_EVENT_DOWNLOAD_GAMERES_START); } else if(_msgCode == MsgBoxResultCode.Button2) { ResumeAll(); } else { //GameCenter.SDKSystem.ExitGame(); } }); } } /// /// 检查资源是否存在 /// /// /// /// /// public bool CheckSceneResExist(int mapID, MyAction callback = null, MyAction returnToLogin = null) { DeclareMapsetting mapInfo = DeclareMapsetting.Get(mapID); return mapInfo != null && CheckSceneResExist(mapID, mapInfo.LevelName, callback, returnToLogin); } /// /// 判断场景是否存在 /// /// /// public bool CheckSceneResExist(int mapID, string levelName, MyAction callback = null, MyAction returnToLogin = null) { bool downloading = false; bool ret = true; if (PathUtils.IsStreaming()) { string mapName = levelName; string tempPath = StringUtils.CombineString(AssetConstDefine.PathScene, mapName, AssetConstDefine.ExtUnity); string absolutPath = PathUtils.GetResourcePath(System.IO.Path.ChangeExtension(tempPath, AssetConstDefine.ExtUnity3d)); ret = UpdateManager.Instance.IsExist(absolutPath, out downloading); //不存在且不在downloading才弹出提示 if (!ret && !downloading) { //先暂停后台下载 //PauseAll(); //DownloadCurSceneRes(mapID); //DownloadNextSceneRes(mapID); long totalSize = 0; long downloaded = 0; GetCurSceneTotalResSize(out totalSize, out downloaded); long needDownloadSize = totalSize - downloaded; string msg = string.Format(DeclareMessageString.Get(DeclareMessageString.C_UPDATESYSTEM_NEXT_SCENE_DOWNLOAD_NOTICE), (float)needDownloadSize / 1024 / 1024); if (IsPaused()) { msg += DeclareMessageString.Get(DeclareMessageString.C_UPDATESYSTEM_CANCEL_PAUSE); } //可能产生的问题:如果needDownloadSize=0,则有可能是场景列表不存在或错误 //这时采用即时下载场景,没有进度提示,可能等比较久 //如果玩家是暂停下载状态,则可能出现卡死 if (needDownloadSize > 0) { //设置更新界面信息 UpdateInfo updateInfo = new UpdateInfo(); updateInfo.TotalSize = totalSize; updateInfo.Message = msg; updateInfo.OkAction = () => { ResumeAll(); if (callback != null) { callback(); } }; updateInfo.NoAction = returnToLogin; //#if UNITY_ANDROID //安卓在非暂停状态下不弹出场景下载提示 if (!IsPaused()) { updateInfo.OkAction = null; updateInfo.NoAction = null; if (callback != null) { callback(); } } //#endif //打开更新提示界面 OpenUI(updateInfo); UnityEngine.Debug.LogWarning("Scene res is not exist, wait for download: " + absolutPath); } else { if (callback != null) { callback(); } UnityEngine.Debug.LogWarning("Need download scene res is zero , please check SceneConfig file!!!"); } } } if ((ret || downloading) && callback != null) { callback(); } return ret; } /// /// 预加载资源时,需要显示进度 /// /// /// public void SetPreloadTotal(int total) { Thousandto.Update.Manager.UpdateManager.Instance.SetPreloadTotal(total); } /// /// 已经预加载好的资源个数 /// /// public void SetPreloadedCount(int count) { Thousandto.Update.Manager.UpdateManager.Instance.SetPreloadedCount(count); } /// /// UpdateManager帧刷新 /// protected override bool OnUpdate(float deltaTime) { Thousandto.Update.Manager.UpdateManager.Instance.Update(); return true; } /// /// 中断更新,一般是退出游戏的时候调用 /// public void AbortUpdate() { Thousandto.Update.Manager.UpdateManager.Instance.AbortFlows(); } public void AddStepPoint(int step, int resultCode = 0, string resultInfo = "") { Thousandto.Update.Recorder.StepRecorder.AddStep(step, resultCode, resultInfo); } public bool IsExistInApp(string relativePath) { string md5 = null; int size = 0; return UpdateManager.Instance.TryGetInAppData(relativePath, out md5, out size); } /// /// 网络是否可用 /// /// public bool IsNetworkEnable() { return Application.internetReachability != NetworkReachability.NotReachable; } /// /// 是否4G流量 /// /// public bool Is4G() { //UnityEngine.Debug.LogError("NetWork: " + Application.internetReachability); return Application.internetReachability == NetworkReachability.ReachableViaCarrierDataNetwork; } //获取app下载地址 public string GetAppUrl() { return UpdateManager.Instance.GetClientUrl(); } //获取日志上传的服务器地址 public string GetUploadServerURL() { return UpdateManager.Instance.GetUploadServerURL(); } #region //打开关闭窗体 public void OpenUI(object obj,object sender=null) { GameCenter.EventManager.PushFixEvent((int)UIEventDefine.UIUpdateNoticeForm_OPEN, obj); } public void CloseUI(object obj,object sender=null) { GameCenter.EventManager.PushFixEvent((int)UIEventDefine.UIUpdateNoticeForm_CLOSE, obj); } #endregion } }