< Summary

Class:DCL.ContentProvider
Assembly:ContentProvider
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ContentProvider/ContentProvider.cs
Covered lines:46
Uncovered lines:34
Coverable lines:80
Total lines:213
Line coverage:57.5% (46 of 80)
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%7.237083.33%
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 System;
 2using DCL.Helpers;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using System.Linq;
 6using MappingPair = DCL.ContentServerUtils.MappingPair;
 7
 8namespace DCL
 9{
 10    public class ContentProvider
 11    {
 12        public static bool VERBOSE = false;
 13
 14        public string baseUrl;
 66315        public List<MappingPair> contents = new List<MappingPair>();
 66316        public Dictionary<string, string> fileToHash = new Dictionary<string, string>();
 17
 18        public override string ToString()
 19        {
 020            string result = $"baseUrl: {baseUrl}\n";
 21
 022            foreach (var pair in contents)
 23            {
 024                result += $"file: {pair.file} ... hash: {pair.hash}\n";
 25            }
 26
 027            return result;
 28        }
 29
 132630        public ContentProvider() { }
 31
 032        public ContentProvider(ContentProvider toCopy)
 33        {
 034            baseUrl = toCopy.baseUrl;
 035            contents = new List<MappingPair>(toCopy.contents);
 036            fileToHash = new Dictionary<string, string>(toCopy.fileToHash);
 037        }
 38
 39        public MappingPair GetMappingForHash(string hash)
 40        {
 041            if (contents == null)
 42            {
 043                return null;
 44            }
 45
 046            return contents.FirstOrDefault((x) => x.hash == hash);
 47        }
 48
 49        public void BakeHashes()
 50        {
 60751            if (contents == null)
 52            {
 47453                return;
 54            }
 55
 13356            if (VERBOSE)
 57            {
 058                Debug.Log("Baking hashes...");
 59            }
 60
 13361            if (fileToHash == null)
 62            {
 063                fileToHash = new Dictionary<string, string>(contents.Count);
 64            }
 65
 127866            for (int i = 0; i < contents.Count; i++)
 67            {
 50668                MappingPair m = contents[i];
 50669                var key = m.file.ToLower();
 50670                if (fileToHash.ContainsKey(key))
 71                {
 272                    Debug.Log($"Hash key: {key} already exists in the map");
 273                    continue;
 74                }
 50475                fileToHash.Add(key, m.hash);
 76
 50477                if (VERBOSE)
 78                {
 079                    Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}");
 80                }
 81            }
 13382        }
 83
 84        public virtual bool HasContentsUrl(string url)
 85        {
 8186            if (string.IsNullOrEmpty(url))
 87            {
 088                return false;
 89            }
 90
 91#if UNITY_EDITOR
 8192            if (HasTestSchema(url))
 93            {
 6694                return true;
 95            }
 96#endif
 1597            if (fileToHash == null)
 98            {
 099                return false;
 100            }
 101
 15102            return fileToHash.ContainsKey(url.ToLower());
 103        }
 104
 105        public virtual string GetContentsUrl(string url)
 106        {
 27107            string result = "";
 108
 27109            if (TryGetContentsUrl(url, out result))
 110            {
 27111                return result;
 112            }
 113
 0114            return null;
 115        }
 116
 117        public virtual bool TryGetContentsUrl_Raw(string url, out string result)
 118        {
 173119            url = url.ToLower();
 173120            result = url;
 121
 122#if UNITY_EDITOR
 173123            if (HasTestSchema(url))
 124            {
 74125                return true;
 126            }
 127#endif
 99128            if (fileToHash != null)
 129            {
 99130                if (!fileToHash.ContainsKey(url))
 131                {
 0132                    Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!");
 0133                    return false;
 134                }
 135
 99136                result = fileToHash[url];
 99137            }
 138            else
 139            {
 0140                result = url;
 141            }
 142
 99143            return true;
 144        }
 145
 146        public virtual bool TryGetContentsUrl(string url, out string result)
 147        {
 45148            url = url.ToLower();
 45149            result = url;
 150
 45151            if (HasTestSchema(url))
 152            {
 45153                return true;
 154            }
 155
 0156            if (fileToHash != null)
 157            {
 0158                if (!fileToHash.ContainsKey(url))
 159                {
 0160                    Debug.LogError($"GetContentsUrl >>> File {url} not found!!!");
 0161                    return false;
 162                }
 163
 0164                result = baseUrl + fileToHash[url];
 0165            }
 166            else
 167            {
 0168                result = baseUrl + url;
 169            }
 170
 0171            if (VERBOSE)
 172            {
 0173                Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}");
 174            }
 175
 0176            return true;
 177        }
 178
 179        public bool HasTestSchema(string url)
 180        {
 181#if UNITY_EDITOR
 299182            if (url.StartsWith("file://"))
 183            {
 185184                return true;
 185            }
 186
 114187            if (url.StartsWith(TestAssetsUtils.GetPath()))
 188            {
 0189                return true;
 190            }
 191#endif
 114192            return false;
 193        }
 194
 195        public bool TryGetContentHash(string file, out string result)
 196        {
 269197            result = string.Empty;
 198
 269199            if (fileToHash == null)
 200            {
 0201                result = file;
 0202                return true;
 203            }
 204
 269205            if (fileToHash.TryGetValue(file.ToLower(), out result))
 206            {
 55207                return true;
 208            }
 209
 214210            return false;
 211        }
 212    }
 213}