< Summary

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

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MapTexturePlugin()0%2100%
MapTexturePlugin()0%12300%
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 System.Linq;
 6using UnityEngine;
 7using UnityEngine.Networking;
 8
 9public class MapTexturePlugin : IPlugin
 10{
 11    private const int RETRY_TIME = 10;
 12    private const string MAIN_TEXTURE_URL = "https://api.decentraland.org/v1/minimap.png";
 13    private const string ESTATES_TEXTURE_URL = "https://api.decentraland.org/v1/estatemap.png";
 14
 15    private CancellationTokenSource cts;
 16
 017    private static readonly TextureFormat?[] PRIORITIZED_FORMATS =
 18    {
 19        // available for iOS/Android WebGL player
 20        TextureFormat.ETC2_RGBA8,
 21        TextureFormat.BC7,
 22        TextureFormat.DXT5,
 23        TextureFormat.RGBA32
 24    };
 25
 026    public MapTexturePlugin()
 27    {
 028        cts = new CancellationTokenSource();
 29
 030        DataStore.i.HUDs.mapMainTexture.Set(Resources.Load<Texture2D>("MapDefault"));
 031        DataStore.i.HUDs.mapEstatesTexture.Set(Resources.Load<Texture2D>("MapDefaultEstates"));
 32
 033        var textureFormat = PRIORITIZED_FORMATS.FirstOrDefault(f => SystemInfo.SupportsTextureFormat(f.Value));
 34
 035        if (textureFormat == null)
 036            return;
 37
 038        DownloadTexture(MAIN_TEXTURE_URL, DataStore.i.HUDs.mapMainTexture, textureFormat.Value, cts.Token);
 039        DownloadTexture(ESTATES_TEXTURE_URL, DataStore.i.HUDs.mapEstatesTexture, textureFormat.Value, cts.Token);
 040    }
 41
 42    private static async UniTaskVoid DownloadTexture(string url, BaseVariable<Texture> textureVariable, TextureFormat te
 43    {
 044        while (true)
 45        {
 046            using (UnityWebRequest www = UnityWebRequest.Get(url))
 47            {
 048                await www.SendWebRequest();
 049                if (www.error != null)
 50                {
 051                    Debug.LogException(new Exception(www.error));
 52                }
 53                else
 54                {
 055                    Texture2D tex = new Texture2D(0, 0, TextureFormat.ETC2_RGBA8, false, true);
 056                    tex.filterMode = FilterMode.Point;
 057                    tex.wrapMode = TextureWrapMode.Clamp;
 058                    tex.LoadImage(www.downloadHandler.data);
 059                    tex.Apply(false, false);
 060                    textureVariable.Set(tex);
 061                    return;
 62                }
 063            }
 64
 65            // We retry in 10 seconds
 066            await UniTask.Delay(TimeSpan.FromSeconds(RETRY_TIME), cancellationToken: ct).AttachExternalCancellation(ct);
 67        }
 068    }
 69
 70    public void Dispose()
 71    {
 072        cts?.Cancel();
 073        cts?.Dispose();
 074        cts = null;
 075    }
 76}