| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Providers |
| | 9 | | { |
| | 10 | | public class AssetBundleResolver : AssetResolverBase, IAssetBundleResolver |
| | 11 | | { |
| | 12 | | private readonly IReadOnlyDictionary<AssetSource, IAssetBundleProvider> providers; |
| | 13 | | private readonly IAssetBundleProvider editorProvider; |
| | 14 | |
|
| | 15 | | public AssetBundleResolver(IReadOnlyDictionary<AssetSource, IAssetBundleProvider> providers, IAssetBundleProvide |
| 741 | 16 | | : base(featureFlags) |
| | 17 | | { |
| 741 | 18 | | this.providers = providers; |
| 741 | 19 | | this.editorProvider = editorProvider; |
| 741 | 20 | | } |
| | 21 | |
|
| | 22 | | public async UniTask<AssetBundle> GetAssetBundleAsync(AssetSource permittedSources, string contentUrl, |
| | 23 | | string hash, CancellationToken cancellationToken = default) |
| | 24 | | { |
| 29 | 25 | | Exception lastException = null; |
| | 26 | |
|
| 29 | 27 | | using var permittedProvidersPool = GetPermittedProviders(this.providers, permittedSources); |
| 29 | 28 | | var permittedProviders = permittedProvidersPool.GetList(); |
| | 29 | |
|
| | 30 | | #if UNITY_EDITOR |
| 29 | 31 | | permittedProviders.Insert(0, editorProvider); |
| | 32 | | #endif |
| | 33 | |
|
| 147 | 34 | | foreach (var provider in permittedProviders) |
| | 35 | | { |
| | 36 | | try |
| | 37 | | { |
| 116 | 38 | | var assetBundle = await provider.GetAssetBundleAsync(contentUrl, hash, cancellationToken); |
| | 39 | |
|
| 51 | 40 | | if (assetBundle) |
| | 41 | | { |
| 22 | 42 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Asset Bundle {hash} loaded from {pro |
| 22 | 43 | | return assetBundle; |
| | 44 | | } |
| 29 | 45 | | } |
| | 46 | |
|
| | 47 | | // Propagate `OperationCanceledException` further as there is no reason to iterate |
| 7 | 48 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 49 | | { |
| 2 | 50 | | AssetResolverLogger.LogVerbose(featureFlags, e); |
| 2 | 51 | | lastException = e; |
| 2 | 52 | | } |
| 31 | 53 | | } |
| | 54 | |
|
| 2 | 55 | | lastException ??= new AssetNotFoundException(permittedSources, hash); |
| 2 | 56 | | throw lastException; |
| 22 | 57 | | } |
| | 58 | | } |
| | 59 | | } |