< Summary

Class:DCL.ContentProvider
Assembly:ContentProvider
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ContentProvider/ContentProvider.cs
Covered lines:42
Uncovered lines:34
Coverable lines:76
Total lines:206
Line coverage:55.2% (42 of 76)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ContentProvider()0%110100%
ContentProvider(...)0%2100%
ToString()0%6200%
GetMappingForHash(...)0%6200%
BakeHashes()0%6.356078.57%
HasContentsUrl(...)0%4.374071.43%
GetContentsUrl(...)0%2.062075%
TryGetContentsUrl_Raw(...)0%4.254075%
TryGetContentsUrl(...)0%14.115028.57%
HasTestSchema(...)0%3.073080%
TryGetContentHash(...)0%3.213071.43%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ContentProvider/ContentProvider.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using System.Linq;
 5using MappingPair = DCL.ContentServerUtils.MappingPair;
 6
 7namespace DCL
 8{
 9    public class ContentProvider
 10    {
 11        public static bool VERBOSE = false;
 12
 13        public string baseUrl;
 81114        public List<MappingPair> contents = new List<MappingPair>();
 81115        public Dictionary<string, string> fileToHash = new Dictionary<string, string>();
 16
 17        public override string ToString()
 18        {
 019            string result = $"baseUrl: {baseUrl}\n";
 20
 021            foreach (var pair in contents)
 22            {
 023                result += $"file: {pair.file} ... hash: {pair.hash}\n";
 24            }
 25
 026            return result;
 27        }
 28
 162229        public ContentProvider() { }
 30
 031        public ContentProvider(ContentProvider toCopy)
 32        {
 033            baseUrl = toCopy.baseUrl;
 034            contents = new List<MappingPair>(toCopy.contents);
 035            fileToHash = new Dictionary<string, string>(toCopy.fileToHash);
 036        }
 37
 38        public MappingPair GetMappingForHash(string hash)
 39        {
 040            if (contents == null)
 41            {
 042                return null;
 43            }
 44
 045            return contents.FirstOrDefault((x) => x.hash == hash);
 46        }
 47
 48        public void BakeHashes()
 49        {
 77850            if (contents == null)
 51            {
 68452                return;
 53            }
 54
 9455            if (VERBOSE)
 56            {
 057                Debug.Log("Baking hashes...");
 58            }
 59
 9460            if (fileToHash == null)
 61            {
 062                fileToHash = new Dictionary<string, string>(contents.Count);
 63            }
 64
 60665            for (int i = 0; i < contents.Count; i++)
 66            {
 20967                MappingPair m = contents[i];
 20968                fileToHash.Add(m.file.ToLower(), m.hash);
 69
 20970                if (VERBOSE)
 71                {
 072                    Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}");
 73                }
 74            }
 9475        }
 76
 77        public virtual bool HasContentsUrl(string url)
 78        {
 7479            if (string.IsNullOrEmpty(url))
 80            {
 081                return false;
 82            }
 83
 84#if UNITY_EDITOR
 7485            if (HasTestSchema(url))
 86            {
 5987                return true;
 88            }
 89#endif
 1590            if (fileToHash == null)
 91            {
 092                return false;
 93            }
 94
 1595            return fileToHash.ContainsKey(url.ToLower());
 96        }
 97
 98        public virtual string GetContentsUrl(string url)
 99        {
 24100            string result = "";
 101
 24102            if (TryGetContentsUrl(url, out result))
 103            {
 24104                return result;
 105            }
 106
 0107            return null;
 108        }
 109
 110        public virtual bool TryGetContentsUrl_Raw(string url, out string result)
 111        {
 93112            url = url.ToLower();
 93113            result = url;
 114
 115#if UNITY_EDITOR
 93116            if (HasTestSchema(url))
 117            {
 64118                return true;
 119            }
 120#endif
 29121            if (fileToHash != null)
 122            {
 29123                if (!fileToHash.ContainsKey(url))
 124                {
 0125                    Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!");
 0126                    return false;
 127                }
 128
 29129                result = fileToHash[url];
 29130            }
 131            else
 132            {
 0133                result = url;
 134            }
 135
 29136            return true;
 137        }
 138
 139        public virtual bool TryGetContentsUrl(string url, out string result)
 140        {
 45141            url = url.ToLower();
 45142            result = url;
 143
 45144            if (HasTestSchema(url))
 145            {
 45146                return true;
 147            }
 148
 0149            if (fileToHash != null)
 150            {
 0151                if (!fileToHash.ContainsKey(url))
 152                {
 0153                    Debug.LogError($"GetContentsUrl >>> File {url} not found!!!");
 0154                    return false;
 155                }
 156
 0157                result = baseUrl + fileToHash[url];
 0158            }
 159            else
 160            {
 0161                result = baseUrl + url;
 162            }
 163
 0164            if (VERBOSE)
 165            {
 0166                Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}");
 167            }
 168
 0169            return true;
 170        }
 171
 172        public bool HasTestSchema(string url)
 173        {
 174#if UNITY_EDITOR
 212175            if (url.StartsWith("file://"))
 176            {
 168177                return true;
 178            }
 179
 44180            if (url.StartsWith(TestAssetsUtils.GetPath()))
 181            {
 0182                return true;
 183            }
 184#endif
 44185            return false;
 186        }
 187
 188        public bool TryGetContentHash(string file, out string result)
 189        {
 295190            result = string.Empty;
 191
 295192            if (fileToHash == null)
 193            {
 0194                result = file;
 0195                return true;
 196            }
 197
 295198            if (fileToHash.TryGetValue(file.ToLower(), out result))
 199            {
 90200                return true;
 201            }
 202
 205203            return false;
 204        }
 205    }
 206}