| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public class WebRequestController : IWebRequestController |
| | 9 | | { |
| | 10 | | private IWebRequest genericWebRequest; |
| | 11 | | private IWebRequestAssetBundle assetBundleWebRequest; |
| | 12 | | private IWebRequestTexture textureWebRequest; |
| | 13 | | private IWebRequestAudio audioClipWebRequest; |
| 748 | 14 | | private List<WebRequestAsyncOperation> ongoingWebRequests = new List<WebRequestAsyncOperation>(); |
| | 15 | |
|
| | 16 | | public void Initialize() |
| | 17 | | { |
| 735 | 18 | | } |
| | 19 | |
|
| | 20 | | public static WebRequestController Create() |
| | 21 | | { |
| 748 | 22 | | WebRequestController newWebRequestController = new WebRequestController(); |
| | 23 | |
|
| 748 | 24 | | newWebRequestController.Initialize( |
| | 25 | | genericWebRequest: new WebRequest(), |
| | 26 | | assetBundleWebRequest: new WebRequestAssetBundle(), |
| | 27 | | textureWebRequest: new WebRequestTexture(), |
| | 28 | | audioClipWebRequest: new WebRequestAudio()); |
| | 29 | |
|
| 748 | 30 | | return newWebRequestController; |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public void Initialize( |
| | 34 | | IWebRequest genericWebRequest, |
| | 35 | | IWebRequestAssetBundle assetBundleWebRequest, |
| | 36 | | IWebRequestTexture textureWebRequest, |
| | 37 | | IWebRequestAudio audioClipWebRequest) |
| | 38 | | { |
| 748 | 39 | | this.genericWebRequest = genericWebRequest; |
| 748 | 40 | | this.assetBundleWebRequest = assetBundleWebRequest; |
| 748 | 41 | | this.textureWebRequest = textureWebRequest; |
| 748 | 42 | | this.audioClipWebRequest = audioClipWebRequest; |
| 748 | 43 | | } |
| | 44 | |
|
| | 45 | | public IWebRequestAsyncOperation Get( |
| | 46 | | string url, |
| | 47 | | DownloadHandler downloadHandler = null, |
| | 48 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 49 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 50 | | int requestAttemps = 3, |
| | 51 | | int timeout = 0, |
| | 52 | | bool disposeOnCompleted = true, |
| | 53 | | Dictionary<string, string> headers = null) |
| | 54 | | { |
| 180 | 55 | | return SendWebRequest(genericWebRequest, url, downloadHandler, OnSuccess, OnFail, requestAttemps, timeout, d |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public WebRequestAsyncOperation GetAssetBundle( |
| | 59 | | string url, |
| | 60 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 61 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 62 | | int requestAttemps = 3, |
| | 63 | | int timeout = 0, |
| | 64 | | bool disposeOnCompleted = true) |
| | 65 | | { |
| 0 | 66 | | return SendWebRequest(assetBundleWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeO |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public WebRequestAsyncOperation GetAssetBundle( |
| | 70 | | string url, |
| | 71 | | Hash128 hash, |
| | 72 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 73 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 74 | | int requestAttemps = 3, |
| | 75 | | int timeout = 0, |
| | 76 | | bool disposeOnCompleted = true) |
| | 77 | | { |
| 27 | 78 | | assetBundleWebRequest.SetHash(hash); |
| 27 | 79 | | return SendWebRequest(assetBundleWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeO |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public WebRequestAsyncOperation GetTexture( |
| | 83 | | string url, |
| | 84 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 85 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 86 | | int requestAttemps = 3, |
| | 87 | | int timeout = 0, |
| | 88 | | bool disposeOnCompleted = true, |
| | 89 | | bool isReadable = true) |
| | 90 | | { |
| 108 | 91 | | textureWebRequest.isReadable = isReadable; |
| 108 | 92 | | return SendWebRequest(textureWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnCom |
| | 93 | | } |
| | 94 | |
|
| | 95 | | public WebRequestAsyncOperation GetAudioClip( |
| | 96 | | string url, |
| | 97 | | AudioType audioType, |
| | 98 | | Action<IWebRequestAsyncOperation> OnSuccess = null, |
| | 99 | | Action<IWebRequestAsyncOperation> OnFail = null, |
| | 100 | | int requestAttemps = 3, |
| | 101 | | int timeout = 0, |
| | 102 | | bool disposeOnCompleted = true) |
| | 103 | | { |
| 26 | 104 | | audioClipWebRequest.SetAudioType(audioType); |
| 26 | 105 | | return SendWebRequest(audioClipWebRequest, url, null, OnSuccess, OnFail, requestAttemps, timeout, disposeOnC |
| | 106 | | } |
| | 107 | |
|
| | 108 | | private WebRequestAsyncOperation SendWebRequest<T>( |
| | 109 | | T requestType, |
| | 110 | | string url, |
| | 111 | | DownloadHandler downloadHandler, |
| | 112 | | Action<IWebRequestAsyncOperation> OnSuccess, |
| | 113 | | Action<IWebRequestAsyncOperation> OnFail, |
| | 114 | | int requestAttemps, |
| | 115 | | int timeout, |
| | 116 | | bool disposeOnCompleted, |
| | 117 | | Dictionary<string, string> headers = null, |
| | 118 | | WebRequestAsyncOperation asyncOp = null |
| | 119 | | ) where T : IWebRequest |
| | 120 | | { |
| 341 | 121 | | int remainingAttemps = Mathf.Clamp(requestAttemps, 1, requestAttemps); |
| | 122 | |
|
| 341 | 123 | | UnityWebRequest request = requestType.CreateWebRequest(url); |
| 341 | 124 | | request.timeout = timeout; |
| 341 | 125 | | if (headers != null) |
| | 126 | | { |
| 0 | 127 | | foreach (var item in headers) |
| | 128 | | { |
| 0 | 129 | | request.SetRequestHeader(item.Key, item.Value); |
| | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 341 | 133 | | if (downloadHandler != null) |
| 167 | 134 | | request.downloadHandler = downloadHandler; |
| | 135 | |
|
| 341 | 136 | | WebRequestAsyncOperation resultOp = asyncOp; |
| | 137 | |
|
| 341 | 138 | | if (resultOp == null) |
| 341 | 139 | | resultOp = new WebRequestAsyncOperation(request); |
| | 140 | | else |
| 0 | 141 | | resultOp.SetNewWebRequest(request); |
| | 142 | |
|
| 341 | 143 | | resultOp.disposeOnCompleted = disposeOnCompleted; |
| 341 | 144 | | ongoingWebRequests.Add(resultOp); |
| | 145 | |
|
| 341 | 146 | | UnityWebRequestAsyncOperation requestOp = resultOp.SendWebRequest(); |
| 341 | 147 | | requestOp.completed += (asyncOp) => |
| | 148 | | { |
| | 149 | | if (!resultOp.isDisposed) |
| | 150 | | { |
| | 151 | | if (resultOp.webRequest.WebRequestSucceded()) |
| | 152 | | { |
| | 153 | | OnSuccess?.Invoke(resultOp); |
| | 154 | | resultOp.SetAsCompleted(true); |
| | 155 | | } |
| | 156 | | else if (!resultOp.webRequest.WebRequestAborted() && resultOp.webRequest.WebRequestServerError()) |
| | 157 | | { |
| | 158 | | remainingAttemps--; |
| | 159 | | if (remainingAttemps > 0) |
| | 160 | | { |
| | 161 | | Debug.LogWarning($"Retrying web request: {url} ({remainingAttemps} attemps remaining)"); |
| | 162 | | resultOp.Dispose(); |
| | 163 | | resultOp = SendWebRequest(requestType, url, downloadHandler, OnSuccess, OnFail, remainingAtt |
| | 164 | | } |
| | 165 | | else |
| | 166 | | { |
| | 167 | | OnFail?.Invoke(resultOp); |
| | 168 | | resultOp.SetAsCompleted(false); |
| | 169 | | } |
| | 170 | | } |
| | 171 | | else |
| | 172 | | { |
| | 173 | | OnFail?.Invoke(resultOp); |
| | 174 | | resultOp.SetAsCompleted(false); |
| | 175 | | } |
| | 176 | | } |
| | 177 | |
|
| | 178 | | ongoingWebRequests.Remove(resultOp); |
| | 179 | | }; |
| | 180 | |
|
| 341 | 181 | | return resultOp; |
| | 182 | | } |
| | 183 | |
|
| | 184 | | public void Dispose() |
| | 185 | | { |
| 1562 | 186 | | foreach (var webRequest in ongoingWebRequests) |
| | 187 | | { |
| 26 | 188 | | webRequest.Dispose(); |
| | 189 | | } |
| 755 | 190 | | } |
| | 191 | | } |
| | 192 | | } |