From 9b83dcf7231831eadb586232e52dec92161edd4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E5=86=A0=E5=B3=B0?= Date: Wed, 7 Jan 2026 15:08:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=E9=87=8D=E6=9E=84=E7=BD=91?= =?UTF-8?q?=E7=BB=9C=E4=B8=8B=E8=BD=BD=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UnityWebRequestCreator.cs | 2 +- .../UnityWebRequestBytesDownloader.cs | 11 +---------- .../UnityWebRequestDownloaderBase.cs | 5 ++++- .../UnityWebRequestTextDownloader.cs | 11 +---------- .../YooAsset/Runtime/DownloadSystem/README.md | 10 ++++++++-- .../Runtime/DownloadSystem/WebRequestCounter.cs | 17 +++++++++++------ .../DefaultBuildinFileSystem/CatalogTools.cs | 3 +++ Assets/YooAsset/Runtime/FileSystem/README.md | 4 ++-- 8 files changed, 31 insertions(+), 32 deletions(-) diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadBackend/UnityWebRequestCreator.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadBackend/UnityWebRequestCreator.cs index c982a434..698103e5 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadBackend/UnityWebRequestCreator.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadBackend/UnityWebRequestCreator.cs @@ -6,5 +6,5 @@ namespace YooAsset /// /// 自定义下载器的请求委托 /// - public delegate UnityWebRequest UnityWebRequestCreator(string url); + public delegate UnityWebRequest UnityWebRequestCreator(string url, string method); } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestBytesDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestBytesDownloader.cs index f12b2857..db887947 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestBytesDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestBytesDownloader.cs @@ -46,16 +46,7 @@ namespace YooAsset /// 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; } } } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs index 60d1c078..f6e6f680 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestDownloaderBase.cs @@ -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 /// UnityWebRequest 实例 protected UnityWebRequest CreateUnityWebRequestHead(string requestUrl) { + if (_webRequestCreator != null) + return _webRequestCreator.Invoke(requestUrl, UnityWebRequest.kHttpVerbHEAD); + return new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbHEAD); } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestTextDownloader.cs b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestTextDownloader.cs index fee9fb06..ee4a0e06 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestTextDownloader.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/DefaultDownloadRequest/UnityWebRequestTextDownloader.cs @@ -46,16 +46,7 @@ namespace YooAsset /// 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; } } } diff --git a/Assets/YooAsset/Runtime/DownloadSystem/README.md b/Assets/YooAsset/Runtime/DownloadSystem/README.md index 065f1b22..26cdff6c 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/README.md +++ b/Assets/YooAsset/Runtime/DownloadSystem/README.md @@ -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 表示禁用,建议根据网络环境设置合理值 diff --git a/Assets/YooAsset/Runtime/DownloadSystem/WebRequestCounter.cs b/Assets/YooAsset/Runtime/DownloadSystem/WebRequestCounter.cs index 72984b55..e06c7cfc 100644 --- a/Assets/YooAsset/Runtime/DownloadSystem/WebRequestCounter.cs +++ b/Assets/YooAsset/Runtime/DownloadSystem/WebRequestCounter.cs @@ -1,9 +1,14 @@ -using System; -using System.Collections; using System.Collections.Generic; namespace YooAsset { + /// + /// 网络请求失败计数器(诊断用) + /// + /// + /// 线程安全:内部使用 Dictionary 且未加锁,约定只在 Unity 主线程调用。 + /// 如需在多线程/回调线程调用,请在外层加锁或改为并发容器实现。 + /// internal class WebRequestCounter { #if UNITY_EDITOR @@ -15,12 +20,12 @@ namespace YooAsset #endif /// - /// 记录网络请求失败事件的次数 + /// 失败计数记录表(key = $"{packageName}_{eventName}") /// private static readonly Dictionary _requestFailedRecorder = new Dictionary(1000); /// - /// 记录请求失败事件 + /// 记录一次失败 /// public static void RecordRequestFailed(string packageName, string eventName) { @@ -31,7 +36,7 @@ namespace YooAsset } /// - /// 获取请求失败的次数 + /// 获取失败次数 /// public static int GetRequestFailedCount(string packageName, string eventName) { @@ -41,4 +46,4 @@ namespace YooAsset return _requestFailedRecorder[key]; } } -} \ No newline at end of file +} diff --git a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/CatalogTools.cs b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/CatalogTools.cs index 3aaf908e..446c2ee1 100644 --- a/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/CatalogTools.cs +++ b/Assets/YooAsset/Runtime/FileSystem/DefaultBuildinFileSystem/CatalogTools.cs @@ -205,6 +205,9 @@ namespace YooAsset /// 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); diff --git a/Assets/YooAsset/Runtime/FileSystem/README.md b/Assets/YooAsset/Runtime/FileSystem/README.md index 85b304f8..9cd92a73 100644 --- a/Assets/YooAsset/Runtime/FileSystem/README.md +++ b/Assets/YooAsset/Runtime/FileSystem/README.md @@ -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; }));