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

110 lines
3.5 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
2023-08-22 19:08:45 +08:00
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 _DownloadCount;
public static List<DownloadConfigData> downloadConfigs = new List<DownloadConfigData>();
2023-10-24 12:15:31 +08:00
public static async UniTask AutoProcessConfig(List<ConfigInfo> m_Configs, string cookie)
2023-08-22 19:08:45 +08:00
{
var dir = new DirectoryInfo(ConfigTools.CONFIG_DIR);
SAVE_PATH = dir.FullName + "Datas/";
downloadConfigs.Clear();
2023-08-22 19:08:45 +08:00
try
{
_DownloadCount = 0;
for (int i = 0; i < m_Configs.Count; i++)
{
var config = m_Configs[i];
if (config.Check)
2023-08-22 19:08:45 +08:00
{
var downloader = StartDownload(config, cookie);
var downloadConfig = new DownloadConfigData();
downloadConfig.excelName = config.Name;
downloadConfig.downloader = downloader;
downloadConfigs.Add(downloadConfig);
2023-08-22 19:08:45 +08:00
}
}
var isFinished = false;
while (!isFinished)
2023-08-22 19:08:45 +08:00
{
isFinished = true;
for (int i = 0; i < downloadConfigs.Count; i++)
2023-08-22 19:08:45 +08:00
{
var config = downloadConfigs[i];
if (config.isDone)
2023-08-22 19:08:45 +08:00
{
continue;
2023-08-22 19:08:45 +08:00
}
isFinished = false;
2023-08-22 19:08:45 +08:00
}
await UniTask.Delay(100);
// 排序,未完成的排在前面
downloadConfigs = downloadConfigs.OrderBy(x => x.isDone).ToList();
2023-08-22 19:08:45 +08:00
}
EditorUtility.ClearProgressBar();
bool hasError = false;
for (int i = 0; i < downloadConfigs.Count; i++)
2023-08-22 19:08:45 +08:00
{
var config = downloadConfigs[i];
if (!config.downloader.isDownloaded)
2023-08-22 19:08:45 +08:00
{
if (config.downloader.isCanceled)
{
Debug.LogError("文件下载被取消name=" + config.excelName);
}
else
{
Debug.LogError("文件下载失败name=" + config.excelName);
}
2023-08-22 19:08:45 +08:00
hasError = true;
}
}
if (!hasError)
{
Debug.Log("所有文件下载成功");
}
}
catch (Exception e)
{
Debug.LogError("出现异常message=" + e.Message + "\n,trace=" + e.StackTrace);
EditorUtility.ClearProgressBar();
}
}
public static WPExcelDownloader StartDownload(ConfigInfo ci, string cookie)
2023-08-22 19:08:45 +08:00
{
2023-10-24 12:15:31 +08:00
var downloader = new WPExcelDownloader(ci.Name, ci.DocID, cookie);
downloader.StartDownload().Forget();
return downloader;
2023-08-22 19:08:45 +08:00
}
2023-08-22 19:08:45 +08:00
}
}