< Summary

Class:MapTexturePlugin
Assembly:MapTexturePlugin
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/MapTexturePlugin/MapTexturePlugin.cs
Covered lines:0
Uncovered lines:27
Coverable lines:27
Total lines:61
Line coverage:0% (0 of 27)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapTexturePlugin()0%2100%
DownloadTexture()0%90900%
Dispose()0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/MapTexturePlugin/MapTexturePlugin.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DCL;
 5using UnityEngine;
 6using UnityEngine.Networking;
 7
 8public class MapTexturePlugin : IPlugin
 9{
 10    private const int RETRY_TIME = 10;
 11    private const string MAIN_TEXTURE_URL = "https://api.decentraland.org/v1/minimap.png";
 12    private const string ESTATES_TEXTURE_URL = "https://api.decentraland.org/v1/estatemap.png";
 13
 14    private CancellationTokenSource cts;
 15
 016    public MapTexturePlugin()
 17    {
 018        cts = new CancellationTokenSource();
 19
 020        DataStore.i.HUDs.mapMainTexture.Set(Resources.Load<Texture2D>("MapDefault"));
 021        DataStore.i.HUDs.mapEstatesTexture.Set(Resources.Load<Texture2D>("MapDefaultEstates"));
 22
 023        DownloadTexture(MAIN_TEXTURE_URL, DataStore.i.HUDs.mapMainTexture, cts.Token);
 024        DownloadTexture(ESTATES_TEXTURE_URL, DataStore.i.HUDs.mapEstatesTexture, cts.Token);
 025    }
 26
 27    private static async UniTaskVoid DownloadTexture(string url, BaseVariable<Texture> textureVariable, CancellationToke
 28    {
 029        while (true)
 30        {
 031            using (UnityWebRequest www = UnityWebRequest.Get(url))
 32            {
 033                await www.SendWebRequest();
 034                if (www.error != null)
 35                {
 036                    Debug.LogException(new Exception(www.error));
 037                }
 38                else
 39                {
 040                    Texture2D tex = new Texture2D(0, 0, TextureFormat.ETC2_RGBA8, false, true);
 041                    tex.filterMode = FilterMode.Point;
 042                    tex.wrapMode = TextureWrapMode.Clamp;
 043                    tex.LoadImage(www.downloadHandler.data);
 044                    tex.Apply(false, false);
 045                    textureVariable.Set(tex);
 046                    return;
 47                }
 048            }
 49
 50            // We retry in 10 seconds
 051            await UniTask.Delay(TimeSpan.FromSeconds(RETRY_TIME), cancellationToken: ct).AttachExternalCancellation(ct);
 52        }
 053    }
 54
 55    public void Dispose()
 56    {
 057        cts?.Cancel();
 058        cts?.Dispose();
 059        cts = null;
 060    }
 61}