| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Threading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Networking; |
| | 7 | |
|
| | 8 | | namespace DCL |
| | 9 | | { |
| | 10 | | public class WebRequestController : IWebRequestController |
| | 11 | | { |
| | 12 | | private IWebRequestFactory getWebRequestFactory; |
| | 13 | | private IWebRequestAssetBundleFactory assetBundleFactory; |
| | 14 | | private IWebRequestTextureFactory textureFactory; |
| | 15 | | private IWebRequestAudioFactory audioClipWebRequestFactory; |
| | 16 | | private IPostWebRequestFactory postWebRequestFactory; |
| | 17 | |
|
| 629 | 18 | | private readonly List<WebRequestAsyncOperation> ongoingWebRequests = new(); |
| | 19 | |
|
| 629 | 20 | | private WebRequestController( |
| | 21 | | IWebRequestFactory getWebRequestFactory, |
| | 22 | | IWebRequestAssetBundleFactory assetBundleFactory, |
| | 23 | | IWebRequestTextureFactory textureFactory, |
| | 24 | | IWebRequestAudioFactory audioClipWebRequestFactory, |
| | 25 | | IPostWebRequestFactory postWebRequestFactory |
| | 26 | | ) |
| | 27 | | { |
| 629 | 28 | | this.getWebRequestFactory = getWebRequestFactory; |
| 629 | 29 | | this.assetBundleFactory = assetBundleFactory; |
| 629 | 30 | | this.textureFactory = textureFactory; |
| 629 | 31 | | this.audioClipWebRequestFactory = audioClipWebRequestFactory; |
| 629 | 32 | | this.postWebRequestFactory = postWebRequestFactory; |
| 629 | 33 | | } |
| | 34 | |
|
| 613 | 35 | | public void Initialize() { } |
| | 36 | |
|
| | 37 | | public static WebRequestController Create() |
| | 38 | | { |
| 629 | 39 | | WebRequestController newWebRequestController = new WebRequestController( |
| | 40 | | new GetWebRequestFactory(), |
| | 41 | | new WebRequestAssetBundleFactory(), |
| | 42 | | new WebRequestTextureFactory(), |
| | 43 | | new WebRequestAudioFactory(), |
| | 44 | | new PostWebRequestFactory() |
| | 45 | | ); |
| | 46 | |
|
| 629 | 47 | | return newWebRequestController; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public void Initialize( |
| | 51 | | IWebRequestFactory genericWebRequest, |
| | 52 | | IWebRequestAssetBundleFactory assetBundleFactoryWebRequest, |
| | 53 | | IWebRequestTextureFactory textureFactoryWebRequest, |
| | 54 | | IWebRequestAudioFactory audioClipWebRequest) |
| | 55 | | { |
| 0 | 56 | | getWebRequestFactory = genericWebRequest; |
| 0 | 57 | | assetBundleFactory = assetBundleFactoryWebRequest; |
| 0 | 58 | | textureFactory = textureFactoryWebRequest; |
| 0 | 59 | | audioClipWebRequestFactory = audioClipWebRequest; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | public async UniTask<UnityWebRequest> GetAsync( |
| | 63 | | string url, |
| | 64 | | DownloadHandler downloadHandler = null, |
| | 65 | | Action<UnityWebRequest> onSuccess = null, |
| | 66 | | Action<UnityWebRequest> onfail = null, |
| | 67 | | int requestAttemps = 3, |
| | 68 | | int timeout = 0, |
| | 69 | | CancellationToken cancellationToken = default, |
| | 70 | | Dictionary<string, string> headers = null) |
| | 71 | | { |
| 0 | 72 | | return await SendWebRequest(getWebRequestFactory, url, downloadHandler, onSuccess, onfail, requestAttemps, |
| | 73 | | timeout, cancellationToken, headers); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public async UniTask<UnityWebRequest> PostAsync( |
| | 77 | | string url, |
| | 78 | | string postData, |
| | 79 | | DownloadHandler downloadHandler = null, |
| | 80 | | Action<UnityWebRequest> onSuccess = null, |
| | 81 | | Action<UnityWebRequest> onfail = null, |
| | 82 | | int requestAttemps = 3, |
| | 83 | | int timeout = 0, |
| | 84 | | CancellationToken cancellationToken = default, |
| | 85 | | Dictionary<string, string> headers = null) |
| | 86 | | { |
| 0 | 87 | | postWebRequestFactory.SetBody(postData); |
| 0 | 88 | | return await SendWebRequest(postWebRequestFactory, url, downloadHandler, onSuccess, onfail, requestAttemps, |
| | 89 | | timeout, cancellationToken, headers); |
| 0 | 90 | | } |
| | 91 | |
|
| | 92 | | public async UniTask<UnityWebRequest> GetAssetBundleAsync( |
| | 93 | | string url, |
| | 94 | | Action<UnityWebRequest> onSuccess = null, |
| | 95 | | Action<UnityWebRequest> onfail = null, |
| | 96 | | int requestAttemps = 3, |
| | 97 | | int timeout = 0, |
| | 98 | | CancellationToken cancellationToken = default) |
| | 99 | | { |
| 0 | 100 | | return await SendWebRequest(assetBundleFactory, url, null, onSuccess, onfail, requestAttemps, |
| | 101 | | timeout, cancellationToken); |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | public async UniTask<UnityWebRequest> GetAssetBundleAsync( |
| | 105 | | string url, |
| | 106 | | Hash128 hash, |
| | 107 | | Action<UnityWebRequest> onSuccess = null, |
| | 108 | | Action<UnityWebRequest> onfail = null, |
| | 109 | | int requestAttemps = 3, |
| | 110 | | int timeout = 0, |
| | 111 | | CancellationToken cancellationToken = default) |
| | 112 | | { |
| 0 | 113 | | assetBundleFactory.SetHash(hash); |
| 0 | 114 | | return await SendWebRequest(assetBundleFactory, url, null, onSuccess, onfail, requestAttemps, |
| | 115 | | timeout, cancellationToken); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | public async UniTask<UnityWebRequest> GetTextureAsync( |
| | 119 | | string url, |
| | 120 | | Action<UnityWebRequest> onSuccess = null, |
| | 121 | | Action<UnityWebRequest> onfail = null, |
| | 122 | | int requestAttemps = 3, |
| | 123 | | int timeout = 0, |
| | 124 | | bool isReadable = true, |
| | 125 | | CancellationToken cancellationToken = default, |
| | 126 | | Dictionary<string, string> headers = null) |
| | 127 | | { |
| 0 | 128 | | textureFactory.isReadable = isReadable; |
| 0 | 129 | | return await SendWebRequest(textureFactory, url, null, onSuccess, onfail, requestAttemps, |
| | 130 | | timeout, cancellationToken, headers); |
| 0 | 131 | | } |
| | 132 | |
|
| | 133 | | public async UniTask<UnityWebRequest> GetAudioClipAsync( |
| | 134 | | string url, |
| | 135 | | AudioType audioType, |
| | 136 | | Action<UnityWebRequest> onSuccess = null, |
| | 137 | | Action<UnityWebRequest> onfail = null, |
| | 138 | | int requestAttemps = 3, |
| | 139 | | int timeout = 0, |
| | 140 | | CancellationToken cancellationToken = default) |
| | 141 | | { |
| 0 | 142 | | audioClipWebRequestFactory.SetAudioType(audioType); |
| 0 | 143 | | return await SendWebRequest(audioClipWebRequestFactory, url, null, onSuccess, onfail, requestAttemps, |
| | 144 | | timeout, cancellationToken); |
| 0 | 145 | | } |
| | 146 | |
|
| | 147 | |
|
| | 148 | | private async UniTask<UnityWebRequest> SendWebRequest<T>( |
| | 149 | | T requestFactory, |
| | 150 | | string url, |
| | 151 | | DownloadHandler downloadHandler, |
| | 152 | | Action<UnityWebRequest> onSuccess, |
| | 153 | | Action<UnityWebRequest> onFail, |
| | 154 | | int requestAttemps, |
| | 155 | | int timeout, |
| | 156 | | CancellationToken cancellationToken, |
| | 157 | | Dictionary<string, string> headers = null) where T : IWebRequestFactory |
| | 158 | | { |
| | 159 | | requestAttemps = Mathf.Max(1, requestAttemps); |
| | 160 | | for (int i = requestAttemps - 1; i >= 0; i--) |
| | 161 | | { |
| | 162 | | UnityWebRequest request = requestFactory.CreateWebRequest(url); |
| | 163 | | request.timeout = timeout; |
| | 164 | |
|
| | 165 | | if (headers != null) |
| | 166 | | { |
| | 167 | | foreach (var item in headers) |
| | 168 | | request.SetRequestHeader(item.Key, item.Value); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | if (downloadHandler != null) |
| | 172 | | request.downloadHandler = downloadHandler; |
| | 173 | |
|
| | 174 | | UnityWebRequestAsyncOperation asyncOp = request.SendWebRequest(); |
| | 175 | | await asyncOp.WithCancellation(cancellationToken); |
| | 176 | |
|
| | 177 | | if (request.WebRequestSucceded()) |
| | 178 | | { |
| | 179 | | onSuccess?.Invoke(asyncOp.webRequest); |
| | 180 | | return asyncOp.webRequest; |
| | 181 | | } |
| | 182 | | else if (!request.WebRequestAborted() && request.WebRequestServerError()) |
| | 183 | | { |
| | 184 | | if (requestAttemps > 0) |
| | 185 | | continue; |
| | 186 | | else |
| | 187 | | { |
| | 188 | | onFail?.Invoke(asyncOp.webRequest); |
| | 189 | | return asyncOp.webRequest; |
| | 190 | | } |
| | 191 | | } |
| | 192 | | else |
| | 193 | | { |
| | 194 | | onFail?.Invoke(asyncOp.webRequest); |
| | 195 | | return asyncOp.webRequest; |
| | 196 | | } |
| | 197 | | } |
| | 198 | |
|
| | 199 | | throw new Exception("WebRequestController SendWebRequest: Unexpected error"); |
| | 200 | | } |
| | 201 | |
|
| | 202 | | [Obsolete] |
| | 203 | | public IWebRequestAsyncOperation Get( |
| | 204 | | string url, |
| | 205 | | DownloadHandler downloadHandler = null, |
| | 206 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 207 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 208 | | int requestAttemps = 3, |
| | 209 | | int timeout = 0, |
| | 210 | | bool disposeOnCompleted = true, |
| | 211 | | Dictionary<string, string> headers = null) |
| | 212 | | { |
| 176 | 213 | | return SendWebRequest(getWebRequestFactory, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeout |
| | 214 | | } |
| | 215 | |
|
| | 216 | | [Obsolete] |
| | 217 | | public IWebRequestAsyncOperation Post( |
| | 218 | | string url, |
| | 219 | | string postData, |
| | 220 | | DownloadHandler downloadHandler = null, |
| | 221 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 222 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 223 | | int requestAttemps = 3, |
| | 224 | | int timeout = 0, |
| | 225 | | bool disposeOnCompleted = true, |
| | 226 | | Dictionary<string, string> headers = null) |
| | 227 | | { |
| 0 | 228 | | postWebRequestFactory.SetBody(postData); |
| 0 | 229 | | return SendWebRequest(postWebRequestFactory, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeou |
| | 230 | | } |
| | 231 | |
|
| | 232 | | [Obsolete] |
| | 233 | | public IWebRequestAsyncOperation GetAssetBundle( |
| | 234 | | string url, |
| | 235 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 236 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 237 | | int requestAttemps = 3, |
| | 238 | | int timeout = 0, |
| | 239 | | bool disposeOnCompleted = true) |
| | 240 | | { |
| 29 | 241 | | return SendWebRequest(assetBundleFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCo |
| | 242 | | } |
| | 243 | |
|
| | 244 | | [Obsolete] |
| | 245 | | public IWebRequestAsyncOperation GetAssetBundle( |
| | 246 | | string url, |
| | 247 | | Hash128 hash, |
| | 248 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 249 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 250 | | int requestAttemps = 3, |
| | 251 | | int timeout = 0, |
| | 252 | | bool disposeOnCompleted = true) |
| | 253 | | { |
| 0 | 254 | | assetBundleFactory.SetHash(hash); |
| 0 | 255 | | return SendWebRequest(assetBundleFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCo |
| | 256 | | } |
| | 257 | |
|
| | 258 | | [Obsolete] |
| | 259 | | public IWebRequestAsyncOperation GetTexture( |
| | 260 | | string url, |
| | 261 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 262 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 263 | | int requestAttemps = 3, |
| | 264 | | int timeout = 0, |
| | 265 | | bool disposeOnCompleted = true, |
| | 266 | | bool isReadable = true, |
| | 267 | | Dictionary<string, string> headers = null) |
| | 268 | | { |
| 229 | 269 | | textureFactory.isReadable = isReadable; |
| 229 | 270 | | return SendWebRequest(textureFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnComple |
| | 271 | | } |
| | 272 | |
|
| | 273 | | [Obsolete] |
| | 274 | | public IWebRequestAsyncOperation GetAudioClip( |
| | 275 | | string url, |
| | 276 | | AudioType audioType, |
| | 277 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 278 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 279 | | int requestAttemps = 3, |
| | 280 | | int timeout = 0, |
| | 281 | | bool disposeOnCompleted = true) |
| | 282 | | { |
| 38 | 283 | | audioClipWebRequestFactory.SetAudioType(audioType); |
| 38 | 284 | | return SendWebRequest(audioClipWebRequestFactory, url, null, OnSuccess, OnFail, requestAttemps, timeout, dis |
| | 285 | | } |
| | 286 | |
|
| | 287 | | private WebRequestAsyncOperation SendWebRequest<T>( |
| | 288 | | T requestFactory, |
| | 289 | | string url, |
| | 290 | | DownloadHandler downloadHandler, |
| | 291 | | Action<IWebRequestAsyncOperation> OnSuccess, |
| | 292 | | Action<IWebRequestAsyncOperation> OnFail, |
| | 293 | | int requestAttemps, |
| | 294 | | int timeout, |
| | 295 | | bool disposeOnCompleted, |
| | 296 | | Dictionary<string, string> headers = null, |
| | 297 | | WebRequestAsyncOperation asyncOp = null |
| | 298 | | ) where T : IWebRequestFactory |
| | 299 | | { |
| 472 | 300 | | int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps); |
| | 301 | |
|
| 472 | 302 | | UnityWebRequest request = requestFactory.CreateWebRequest(url); |
| 472 | 303 | | request.timeout = timeout; |
| | 304 | |
|
| 472 | 305 | | if (headers != null) |
| | 306 | | { |
| 0 | 307 | | foreach (var item in headers) |
| 0 | 308 | | { request.SetRequestHeader(item.Key, item.Value); } |
| | 309 | | } |
| | 310 | |
|
| 472 | 311 | | if (downloadHandler != null) |
| 163 | 312 | | request.downloadHandler = downloadHandler; |
| | 313 | |
|
| 472 | 314 | | WebRequestAsyncOperation resultOp = asyncOp; |
| | 315 | |
|
| 472 | 316 | | if (resultOp == null) |
| 472 | 317 | | resultOp = new WebRequestAsyncOperation(request); |
| | 318 | | else |
| 0 | 319 | | resultOp.SetNewWebRequest(request); |
| | 320 | |
|
| 472 | 321 | | resultOp.disposeOnCompleted = disposeOnCompleted; |
| 472 | 322 | | ongoingWebRequests.Add(resultOp); |
| | 323 | |
|
| 472 | 324 | | UnityWebRequestAsyncOperation requestOp = resultOp.SendWebRequest(); |
| | 325 | |
|
| 472 | 326 | | requestOp.completed += (asyncOp) => |
| | 327 | | { |
| | 328 | | if (!resultOp.isDisposed) |
| | 329 | | { |
| | 330 | | if (resultOp.webRequest.WebRequestSucceded()) |
| | 331 | | { |
| | 332 | | OnSuccess?.Invoke(resultOp); |
| | 333 | | resultOp.SetAsCompleted(true); |
| | 334 | | } |
| | 335 | | else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError()) |
| | 336 | | { |
| | 337 | | remainingAttemps--; |
| | 338 | |
|
| | 339 | | if (remainingAttemps > 0) |
| | 340 | | { |
| | 341 | | resultOp.Dispose(); |
| | 342 | | resultOp = SendWebRequest(requestFactory, url, downloadHandler, OnSuccess, OnFail, remaining |
| | 343 | | } |
| | 344 | | else |
| | 345 | | { |
| | 346 | | OnFail?.Invoke(resultOp); |
| | 347 | | resultOp.SetAsCompleted(false); |
| | 348 | | } |
| | 349 | | } |
| | 350 | | else |
| | 351 | | { |
| | 352 | | OnFail?.Invoke(resultOp); |
| | 353 | | resultOp.SetAsCompleted(false); |
| | 354 | | } |
| | 355 | | } |
| | 356 | |
|
| | 357 | | ongoingWebRequests.Remove(resultOp); |
| | 358 | | }; |
| | 359 | |
|
| 472 | 360 | | return resultOp; |
| | 361 | | } |
| | 362 | |
|
| | 363 | |
|
| | 364 | | public void Dispose() |
| | 365 | | { |
| 1506 | 366 | | foreach (var webRequest in ongoingWebRequests) |
| 120 | 367 | | webRequest.Dispose(); |
| 633 | 368 | | } |
| | 369 | | } |
| | 370 | | } |