| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | |
|
| | 6 | | namespace DCL |
| | 7 | | { |
| | 8 | | public interface IWrappedTextureHelper |
| | 9 | | { |
| | 10 | | IEnumerator GetHeader(string url, string headerField, Action<string> OnSuccess, Action<string> OnFail); |
| | 11 | | IEnumerator Fetch(string url, Action<IPromiseLike_TextureAsset> OnSuccess, Action OnFail = null); |
| | 12 | | IEnumerator Fetch(string contentType, string url, Action<IPromiseLike_TextureAsset> OnSuccess, Action OnFail = n |
| | 13 | | } |
| | 14 | |
|
| | 15 | | public class WrappedTextureUtils : IWrappedTextureHelper |
| | 16 | | { |
| | 17 | | public IEnumerator GetHeader(string url, string headerField, Action<string> OnSuccess, Action<string> OnFail) |
| | 18 | | { |
| 7 | 19 | | using (var headReq = UnityWebRequest.Head(url)) |
| | 20 | | { |
| 7 | 21 | | yield return headReq.SendWebRequest(); |
| | 22 | |
|
| 7 | 23 | | if (headReq.WebRequestSucceded()) |
| | 24 | | { |
| 7 | 25 | | OnSuccess?.Invoke(headReq.GetResponseHeader(headerField)); |
| 7 | 26 | | } |
| | 27 | | else |
| | 28 | | { |
| 0 | 29 | | OnFail?.Invoke(headReq.error); |
| | 30 | | } |
| 7 | 31 | | } |
| 7 | 32 | | } |
| | 33 | |
|
| | 34 | | public IEnumerator Fetch(string url, Action<IPromiseLike_TextureAsset> OnSuccess, Action OnFail = null) |
| | 35 | | { |
| 7 | 36 | | string contentType = null; |
| 14 | 37 | | yield return GetHeader(url, "Content-Type", type => contentType = type, null); |
| 7 | 38 | | yield return Fetch(contentType, url, OnSuccess, OnFail); |
| 7 | 39 | | } |
| | 40 | |
|
| | 41 | | public IEnumerator Fetch(string contentType, string url, Action<IPromiseLike_TextureAsset> OnSuccess, Action OnF |
| | 42 | | { |
| 7 | 43 | | if (contentType == "image/gif") |
| | 44 | | { |
| 0 | 45 | | AssetPromise_Gif gifPromise = new AssetPromise_Gif(url); |
| 0 | 46 | | gifPromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(new PromiseLike_Gif(gifPromise)); }; |
| 0 | 47 | | gifPromise.OnFailEvent += (x) => OnFail?.Invoke(); |
| | 48 | |
|
| 0 | 49 | | AssetPromiseKeeper_Gif.i.Keep(gifPromise); |
| | 50 | |
|
| 0 | 51 | | yield return gifPromise; |
| 0 | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 7 | 55 | | AssetPromise_Texture texturePromise = new AssetPromise_Texture(url); |
| 21 | 56 | | texturePromise.OnSuccessEvent += texture => { OnSuccess?.Invoke(new PromiseLike_Texture(texturePromise)) |
| 7 | 57 | | texturePromise.OnFailEvent += (x) => OnFail?.Invoke(); |
| | 58 | |
|
| 7 | 59 | | AssetPromiseKeeper_Texture.i.Keep(texturePromise); |
| | 60 | |
|
| 7 | 61 | | yield return texturePromise; |
| | 62 | | } |
| 7 | 63 | | } |
| | 64 | | } |
| | 65 | | } |