< Summary

Class:DependencyMapLoadHelper
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/DependencyMapLoadHelper.cs
Covered lines:43
Uncovered lines:6
Coverable lines:49
Total lines:125
Line coverage:87.7% (43 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
DependencyMapLoadHelper()0%110100%
WaitUntilDepMapIsResolved()0%550100%
GetDepMap()0%8.817066.67%
SavePersistentCache()0%110100%
LoadPersistentCache()0%3.023087.5%
RendererState_OnChange(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/DependencyMapLoadHelper.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using System.Collections;
 4using System.Collections.Generic;
 5using System.Linq;
 6using Newtonsoft.Json;
 7using UnityEngine;
 8using UnityEngine.Networking;
 9using WaitUntil = DCL.WaitUntil;
 10using DCL;
 11using Environment = System.Environment;
 12
 13public static class DependencyMapLoadHelper
 14{
 115    static bool VERBOSE = false;
 16
 17    private const string PERSISTENT_CACHE_KEY = "DepMapCache_V2";
 18    private const float MIN_TIME_BETWEEN_SAVING_PERSISTENT_CACHE = 300.0f;
 19
 120    private static bool persistentCacheLoaded = false;
 121    private static float lastTimeSavedPersistentCache = 0;
 22
 123    public static Dictionary<string, List<string>> dependenciesMap = new Dictionary<string, List<string>>();
 24
 125    static HashSet<string> failedRequests = new HashSet<string>();
 126    static HashSet<string> downloadingDepmap = new HashSet<string>();
 27
 28    [System.Serializable]
 29    public class AssetDependencyMap
 30    {
 31        public string[] dependencies;
 32    }
 33
 34    public static IEnumerator WaitUntilDepMapIsResolved(string hash)
 35    {
 536        while (true)
 37        {
 2438            bool depmapBeingDownloaded = downloadingDepmap.Contains(hash);
 2439            bool depmapRequestIsDone = dependenciesMap.ContainsKey(hash) || failedRequests.Contains(hash);
 40
 2441            if (!depmapBeingDownloaded && depmapRequestIsDone)
 42                break;
 43
 544            yield return null;
 45        }
 1946    }
 47
 48    public static IEnumerator GetDepMap(string baseUrl, string hash)
 49    {
 550        string url = baseUrl + hash + ".depmap";
 51
 552        LoadPersistentCache();
 53
 554        if (dependenciesMap.ContainsKey(hash))
 055            yield break;
 56
 557        if (failedRequests.Contains(hash))
 058            yield break;
 59
 560        if (downloadingDepmap.Contains(hash))
 61        {
 062            yield return WaitUntilDepMapIsResolved(hash);
 063            yield break;
 64        }
 65
 566        downloadingDepmap.Add(hash);
 567        yield return DCL.Environment.i.platform.webRequest.Get(
 68            url: url,
 69            OnSuccess: (depmapRequest) =>
 70            {
 271                AssetDependencyMap map = JsonUtility.FromJson<AssetDependencyMap>(depmapRequest.downloadHandler.text);
 372                map.dependencies = map.dependencies.Where(x => !x.Contains("mainshader")).ToArray();
 73
 274                dependenciesMap.Add(hash, new List<string>(map.dependencies));
 75
 276                downloadingDepmap.Remove(hash);
 77
 278                if (DCLTime.realtimeSinceStartup - lastTimeSavedPersistentCache >= MIN_TIME_BETWEEN_SAVING_PERSISTENT_CA
 79                {
 080                    SavePersistentCache();
 81                }
 282            },
 83            OnFail: (depmapRequest) =>
 84            {
 385                failedRequests.Add(hash);
 386                downloadingDepmap.Remove(hash);
 387            });
 588    }
 89
 90    private static void SavePersistentCache()
 91    {
 24892        lastTimeSavedPersistentCache = DCLTime.realtimeSinceStartup;
 93
 94        //NOTE(Brian): Use JsonConvert because unity JsonUtility doesn't support dictionaries
 24895        string cacheJson = JsonConvert.SerializeObject(dependenciesMap);
 24896        PlayerPrefsUtils.SetString(PERSISTENT_CACHE_KEY, cacheJson);
 24897    }
 98
 99    private static void LoadPersistentCache()
 100    {
 5101        if (persistentCacheLoaded)
 4102            return;
 103
 1104        persistentCacheLoaded = true;
 1105        CommonScriptableObjects.rendererState.OnChange += RendererState_OnChange;
 106
 107        // Beware that if a wrongly-constructed depmap was created previously, this will always load that depmap
 108        // when testing a new dump process locally it's safer to first run Edit->ClearAllPlayerPrefs from UnityEditor
 1109        string depMapCache = PlayerPrefs.GetString(PERSISTENT_CACHE_KEY, String.Empty);
 110
 1111        if (!string.IsNullOrEmpty(depMapCache))
 112        {
 0113            dependenciesMap = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(depMapCache);
 114        }
 1115    }
 116
 117    private static void RendererState_OnChange(bool current, bool previous)
 118    {
 248119        if (persistentCacheLoaded)
 120        {
 121            // Once the persistent cache has been loaded the first time, it will go being saved each time the RendererSt
 248122            SavePersistentCache();
 123        }
 248124    }
 125}