< Summary

Class:DCL.ContentProvider
Assembly:ContentProvider
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/ContentProvider/ContentProvider.cs
Covered lines:45
Uncovered lines:22
Coverable lines:67
Total lines:176
Line coverage:67.1% (45 of 67)
Covered branches:0
Total branches:0
Covered methods:8
Total methods:11
Method coverage:72.7% (8 of 11)

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.014090.91%
TryGetContentsUrl(...)0%6.356078.57%
HasTestSchema(...)0%3.043083.33%
TryGetContentHash(...)0%12300%

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;
 15        public string baseUrlBundles;
 16        public string assetBundlesBaseUrl;
 17        public string assetBundlesVersion;
 18        public string sceneCid;
 19
 82620        public List<MappingPair> contents = new ();
 82621        public Dictionary<string, string> fileToHash = new ();
 82622        public HashSet<string> assetBundles = new ();
 23        public bool assetBundlesFetched;
 24
 25        public override string ToString()
 26        {
 027            string result = $"baseUrl: {baseUrl}\n";
 28
 029            foreach (var pair in contents) { result += $"file: {pair.file} ... hash: {pair.hash}\n"; }
 30
 031            return result;
 32        }
 33
 165234        public ContentProvider() { }
 35
 036        public ContentProvider(ContentProvider toCopy)
 37        {
 038            baseUrl = toCopy.baseUrl;
 039            contents = new List<MappingPair>(toCopy.contents);
 040            fileToHash = new Dictionary<string, string>(toCopy.fileToHash);
 041        }
 42
 43        public MappingPair GetMappingForHash(string hash)
 44        {
 045            if (contents == null) { return null; }
 46
 047            return contents.FirstOrDefault((x) => x.hash == hash);
 48        }
 49
 50        //todo: thread this
 51        public void BakeHashes()
 52        {
 85053            if (contents == null) { return; }
 54
 5255            if (VERBOSE) { Debug.Log("Baking hashes..."); }
 56
 5257            if (fileToHash == null) { fileToHash = new Dictionary<string, string>(contents.Count); }
 58
 38059            for (int i = 0; i < contents.Count; i++)
 60            {
 13861                MappingPair m = contents[i];
 13862                var key = m.file.ToLower();
 63
 13864                if (fileToHash.ContainsKey(key))
 65                {
 266                    Debug.Log($"Hash key: {key} already exists in the map");
 267                    continue;
 68                }
 69
 13670                fileToHash.Add(key, m.hash);
 71
 13672                if (VERBOSE) { Debug.Log($"found file = {m.file} ... hash = {m.hash}\nfull url = {baseUrl}\\{m.hash}"); 
 73            }
 5274        }
 75
 76        public virtual bool HasContentsUrl(string url)
 77        {
 5278            if (string.IsNullOrEmpty(url)) { return false; }
 79
 5280            url = url.ToLower();
 81
 82#if UNITY_EDITOR
 10483            if (HasTestSchema(url)) { return true; }
 84#endif
 085            if (fileToHash == null) { return false; }
 86
 087            return fileToHash.ContainsKey(url);
 88        }
 89
 90        public virtual string GetContentsUrl(string url)
 91        {
 4092            string result = "";
 4093            url = url.ToLower();
 94
 8095            if (TryGetContentsUrl(url, out result)) { return result; }
 96
 097            return null;
 98        }
 99
 100        public virtual bool TryGetContentsUrl_Raw(string url, out string result)
 101        {
 198102            url = url.ToLower();
 198103            result = url;
 104
 105#if UNITY_EDITOR
 318106            if (HasTestSchema(url)) { return true; }
 107#endif
 78108            if (fileToHash != null)
 109            {
 78110                if (!fileToHash.ContainsKey(url))
 111                {
 2112                    Debug.LogError($"GetContentsUrl_Raw >>> File {url} not found!!!");
 2113                    return false;
 114                }
 115
 76116                result = fileToHash[url];
 117            }
 0118            else { result = url; }
 119
 76120            return true;
 121        }
 122
 123        public virtual bool TryGetContentsUrl(string url, out string result)
 124        {
 110125            url = url.ToLower();
 110126            result = url;
 127
 166128            if (HasTestSchema(url)) { return true; }
 129
 54130            if (fileToHash != null)
 131            {
 54132                if (!fileToHash.ContainsKey(url))
 133                {
 39134                    if (VERBOSE) { Debug.LogError($"GetContentsUrl >>> File {url} not found!!!"); }
 135
 39136                    return false;
 137                }
 138
 15139                result = baseUrl + fileToHash[url];
 140            }
 0141            else { result = baseUrl + url; }
 142
 15143            if (VERBOSE) { Debug.Log($"GetContentsURL >>> from ... {url} ... RESULTING URL... = {result}"); }
 144
 15145            return true;
 146        }
 147
 148        public bool HasTestSchema(string url)
 149        {
 360150            url = url.ToLower();
 151
 152#if UNITY_EDITOR
 588153            if (url.StartsWith("file://")) { return true; }
 154
 132155            if (url.StartsWith(TestAssetsUtils.GetPath())) { return true; }
 156#endif
 132157            return false;
 158        }
 159
 160        public bool TryGetContentHash(string file, out string result)
 161        {
 0162            file = file.ToLower();
 0163            result = string.Empty;
 164
 0165            if (fileToHash == null)
 166            {
 0167                result = file;
 0168                return true;
 169            }
 170
 0171            if (fileToHash.TryGetValue(file.ToLower(), out result)) { return true; }
 172
 0173            return false;
 174        }
 175    }
 176}