mirror of
https://github.com/tuyoogame/YooAsset.git
synced 2026-05-25 10:11:51 +00:00
refactor : 重构代码
This commit is contained in:
@@ -21,7 +21,7 @@ namespace YooAsset
|
||||
public int ReferenceCount;
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态
|
||||
/// 资源包的加载状态
|
||||
/// </summary>
|
||||
public string Status;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace YooAsset
|
||||
public string OperationDesc;
|
||||
|
||||
/// <summary>
|
||||
///异步操作的优先级
|
||||
/// 异步操作的优先级
|
||||
/// </summary>
|
||||
public uint Priority;
|
||||
|
||||
@@ -36,12 +36,12 @@ namespace YooAsset
|
||||
public long ElapsedMS;
|
||||
|
||||
/// <summary>
|
||||
/// 当前进度
|
||||
/// 异步操作的执行进度
|
||||
/// </summary>
|
||||
public float Progress;
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态
|
||||
/// 异步操作的执行状态
|
||||
/// </summary>
|
||||
public string Status;
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace YooAsset
|
||||
public string AssetPath;
|
||||
|
||||
/// <summary>
|
||||
/// 资源出生的场景
|
||||
/// 资源加载时所在的场景
|
||||
/// </summary>
|
||||
public string OriginScene;
|
||||
|
||||
@@ -36,7 +36,7 @@ namespace YooAsset
|
||||
public int ReferenceCount;
|
||||
|
||||
/// <summary>
|
||||
/// 当前状态
|
||||
/// 资源的加载状态
|
||||
/// </summary>
|
||||
public string Status;
|
||||
|
||||
|
||||
@@ -28,16 +28,20 @@ namespace YooAsset
|
||||
public List<DiagnosticPackageData> PackageDataList = new List<DiagnosticPackageData>(10);
|
||||
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// 序列化诊断报告为字节数组
|
||||
/// </summary>
|
||||
/// <param name="report">要序列化的诊断报告</param>
|
||||
/// <returns>UTF-8 编码的 JSON 字节数组</returns>
|
||||
public static byte[] Serialize(DiagnosticReport report)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(report));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// 从字节数组反序列化诊断报告
|
||||
/// </summary>
|
||||
/// <param name="data">UTF-8 编码的 JSON 字节数组</param>
|
||||
/// <returns>反序列化后的诊断报告</returns>
|
||||
public static DiagnosticReport Deserialize(byte[] data)
|
||||
{
|
||||
return JsonUtility.FromJson<DiagnosticReport>(Encoding.UTF8.GetString(data));
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 远程调试命令类型
|
||||
/// </summary>
|
||||
internal enum EDebugCommandType
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -23,16 +23,20 @@ namespace YooAsset
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 序列化
|
||||
/// 序列化命令为字节数组
|
||||
/// </summary>
|
||||
/// <param name="command">要序列化的远程调试命令</param>
|
||||
/// <returns>UTF-8 编码的 JSON 字节数组</returns>
|
||||
public static byte[] Serialize(RemoteDebugCommand command)
|
||||
{
|
||||
return Encoding.UTF8.GetBytes(JsonUtility.ToJson(command));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 反序列化
|
||||
/// 从字节数组反序列化命令
|
||||
/// </summary>
|
||||
/// <param name="data">UTF-8 编码的 JSON 字节数组</param>
|
||||
/// <returns>反序列化后的远程调试命令</returns>
|
||||
public static RemoteDebugCommand Deserialize(byte[] data)
|
||||
{
|
||||
return JsonUtility.FromJson<RemoteDebugCommand>(Encoding.UTF8.GetString(data));
|
||||
|
||||
@@ -3,8 +3,12 @@ using System.Collections.Generic;
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 网络请求失败计数器
|
||||
/// 网络请求失败计数器(诊断用)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 线程安全:内部使用 Dictionary 且未加锁,约定只在 Unity 主线程调用。
|
||||
/// 如需在多线程/回调线程调用,请在外层加锁或改为并发容器实现。
|
||||
/// </remarks>
|
||||
internal class DownloadFailureCounter
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
@@ -18,11 +22,16 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 失败计数记录表
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Key 格式:$"{packageName}_{eventName}"
|
||||
/// </remarks>
|
||||
private static readonly Dictionary<string, int> _failureRecords = new Dictionary<string, int>(1000);
|
||||
|
||||
/// <summary>
|
||||
/// 记录一次失败
|
||||
/// </summary>
|
||||
/// <param name="packageName">资源包名称</param>
|
||||
/// <param name="eventName">事件名称</param>
|
||||
public static void RecordFailure(string packageName, string eventName)
|
||||
{
|
||||
string key = $"{packageName}_{eventName}";
|
||||
@@ -34,6 +43,9 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 获取失败次数
|
||||
/// </summary>
|
||||
/// <param name="packageName">资源包名称</param>
|
||||
/// <param name="eventName">事件名称</param>
|
||||
/// <returns>失败次数,如果未记录过则返回 0</returns>
|
||||
public static int GetFailureCount(string packageName, string eventName)
|
||||
{
|
||||
string key = $"{packageName}_{eventName}";
|
||||
|
||||
@@ -66,11 +66,22 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 自定义请求头(可选)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用 AddRequestHeader 方法添加请求头。
|
||||
/// 注意:相同 key 重复添加会抛出异常。
|
||||
/// </remarks>
|
||||
public Dictionary<string, string> Headers;
|
||||
|
||||
/// <summary>
|
||||
/// 构造文件下载请求参数
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="savePath">文件保存路径</param>
|
||||
/// <param name="timeout">响应超时时间(秒),0 表示不应用超时</param>
|
||||
/// <param name="watchdogTimeout">看门狗超时时间(秒),0 表示禁用</param>
|
||||
/// <param name="appendToFile">是否追加写入文件(用于断点续传)</param>
|
||||
/// <param name="removeFileOnAbort">中止时是否删除目标文件</param>
|
||||
/// <param name="resumeOffset">断点续传的起始字节位置</param>
|
||||
public DownloadFileRequestArgs(
|
||||
string url,
|
||||
string savePath,
|
||||
@@ -139,11 +150,18 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 自定义请求头(可选)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用 AddRequestHeader 方法添加请求头。
|
||||
/// 注意:相同 key 重复添加会抛出异常。
|
||||
/// </remarks>
|
||||
public Dictionary<string, string> Headers;
|
||||
|
||||
/// <summary>
|
||||
/// 构造数据下载请求参数
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="timeout">响应超时时间(秒),0 表示不应用超时</param>
|
||||
/// <param name="watchdogTimeout">看门狗超时时间(秒),0 表示禁用</param>
|
||||
public DownloadDataRequestArgs(string url, int timeout, int watchdogTimeout)
|
||||
{
|
||||
URL = url;
|
||||
@@ -219,11 +237,21 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 自定义请求头(可选)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用 AddRequestHeader 方法添加请求头。
|
||||
/// 注意:相同 key 重复添加会抛出异常。
|
||||
/// </remarks>
|
||||
public Dictionary<string, string> Headers;
|
||||
|
||||
/// <summary>
|
||||
/// 构造 AssetBundle 下载请求参数
|
||||
/// </summary>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="timeout">响应超时时间(秒),0 表示不应用超时</param>
|
||||
/// <param name="watchdogTimeout">看门狗超时时间(秒),0 表示禁用</param>
|
||||
/// <param name="disableUnityWebCache">是否禁用 Unity 内置缓存</param>
|
||||
/// <param name="fileHash">文件哈希(启用缓存时必须提供)</param>
|
||||
/// <param name="unityCrc">Unity CRC 校验值</param>
|
||||
public DownloadAssetBundleRequestArgs(
|
||||
string url,
|
||||
int timeout,
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
|
||||
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 下载系统工具类
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 提供跨平台的 URL 转换和判断功能。
|
||||
/// </remarks>
|
||||
internal class DownloadSystemTools
|
||||
{
|
||||
/// <summary>
|
||||
/// 转换为本地文件请求地址
|
||||
/// </summary>
|
||||
/// <param name="path">本地文件路径</param>
|
||||
/// <returns>可用于 UnityWebRequest 的文件协议 URL</returns>
|
||||
public static string ToLocalURL(string path)
|
||||
{
|
||||
string url;
|
||||
@@ -58,13 +66,15 @@ namespace YooAsset
|
||||
throw new System.NotSupportedException($"Platform '{UnityEngine.Application.platform}' is not supported.");
|
||||
#endif
|
||||
|
||||
// For some special cases when users have special characters in their devices, url paths can not be identified correctly.
|
||||
// 处理特殊字符:用户设备路径可能包含特殊字符导致 URL 无法正确识别
|
||||
return url.Replace("+", "%2B").Replace("#", "%23").Replace("?", "%3F");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 是否请求的本地文件
|
||||
/// 判断是否为本地文件 URL
|
||||
/// </summary>
|
||||
/// <param name="url">要判断的 URL</param>
|
||||
/// <returns>如果是本地文件 URL 返回 true,否则返回 false</returns>
|
||||
public static bool IsLocalFileURL(string url)
|
||||
{
|
||||
//TODO UNITY_STANDALONE_OSX平台目前无法确定
|
||||
|
||||
@@ -29,6 +29,9 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 已中止
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 可能由用户主动调用 AbortRequest() 或看门狗超时触发。
|
||||
/// </remarks>
|
||||
Aborted,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,10 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 下载后台接口
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 不同网络库(UnityWebRequest / BestHTTP / 自研)实现该接口,用于创建具体下载请求。
|
||||
/// 每个后台实例是独立的,不共享全局状态。
|
||||
/// </remarks>
|
||||
internal interface IDownloadBackend : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
|
||||
@@ -5,6 +5,9 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 可轮询的下载请求接口
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 上层通常在每帧检查 IsDone 属性,完成后读取结果并调用 Dispose() 释放资源。
|
||||
/// </remarks>
|
||||
internal interface IDownloadRequest : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
@@ -15,6 +18,9 @@ namespace YooAsset
|
||||
/// <summary>
|
||||
/// 是否完成(成功/失败/中止)
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 注意:访问此属性时会自动调用 PollingRequest() 进行轮询。
|
||||
/// </remarks>
|
||||
bool IsDone { get; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -3,7 +3,14 @@ using UnityEngine.Networking;
|
||||
namespace YooAsset
|
||||
{
|
||||
/// <summary>
|
||||
/// 自定义下载器的请求委托
|
||||
/// 自定义 UnityWebRequest 创建委托
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 用于自定义 UnityWebRequest 的创建方式,例如添加证书验证、代理设置等。
|
||||
/// 通过 UnityWebRequestBackend 构造函数传入。
|
||||
/// </remarks>
|
||||
/// <param name="url">请求地址</param>
|
||||
/// <param name="method">HTTP 方法(如 GET、HEAD)</param>
|
||||
/// <returns>自定义配置的 UnityWebRequest 实例</returns>
|
||||
public delegate UnityWebRequest UnityWebRequestCreator(string url, string method);
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ namespace YooAsset
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 释放资源
|
||||
/// 清理 WebRequest 资源
|
||||
/// </summary>
|
||||
private void CleanupWebRequest()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user