< Summary

Class:DCL.ContentProvider
Assembly:ContentProvider
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ContentProvider/ContentProvider.cs
Covered lines:43
Uncovered lines:27
Coverable lines:70
Total lines:181
Line coverage:61.4% (43 of 70)
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%64050%
GetContentsUrl(...)0%2.032080%
TryGetContentsUrl_Raw(...)0%4.324072.73%
TryGetContentsUrl(...)0%19.126028.57%
HasTestSchema(...)0%3.043083.33%
TryGetContentHash(...)0%3.143075%

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;
 64815        public List<MappingPair> contents = new ();
 64816        public Dictionary<string, string> fileToHash = new ();
 17
 18        public override string ToString()
 19        {
 020            string result = $"baseUrl: {baseUrl}\n";
 21
 022            foreach (var pair in contents) { result += $"file: {pair.file} ... hash: {pair.hash}\n"; }
 23
 024            return result;
 25        }
 26
 129627        public ContentProvider() { }
 28
 029        public ContentProvider(ContentProvider toCopy)
 30        {
 031            baseUrl = toCopy.baseUrl;
 032            contents = new List<MappingPair>(toCopy.contents);
 033            fileToHash = new Dictionary<string, string>(toCopy.fileToHash);
 034        }
 35
 36        public MappingPair GetMappingForHash(string hash)
 37        {
 038            if (contents == null) { return null; }
 39
 040            return contents.FirstOrDefault((x) => x.hash == hash);
 41        }
 42
 43        //todo: thread this
 44        public void BakeHashes()
 45        {
 77046            if (contents == null) { return; }
 47
 6048            if (VERBOSE) { Debug.Log("Baking hashes..."); }
 49
 6050            if (fileToHash == null) { fileToHash = new Dictionary<string, string>(contents.Count); }
 51
 51052            for (int i = 0; i < contents.Count; i++)
 53            {
 19554                MappingPair m = contents[i];
 19555                var key = m.file.ToLower();
 56
 19557                if (fileToHash.ContainsKey(key))
 58                {
 259                    Debug.Log($"Hash key: {key} already exists in the map");
 260                    continue;
 61                }
 62
 19363                fileToHash.Add(key, m.hash);
 64
 19365                if (VERBOSE) { Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}"); 
 66            }
 6067        }
 68
 69        public virtual bool HasContentsUrl(string url)
 70        {
 6071            url = url.ToLower();
 72
 6073            if (string.IsNullOrEmpty(url)) { return false; }
 74
 75#if UNITY_EDITOR
 6076            if (HasTestSchema(url))
 77            {
 6078                return true;
 79            }
 80#endif
 081            if (fileToHash == null) { return false; }
 82
 083            return fileToHash.ContainsKey(url);
 84        }
 85
 86        public virtual string GetContentsUrl(string url)
 87        {
 3988            string result = "";
 3989            url = url.ToLower();
 90
 7891            if (TryGetContentsUrl(url, out result)) { return result; }
 92
 093            return null;
 94        }
 95
 96        public virtual bool TryGetContentsUrl_Raw(string url, out string result)
 97        {
 8898            url = url.ToLower();
 8899            result = url;
 100
 101#if UNITY_EDITOR
 88102            if (HasTestSchema(url))
 103            {
 68104                return true;
 105            }
 106#endif
 20107            if (fileToHash != null)
 108            {
 20109                if (!fileToHash.ContainsKey(url))
 110                {
 0111                    Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!");
 0112                    return false;
 113                }
 114
 20115                result = fileToHash[url];
 116            }
 0117            else { result = url; }
 118
 20119            return true;
 120        }
 121
 122        public virtual bool TryGetContentsUrl(string url, out string result)
 123        {
 56124            url = url.ToLower();
 56125            result = url;
 126
 112127            if (HasTestSchema(url)) { return true; }
 128
 0129            if (fileToHash != null)
 130            {
 0131                if (!fileToHash.ContainsKey(url))
 132                {
 0133                    if (VERBOSE) { Debug.LogError($"GetContentsUrl >>> File {url} not found!!!"); }
 134
 0135                    return false;
 136                }
 137
 0138                result = baseUrl + fileToHash[url];
 139            }
 0140            else { result = baseUrl + url; }
 141
 0142            if (VERBOSE) { Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}"); }
 143
 0144            return true;
 145        }
 146
 147        public bool HasTestSchema(string url)
 148        {
 204149            url = url.ToLower();
 150
 151#if UNITY_EDITOR
 204152            if (url.StartsWith("file://"))
 153            {
 184154                return true;
 155            }
 156
 20157            if (url.StartsWith(TestAssetsUtils.GetPath()))
 158            {
 0159                return true;
 160            }
 161#endif
 20162            return false;
 163        }
 164
 165        public bool TryGetContentHash(string file, out string result)
 166        {
 281167            file = file.ToLower();
 281168            result = string.Empty;
 169
 281170            if (fileToHash == null)
 171            {
 0172                result = file;
 0173                return true;
 174            }
 175
 335176            if (fileToHash.TryGetValue(file.ToLower(), out result)) { return true; }
 177
 227178            return false;
 179        }
 180    }
 181}