NLDClient-yudde/ProjectNLD/Assets/Editor/Tools/AutoConfig/DownloadConfig.cs

109 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutoConfig
{
using Cysharp.Threading.Tasks;
using System.IO;
using System.Net;
using UnityEditor;
using UnityEngine;
using UnityEngine.Networking;
public class DownloadConfig
{
public static string SAVE_PATH = "";
private static int _totalDownloadCount;
private static int _DownloadCount;
public static async UniTask AutoProcessConfig(List<ConfigInfo> m_Configs, string cookie)
{
var dir = new DirectoryInfo(ConfigTools.CONFIG_DIR);
SAVE_PATH = dir.FullName + "Datas/";
try
{
_totalDownloadCount = 0;
_DownloadCount = 0;
for (int i = 0; i < m_Configs.Count; i++)
{
if (m_Configs[i].Check)
{
StartDownload(m_Configs[i], cookie);
_totalDownloadCount++;
}
}
EditorUtility.DisplayProgressBar("配置工具", $"正在下载 0/{_totalDownloadCount}", 0);
while (_DownloadCount < _totalDownloadCount)
{
_DownloadCount = 0;
for (int i = 0; i < m_Configs.Count; i++)
{
if (m_Configs[i].Check)
{
if (m_Configs[i].IsError || m_Configs[i].IsDownloadOK)
{
_DownloadCount++;
}
}
}
await UniTask.Delay(100);
var remainCount = _totalDownloadCount - _DownloadCount;
EditorUtility.DisplayCancelableProgressBar("配置工具", $"正在下载 {remainCount}/{_totalDownloadCount}",
_DownloadCount / (float)_totalDownloadCount);
}
EditorUtility.ClearProgressBar();
bool hasError = false;
for (int i = 0; i < m_Configs.Count; i++)
{
if (m_Configs[i].Check && m_Configs[i].IsError)
{
hasError = true;
Debug.LogError("文件下载失败name=" + m_Configs[i].Name);
}
}
if (!hasError)
{
Debug.Log("所有文件下载成功");
}
}
catch (Exception e)
{
Debug.LogError("出现异常message=" + e.Message + "\n,trace=" + e.StackTrace);
EditorUtility.ClearProgressBar();
}
}
public static async void StartDownload(ConfigInfo ci, string cookie)
{
var downloader = new WPExcelDownloader(ci.Name, ci.DocID, cookie);
var result = await downloader.StartDownload();
if (result)
{
ci.IsDownloadOK = true;
}
else
{
ci.IsError = true;
}
}
}
}