| | 1 | | using System.Collections; |
| | 2 | | using System; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using UnityEngine.Networking; |
| | 5 | | using DCL; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// GifProcessor: Is in charge of choosing which gif processor tu use (typescript's webworker through GIFProcessingBridg |
| | 9 | | /// for downloading, processing and discarding gifs |
| | 10 | | /// </summary> |
| | 11 | | public class GifProcessor |
| | 12 | | { |
| | 13 | | private bool jsGIFProcessingEnabled = false; |
| | 14 | | private WebRequestAsyncOperation webRequestOp; |
| | 15 | | private string url; |
| | 16 | |
|
| 10 | 17 | | public GifProcessor(string url) |
| | 18 | | { |
| 10 | 19 | | this.url = url; |
| 10 | 20 | | KernelConfig.i.EnsureConfigInitialized().Then(config => jsGIFProcessingEnabled = config.gifSupported); |
| 10 | 21 | | } |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Request the download and processing of a gif |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="OnSuccess">success callback with gif's frames arry</param> |
| | 27 | | /// <param name="OnFail">fail callback</param> |
| | 28 | | /// <returns></returns> |
| | 29 | | public IEnumerator Load(Action<GifFrameData[]> OnSuccess, Action OnFail) |
| | 30 | | { |
| 10 | 31 | | if (jsGIFProcessingEnabled) |
| | 32 | | { |
| 0 | 33 | | yield return JSProcessorLoad(url, OnSuccess, OnFail); |
| 0 | 34 | | } |
| | 35 | | else |
| | 36 | | { |
| 10 | 37 | | yield return UniGifProcessorLoad(url, OnSuccess, OnFail); |
| | 38 | | } |
| 4 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Notify processor that the gif is disposed. |
| | 43 | | /// If using UniGif plugin we just cancel the download if pending |
| | 44 | | /// If using webworker we send a message to kernel to cancel download and/or remove created texture from memory |
| | 45 | | /// </summary> |
| | 46 | | public void DisposeGif() |
| | 47 | | { |
| 10 | 48 | | if (jsGIFProcessingEnabled) |
| | 49 | | { |
| 0 | 50 | | DCL.GIFProcessingBridge.i.DeleteGIF(url); |
| 0 | 51 | | } |
| 10 | 52 | | else if (webRequestOp != null) |
| | 53 | | { |
| 10 | 54 | | webRequestOp.Dispose(); |
| | 55 | | } |
| 10 | 56 | | } |
| | 57 | |
|
| | 58 | | private IEnumerator JSProcessorLoad(string url, Action<GifFrameData[]> OnSuccess, Action OnFail) |
| | 59 | | { |
| 0 | 60 | | bool fetchFailed = false; |
| 0 | 61 | | yield return DCL.GIFProcessingBridge.i.RequestGIFProcessor(url, |
| | 62 | | (GifFrameData[] newTextures) => |
| | 63 | | { |
| 0 | 64 | | if (newTextures == null || newTextures.Length == 0) |
| | 65 | | { |
| 0 | 66 | | fetchFailed = true; |
| 0 | 67 | | return; |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | OnSuccess?.Invoke(newTextures); |
| 0 | 71 | | }, () => fetchFailed = true); |
| | 72 | |
|
| 0 | 73 | | if (fetchFailed) |
| | 74 | | { |
| 0 | 75 | | OnFail?.Invoke(); |
| | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | private IEnumerator UniGifProcessorLoad(string url, Action<GifFrameData[]> OnSuccess, Action OnFail) |
| | 80 | | { |
| 10 | 81 | | webRequestOp = DCL.Environment.i.platform.webRequest.Get(url: url, disposeOnCompleted: false); |
| | 82 | |
|
| 10 | 83 | | yield return webRequestOp; |
| | 84 | |
|
| 7 | 85 | | if (webRequestOp.isSucceded) |
| | 86 | | { |
| 4 | 87 | | var bytes = webRequestOp.webRequest.downloadHandler.data; |
| 4 | 88 | | yield return UniGif.GetTextureListCoroutine(bytes, |
| | 89 | | (frames, loopCount, width, height) => |
| | 90 | | { |
| 4 | 91 | | if (frames != null) |
| | 92 | | { |
| 4 | 93 | | OnSuccess?.Invoke(frames); |
| 4 | 94 | | } |
| | 95 | | else |
| | 96 | | { |
| 0 | 97 | | OnFail?.Invoke(); |
| | 98 | | } |
| 0 | 99 | | }); |
| 4 | 100 | | } |
| | 101 | | else |
| | 102 | | { |
| 3 | 103 | | OnFail?.Invoke(); |
| | 104 | | } |
| | 105 | |
|
| 7 | 106 | | webRequestOp.Dispose(); |
| 7 | 107 | | } |
| | 108 | | } |