2024-12-11 19:52:38 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
|
using Gameplay;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
|
|
public partial class SROptions
|
|
|
|
|
|
{
|
2024-12-24 14:08:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 成功时的最大间隔
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const float Success_MAX_INTERVALS = 300f; // 5分钟
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 失败时的最大间隔
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private const float FAIL_MAX_INTERVALS = 15f; // 15 秒
|
2024-12-13 15:15:19 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 超时时间
|
|
|
|
|
|
/// </summary>
|
2024-12-11 19:52:38 +08:00
|
|
|
|
private readonly double timeOutTick = 10f; // 10秒没下完超时
|
|
|
|
|
|
/// <summary>
|
2024-12-24 14:08:01 +08:00
|
|
|
|
/// 当前下载间隔
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private float intervals = Success_MAX_INTERVALS;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下载计时
|
2024-12-11 19:52:38 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
private float timer = 0f;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 减少最大间隔时间次数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private int count = 0;
|
2024-12-12 12:23:25 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 总下载次数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private int total;
|
|
|
|
|
|
|
|
|
|
|
|
private StreamWriter writerUnityWebRequest;
|
|
|
|
|
|
private StreamWriter writerHttpClient;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
2024-12-24 14:08:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 是否下载中
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private bool isDownloading = false;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下载地址1
|
|
|
|
|
|
/// </summary>
|
2024-12-24 17:12:38 +08:00
|
|
|
|
private const string DOWNLOAD_URL_1 = "http://1.14.62.163/Build/TapTapDev/Android/0.1.0/_ExtBuildInfo.json";
|
2024-12-24 14:08:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 下载地址2
|
|
|
|
|
|
/// </summary>
|
2024-12-24 17:12:38 +08:00
|
|
|
|
private const string DOWNLOAD_URL_2 = "http://1.14.62.163/Build/TapTapDev/Android/0.1.0/OutputCache.manifest";
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
|
|
|
|
|
[Category("下载测试"), DisplayName("开始下载")]
|
|
|
|
|
|
public async void BeginDownload()
|
|
|
|
|
|
{
|
2024-12-24 14:50:55 +08:00
|
|
|
|
Screen.sleepTimeout = SleepTimeout.NeverSleep;
|
2024-12-12 12:23:25 +08:00
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
string fileUnityWebRequestPath = Path.Combine(Application.streamingAssetsPath, "UnityWebRequestTest.txt");
|
|
|
|
|
|
string fileHttpClientPath = Path.Combine(Application.streamingAssetsPath, "HttpClientTest.txt");
|
|
|
|
|
|
#else
|
|
|
|
|
|
string fileUnityWebRequestPath = Path.Combine(Application.persistentDataPath, "UnityWebRequestTest.txt");
|
|
|
|
|
|
string fileHttpClientPath = Path.Combine(Application.persistentDataPath, "HttpClientTest.txt");
|
|
|
|
|
|
#endif
|
|
|
|
|
|
writerUnityWebRequest = File.CreateText(fileUnityWebRequestPath);
|
|
|
|
|
|
writerHttpClient = File.CreateText(fileHttpClientPath);
|
|
|
|
|
|
|
2024-12-11 19:52:38 +08:00
|
|
|
|
timer = intervals;
|
2024-12-12 12:23:25 +08:00
|
|
|
|
total = 0;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
isDownloading = true;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
2024-12-24 14:08:01 +08:00
|
|
|
|
while (isDownloading)
|
2024-12-11 19:52:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (timer >= intervals)
|
|
|
|
|
|
{
|
|
|
|
|
|
timer -= intervals;
|
2024-12-12 12:23:25 +08:00
|
|
|
|
++total;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
string downLoadUrl = ((total / 2) & 1) == 1 ? DOWNLOAD_URL_1 : DOWNLOAD_URL_2;
|
|
|
|
|
|
string networdType = Application.internetReachability switch
|
|
|
|
|
|
{
|
|
|
|
|
|
NetworkReachability.ReachableViaCarrierDataNetwork => "流量",
|
|
|
|
|
|
NetworkReachability.ReachableViaLocalAreaNetwork => "Wifi",
|
|
|
|
|
|
_ => "没有网络"
|
|
|
|
|
|
};
|
|
|
|
|
|
if ((total & 1) == 1)
|
|
|
|
|
|
DownloadByUnityWebRequest(downLoadUrl, networdType);
|
|
|
|
|
|
else
|
2024-12-24 17:12:38 +08:00
|
|
|
|
DownloadByHttpClient(downLoadUrl, networdType);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
}
|
2024-12-24 14:08:01 +08:00
|
|
|
|
await UniTask.DelayFrame(1);
|
|
|
|
|
|
timer += Time.deltaTime;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Category("下载测试"), DisplayName("停止下载")]
|
|
|
|
|
|
public void StopDownload()
|
|
|
|
|
|
{
|
2024-12-24 14:50:55 +08:00
|
|
|
|
Screen.sleepTimeout = SleepTimeout.SystemSetting;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
isDownloading = false;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerUnityWebRequest.Flush();
|
2024-12-11 20:02:54 +08:00
|
|
|
|
writerUnityWebRequest.Close();
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerHttpClient.Flush();
|
2024-12-11 20:02:54 +08:00
|
|
|
|
writerHttpClient.Close();
|
2024-12-11 19:52:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 14:08:01 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送企业微信通知
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public async void SendWechatMessage(string content)
|
|
|
|
|
|
{
|
|
|
|
|
|
string url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bf89f555-f710-48c4-87d0-7a8182dec13e";
|
|
|
|
|
|
string message = "{\r\n" +
|
|
|
|
|
|
"\"msgtype\":\"text\",\r\n" +
|
|
|
|
|
|
"\"text\":{\r\n" +
|
|
|
|
|
|
$"\"content\":\"{content}\",\r\n" +
|
|
|
|
|
|
$"\"mentioned_list\":[\"@all\"]" +
|
|
|
|
|
|
"}\r\n" + "}";
|
|
|
|
|
|
|
|
|
|
|
|
UnityWebRequest request = UnityWebRequest.Post(url, message, "application/json");
|
|
|
|
|
|
await request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
|
|
|
|
Debug.LogError($"Error: {request.error}");
|
|
|
|
|
|
else
|
|
|
|
|
|
Debug.Log($"Response: {request.downloadHandler.text}"); // 请求成功,输出响应数据
|
|
|
|
|
|
|
|
|
|
|
|
request.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-11 19:52:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过WebRequest下载
|
|
|
|
|
|
/// </summary>
|
2024-12-24 14:08:01 +08:00
|
|
|
|
private async void DownloadByUnityWebRequest(string url, string netword)
|
2024-12-11 19:52:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
string log = string.Empty;
|
|
|
|
|
|
DateTime startTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
|
|
|
|
|
DateTime endTime;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
using UnityWebRequest request = UnityWebRequest.Get(url);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
|
|
|
|
|
UnityWebRequestAsyncOperation asyncOperation = request.SendWebRequest();
|
|
|
|
|
|
|
|
|
|
|
|
while (!asyncOperation.isDone)
|
|
|
|
|
|
{
|
|
|
|
|
|
await UniTask.DelayFrame(1);
|
|
|
|
|
|
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
|
|
|
|
|
TimeSpan timeSpan = endTime - startTime;
|
|
|
|
|
|
if (timeSpan.TotalSeconds > timeOutTick)
|
|
|
|
|
|
{
|
|
|
|
|
|
request.Abort();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, {timeOutTick}s超时,URL:{url}";
|
2024-12-24 16:12:05 +08:00
|
|
|
|
Debug.LogWarning(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerUnityWebRequest.WriteLine(log);
|
|
|
|
|
|
writerUnityWebRequest.Flush();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = FAIL_MAX_INTERVALS;
|
2024-12-24 17:12:38 +08:00
|
|
|
|
count = 6;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
SendWechatMessage(log);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
|
|
|
|
|
if (request.result != UnityWebRequest.Result.Success)
|
|
|
|
|
|
{
|
2024-12-24 16:12:05 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,下载失败,URL:{url}";
|
|
|
|
|
|
Debug.LogError(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerUnityWebRequest.WriteLine(log);
|
|
|
|
|
|
writerUnityWebRequest.Flush();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = FAIL_MAX_INTERVALS;
|
2024-12-24 17:12:38 +08:00
|
|
|
|
count = 6;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
SendWechatMessage(log);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 16:12:05 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(UnityWebRequest)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,下载成功,URL:{url}";
|
2024-12-11 19:52:38 +08:00
|
|
|
|
Debug.Log(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerUnityWebRequest.WriteLine(log);
|
|
|
|
|
|
writerUnityWebRequest.Flush();
|
2024-12-11 19:52:38 +08:00
|
|
|
|
--count;
|
|
|
|
|
|
if (count <= 0)
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = Success_MAX_INTERVALS;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 通过HttpClient下载
|
|
|
|
|
|
/// </summary>
|
2024-12-24 17:12:38 +08:00
|
|
|
|
private async void DownloadByHttpClient(string url, string netword)
|
2024-12-11 19:52:38 +08:00
|
|
|
|
{
|
|
|
|
|
|
string log = string.Empty;
|
|
|
|
|
|
DateTime startTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
|
|
|
|
|
DateTime endTime;
|
|
|
|
|
|
|
|
|
|
|
|
CancellationTokenSource cancellationTokenSource = new();
|
|
|
|
|
|
using HttpClient client = new();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
Task<HttpResponseMessage> response = client.GetAsync(url, cancellationTokenSource.Token);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
|
|
|
|
|
|
while (!response.IsCompleted)
|
|
|
|
|
|
{
|
|
|
|
|
|
await UniTask.DelayFrame(1);
|
|
|
|
|
|
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
|
|
|
|
|
TimeSpan timeSpan = endTime - startTime;
|
|
|
|
|
|
if (timeSpan.TotalSeconds > timeOutTick)
|
|
|
|
|
|
{
|
|
|
|
|
|
cancellationTokenSource.Cancel();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, {timeOutTick}s超时,URL:{url}";
|
2024-12-24 16:12:05 +08:00
|
|
|
|
Debug.LogWarning(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerHttpClient.WriteLine(log);
|
|
|
|
|
|
writerHttpClient.Flush();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = FAIL_MAX_INTERVALS;
|
2024-12-24 17:12:38 +08:00
|
|
|
|
count = 6;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
SendWechatMessage(log);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
endTime = CommonUtils.GetCurTime(CommonUtils.E_TimeType.Local);
|
2024-12-24 17:12:38 +08:00
|
|
|
|
if (!response.IsCompletedSuccessfully || !response.Result.IsSuccessStatusCode)
|
2024-12-11 19:52:38 +08:00
|
|
|
|
{
|
2024-12-24 14:08:01 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,下载失败,URL:{url}";
|
2024-12-24 16:12:05 +08:00
|
|
|
|
Debug.LogError(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerHttpClient.WriteLine(log);
|
|
|
|
|
|
writerHttpClient.Flush();
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = FAIL_MAX_INTERVALS;
|
2024-12-24 17:12:38 +08:00
|
|
|
|
count = 6;
|
2024-12-24 14:08:01 +08:00
|
|
|
|
SendWechatMessage(log);
|
2024-12-11 19:52:38 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-24 14:08:01 +08:00
|
|
|
|
log = $"第{total}次下载,类型:{nameof(HttpClient)}, 网络:{netword}, 开始时间:{startTime},完成时间:{endTime},间隔:{(endTime - startTime).TotalSeconds}秒,下载成功,URL:{url}";
|
2024-12-11 19:52:38 +08:00
|
|
|
|
Debug.Log(log);
|
2024-12-12 12:23:25 +08:00
|
|
|
|
writerHttpClient.WriteLine(log);
|
|
|
|
|
|
writerHttpClient.Flush();
|
2024-12-11 19:52:38 +08:00
|
|
|
|
--count;
|
|
|
|
|
|
if (count <= 0)
|
2024-12-24 14:08:01 +08:00
|
|
|
|
intervals = Success_MAX_INTERVALS;
|
2024-12-11 19:52:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|