using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using Snxxz.UI; using System.Threading; [XLua.LuaCallCSharp] public class DownLoadAndDiscompressTask : Singleton { public const int BYTE_PER_KILOBYTE = 1024; public const int BYTE_PER_MILLIONBYTE = 1048576; public float progress { get { return Mathf.Clamp01((float)RemoteFile.TotalDownloadedSize / totalSize); } } List tasks = new List(); public bool isDone { get { return step == Step.Completed; } } public int totalSize { get; private set; } public int totalCount { get; private set; } public int okCount { get; private set; } public bool restartApp { get; private set; } public bool reinitedBuiltInAsset { get; private set; } public event Action downLoadStepChangeEvent; Action downLoadOkCallBack; Step m_Step = Step.None; public Step step { get { return m_Step; } set { if (m_Step != value) { m_Step = value; if (downLoadStepChangeEvent != null) { downLoadStepChangeEvent(m_Step); } } } } public void Prepare(List _downLoadTasks, Action _downLoadOkCallBack) { tasks = _downLoadTasks; downLoadOkCallBack = _downLoadOkCallBack; totalCount = tasks.Count; okCount = 0; step = Step.DownLoadPrepared; restartApp = false; reinitedBuiltInAsset = false; totalSize = 0; for (int i = 0; i < tasks.Count; i++) { var task = tasks[i]; totalSize += task.size; #if UNITY_ANDROID if (!restartApp && task.GetAssetCategory() == AssetVersion.AssetCategory.Dll) { restartApp = true; } #endif if (!reinitedBuiltInAsset && task.relativePath.Contains("builtin")) { reinitedBuiltInAsset = true; } } if (totalSize > BYTE_PER_MILLIONBYTE) { WindowCenter.Instance.OpenFromLocal(); } else { StartDownLoad(); } } public void StartDownLoad() { step = Step.DownLoad; SnxxzGame.Instance.StartCoroutine(Co_StartDownLoad()); GameNotice.OpenGameNotice(); } IEnumerator Co_StartDownLoad() { RemoteFile.maxDownLoadTask = 48; RemoteFile.MaxConnectLimit = 48; RemoteFile.Prepare(); for (int i = 0; i < tasks.Count; i++) { var assetVersion = tasks[i]; var remoteURL = StringUtility.Contact(VersionUtility.Instance.versionInfo.GetResourcesURL(VersionConfig.Get().branch), "/", assetVersion.relativePath); var localURL = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, assetVersion.relativePath); var remoteFile = new RemoteFile(); remoteFile.Init(remoteURL, localURL, assetVersion); remoteFile.Begin(OnFileDownLoadCompleted); } while (okCount < totalCount) { yield return null; } step = Step.Completed; WindowCenter.Instance.Close(); WindowCenter.Instance.Close(); if (downLoadOkCallBack != null) { downLoadOkCallBack(); downLoadOkCallBack = null; } RemoteFile.TotalDownloadedSize = 0; try { if (reinitedBuiltInAsset) { AssetBundleUtility.Instance.ReInitBuiltInAsset(); } } catch (System.Exception ex) { Debug.Log(ex); } if (restartApp) { SDKUtility.Instance.RestartApp(); } } private void OnFileDownLoadCompleted(bool _ok, AssetVersion _assetVersion) { var correctFile = false; if (_ok) { correctFile = _assetVersion.CheckLocalFileValid(true); } if (_ok && correctFile) { okCount++; _assetVersion.localValid = true; } else { ExceptionCatcher.ReportException("游戏启动阶段文件下载失败", "文件名:" + _assetVersion.relativePath); var remoteURL = StringUtility.Contact(VersionUtility.Instance.versionInfo.GetResourcesURL(VersionConfig.Get().branch), "/", _assetVersion.relativePath); var localURL = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, _assetVersion.relativePath); var remoteFile = new RemoteFile(); remoteFile.Init(remoteURL, localURL, _assetVersion); remoteFile.Begin(OnFileDownLoadCompleted); } } public enum Step { None, DownLoadPrepared, DownLoad, Completed, } }