| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using System.Collections; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using Newtonsoft.Json; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.Networking; |
| | 9 | | using WaitUntil = DCL.WaitUntil; |
| | 10 | | using DCL; |
| | 11 | | using Environment = System.Environment; |
| | 12 | |
|
| | 13 | | public static class DependencyMapLoadHelper |
| | 14 | | { |
| 1 | 15 | | 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 | |
|
| 1 | 20 | | private static bool persistentCacheLoaded = false; |
| 1 | 21 | | private static float lastTimeSavedPersistentCache = 0; |
| | 22 | |
|
| 1 | 23 | | public static Dictionary<string, List<string>> dependenciesMap = new Dictionary<string, List<string>>(); |
| | 24 | |
|
| 1 | 25 | | static HashSet<string> failedRequests = new HashSet<string>(); |
| 1 | 26 | | 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 | | { |
| 5 | 36 | | while (true) |
| | 37 | | { |
| 24 | 38 | | bool depmapBeingDownloaded = downloadingDepmap.Contains(hash); |
| 24 | 39 | | bool depmapRequestIsDone = dependenciesMap.ContainsKey(hash) || failedRequests.Contains(hash); |
| | 40 | |
|
| 24 | 41 | | if (!depmapBeingDownloaded && depmapRequestIsDone) |
| | 42 | | break; |
| | 43 | |
|
| 5 | 44 | | yield return null; |
| | 45 | | } |
| 19 | 46 | | } |
| | 47 | |
|
| | 48 | | public static IEnumerator GetDepMap(string baseUrl, string hash) |
| | 49 | | { |
| 5 | 50 | | string url = baseUrl + hash + ".depmap"; |
| | 51 | |
|
| 5 | 52 | | LoadPersistentCache(); |
| | 53 | |
|
| 5 | 54 | | if (dependenciesMap.ContainsKey(hash)) |
| 0 | 55 | | yield break; |
| | 56 | |
|
| 5 | 57 | | if (failedRequests.Contains(hash)) |
| 0 | 58 | | yield break; |
| | 59 | |
|
| 5 | 60 | | if (downloadingDepmap.Contains(hash)) |
| | 61 | | { |
| 0 | 62 | | yield return WaitUntilDepMapIsResolved(hash); |
| 0 | 63 | | yield break; |
| | 64 | | } |
| | 65 | |
|
| 5 | 66 | | downloadingDepmap.Add(hash); |
| 5 | 67 | | yield return DCL.Environment.i.platform.webRequest.Get( |
| | 68 | | url: url, |
| | 69 | | OnSuccess: (depmapRequest) => |
| | 70 | | { |
| 2 | 71 | | AssetDependencyMap map = JsonUtility.FromJson<AssetDependencyMap>(depmapRequest.downloadHandler.text); |
| 3 | 72 | | map.dependencies = map.dependencies.Where(x => !x.Contains("mainshader")).ToArray(); |
| | 73 | |
|
| 2 | 74 | | dependenciesMap.Add(hash, new List<string>(map.dependencies)); |
| | 75 | |
|
| 2 | 76 | | downloadingDepmap.Remove(hash); |
| | 77 | |
|
| 2 | 78 | | if (DCLTime.realtimeSinceStartup - lastTimeSavedPersistentCache >= MIN_TIME_BETWEEN_SAVING_PERSISTENT_CA |
| | 79 | | { |
| 0 | 80 | | SavePersistentCache(); |
| | 81 | | } |
| 2 | 82 | | }, |
| | 83 | | OnFail: (depmapRequest) => |
| | 84 | | { |
| 3 | 85 | | failedRequests.Add(hash); |
| 3 | 86 | | downloadingDepmap.Remove(hash); |
| 3 | 87 | | }); |
| 5 | 88 | | } |
| | 89 | |
|
| | 90 | | private static void SavePersistentCache() |
| | 91 | | { |
| 248 | 92 | | lastTimeSavedPersistentCache = DCLTime.realtimeSinceStartup; |
| | 93 | |
|
| | 94 | | //NOTE(Brian): Use JsonConvert because unity JsonUtility doesn't support dictionaries |
| 248 | 95 | | string cacheJson = JsonConvert.SerializeObject(dependenciesMap); |
| 248 | 96 | | PlayerPrefsUtils.SetString(PERSISTENT_CACHE_KEY, cacheJson); |
| 248 | 97 | | } |
| | 98 | |
|
| | 99 | | private static void LoadPersistentCache() |
| | 100 | | { |
| 5 | 101 | | if (persistentCacheLoaded) |
| 4 | 102 | | return; |
| | 103 | |
|
| 1 | 104 | | persistentCacheLoaded = true; |
| 1 | 105 | | 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 |
| 1 | 109 | | string depMapCache = PlayerPrefs.GetString(PERSISTENT_CACHE_KEY, String.Empty); |
| | 110 | |
|
| 1 | 111 | | if (!string.IsNullOrEmpty(depMapCache)) |
| | 112 | | { |
| 0 | 113 | | dependenciesMap = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(depMapCache); |
| | 114 | | } |
| 1 | 115 | | } |
| | 116 | |
|
| | 117 | | private static void RendererState_OnChange(bool current, bool previous) |
| | 118 | | { |
| 248 | 119 | | if (persistentCacheLoaded) |
| | 120 | | { |
| | 121 | | // Once the persistent cache has been loaded the first time, it will go being saved each time the RendererSt |
| 248 | 122 | | SavePersistentCache(); |
| | 123 | | } |
| 248 | 124 | | } |
| | 125 | | } |