| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Networking; |
| | 8 | |
|
| | 9 | | namespace MainScripts.DCL.AssetsEmbedment.Editor |
| | 10 | | { |
| | 11 | | public static class EditorAssetBundlesDownloader |
| | 12 | | { |
| | 13 | | public static async UniTask<bool> DownloadAssetBundleWithDependenciesAsync(string baseUrl, string hash, Dictiona |
| | 14 | | { |
| 0 | 15 | | if (loadedData.ContainsKey(hash)) |
| 0 | 16 | | return true; |
| | 17 | |
|
| 0 | 18 | | var url = $"{baseUrl}{hash}"; |
| | 19 | |
|
| | 20 | | try |
| | 21 | | { |
| | 22 | | // Passing no extra arguments will bypass caching and crc checks |
| 0 | 23 | | var wr = await UnityWebRequest.Get(url).SendWebRequest(); |
| | 24 | |
|
| 0 | 25 | | if (wr.WebRequestSucceded()) |
| | 26 | | { |
| 0 | 27 | | var data = wr.downloadHandler.data; |
| 0 | 28 | | var assetBundle = await AssetBundle.LoadFromMemoryAsync(data); |
| | 29 | |
|
| | 30 | | try |
| | 31 | | { |
| 0 | 32 | | var dependencies = await assetBundle.GetDependenciesAsync(baseUrl, hash, CancellationToken.None) |
| | 33 | |
|
| 0 | 34 | | var result = await UniTask.WhenAll(dependencies.Select(d => DownloadAssetBundleWithDependenciesA |
| | 35 | |
|
| 0 | 36 | | if (result.All(r => r)) |
| | 37 | | { |
| 0 | 38 | | loadedData.Add(hash, data); |
| 0 | 39 | | return true; |
| | 40 | | } |
| 0 | 41 | | } |
| | 42 | | finally |
| | 43 | | { |
| 0 | 44 | | if (assetBundle) |
| | 45 | | { |
| 0 | 46 | | assetBundle.Unload(true); |
| 0 | 47 | | Object.DestroyImmediate(assetBundle); |
| | 48 | | } |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | return false; |
| | 52 | | } |
| | 53 | |
|
| 0 | 54 | | Debug.LogError(wr.error); |
| 0 | 55 | | } |
| 0 | 56 | | catch (UnityWebRequestException) |
| | 57 | | { |
| 0 | 58 | | Debug.LogError($"Failed to download AB: {hash}. Url: {url}"); |
| | 59 | | // ignore for now |
| 0 | 60 | | return true; |
| | 61 | | } |
| | 62 | |
|
| 0 | 63 | | return false; |
| 0 | 64 | | } |
| | 65 | | } |
| | 66 | | } |