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