mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-28 03:28:47 +00:00
Compare commits
2 Commits
ee67a55c0f
...
f0563cce0b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f0563cce0b | ||
|
|
9b83dcf723 |
@@ -6,5 +6,5 @@ namespace YooAsset
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 自定义下载器的请求委托
|
/// 自定义下载器的请求委托
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public delegate UnityWebRequest UnityWebRequestCreator(string url);
|
public delegate UnityWebRequest UnityWebRequestCreator(string url, string method);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,6 +75,9 @@ namespace YooAsset
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
if (string.IsNullOrEmpty(_args.FileHash))
|
||||||
|
throw new YooInternalException("File hash is null or empty !");
|
||||||
|
|
||||||
// 使用 Unity 缓存
|
// 使用 Unity 缓存
|
||||||
// 说明:The file hash defining the version of the asset bundle.
|
// 说明:The file hash defining the version of the asset bundle.
|
||||||
Hash128 fileHash = Hash128.Parse(_args.FileHash);
|
Hash128 fileHash = Hash128.Parse(_args.FileHash);
|
||||||
|
|||||||
@@ -46,16 +46,7 @@ namespace YooAsset
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected override void OnRequestSucceed()
|
protected override void OnRequestSucceed()
|
||||||
{
|
{
|
||||||
var fileData = _webRequest.downloadHandler.data;
|
Result = _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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ namespace YooAsset
|
|||||||
protected UnityWebRequest CreateUnityWebRequestGet(string requestUrl)
|
protected UnityWebRequest CreateUnityWebRequestGet(string requestUrl)
|
||||||
{
|
{
|
||||||
if (_webRequestCreator != null)
|
if (_webRequestCreator != null)
|
||||||
return _webRequestCreator.Invoke(requestUrl);
|
return _webRequestCreator.Invoke(requestUrl, UnityWebRequest.kHttpVerbGET);
|
||||||
|
|
||||||
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbGET);
|
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbGET);
|
||||||
}
|
}
|
||||||
@@ -214,6 +214,9 @@ namespace YooAsset
|
|||||||
/// <returns>UnityWebRequest 实例</returns>
|
/// <returns>UnityWebRequest 实例</returns>
|
||||||
protected UnityWebRequest CreateUnityWebRequestHead(string requestUrl)
|
protected UnityWebRequest CreateUnityWebRequestHead(string requestUrl)
|
||||||
{
|
{
|
||||||
|
if (_webRequestCreator != null)
|
||||||
|
return _webRequestCreator.Invoke(requestUrl, UnityWebRequest.kHttpVerbHEAD);
|
||||||
|
|
||||||
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbHEAD);
|
return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbHEAD);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,16 +46,7 @@ namespace YooAsset
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected override void OnRequestSucceed()
|
protected override void OnRequestSucceed()
|
||||||
{
|
{
|
||||||
var fileText = _webRequest.downloadHandler.text;
|
Result = _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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ namespace YooAsset
|
|||||||
#elif UNITY_STANDALONE_LINUX
|
#elif UNITY_STANDALONE_LINUX
|
||||||
url = StringUtility.Format("file:///root/{0}", path);
|
url = StringUtility.Format("file:///root/{0}", path);
|
||||||
#else
|
#else
|
||||||
throw new System.NotImplementedException();
|
throw new System.NotSupportedException($"[{nameof(DownloadSystemHelper.ConvertToWWWPath)}] not implemented platform: {UnityEngine.Application.platform}");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// For some special cases when users have special characters in their devices, url paths can not be identified correctly.
|
// For some special cases when users have special characters in their devices, url paths can not be identified correctly.
|
||||||
@@ -82,4 +82,4 @@ namespace YooAsset
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -268,9 +268,9 @@ public struct DownloadSimulateRequestArgs
|
|||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// 自定义 UnityWebRequest 创建(建议通过 backend 构造函数传入)
|
// 自定义 UnityWebRequest 创建(建议通过 backend 构造函数传入)
|
||||||
UnityWebRequestCreator creator = (url) =>
|
UnityWebRequestCreator creator = (url, method) =>
|
||||||
{
|
{
|
||||||
var request = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET);
|
var request = new UnityWebRequest(url, method);
|
||||||
// 自定义配置...
|
// 自定义配置...
|
||||||
return request;
|
return request;
|
||||||
};
|
};
|
||||||
@@ -530,9 +530,8 @@ VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
|
|||||||
|
|
||||||
提供跨平台的工具函数:
|
提供跨平台的工具函数:
|
||||||
|
|
||||||
| 成员 | 说明 |
|
| 方法 | 说明 |
|
||||||
|------|------|
|
|------|------|
|
||||||
| `UnityWebRequestCreater` | 兼容保留的全局 UnityWebRequest 创建委托(默认文件系统不再读取,推荐改为 FileSystemParameters 注入) |
|
|
||||||
| `ConvertToWWWPath()` | 转换本地路径为 WWW 协议 URL |
|
| `ConvertToWWWPath()` | 转换本地路径为 WWW 协议 URL |
|
||||||
| `IsRequestLocalFile()` | 判断是否本地文件请求 |
|
| `IsRequestLocalFile()` | 判断是否本地文件请求 |
|
||||||
|
|
||||||
@@ -540,6 +539,10 @@ VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
|
|||||||
|
|
||||||
请求失败计数器,用于诊断统计:
|
请求失败计数器,用于诊断统计:
|
||||||
|
|
||||||
|
- 线程安全:内部使用 `Dictionary` 且未加锁,约定只在主线程调用;如需多线程统计请在外层加锁或改造实现
|
||||||
|
- Key 规则:`$"{packageName}_{eventName}"`
|
||||||
|
- 统计口径:**仅统计网络请求失败**(`IDownloadRequest.Status != Succeed` 时记录),不统计内容为空、校验失败、解析失败等业务层失败
|
||||||
|
|
||||||
```csharp
|
```csharp
|
||||||
// 记录失败
|
// 记录失败
|
||||||
WebRequestCounter.RecordRequestFailed(packageName, eventName);
|
WebRequestCounter.RecordRequestFailed(packageName, eventName);
|
||||||
@@ -553,6 +556,8 @@ int count = WebRequestCounter.GetRequestFailedCount(packageName, eventName);
|
|||||||
## 注意事项
|
## 注意事项
|
||||||
|
|
||||||
1. **资源释放**:使用完毕后务必调用 `Dispose()` 释放资源
|
1. **资源释放**:使用完毕后务必调用 `Dispose()` 释放资源
|
||||||
|
- `AbortRequest()` 仅用于中止请求与切换状态,不等同于释放资源;无论成功/失败/中止都需要 `Dispose()`
|
||||||
|
- 推荐使用 `try/finally` 确保释放(尤其是上层可能提前中止的场景)
|
||||||
2. **断点续传**:需要服务器支持 `Range` 请求头和 `206 Partial Content` 响应
|
2. **断点续传**:需要服务器支持 `Range` 请求头和 `206 Partial Content` 响应
|
||||||
- 若服务端不支持 Range 仍返回 200,全量内容可能会被追加写入,导致文件损坏
|
- 若服务端不支持 Range 仍返回 200,全量内容可能会被追加写入,导致文件损坏
|
||||||
3. **看门狗超时**:设置为 0 表示禁用,建议根据网络环境设置合理值
|
3. **看门狗超时**:设置为 0 表示禁用,建议根据网络环境设置合理值
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
using System;
|
|
||||||
using System.Collections;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace YooAsset
|
namespace YooAsset
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 网络请求失败计数器(诊断用)
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// 线程安全:内部使用 Dictionary 且未加锁,约定只在 Unity 主线程调用。
|
||||||
|
/// 如需在多线程/回调线程调用,请在外层加锁或改为并发容器实现。
|
||||||
|
/// </remarks>
|
||||||
internal class WebRequestCounter
|
internal class WebRequestCounter
|
||||||
{
|
{
|
||||||
#if UNITY_EDITOR
|
#if UNITY_EDITOR
|
||||||
@@ -15,12 +20,12 @@ namespace YooAsset
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 记录网络请求失败事件的次数
|
/// 失败计数记录表(key = $"{packageName}_{eventName}")
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private static readonly Dictionary<string, int> _requestFailedRecorder = new Dictionary<string, int>(1000);
|
private static readonly Dictionary<string, int> _requestFailedRecorder = new Dictionary<string, int>(1000);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 记录请求失败事件
|
/// 记录一次失败
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static void RecordRequestFailed(string packageName, string eventName)
|
public static void RecordRequestFailed(string packageName, string eventName)
|
||||||
{
|
{
|
||||||
@@ -31,14 +36,14 @@ namespace YooAsset
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取请求失败的次数
|
/// 获取失败次数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static int GetRequestFailedCount(string packageName, string eventName)
|
public static int GetRequestFailedCount(string packageName, string eventName)
|
||||||
{
|
{
|
||||||
string key = $"{packageName}_{eventName}";
|
string key = $"{packageName}_{eventName}";
|
||||||
if (_requestFailedRecorder.ContainsKey(key) == false)
|
if (_requestFailedRecorder.TryGetValue(key, out int count))
|
||||||
_requestFailedRecorder.Add(key, 0);
|
return count;
|
||||||
return _requestFailedRecorder[key];
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -205,6 +205,9 @@ namespace YooAsset
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static DefaultBuildinFileCatalog DeserializeFromBinary(byte[] binaryData)
|
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);
|
BufferReader buffer = new BufferReader(binaryData);
|
||||||
|
|
||||||
|
|||||||
@@ -355,9 +355,9 @@ var webRemoteParams = FileSystemParameters.CreateDefaultWebRemoteFileSystemParam
|
|||||||
var cacheParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
|
var cacheParams = FileSystemParameters.CreateDefaultCacheFileSystemParameters(remoteServices);
|
||||||
|
|
||||||
// 设置 UnityWebRequest 创建委托(用于证书/代理/自定义 Header 等)
|
// 设置 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;
|
return request;
|
||||||
}));
|
}));
|
||||||
|
|||||||
Reference in New Issue
Block a user