using System.Collections;
|
using System.Collections.Generic;
|
using UnityEngine;
|
using System;
|
|
using Cysharp.Threading.Tasks;
|
|
|
public class DownLoadAndDiscompressTask : Singleton<DownLoadAndDiscompressTask>
|
|
{
|
public const int BYTE_PER_KILOBYTE = 1024;
|
public const int BYTE_PER_MILLIONBYTE = 1048576;
|
|
public float progress { get { return Mathf.Clamp01((float)DownloadMgr.Instance.DownloadedBytes / totalSize); } }
|
|
List<AssetVersion> tasks = new List<AssetVersion>();
|
public bool isDone { get { return step == Step.Completed; } }
|
public int totalSize { get; private set; }
|
public int totalCount { get; private set; }
|
public bool restartApp { get; private set; }
|
public bool reinitedBuiltInAsset { get; private set; }
|
|
public event Action<Step> 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<AssetVersion> _downLoadTasks, Action _downLoadOkCallBack)
|
{
|
tasks = _downLoadTasks;
|
downLoadOkCallBack = _downLoadOkCallBack;
|
|
totalCount = tasks.Count;
|
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)
|
{
|
// TODO YYL
|
// UIManager.Instance.OpenWindow<DownLoadWin>();
|
// WindowCenter.Instance.OpenFromLocal<DownLoadWin>();
|
}
|
else
|
{
|
StartDownLoad();
|
}
|
SDKUtils.Instance.DownloadStart();
|
}
|
|
public void StartDownLoad()
|
{
|
step = Step.DownLoad;
|
|
Co_StartDownLoad();
|
// SnxxzGame.Instance.StartCoroutine(Co_StartDownLoad());
|
|
// TODO YYL
|
// GameNotice.OpenGameNotice();
|
}
|
|
protected async UniTask Co_StartDownLoad()
|
{
|
while (tasks.Count > 0)
|
{
|
DownloadMgr.Instance.Prepare();
|
for (int i = 0; i < tasks.Count; i++)
|
{
|
var assetVersion = tasks[i];
|
var remoteURL = StringUtility.Contact(VersionUtility.Instance.versionInfo.GetResourcesURL(VersionConfig.Get().branch), Language.fixPath, "/", assetVersion.relativePath);
|
var localURL = StringUtility.Contact(ResourcesPath.Instance.ExternalStorePath, assetVersion.relativePath);
|
DownloadMgr.Instance.AddTask(new DownloadTask(remoteURL, localURL));
|
}
|
|
DownloadMgr.Instance.Begin();
|
|
while (!DownloadMgr.Instance.IsFinished)
|
{
|
await UniTask.DelayFrame(1);
|
}
|
|
//检查下载完成后的文件是否有效
|
var filledTasks = new List<AssetVersion>();//无效的文件
|
for (int i = 0; i < tasks.Count; i++)
|
{
|
var assetVersion = tasks[i];
|
var correctFile = assetVersion.CheckLocalFileValid(true);
|
if (correctFile)
|
assetVersion.localValid = true;
|
else
|
filledTasks.Add(assetVersion);
|
}
|
if (filledTasks.Count > 0)
|
Debug.LogErrorFormat("资源下载完后有{0}个文件无效({1}),重新开始下载", filledTasks.Count, filledTasks[0].fileName);
|
tasks = filledTasks;
|
}
|
|
Debug.Log("资源全部下载完成");
|
|
step = Step.Completed;
|
|
|
UIManager.Instance.OpenWindow<DownLoadWin>();
|
UIManager.Instance.OpenWindow<InGameDownLoadWin>();
|
|
if (downLoadOkCallBack != null)
|
{
|
downLoadOkCallBack();
|
downLoadOkCallBack = null;
|
}
|
|
try
|
{
|
if (reinitedBuiltInAsset)
|
{
|
AssetBundleUtility.Instance.ReInitBuiltInAsset();
|
}
|
}
|
catch (System.Exception ex)
|
{
|
Debug.Log(ex);
|
}
|
|
if (restartApp)
|
{
|
SDKUtils.Instance.RestartApp();
|
}
|
SDKUtils.Instance.DownloadEnd();
|
}
|
|
// 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), "/", Language.Id, "/", _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,
|
}
|
|
}
|