refactor : 重构代码

This commit is contained in:
何冠峰
2026-01-20 17:39:19 +08:00
parent 5df021204d
commit 4bff0ef895
14 changed files with 132 additions and 97 deletions

View File

@@ -11,27 +11,22 @@ namespace YooAsset
internal struct DiagnosticOperationInfo : IComparer<DiagnosticOperationInfo>, IComparable<DiagnosticOperationInfo>
{
/// <summary>
/// 任务名称
/// 异步操作的名称
/// </summary>
public string OperationName;
/// <summary>
/// 任务说明
/// 异步操作的说明
/// </summary>
public string OperationDesc;
/// <summary>
/// 优先级
///异步操作的优先级
/// </summary>
public uint Priority;
/// <summary>
/// 任务进度
/// </summary>
public float Progress;
/// <summary>
/// 任务开始的时间
/// 开始的时间
/// </summary>
public string StartTime;
@@ -41,7 +36,12 @@ namespace YooAsset
public long ElapsedMS;
/// <summary>
/// 任务状态
/// 当前进度
/// </summary>
public float Progress;
/// <summary>
/// 当前状态
/// </summary>
public string Status;

View File

@@ -34,7 +34,7 @@ namespace YooAsset
private bool _isParsed = false;
/// <summary>
/// 获取调试资源包信息
/// 获取资源包的诊断信息
/// </summary>
public DiagnosticBundleInfo GetBundleInfo(string bundleName)
{

View File

@@ -36,7 +36,7 @@ namespace YooAsset
public int ReferenceCount;
/// <summary>
/// 加载状态
/// 当前状态
/// </summary>
public string Status;

View File

@@ -18,12 +18,12 @@ namespace YooAsset
public string DebuggerVersion = DiagnosticSystemDefine.DebuggerVersion;
/// <summary>
/// 游戏帧
/// 报告发生的游戏帧
/// </summary>
public int FrameCount;
/// <summary>
/// 调试的包裹数据列表
/// 包裹数据列表
/// </summary>
public List<DiagnosticPackageData> PackageDataList = new List<DiagnosticPackageData>(10);

View File

@@ -10,7 +10,7 @@ namespace YooAsset
/// <summary>
/// 调试器版本号
/// </summary>
public const string DebuggerVersion = "2026.1.20";
public const string DebuggerVersion = "1.0";
/// <summary>
/// Player 向 Editor 发送消息的标识符

View File

@@ -21,6 +21,10 @@ namespace YooAsset
#endif
private static MockEditorConnection _instance;
/// <summary>
/// 获取单例实例
/// </summary>
public static MockEditorConnection Instance
{
get
@@ -33,10 +37,19 @@ namespace YooAsset
private readonly Dictionary<Guid, UnityAction<MessageEventArgs>> _messageHandlers = new Dictionary<Guid, UnityAction<MessageEventArgs>>();
/// <summary>
/// 初始化连接,清空所有已注册的消息处理器
/// </summary>
public void Initialize()
{
_messageHandlers.Clear();
}
/// <summary>
/// 注册消息处理回调
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="callback">收到消息时的回调函数</param>
public void Register(Guid messageID, UnityAction<MessageEventArgs> callback)
{
if (messageID == Guid.Empty)
@@ -45,11 +58,22 @@ namespace YooAsset
if (_messageHandlers.ContainsKey(messageID) == false)
_messageHandlers.Add(messageID, callback);
}
/// <summary>
/// 注销消息处理回调
/// </summary>
/// <param name="messageID">消息标识符</param>
public void Unregister(Guid messageID)
{
if (_messageHandlers.ContainsKey(messageID))
_messageHandlers.Remove(messageID);
}
/// <summary>
/// 向 Player 端发送消息
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="data">消息数据</param>
public void Send(Guid messageID, byte[] data)
{
if (messageID == Guid.Empty)
@@ -59,6 +83,11 @@ namespace YooAsset
MockPlayerConnection.Instance.HandleEditorMessage(messageID, data);
}
/// <summary>
/// 处理来自 Player 端的消息
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="data">消息数据</param>
internal void HandlePlayerMessage(Guid messageID, byte[] data)
{
if (_messageHandlers.TryGetValue(messageID, out UnityAction<MessageEventArgs> value))

View File

@@ -21,6 +21,10 @@ namespace YooAsset
#endif
private static MockPlayerConnection _instance;
/// <summary>
/// 获取单例实例
/// </summary>
public static MockPlayerConnection Instance
{
get
@@ -33,10 +37,19 @@ namespace YooAsset
private readonly Dictionary<Guid, UnityAction<MessageEventArgs>> _messageHandlers = new Dictionary<Guid, UnityAction<MessageEventArgs>>();
/// <summary>
/// 初始化连接,清空所有已注册的消息处理器
/// </summary>
public void Initialize()
{
_messageHandlers.Clear();
}
/// <summary>
/// 注册消息处理回调
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="callback">收到消息时的回调函数</param>
public void Register(Guid messageID, UnityAction<MessageEventArgs> callback)
{
if (messageID == Guid.Empty)
@@ -45,11 +58,22 @@ namespace YooAsset
if (_messageHandlers.ContainsKey(messageID) == false)
_messageHandlers.Add(messageID, callback);
}
/// <summary>
/// 注销消息处理回调
/// </summary>
/// <param name="messageID">消息标识符</param>
public void Unregister(Guid messageID)
{
if (_messageHandlers.ContainsKey(messageID))
_messageHandlers.Remove(messageID);
}
/// <summary>
/// 向 Editor 端发送消息
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="data">消息数据</param>
public void Send(Guid messageID, byte[] data)
{
if (messageID == Guid.Empty)
@@ -59,6 +83,11 @@ namespace YooAsset
MockEditorConnection.Instance.HandlePlayerMessage(messageID, data);
}
/// <summary>
/// 处理来自 Editor 端的消息
/// </summary>
/// <param name="messageID">消息标识符</param>
/// <param name="data">消息数据</param>
internal void HandleEditorMessage(Guid messageID, byte[] data)
{
if (_messageHandlers.TryGetValue(messageID, out UnityAction<MessageEventArgs> value))

View File

@@ -3,12 +3,8 @@ using System.Collections.Generic;
namespace YooAsset
{
/// <summary>
/// 网络请求失败计数器(诊断用)
/// 网络请求失败计数器
/// </summary>
/// <remarks>
/// 线程安全:内部使用 Dictionary 且未加锁,约定只在 Unity 主线程调用。
/// 如需在多线程/回调线程调用,请在外层加锁或改为并发容器实现。
/// </remarks>
internal class DownloadFailureCounter
{
#if UNITY_EDITOR
@@ -20,7 +16,7 @@ namespace YooAsset
#endif
/// <summary>
/// 失败计数记录表key = $"{packageName}_{eventName}"
/// 失败计数记录表
/// </summary>
private static readonly Dictionary<string, int> _failureRecords = new Dictionary<string, int>(1000);

View File

@@ -5,10 +5,6 @@ namespace YooAsset
/// <summary>
/// 下载后台接口
/// </summary>
/// <remarks>
/// 不同网络库UnityWebRequest / BestHTTP / 自研)实现该接口,用于创建具体下载请求。
/// 每个后台实例是独立的,不共享全局状态。
/// </remarks>
internal interface IDownloadBackend : IDisposable
{
/// <summary>

View File

@@ -5,9 +5,6 @@ namespace YooAsset
/// <summary>
/// 可轮询的下载请求接口
/// </summary>
/// <remarks>
/// 上层通常会在每帧轮询 Status IsDone并在完成后读取结果。
/// </remarks>
internal interface IDownloadRequest : IDisposable
{
/// <summary>

View File

@@ -6,7 +6,7 @@ DownloadSystem 是 YooAsset 资源管理系统的**底层网络下载层**,负
### 可见性说明
DownloadSystem 属于 YooAsset Runtime 的内部基础模块,目录内大多数类型为 `internal`(仅供 YooAsset Runtime 内部程序集使用)。本文示例以模块内部调用方式展示;业务层建议优先通过 `ResourcePackage / FileSystem / ResourceManager` 等上层接口使用下载能力,避免直接依赖本模块的内部类型。
DownloadSystem 属于 YooAsset Runtime 的内部基础模块,目录内大多数类型为 `internal`(仅供 YooAsset Runtime 内部程序集使用)。本文示例以"模块内部调用方式"展示;业务层建议优先通过 `ResourcePackage / FileSystem / ResourceManager` 等上层接口使用下载能力,避免直接依赖本模块的内部类型。
### 核心职责
@@ -20,7 +20,7 @@ DownloadSystem 属于 YooAsset Runtime 的内部基础模块,目录内大多
## 边界与上层协作
DownloadSystem 的职责是提供可替换后端 + 统一请求接口 + 轮询式生命周期的基础能力:
DownloadSystem 的职责是提供"可替换后端 + 统一请求接口 + 轮询式生命周期"的基础能力:
- **本模块不负责并发队列/限流调度**:并发通常由上层同时创建多个 request 并自行控制并发数。
- **本模块不负责重试/回退策略**:失败后的重试、切换 CDN、降级等策略通常由上层系统实现。
@@ -83,22 +83,23 @@ DownloadSystem/
│ ├── IDownloadBackend.cs # 后端接口(工厂模式)
│ └── IDownloadRequest.cs # 请求接口层次结构
├── DefaultDownloadBackend/ # 默认后端实现
├── UnityWebBackend/ # 默认后端实现
│ ├── UnityWebRequestBackend.cs # UnityWebRequest 后端
│ └── UnityWebRequestCreator.cs # UnityWebRequest 创建委托
├── DefaultDownloadRequest/ # 默认请求实现
│ ├── UnityWebRequestDownloaderBase.cs # 基础下载器(抽象类)
│ ├── UnityWebRequestFileDownloader.cs # 文件下载器
│ ├── UnityWebRequestHeadDownloader.cs # HEAD 请求器
│ ├── UnityWebRequestBytesDownloader.cs # 字节下载器
│ ├── UnityWebRequestTextDownloader.cs # 文本下载器
│ ├── UnityWebRequestAssetBundleDownloader.cs # AssetBundle 下载器
│ └── VirtualFileDownloader.cs # 模拟下载器(编辑器用)
├── UnityWebRequest/ # 默认请求实现
│ ├── UnityWebRequestBase.cs # 基础下载器(抽象类)
│ ├── UnityWebRequestFile.cs # 文件下载器
│ ├── UnityWebRequestHead.cs # HEAD 请求器
│ ├── UnityWebRequestBytes.cs # 字节下载器
│ ├── UnityWebRequestText.cs # 文本下载器
│ ├── UnityWebRequestAssetBundle.cs # AssetBundle 下载器
│ └── SimulateRequestFile.cs # 模拟下载器(编辑器用)
├── DownloadSystemDefine.cs # 枚举、结构体定义
├── DownloadSystemHelper.cs # 工具函数
── WebRequestCounter.cs # 请求失败计数器
├── EDownloadRequestStatus.cs # 下载请求状态枚举
├── DownloadRequestArgs.cs # 请求参数结构体定义
── DownloadSystemTools.cs # 工具函数
└── DownloadFailureCounter.cs # 请求失败计数器
```
---
@@ -197,12 +198,12 @@ internal struct DownloadFileRequestArgs
{
public readonly string URL; // 请求地址
public readonly int Timeout; // 响应超时0=不应用超时
public readonly int WatchdogTime; // 看门狗超时0=禁用
public readonly int WatchdogTimeout; // 看门狗超时0=禁用
public readonly string SavePath; // 文件保存路径
public readonly bool AppendToFile; // 追加写入(断点续传)
public readonly bool RemoveFileOnAbort; // 中止时删除文件
public readonly long ResumeFromBytes; // 断点续传起始位置(>0 时推荐由后端设置 Range 头)
public readonly long ResumeOffset; // 断点续传起始位置(>0 时推荐由后端设置 Range 头)
public Dictionary<string, string> Headers; // 自定义请求头(可选)
@@ -210,10 +211,10 @@ internal struct DownloadFileRequestArgs
string url,
string savePath,
int timeout,
int watchdogTime,
int watchdogTimeout,
bool appendToFile = false,
bool removeFileOnAbort = true,
long resumeFromBytes = 0);
long resumeOffset = 0);
/// <summary>
/// 添加请求头(注意:相同 key 重复添加会抛异常)
@@ -229,10 +230,10 @@ internal struct DownloadDataRequestArgs
{
public readonly string URL; // 请求地址
public readonly int Timeout; // 响应超时0=不应用超时
public readonly int WatchdogTime; // 看门狗超时0=禁用
public readonly int WatchdogTimeout; // 看门狗超时0=禁用
public Dictionary<string, string> Headers; // 自定义请求头(可选)
public DownloadDataRequestArgs(string url, int timeout, int watchdogTime);
public DownloadDataRequestArgs(string url, int timeout, int watchdogTimeout);
/// <summary>
/// 添加请求头(注意:相同 key 重复添加会抛异常)
@@ -248,7 +249,7 @@ internal struct DownloadAssetBundleRequestArgs
{
public readonly string URL; // 请求地址
public readonly int Timeout; // 响应超时0=不应用超时
public readonly int WatchdogTime; // 看门狗超时0=禁用
public readonly int WatchdogTimeout; // 看门狗超时0=禁用
public readonly bool DisableUnityWebCache; // 禁用 Unity 缓存(默认 true
public readonly string FileHash; // 文件哈希(缓存启用时需要,且不能为空)
@@ -259,7 +260,7 @@ internal struct DownloadAssetBundleRequestArgs
public DownloadAssetBundleRequestArgs(
string url,
int timeout,
int watchdogTime,
int watchdogTimeout,
bool disableUnityWebCache = true,
string fileHash = null,
uint unityCrc = 0);
@@ -284,19 +285,6 @@ internal struct DownloadSimulateRequestArgs
}
```
#### ImportBundleInfo导入资源包信息
> 说明:该结构体位于 `DownloadSystemDefine.cs`,主要用于资源包导入(离线文件导入)相关流程,并不参与具体的网络请求参数配置。
```csharp
public struct ImportBundleInfo
{
public string FilePath; // 本地文件路径
public string BundleName; // 资源包名称
public string BundleGUID; // 资源包GUID
}
```
---
## 核心类说明
@@ -321,7 +309,7 @@ UnityWebRequestCreator creator = (url, method) =>
IDownloadBackend backend = new UnityWebRequestBackend(creator);
```
### UnityWebRequestDownloaderBase
### UnityWebRequestBase
抽象基类,封装所有下载器的通用逻辑。
@@ -343,12 +331,12 @@ None ──► SendRequest() ──► Running ──► PollingRequest() ──
| 下载器 | 实现接口 | 使用场景 |
|--------|----------|----------|
| `UnityWebRequestFileDownloader` | `IDownloadFileRequest` | 大文件下载到本地 |
| `UnityWebRequestHeadDownloader` | `IDownloadHeadRequest` | 检查资源信息 |
| `UnityWebRequestBytesDownloader` | `IDownloadBytesRequest` | 小文件内存加载 |
| `UnityWebRequestTextDownloader` | `IDownloadTextRequest` | 文本文件下载 |
| `UnityWebRequestAssetBundleDownloader` | `IDownloadAssetBundleRequest` | AB 包下载加载 |
| `VirtualFileDownloader` | `IDownloadFileRequest` | 编辑器模拟下载 |
| `UnityWebRequestFile` | `IDownloadFileRequest` | 大文件下载到本地 |
| `UnityWebRequestHead` | `IDownloadHeadRequest` | 检查资源信息 |
| `UnityWebRequestBytes` | `IDownloadBytesRequest` | 小文件内存加载 |
| `UnityWebRequestText` | `IDownloadTextRequest` | 文本文件下载 |
| `UnityWebRequestAssetBundle` | `IDownloadAssetBundleRequest` | AB 包下载加载 |
| `SimulateRequestFile` | `IDownloadFileRequest` | 编辑器模拟下载 |
---
@@ -366,7 +354,7 @@ try
url: "https://example.com/file.zip",
savePath: "/path/to/save/file.zip",
timeout: 30,
watchdogTime: 0);
watchdogTimeout: 0);
request = backend.CreateFileRequest(args);
// 2. 发起并轮询
@@ -401,10 +389,10 @@ var args = new DownloadFileRequestArgs(
url: url,
savePath: savePath,
timeout: 30,
watchdogTime: 0,
appendToFile: true, // 追加写入
removeFileOnAbort: false, // 中止时保留文件
resumeFromBytes: existingFileSize); // 断点位置
watchdogTimeout: 0,
appendToFile: true, // 追加写入
removeFileOnAbort: false, // 中止时保留文件
resumeOffset: existingFileSize); // 断点位置
IDownloadFileRequest request = backend.CreateFileRequest(args);
request.SendRequest();
@@ -418,7 +406,7 @@ var args = new DownloadFileRequestArgs(
url: url,
savePath: path,
timeout: 30,
watchdogTime: 30); // 30秒无数据自动中止
watchdogTimeout: 30); // 30秒无数据自动中止
IDownloadFileRequest request = backend.CreateFileRequest(args);
request.SendRequest();
@@ -441,7 +429,7 @@ if (request.Status == EDownloadRequestStatus.Aborted)
var args = new DownloadDataRequestArgs(
url: "https://example.com/file.zip",
timeout: 30,
watchdogTime: 0);
watchdogTimeout: 0);
IDownloadHeadRequest request = backend.CreateHeadRequest(args);
request.SendRequest();
@@ -467,7 +455,7 @@ if (request.Status == EDownloadRequestStatus.Succeed)
var args = new DownloadDataRequestArgs(
url: "https://example.com/data.json",
timeout: 30,
watchdogTime: 0);
watchdogTimeout: 0);
IDownloadBytesRequest request = backend.CreateBytesRequest(args);
request.SendRequest();
@@ -540,7 +528,7 @@ IDownloadBackend (接口)
└── 未收到数据 ──► 计时器累加
└── 超过 WatchdogTime ──► AbortRequest()
└── 超过 WatchdogTimeout ──► AbortRequest()
```
---
@@ -556,31 +544,31 @@ IDownloadRequest (基础接口)
├── IDownloadTextRequest (文本下载)
└── IDownloadAssetBundleRequest (AssetBundle 下载)
UnityWebRequestDownloaderBase (抽象基类)
UnityWebRequestBase (抽象基类)
├── UnityWebRequestFileDownloader ──► IDownloadFileRequest
├── UnityWebRequestHeadDownloader ──► IDownloadHeadRequest
├── UnityWebRequestBytesDownloader ──► IDownloadBytesRequest
├── UnityWebRequestTextDownloader ──► IDownloadTextRequest
└── UnityWebRequestAssetBundleDownloader ──► IDownloadAssetBundleRequest
├── UnityWebRequestFile ──► IDownloadFileRequest
├── UnityWebRequestHead ──► IDownloadHeadRequest
├── UnityWebRequestBytes ──► IDownloadBytesRequest
├── UnityWebRequestText ──► IDownloadTextRequest
└── UnityWebRequestAssetBundle ──► IDownloadAssetBundleRequest
VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
SimulateRequestFile (独立实现) ──► IDownloadFileRequest
```
---
## 工具类
### DownloadSystemHelper
### DownloadSystemTools
提供跨平台的工具函数:
| 方法 | 说明 |
|------|------|
| `ConvertToWWWPath()` | 转换本地路径为 WWW 协议 URL |
| `IsRequestLocalFile()` | 判断是否本地文件请求 |
| `ToLocalURL()` | 转换本地路径为文件协议 URL |
| `IsLocalFileURL()` | 判断是否本地文件 URL |
### WebRequestCounter
### DownloadFailureCounter
请求失败计数器,用于诊断统计:
@@ -590,10 +578,10 @@ VirtualFileDownloader (独立实现) ──► IDownloadFileRequest
```csharp
// 记录失败
WebRequestCounter.RecordRequestFailed(packageName, eventName);
DownloadFailureCounter.RecordFailure(packageName, eventName);
// 查询失败次数
int count = WebRequestCounter.GetRequestFailedCount(packageName, eventName);
int count = DownloadFailureCounter.GetFailureCount(packageName, eventName);
```
---
@@ -611,4 +599,4 @@ int count = WebRequestCounter.GetRequestFailedCount(packageName, eventName);
5. **驱动更新**:部分第三方网络库实现的 backend 可能需要每帧调用 `IDownloadBackend.Update()` 进行驱动
6. **中止语义**`Aborted` 可能来自用户主动 `AbortRequest()` 或看门狗超时;中止场景下 `HttpCode/Error` 可能为默认值(例如 0/空)
7. **线程安全**:所有下载请求的创建和轮询应在主线程进行
8. **模拟下载器**`VirtualFileDownloader` 仅用于模拟进度,不会落盘,且 `SavePath` 始终为 `null`;成功时 `HttpCode` 固定为 `200`
8. **模拟下载器**`SimulateRequestFile` 仅用于模拟进度,不会落盘,且 `SavePath` 始终为 `null`;成功时 `HttpCode` 固定为 `200`

View File

@@ -221,7 +221,7 @@ namespace YooAsset
}
/// <summary>
/// 应用通用请求参数
/// 配置通用请求参数
/// </summary>
protected void ConfigureRequest(int timeout, int watchdogTimeout, Dictionary<string, string> headers)
{

View File

@@ -327,12 +327,12 @@ namespace YooAsset
/// <summary>
/// 出生的场景
/// </summary>
public string SpawnScene = string.Empty;
public string OriginScene = string.Empty;
[Conditional("DEBUG")]
public void InitProviderDebugInfo()
{
SpawnScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
OriginScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name;
}
/// <summary>

View File

@@ -425,7 +425,7 @@ namespace YooAsset
{
DiagnosticProviderInfo providerInfo = new DiagnosticProviderInfo();
providerInfo.AssetPath = provider.MainAssetInfo.AssetPath;
providerInfo.OriginScene = provider.SpawnScene;
providerInfo.OriginScene = provider.OriginScene;
providerInfo.StartTime = provider.BeginTime;
providerInfo.ElapsedMS = provider.ProcessTime;
providerInfo.ReferenceCount = provider.RefCount;