| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 3 | | using System; |
| | 4 | | using System.Text; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | | using UnityEngine.Pool; |
| | 8 | |
|
| | 9 | | namespace DCL.Providers |
| | 10 | | { |
| | 11 | | public class AssetBundleWebLoader : AssetBundleWebRequestBasedProvider, IAssetBundleProvider |
| | 12 | | { |
| | 13 | | private const string CACHING_FEATURE_FLAG = "ab_caching"; |
| | 14 | |
|
| | 15 | | private Service<IWebRequestController> webRequestController; |
| | 16 | | private readonly DataStore_FeatureFlag featureFlags; |
| | 17 | | private readonly DataStore_Performance performance; |
| | 18 | |
|
| 29 | 19 | | private static int maxConcurrentRequests => CommonScriptableObjects.rendererState.Get() ? 30 : 256; |
| 58 | 20 | | private bool cachingEnabled => featureFlags.flags.Get().IsFeatureEnabled(CACHING_FEATURE_FLAG); |
| | 21 | |
|
| 741 | 22 | | public AssetBundleWebLoader(DataStore_FeatureFlag featureFlags, DataStore_Performance performance) |
| | 23 | | { |
| 741 | 24 | | this.featureFlags = featureFlags; |
| 741 | 25 | | this.performance = performance; |
| 741 | 26 | | } |
| | 27 | |
|
| | 28 | | public async UniTask<AssetBundle> GetAssetBundleAsync(string contentUrl, string hash, CancellationToken cancella |
| | 29 | | { |
| 87 | 30 | | await WaitForConcurrentRequestsSlot(); |
| 29 | 31 | | performance.concurrentABRequests.Set(performance.concurrentABRequests.Get() + 1); |
| | 32 | |
|
| | 33 | | try |
| | 34 | | { |
| 29 | 35 | | var url = contentUrl + hash; |
| 29 | 36 | | var hash128 = ComputeHash(contentUrl, hash); |
| | 37 | |
|
| 29 | 38 | | if (cachingEnabled) |
| 0 | 39 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Asset Bundle {hash} is cached..."); |
| | 40 | |
|
| 29 | 41 | | using var webRequest = cachingEnabled |
| | 42 | | ? webRequestController.Ref.GetAssetBundle(url, hash: hash128, disposeOnCompleted: false) |
| | 43 | | : webRequestController.Ref.GetAssetBundle(url, disposeOnCompleted: false); |
| | 44 | |
|
| 79 | 45 | | return await FromWebRequestAsync(webRequest, url, cancellationToken); |
| | 46 | | } |
| | 47 | | finally |
| | 48 | | { |
| 29 | 49 | | performance.concurrentABRequests.Set(performance.concurrentABRequests.Get() - 1); |
| | 50 | | } |
| 22 | 51 | | } |
| | 52 | |
|
| | 53 | | // According to https://adr.decentraland.org/adr/ADR-11 |
| | 54 | | // we use /vX for versioning so caching system should respect it |
| | 55 | | internal static Hash128 ComputeHash(string contentUrl, string hash) |
| | 56 | | { |
| 29 | 57 | | var hashBuilder = GenericPool<StringBuilder>.Get(); |
| 29 | 58 | | hashBuilder.Clear(); |
| 29 | 59 | | hashBuilder.Append(hash); |
| | 60 | |
|
| 29 | 61 | | var span = contentUrl.AsSpan(); |
| | 62 | |
|
| | 63 | | // content URL always ends with '/' |
| | 64 | | int indexOfVersionStart; |
| | 65 | |
|
| 754 | 66 | | for (indexOfVersionStart = span.Length - 2; span[indexOfVersionStart] != '/'; indexOfVersionStart--) { } |
| | 67 | |
|
| 29 | 68 | | indexOfVersionStart++; |
| | 69 | |
|
| 29 | 70 | | if (span[indexOfVersionStart] == 'v') |
| 0 | 71 | | hashBuilder.Insert(0, span.Slice(indexOfVersionStart, span.Length - indexOfVersionStart - 1)); |
| | 72 | |
|
| 29 | 73 | | var hash128 = Hash128.Compute(hashBuilder.ToString()); |
| 29 | 74 | | GenericPool<StringBuilder>.Release(hashBuilder); |
| 29 | 75 | | return hash128; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | private UniTask WaitForConcurrentRequestsSlot() |
| | 79 | | { |
| 58 | 80 | | return UniTask.WaitUntil(() => performance.concurrentABRequests.Get() < maxConcurrentRequests); |
| | 81 | | } |
| | 82 | | } |
| | 83 | | } |