| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Threading.Tasks; |
| | 4 | | using GLTFast.Loading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Networking; |
| | 7 | |
|
| | 8 | | namespace DCL.GLTFast.Wrappers |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// With this class we replace all of GLTFast web requests with our own |
| | 12 | | /// </summary> |
| | 13 | | internal class GltFastDownloadProvider : IDownloadProvider, IDisposable |
| | 14 | | { |
| | 15 | | private readonly IWebRequestController webRequestController; |
| | 16 | | private readonly AssetIdConverter fileToUrl; |
| 0 | 17 | | private List<IDisposable> disposables = new (); |
| | 18 | |
|
| 0 | 19 | | public GltFastDownloadProvider(IWebRequestController webRequestController, AssetIdConverter fileToUrl) |
| | 20 | | { |
| 0 | 21 | | this.webRequestController = webRequestController; |
| 0 | 22 | | this.fileToUrl = fileToUrl; |
| 0 | 23 | | } |
| | 24 | |
|
| | 25 | | public async Task<IDownload> Request(Uri uri) |
| | 26 | | { |
| 0 | 27 | | string finalUrl = uri.OriginalString; |
| | 28 | |
|
| 0 | 29 | | string fileName = uri.AbsolutePath.Substring(uri.AbsolutePath.LastIndexOf('/') + 1); |
| | 30 | |
|
| 0 | 31 | | if (fileToUrl(fileName, out string url)) { finalUrl = url; } |
| | 32 | |
|
| 0 | 33 | | WebRequestAsyncOperation asyncOp = (WebRequestAsyncOperation)webRequestController.Get( |
| | 34 | | url: finalUrl, |
| | 35 | | downloadHandler: new DownloadHandlerBuffer(), |
| | 36 | | timeout: 30, |
| | 37 | | disposeOnCompleted: false, |
| | 38 | | requestAttemps: 3); |
| | 39 | |
|
| 0 | 40 | | GltfDownloaderWrapper wrapper = new GltfDownloaderWrapper(asyncOp); |
| 0 | 41 | | disposables.Add(wrapper); |
| | 42 | |
|
| 0 | 43 | | while (wrapper.MoveNext()) { await Task.Yield(); } |
| | 44 | |
|
| 0 | 45 | | if (!wrapper.success) { Debug.LogError($"<color=Red>[GLTFast WebRequest Failed]</color> {asyncOp.asyncOp.web |
| | 46 | |
|
| 0 | 47 | | return wrapper; |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | public async Task<ITextureDownload> RequestTexture(Uri uri, bool nonReadable) |
| | 51 | | { |
| 0 | 52 | | string fileName = uri.AbsolutePath.Substring(uri.AbsolutePath.LastIndexOf('/') + 1); |
| 0 | 53 | | fileToUrl(fileName, out string url); |
| | 54 | |
|
| 0 | 55 | | WebRequestAsyncOperation asyncOp = webRequestController.GetTexture( |
| | 56 | | url: url, |
| | 57 | | timeout: 30, |
| | 58 | | disposeOnCompleted: false, |
| | 59 | | requestAttemps: 3); |
| | 60 | |
|
| 0 | 61 | | GltfTextureDownloaderWrapper wrapper = new GltfTextureDownloaderWrapper(asyncOp); |
| 0 | 62 | | disposables.Add(wrapper); |
| | 63 | |
|
| 0 | 64 | | while (wrapper.MoveNext()) { await Task.Yield(); } |
| | 65 | |
|
| 0 | 66 | | if (!wrapper.success) { Debug.LogError("[GLTFast Texture WebRequest Failed] " + asyncOp.asyncOp.webRequest.u |
| | 67 | |
|
| 0 | 68 | | return wrapper; |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | public void Dispose() |
| | 72 | | { |
| 0 | 73 | | foreach (IDisposable disposable in disposables) { disposable.Dispose(); } |
| | 74 | |
|
| 0 | 75 | | disposables = null; |
| 0 | 76 | | } |
| | 77 | | } |
| | 78 | | } |