| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | public static class AssetBundleDepMapLoadHelper |
| | 10 | | { |
| 1 | 11 | | static bool VERBOSE = false; |
| | 12 | | private const string MAIN_SHADER_FILENAME = "mainshader"; |
| | 13 | |
|
| 1 | 14 | | public static Dictionary<string, List<string>> dependenciesMap = new (); |
| | 15 | |
|
| 1 | 16 | | static HashSet<string> failedRequests = new (); |
| 1 | 17 | | static HashSet<string> downloadingDepmap = new (); |
| | 18 | |
|
| | 19 | | public static async UniTask<IReadOnlyList<string>> GetDependenciesAsync(this AssetBundle assetBundle, string baseUrl |
| | 20 | | { |
| | 21 | | // Check internal metadata file (dependencies, version, timestamp) and if it doesn't exist, fetch the external d |
| 22 | 22 | | TextAsset metadata = Asset_AB.GetMetadata(assetBundle); |
| | 23 | |
|
| 22 | 24 | | if (metadata != null) |
| 5 | 25 | | LoadDepMapFromJSON(metadata.text, hash); |
| | 26 | | else |
| | 27 | | { |
| 17 | 28 | | if (!dependenciesMap.ContainsKey(hash)) |
| 10 | 29 | | await LoadExternalDepMap(baseUrl, hash, cancellationToken); |
| | 30 | | } |
| | 31 | |
|
| 22 | 32 | | if (!dependenciesMap.TryGetValue(hash, out List<string> dependencies)) |
| 2 | 33 | | dependencies = new List<string>(); |
| | 34 | |
|
| 22 | 35 | | return dependencies; |
| 22 | 36 | | } |
| | 37 | |
|
| | 38 | | public static UniTask WaitUntilExternalDepMapIsResolved(string hash) |
| | 39 | | { |
| 0 | 40 | | return UniTask.WaitUntil(() => !downloadingDepmap.Contains(hash) && (dependenciesMap.ContainsKey(hash) || failed |
| | 41 | | } |
| | 42 | |
|
| | 43 | | public static async UniTask LoadExternalDepMap(string baseUrl, string hash, CancellationToken cancellationToken) |
| | 44 | | { |
| 4 | 45 | | if (dependenciesMap.ContainsKey(hash)) |
| 0 | 46 | | return; |
| | 47 | |
|
| 4 | 48 | | if (failedRequests.Contains(hash)) |
| 1 | 49 | | return; |
| | 50 | |
|
| 3 | 51 | | if (downloadingDepmap.Contains(hash)) |
| | 52 | | { |
| 0 | 53 | | await WaitUntilExternalDepMapIsResolved(hash); |
| 0 | 54 | | return; |
| | 55 | | } |
| | 56 | |
|
| 3 | 57 | | string url = baseUrl + hash + ".depmap"; |
| | 58 | |
|
| 3 | 59 | | downloadingDepmap.Add(hash); |
| | 60 | |
|
| 9 | 61 | | await DCL.Environment.i.platform.webRequest.Get( |
| | 62 | | url: url, |
| | 63 | | OnSuccess: (depmapRequest) => |
| | 64 | | { |
| 2 | 65 | | LoadDepMapFromJSON(depmapRequest.webRequest.downloadHandler.text, hash); |
| | 66 | |
|
| 2 | 67 | | downloadingDepmap.Remove(hash); |
| 2 | 68 | | }, |
| | 69 | | OnFail: _ => |
| | 70 | | { |
| 1 | 71 | | failedRequests.Add(hash); |
| 1 | 72 | | downloadingDepmap.Remove(hash); |
| 1 | 73 | | }).WithCancellation(cancellationToken); |
| 4 | 74 | | } |
| | 75 | |
|
| | 76 | | public static void LoadDepMapFromJSON(string metadataJSON, string hash) |
| | 77 | | { |
| 7 | 78 | | AssetBundleMetadata metadata = JsonUtility.FromJson<AssetBundleMetadata>(metadataJSON); |
| | 79 | |
|
| 7 | 80 | | if (VERBOSE) |
| | 81 | | { |
| 0 | 82 | | Debug.Log($"DependencyMapLoadHelper: {hash} asset bundle version: " + metadata.version); |
| 0 | 83 | | Debug.Log($"DependencyMapLoadHelper: {hash} asset bundle timestamp: " + (metadata.timestamp > 0 ? new DateTi |
| | 84 | | } |
| | 85 | |
|
| 8 | 86 | | metadata.dependencies = metadata.dependencies.Where(x => !x.Contains(MAIN_SHADER_FILENAME)).ToArray(); |
| | 87 | |
|
| 7 | 88 | | dependenciesMap[hash] = new List<string>(metadata.dependencies); |
| 7 | 89 | | } |
| | 90 | | } |