< 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:214
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;
 75915        public List<MappingPair> contents = new List<MappingPair>();
 75916        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
 151830        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        //todo: thread this
 50        public void BakeHashes()
 51        {
 59452            if (contents == null)
 53            {
 50654                return;
 55            }
 56
 8857            if (VERBOSE)
 58            {
 059                Debug.Log("Baking hashes...");
 60            }
 61
 8862            if (fileToHash == null)
 63            {
 064                fileToHash = new Dictionary<string, string>(contents.Count);
 65            }
 66
 69067            for (int i = 0; i < contents.Count; i++)
 68            {
 25769                MappingPair m = contents[i];
 25770                var key = m.file.ToLower();
 25771                if (fileToHash.ContainsKey(key))
 72                {
 273                    Debug.Log($"Hash key: {key} already exists in the map");
 274                    continue;
 75                }
 25576                fileToHash.Add(key, m.hash);
 77
 25578                if (VERBOSE)
 79                {
 080                    Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}");
 81                }
 82            }
 8883        }
 84
 85        public virtual bool HasContentsUrl(string url)
 86        {
 8987            if (string.IsNullOrEmpty(url))
 88            {
 089                return false;
 90            }
 91
 92#if UNITY_EDITOR
 8993            if (HasTestSchema(url))
 94            {
 7495                return true;
 96            }
 97#endif
 1598            if (fileToHash == null)
 99            {
 0100                return false;
 101            }
 102
 15103            return fileToHash.ContainsKey(url.ToLower());
 104        }
 105
 106        public virtual string GetContentsUrl(string url)
 107        {
 39108            string result = "";
 109
 39110            if (TryGetContentsUrl(url, out result))
 111            {
 39112                return result;
 113            }
 114
 0115            return null;
 116        }
 117
 118        public virtual bool TryGetContentsUrl_Raw(string url, out string result)
 119        {
 117120            url = url.ToLower();
 117121            result = url;
 122
 123#if UNITY_EDITOR
 117124            if (HasTestSchema(url))
 125            {
 82126                return true;
 127            }
 128#endif
 35129            if (fileToHash != null)
 130            {
 35131                if (!fileToHash.ContainsKey(url))
 132                {
 0133                    Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!");
 0134                    return false;
 135                }
 136
 35137                result = fileToHash[url];
 35138            }
 139            else
 140            {
 0141                result = url;
 142            }
 143
 35144            return true;
 145        }
 146
 147        public virtual bool TryGetContentsUrl(string url, out string result)
 148        {
 54149            url = url.ToLower();
 54150            result = url;
 151
 54152            if (HasTestSchema(url))
 153            {
 54154                return true;
 155            }
 156
 0157            if (fileToHash != null)
 158            {
 0159                if (!fileToHash.ContainsKey(url))
 160                {
 0161                    Debug.LogError($"GetContentsUrl >>> File {url} not found!!!");
 0162                    return false;
 163                }
 164
 0165                result = baseUrl + fileToHash[url];
 0166            }
 167            else
 168            {
 0169                result = baseUrl + url;
 170            }
 171
 0172            if (VERBOSE)
 173            {
 0174                Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}");
 175            }
 176
 0177            return true;
 178        }
 179
 180        public bool HasTestSchema(string url)
 181        {
 182#if UNITY_EDITOR
 260183            if (url.StartsWith("file://"))
 184            {
 210185                return true;
 186            }
 187
 50188            if (url.StartsWith(TestAssetsUtils.GetPath()))
 189            {
 0190                return true;
 191            }
 192#endif
 50193            return false;
 194        }
 195
 196        public bool TryGetContentHash(string file, out string result)
 197        {
 305198            result = string.Empty;
 199
 305200            if (fileToHash == null)
 201            {
 0202                result = file;
 0203                return true;
 204            }
 205
 305206            if (fileToHash.TryGetValue(file.ToLower(), out result))
 207            {
 64208                return true;
 209            }
 210
 241211            return false;
 212        }
 213    }
 214}