< Summary

Class:GifProcessor
Assembly:GifProcessor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Gif/GifProcessor/GifProcessor.cs
Covered lines:23
Uncovered lines:16
Coverable lines:39
Total lines:108
Line coverage:58.9% (23 of 39)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GifProcessor(...)0%110100%
Load()0%6.65060%
DisposeGif()0%3.333066.67%
JSProcessorLoad()0%30500%
UniGifProcessorLoad()0%660100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Gif/GifProcessor/GifProcessor.cs

#LineLine coverage
 1using System.Collections;
 2using System;
 3using DCL.Helpers;
 4using UnityEngine.Networking;
 5using 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>
 11public class GifProcessor
 12{
 13    private bool jsGIFProcessingEnabled = false;
 14    private WebRequestAsyncOperation webRequestOp;
 15    private string url;
 16
 1017    public GifProcessor(string url)
 18    {
 1019        this.url = url;
 1020        KernelConfig.i.EnsureConfigInitialized().Then(config => jsGIFProcessingEnabled = config.gifSupported);
 1021    }
 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    {
 1031        if (jsGIFProcessingEnabled)
 32        {
 033            yield return JSProcessorLoad(url, OnSuccess, OnFail);
 034        }
 35        else
 36        {
 1037            yield return UniGifProcessorLoad(url, OnSuccess, OnFail);
 38        }
 439    }
 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    {
 1048        if (jsGIFProcessingEnabled)
 49        {
 050            DCL.GIFProcessingBridge.i.DeleteGIF(url);
 051        }
 1052        else if (webRequestOp != null)
 53        {
 1054            webRequestOp.Dispose();
 55        }
 1056    }
 57
 58    private IEnumerator JSProcessorLoad(string url, Action<GifFrameData[]> OnSuccess, Action OnFail)
 59    {
 060        bool fetchFailed = false;
 061        yield return DCL.GIFProcessingBridge.i.RequestGIFProcessor(url,
 62            (GifFrameData[] newTextures) =>
 63            {
 064                if (newTextures == null || newTextures.Length == 0)
 65                {
 066                    fetchFailed = true;
 067                    return;
 68                }
 69
 070                OnSuccess?.Invoke(newTextures);
 071            }, () => fetchFailed = true);
 72
 073        if (fetchFailed)
 74        {
 075            OnFail?.Invoke();
 76        }
 077    }
 78
 79    private IEnumerator UniGifProcessorLoad(string url, Action<GifFrameData[]> OnSuccess, Action OnFail)
 80    {
 1081        webRequestOp = DCL.Environment.i.platform.webRequest.Get(url: url, disposeOnCompleted: false);
 82
 1083        yield return webRequestOp;
 84
 785        if (webRequestOp.isSucceded)
 86        {
 487            var bytes = webRequestOp.webRequest.downloadHandler.data;
 488            yield return UniGif.GetTextureListCoroutine(bytes,
 89                (frames, loopCount, width, height) =>
 90                {
 491                    if (frames != null)
 92                    {
 493                        OnSuccess?.Invoke(frames);
 494                    }
 95                    else
 96                    {
 097                        OnFail?.Invoke();
 98                    }
 099                });
 4100        }
 101        else
 102        {
 3103            OnFail?.Invoke();
 104        }
 105
 7106        webRequestOp.Dispose();
 7107    }
 108}