mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-14 19:40:47 +00:00
refactor : 重构网络下载模块
This commit is contained in:
@@ -6,5 +6,5 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 自定义下载器的请求委托
|
||||
/// </summary>
|
||||
public delegate UnityWebRequest UnityWebRequestCreator(string url);
|
||||
public delegate UnityWebRequest UnityWebRequestCreator(string url, string method);
|
||||
}
|
||||
|
||||
@@ -46,16 +46,7 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
protected override void OnRequestSucceed()
|
||||
{
|
||||
var fileData = _webRequest.downloadHandler.data;
|
||||
if (fileData == null || fileData.Length == 0)
|
||||
{
|
||||
Status = EDownloadRequestStatus.Failed;
|
||||
Error = $"[{GetType().Name}] URL: {URL} - Download bytes data is null or empty !";
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = fileData;
|
||||
}
|
||||
Result = _webRequest.downloadHandler.data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,7 +202,7 @@ namespace YooAsset
|
||||
protected UnityWebRequest CreateUnityWebRequestGet(string requestUrl)
|
||||
{
|
||||
if (_webRequestCreator != null)
|
||||
return _webRequestCreator.Invoke(requestUrl);
|
||||
return _webRequestCreator.Invoke(requestUrl, UnityWebRequest.kHttpVerbGET);
|
||||
|
||||
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbGET);
|
||||
}
|
||||
@@ -214,6 +214,9 @@ namespace YooAsset
|
||||
/// <returns>UnityWebRequest 实例</returns>
|
||||
protected UnityWebRequest CreateUnityWebRequestHead(string requestUrl)
|
||||
{
|
||||
if (_webRequestCreator != null)
|
||||
return _webRequestCreator.Invoke(requestUrl, UnityWebRequest.kHttpVerbHEAD);
|
||||
|
||||
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbHEAD);
|
||||
}
|
||||
|
||||
|
||||
@@ -46,16 +46,7 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
protected override void OnRequestSucceed()
|
||||
{
|
||||
var fileText = _webRequest.downloadHandler.text;
|
||||
if (string.IsNullOrEmpty(fileText))
|
||||
{
|
||||
Status = EDownloadRequestStatus.Failed;
|
||||
Error = $"[{GetType().Name}] URL: {URL} - Download text data is null or empty";
|
||||
}
|
||||
else
|
||||
{
|
||||
Result = fileText;
|
||||
}
|
||||
Result = _webRequest.downloadHandler.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -268,9 +268,9 @@ public struct DownloadSimulateRequestArgs
|
||||
|
||||
```csharp
|
||||
// 自定义 UnityWebRequest 创建(建议通过 backend 构造函数传入)
|
||||
UnityWebRequestCreator creator = (url) =>
|
||||
UnityWebRequestCreator creator = (url, method) =>
|
||||
{
|
||||
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
|
||||
var request = new UnityWebRequest(url, method);
|
||||
// 自定义配置...
|
||||
return request;
|
||||
};
|
||||
@@ -540,6 +540,10 @@ VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
|
||||
|
||||
请求失败计数器,用于诊断统计:
|
||||
|
||||
- 线程安全:内部使用 `Dictionary` 且未加锁,约定只在主线程调用;如需多线程统计请在外层加锁或改造实现
|
||||
- Key 规则:`$"{packageName}_{eventName}"`
|
||||
- 统计口径:**仅统计网络请求失败**(`IDownloadRequest.Status != Succeed` 时记录),不统计内容为空、校验失败、解析失败等业务层失败
|
||||
|
||||
```csharp
|
||||
// 记录失败
|
||||
WebRequestCounter.RecordRequestFailed(packageName, eventName);
|
||||
@@ -553,6 +557,8 @@ int count = WebRequestCounter.GetRequestFailedCount(packageName, eventName);
|
||||
## 注意事项
|
||||
|
||||
1. **资源释放**:使用完毕后务必调用 `Dispose()` 释放资源
|
||||
- `AbortRequest()` 仅用于中止请求与切换状态,不等同于释放资源;无论成功/失败/中止都需要 `Dispose()`
|
||||
- 推荐使用 `try/finally` 确保释放(尤其是上层可能提前中止的场景)
|
||||
2. **断点续传**:需要服务器支持 `Range` 请求头和 `206 Partial Content` 响应
|
||||
- 若服务端不支持 Range 仍返回 200,全量内容可能会被追加写入,导致文件损坏
|
||||
3. **看门狗超时**:设置为 0 表示禁用,建议根据网络环境设置合理值
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络请求失败计数器(诊断用)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 线程安全:内部使用 Dictionary 且未加锁,约定只在 Unity 主线程调用。
|
||||
/// 如需在多线程/回调线程调用,请在外层加锁或改为并发容器实现。
|
||||
/// </remarks>
|
||||
internal class WebRequestCounter
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
@@ -15,12 +20,12 @@ namespace YooAsset
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
/// 记录网络请求失败事件的次数
|
||||
/// 失败计数记录表(key = $"{packageName}_{eventName}")
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, int> _requestFailedRecorder = new Dictionary<string, int>(1000);
|
||||
|
||||
/// <summary>
|
||||
/// 记录请求失败事件
|
||||
/// 记录一次失败
|
||||
/// </summary>
|
||||
public static void RecordRequestFailed(string packageName, string eventName)
|
||||
{
|
||||
@@ -31,7 +36,7 @@ namespace YooAsset
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取请求失败的次数
|
||||
/// 获取失败次数
|
||||
/// </summary>
|
||||
public static int GetRequestFailedCount(string packageName, string eventName)
|
||||
{
|
||||
@@ -41,4 +46,4 @@ namespace YooAsset
|
||||
return _requestFailedRecorder[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,6 +205,9 @@ namespace YooAsset
|
||||
/// </summary>
|
||||
public static DefaultBuildinFileCatalog DeserializeFromBinary(byte[] binaryData)
|
||||
{
|
||||
if (binaryData == null || binaryData.Length == 0)
|
||||
throw new Exception("Catalog file data is null or empty !");
|
||||
|
||||
// 创建缓存器
|
||||
BufferReader buffer = new BufferReader(binaryData);
|
||||
|
||||
|
||||
@@ -355,9 +355,9 @@ var webRemoteParams = FileSystemParameters.CreateDefaultWebRemoteFileSystemParam
|
||||
var cacheParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
|
||||
|
||||
// 设置 UnityWebRequest 创建委托(用于证书/代理/自定义 Header 等)
|
||||
cacheParams.AddParameter(FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR, (UnityWebRequestCreator)((url) =>
|
||||
cacheParams.AddParameter(FileSystemParametersDefine.UNITY_WEB_REQUEST_CREATOR, (UnityWebRequestCreator)((url, method) =>
|
||||
{
|
||||
var request = new UnityEngine.Networking.UnityWebRequest(url, UnityEngine.Networking.UnityWebRequest.kHttpVerbGET);
|
||||
var request = new UnityEngine.Networking.UnityWebRequest(url, method);
|
||||
// 自定义配置...
|
||||
return request;
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user