| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | public static class DependencyMapLoadHelper |
| | 7 | | { |
| 1 | 8 | | static bool VERBOSE = false; |
| | 9 | | private const string MAIN_SHADER_FILENAME = "mainshader"; |
| | 10 | |
|
| 1 | 11 | | public static Dictionary<string, List<string>> dependenciesMap = new Dictionary<string, List<string>>(); |
| | 12 | |
|
| 1 | 13 | | static HashSet<string> failedRequests = new HashSet<string>(); |
| 1 | 14 | | static HashSet<string> downloadingDepmap = new HashSet<string>(); |
| | 15 | |
|
| | 16 | | [System.Serializable] |
| | 17 | | public class AssetDependencyMap |
| | 18 | | { |
| | 19 | | public string[] dependencies; |
| | 20 | | } |
| | 21 | |
|
| | 22 | | public static IEnumerator WaitUntilDepMapIsResolved(string hash) |
| | 23 | | { |
| 5 | 24 | | while (true) |
| | 25 | | { |
| 24 | 26 | | bool depmapBeingDownloaded = downloadingDepmap.Contains(hash); |
| 24 | 27 | | bool depmapRequestIsDone = dependenciesMap.ContainsKey(hash) || failedRequests.Contains(hash); |
| | 28 | |
|
| 24 | 29 | | if (!depmapBeingDownloaded && depmapRequestIsDone) |
| | 30 | | break; |
| | 31 | |
|
| 5 | 32 | | yield return null; |
| | 33 | | } |
| 19 | 34 | | } |
| | 35 | |
|
| | 36 | | public static IEnumerator GetDepMap(string baseUrl, string hash) |
| | 37 | | { |
| 5 | 38 | | if (dependenciesMap.ContainsKey(hash)) |
| 0 | 39 | | yield break; |
| | 40 | |
|
| 5 | 41 | | if (failedRequests.Contains(hash)) |
| 0 | 42 | | yield break; |
| | 43 | |
|
| 5 | 44 | | if (downloadingDepmap.Contains(hash)) |
| | 45 | | { |
| 0 | 46 | | yield return WaitUntilDepMapIsResolved(hash); |
| 0 | 47 | | yield break; |
| | 48 | | } |
| | 49 | |
|
| 5 | 50 | | string url = baseUrl + hash + ".depmap"; |
| | 51 | |
|
| 5 | 52 | | downloadingDepmap.Add(hash); |
| 5 | 53 | | yield return DCL.Environment.i.platform.webRequest.Get( |
| | 54 | | url: url, |
| | 55 | | OnSuccess: (depmapRequest) => |
| | 56 | | { |
| 2 | 57 | | AssetDependencyMap map = JsonUtility.FromJson<AssetDependencyMap>(depmapRequest.downloadHandler.text); |
| 3 | 58 | | map.dependencies = map.dependencies.Where(x => !x.Contains(MAIN_SHADER_FILENAME)).ToArray(); |
| | 59 | |
|
| 2 | 60 | | dependenciesMap.Add(hash, new List<string>(map.dependencies)); |
| | 61 | |
|
| 2 | 62 | | downloadingDepmap.Remove(hash); |
| 2 | 63 | | }, |
| | 64 | | OnFail: (depmapRequest) => |
| | 65 | | { |
| 3 | 66 | | failedRequests.Add(hash); |
| 3 | 67 | | downloadingDepmap.Remove(hash); |
| 3 | 68 | | }); |
| 5 | 69 | | } |
| | 70 | | } |