| | 1 | | using System; |
| | 2 | | using UnityEngine; |
| | 3 | | using UnityEngine.Networking; |
| | 4 | |
|
| | 5 | | namespace DCL |
| | 6 | | { |
| | 7 | | public interface IWebRequestAsyncOperation |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Event that will be invoked when the request has been completed. |
| | 11 | | /// </summary> |
| | 12 | | event Action<WebRequestAsyncOperation> completed; |
| | 13 | |
|
| | 14 | | /// <summary> |
| | 15 | | /// WebRequest that is being managed. |
| | 16 | | /// </summary> |
| | 17 | | UnityWebRequest webRequest { get; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Returns true after the request has finished communicating with the remote server. |
| | 21 | | /// </summary> |
| | 22 | | bool isDone { get; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Returns true if the request was successfully finished. |
| | 26 | | /// </summary> |
| | 27 | | bool isSucceded { get; } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 31 | | /// </summary> |
| | 32 | | bool isDisposed { get; } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Set to true for disposing the request just after it has been completed. |
| | 36 | | /// </summary> |
| | 37 | | bool disposeOnCompleted { get; } |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar |
| | 41 | | /// </summary> |
| | 42 | | byte[] GetResultData(); |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// If in progress, halts the request as soon as possible. |
| | 46 | | /// </summary> |
| | 47 | | void Abort(); |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 51 | | /// </summary> |
| | 52 | | void Dispose(); |
| | 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 | | void SetAsCompleted(bool success); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// Our custom request async operation to be used with the WebRequestController. |
| | 63 | | /// </summary> |
| | 64 | | public class WebRequestAsyncOperation : CustomYieldInstruction, IWebRequestAsyncOperation |
| | 65 | | { |
| | 66 | | /// <summary> |
| | 67 | | /// Event that will be invoked when the request has been completed. |
| | 68 | | /// </summary> |
| | 69 | | public event Action<WebRequestAsyncOperation> completed; |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// WebRequest that is being managed. |
| | 73 | | /// </summary> |
| 79 | 74 | | public UnityWebRequest webRequest { get; private set; } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Returns true after the request has finished communicating with the remote server. |
| | 78 | | /// </summary> |
| 0 | 79 | | public bool isDone { get; private set; } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Returns true if the request was successfully finished. |
| | 83 | | /// </summary> |
| 9 | 84 | | public bool isSucceded { get; private set; } |
| | 85 | |
|
| | 86 | | /// <summary> |
| | 87 | | /// Returns true if webRequest has been disposed (webRequest = null). |
| | 88 | | /// </summary> |
| 0 | 89 | | public bool isDisposed { get { return webRequest == null; } } |
| | 90 | |
|
| | 91 | | /// <summary> |
| | 92 | | /// Set to true for disposing the request just after it has been completed. |
| | 93 | | /// </summary> |
| 0 | 94 | | public bool disposeOnCompleted { get; set; } |
| | 95 | |
|
| 635 | 96 | | public override bool keepWaiting { get { return !isDone; } } |
| | 97 | |
|
| 387 | 98 | | public WebRequestAsyncOperation(UnityWebRequest webRequest) |
| | 99 | | { |
| 387 | 100 | | this.webRequest = webRequest; |
| 387 | 101 | | isDone = false; |
| 387 | 102 | | isSucceded = false; |
| 387 | 103 | | disposeOnCompleted = true; |
| 387 | 104 | | } |
| | 105 | |
|
| | 106 | | /// <summary> |
| | 107 | | /// Mark the request as completed and throw the corresponding event. |
| | 108 | | /// </summary> |
| | 109 | | /// <param name="success">True if the request was successfully ended.</param> |
| | 110 | | public void SetAsCompleted(bool success) |
| | 111 | | { |
| 309 | 112 | | completed?.Invoke(this); |
| 309 | 113 | | isDone = true; |
| 309 | 114 | | isSucceded = success; |
| | 115 | |
|
| 309 | 116 | | if (disposeOnCompleted) |
| 106 | 117 | | Dispose(); |
| 309 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// If in progress, halts the request as soon as possible. |
| | 122 | | /// </summary> |
| | 123 | | public void Abort() |
| | 124 | | { |
| 491 | 125 | | if (webRequest == null || isDone) |
| 389 | 126 | | return; |
| | 127 | |
|
| 102 | 128 | | webRequest.Abort(); |
| 102 | 129 | | } |
| | 130 | |
|
| | 131 | | /// <summary> |
| | 132 | | /// Signals that this request is no longer being used, and should clean up any resources it is using (it aborts |
| | 133 | | /// </summary> |
| | 134 | | public void Dispose() |
| | 135 | | { |
| 491 | 136 | | Abort(); |
| | 137 | |
|
| 491 | 138 | | if (webRequest == null) |
| 108 | 139 | | return; |
| | 140 | |
|
| 383 | 141 | | webRequest.Dispose(); |
| 383 | 142 | | webRequest = null; |
| 383 | 143 | | } |
| | 144 | |
|
| | 145 | | /// <summary> |
| | 146 | | /// Returns the data that has been downloaded as a byte array, if not done or success it will return an empty ar |
| | 147 | | /// </summary> |
| | 148 | | public byte[] GetResultData() |
| | 149 | | { |
| 0 | 150 | | return webRequest.downloadHandler.data; |
| | 151 | | } |
| | 152 | |
|
| 0 | 153 | | internal void SetNewWebRequest(UnityWebRequest newRequest) { webRequest = newRequest; } |
| | 154 | | } |
| | 155 | | } |