From d6c9de0e43b560ad35da00c0d1e271d51d4bc5ef Mon Sep 17 00:00:00 2001 From: hevinci Date: Mon, 25 Sep 2023 11:38:01 +0800 Subject: [PATCH] fix #169 --- .../Runtime/DownloadSystem/RequestHelper.cs | 36 +++++++++++++++++++ .../DownloadSystem/RequestHelper.cs.meta | 11 ++++++ .../Internal/DownloadManifestOperation.cs | 14 ++++---- .../Internal/LoadRemoteManifestOperation.cs | 11 +++--- .../QueryRemotePackageHashOperation.cs | 11 +++--- .../QueryRemotePackageVersionOperation.cs | 11 +++--- 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs create mode 100644 Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs.meta diff --git a/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs new file mode 100644 index 0000000..22f4652 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace YooAsset +{ + public class RequestHelper + { + /// + /// 记录网络请求失败事件的次数 + /// + private static readonly Dictionary _requestFailedRecorder = new Dictionary(1000); + + /// + /// 记录请求失败事件 + /// + public static void RecordRequestFailed(string packageName, string eventName) + { + string key = $"{packageName}_{eventName}"; + if (_requestFailedRecorder.ContainsKey(key) == false) + _requestFailedRecorder.Add(key, 0); + _requestFailedRecorder[key]++; + } + + /// + /// 获取请求失败的次数 + /// + public static int GetRequestFailedCount(string packageName, string eventName) + { + string key = $"{packageName}_{eventName}"; + if (_requestFailedRecorder.ContainsKey(key) == false) + _requestFailedRecorder.Add(key, 0); + return _requestFailedRecorder[key]; + } + } +} \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs.meta b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs.meta new file mode 100644 index 0000000..394f817 --- /dev/null +++ b/Assets/YooAsset/Runtime/DownloadSystem/RequestHelper.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ca9b2c6456d21bb4e9eecd9dc820a641 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/DownloadManifestOperation.cs b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/DownloadManifestOperation.cs index 294dec9..60505c8 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/DownloadManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/DownloadManifestOperation.cs @@ -10,8 +10,7 @@ namespace YooAsset DownloadManifestFile, Done, } - - private static int RequestCount = 0; + private readonly IRemoteServices _remoteServices; private readonly string _packageName; private readonly string _packageVersion; @@ -19,6 +18,7 @@ namespace YooAsset private UnityWebFileRequester _downloader1; private UnityWebFileRequester _downloader2; private ESteps _steps = ESteps.None; + private int _requestCount = 0; internal DownloadManifestOperation(IRemoteServices remoteServices, string packageName, string packageVersion, int timeout) { @@ -29,7 +29,7 @@ namespace YooAsset } internal override void Start() { - RequestCount++; + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(DownloadManifestOperation)); _steps = ESteps.DownloadPackageHashFile; } internal override void Update() @@ -58,6 +58,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloader1.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(DownloadManifestOperation)); } else { @@ -88,6 +89,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloader2.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(DownloadManifestOperation)); } else { @@ -102,10 +104,10 @@ namespace YooAsset private string GetDownloadRequestURL(string fileName) { // 轮流返回请求地址 - if (RequestCount % 2 == 0) - return _remoteServices.GetRemoteFallbackURL(fileName); - else + if (_requestCount % 2 == 0) return _remoteServices.GetRemoteMainURL(fileName); + else + return _remoteServices.GetRemoteFallbackURL(fileName); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/LoadRemoteManifestOperation.cs b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/LoadRemoteManifestOperation.cs index 3344b95..4fadd50 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/LoadRemoteManifestOperation.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/LoadRemoteManifestOperation.cs @@ -13,7 +13,6 @@ namespace YooAsset Done, } - private static int RequestCount = 0; private readonly IRemoteServices _remoteServices; private readonly string _packageName; private readonly string _packageVersion; @@ -23,6 +22,7 @@ namespace YooAsset private DeserializeManifestOperation _deserializer; private byte[] _fileData; private ESteps _steps = ESteps.None; + private int _requestCount = 0; /// /// 加载的清单实例 @@ -39,7 +39,7 @@ namespace YooAsset } internal override void Start() { - RequestCount++; + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(LoadRemoteManifestOperation)); _steps = ESteps.DownloadPackageHashFile; } internal override void Update() @@ -90,6 +90,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(LoadRemoteManifestOperation)); } else { @@ -141,10 +142,10 @@ namespace YooAsset private string GetDownloadRequestURL(string fileName) { // 轮流返回请求地址 - if (RequestCount % 2 == 0) - return _remoteServices.GetRemoteFallbackURL(fileName); - else + if (_requestCount % 2 == 0) return _remoteServices.GetRemoteMainURL(fileName); + else + return _remoteServices.GetRemoteFallbackURL(fileName); } } } \ No newline at end of file diff --git a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageHashOperation.cs b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageHashOperation.cs index e885358..41ab26a 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageHashOperation.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageHashOperation.cs @@ -10,13 +10,13 @@ namespace YooAsset Done, } - private static int RequestCount = 0; private readonly IRemoteServices _remoteServices; private readonly string _packageName; private readonly string _packageVersion; private readonly int _timeout; private UnityWebDataRequester _downloader; private ESteps _steps = ESteps.None; + private int _requestCount = 0; /// /// 包裹哈希值 @@ -33,7 +33,7 @@ namespace YooAsset } internal override void Start() { - RequestCount++; + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageHashOperation)); _steps = ESteps.DownloadPackageHash; } internal override void Update() @@ -62,6 +62,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageHashOperation)); } else { @@ -88,10 +89,10 @@ namespace YooAsset string url; // 轮流返回请求地址 - if (RequestCount % 2 == 0) - url = _remoteServices.GetRemoteFallbackURL(fileName); - else + if (_requestCount % 2 == 0) url = _remoteServices.GetRemoteMainURL(fileName); + else + url = _remoteServices.GetRemoteFallbackURL(fileName); return url; } diff --git a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs index 7fbb043..c64deb5 100644 --- a/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs +++ b/Assets/YooAsset/Runtime/PackageSystem/Operations/Internal/QueryRemotePackageVersionOperation.cs @@ -10,13 +10,13 @@ namespace YooAsset Done, } - private static int RequestCount = 0; private readonly IRemoteServices _remoteServices; private readonly string _packageName; private readonly bool _appendTimeTicks; private readonly int _timeout; private UnityWebDataRequester _downloader; private ESteps _steps = ESteps.None; + private int _requestCount = 0; /// /// 包裹版本 @@ -33,7 +33,7 @@ namespace YooAsset } internal override void Start() { - RequestCount++; + _requestCount = RequestHelper.GetRequestFailedCount(_packageName, nameof(QueryRemotePackageVersionOperation)); _steps = ESteps.DownloadPackageVersion; } internal override void Update() @@ -62,6 +62,7 @@ namespace YooAsset _steps = ESteps.Done; Status = EOperationStatus.Failed; Error = _downloader.GetError(); + RequestHelper.RecordRequestFailed(_packageName, nameof(QueryRemotePackageVersionOperation)); } else { @@ -88,10 +89,10 @@ namespace YooAsset string url; // 轮流返回请求地址 - if (RequestCount % 2 == 0) - url = _remoteServices.GetRemoteFallbackURL(fileName); - else + if (_requestCount % 2 == 0) url = _remoteServices.GetRemoteMainURL(fileName); + else + url = _remoteServices.GetRemoteFallbackURL(fileName); // 在URL末尾添加时间戳 if (_appendTimeTicks)