少年修仙传客户端代码仓库
Client_PangDeRong
2018-09-21 d1a2ab5b56bae336a1b5cbc4f307cb67e76c22e6
System/AssetVersion/DownLoadAndDiscompressTask.cs
@@ -1,151 +1,157 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Snxxz.UI;
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)RemoteFile.TotalDownloadedSize / 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 int okCount { get; private set; }
    public bool restartApp { 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;
        okCount = 0;
        step = Step.DownLoadPrepared;
        restartApp = 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 (totalSize > BYTE_PER_MILLIONBYTE)
        {
            WindowCenter.Instance.OpenFromLocal<DownLoadWin>();
        }
        else
        {
            StartDownLoad();
        }
    }
    public void StartDownLoad()
    {
        step = Step.DownLoad;
        SnxxzGame.Instance.StartCoroutine(Co_StartDownLoad());
    }
    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<DownLoadWin>();
        WindowCenter.Instance.Close<InGameDownLoadWin>();
        if (downLoadOkCallBack != null)
        {
            downLoadOkCallBack();
            downLoadOkCallBack = null;
        }
        RemoteFile.TotalDownloadedSize = 0;
        if (restartApp)
        {
            SDKUtility.Instance.RestartApp();
        }
    }
    private void OnFileDownLoadCompleted(bool _ok, AssetVersion _assetVersion)
    {
        if (_ok)
        {
            okCount++;
            _assetVersion.localValid = true;
        }
        else
        {
            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,
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Snxxz.UI;
using System.Threading;
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)RemoteFile.TotalDownloadedSize / 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 int okCount { get; private set; }
    public bool restartApp { 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;
        okCount = 0;
        step = Step.DownLoadPrepared;
        restartApp = 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 (totalSize > BYTE_PER_MILLIONBYTE)
        {
            WindowCenter.Instance.OpenFromLocal<DownLoadWin>();
        }
        else
        {
            StartDownLoad();
        }
    }
    public void StartDownLoad()
    {
        step = Step.DownLoad;
        SnxxzGame.Instance.StartCoroutine(Co_StartDownLoad());
    }
    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<DownLoadWin>();
        WindowCenter.Instance.Close<InGameDownLoadWin>();
        if (downLoadOkCallBack != null)
        {
            downLoadOkCallBack();
            downLoadOkCallBack = null;
        }
        RemoteFile.TotalDownloadedSize = 0;
        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,
    }
}