| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Our custom request async operation to be used with the WebRequestController. |
| | 9 | | /// </summary> |
| | 10 | | public class WebRequestAsyncOperation : CustomYieldInstruction, IWebRequestAsyncOperation |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Event that will be invoked when the request has been completed. |
| | 14 | | /// </summary> |
| | 15 | | public event Action<IWebRequestAsyncOperation> completed; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// WebRequest that is being managed. |
| | 19 | | /// </summary> |
| 1852 | 20 | | public UnityWebRequest webRequest { get; private set; } |
| | 21 | |
|
| 404 | 22 | | public UnityWebRequestAsyncOperation asyncOp { get; private set; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Returns true after the request has finished communicating with the remote server. |
| | 26 | | /// </summary> |
| 2025 | 27 | | public bool isDone { get; private set; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Returns true if the request was successfully finished. |
| | 31 | | /// </summary> |
| 519 | 32 | | public bool isSucceeded { get; private set; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 36 | | /// </summary> |
| 226 | 37 | | public bool isDisposed => webRequest == null; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Set to true for disposing the request just after it has been completed. |
| | 41 | | /// </summary> |
| 581 | 42 | | public bool disposeOnCompleted { get; set; } |
| | 43 | |
|
| 1453 | 44 | | public override bool keepWaiting { get { return !isDone; } } |
| | 45 | |
|
| 202 | 46 | | public WebRequestAsyncOperation(UnityWebRequest webRequest) |
| | 47 | | { |
| 202 | 48 | | this.webRequest = webRequest; |
| 202 | 49 | | isDone = false; |
| 202 | 50 | | isSucceeded = false; |
| 202 | 51 | | disposeOnCompleted = true; |
| 202 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Mark the request as completed and throw the corresponding event. |
| | 56 | | /// </summary> |
| | 57 | | /// <param name="success">True if the request was successfully ended.</param> |
| | 58 | | public void SetAsCompleted(bool success) |
| | 59 | | { |
| 177 | 60 | | completed?.Invoke(this); |
| 177 | 61 | | isDone = true; |
| 177 | 62 | | isSucceeded = success; |
| | 63 | |
|
| 177 | 64 | | if (disposeOnCompleted) |
| 4 | 65 | | Dispose(); |
| 177 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// If in progress, halts the request as soon as possible. |
| | 70 | | /// </summary> |
| | 71 | | public void Abort() |
| | 72 | | { |
| 206 | 73 | | if (webRequest == null || isDone) |
| 153 | 74 | | return; |
| | 75 | |
|
| 53 | 76 | | webRequest.Abort(); |
| 53 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 81 | | /// </summary> |
| | 82 | | public void Dispose() |
| | 83 | | { |
| 206 | 84 | | Abort(); |
| | 85 | |
|
| 206 | 86 | | if (webRequest == null) |
| 13 | 87 | | return; |
| | 88 | |
|
| 193 | 89 | | webRequest.Dispose(); |
| 193 | 90 | | webRequest = null; |
| 193 | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar |
| | 95 | | /// </summary> |
| | 96 | | public byte[] GetResultData() |
| | 97 | | { |
| 0 | 98 | | return webRequest.downloadHandler.data; |
| | 99 | | } |
| | 100 | |
|
| 0 | 101 | | internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; } |
| | 102 | |
|
| | 103 | | public UnityWebRequestAsyncOperation SendWebRequest() |
| | 104 | | { |
| 202 | 105 | | asyncOp = webRequest.SendWebRequest(); |
| 202 | 106 | | return asyncOp; |
| | 107 | | } |
| | 108 | | } |
| | 109 | | } |