| | 1 | | using System; |
| | 2 | | using DCL; |
| | 3 | | using GLTFast; |
| | 4 | | using GLTFast.Loading; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Networking; |
| | 7 | |
|
| | 8 | | namespace DCL.GLTFast.Wrappers |
| | 9 | | { |
| | 10 | | public class GltfDownloaderWrapper : IDownload |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// First four bytes of a glTF-Binary file are made up of this signature |
| | 14 | | /// Represents glTF in ASCII |
| | 15 | | /// </summary> |
| | 16 | | private const uint GLB_SIGNATURE = 0x46546c67; |
| | 17 | |
|
| | 18 | | private readonly WebRequestAsyncOperation asyncOp; |
| | 19 | | private byte[] cachedData; |
| | 20 | | private bool isDisposed; |
| | 21 | |
|
| 0 | 22 | | public GltfDownloaderWrapper(WebRequestAsyncOperation asyncOp) |
| | 23 | | { |
| 0 | 24 | | this.asyncOp = asyncOp; |
| 0 | 25 | | } |
| | 26 | |
|
| 0 | 27 | | public bool Success => asyncOp.isSucceeded; |
| 0 | 28 | | public string Error => asyncOp.webRequest.error; |
| | 29 | |
|
| | 30 | | public byte[] Data |
| | 31 | | { |
| | 32 | | get |
| | 33 | | { |
| 0 | 34 | | if (isDisposed) return null; |
| 0 | 35 | | if (cachedData != null) return cachedData; |
| | 36 | |
|
| 0 | 37 | | UnityWebRequest asyncOpWebRequest = asyncOp.webRequest; |
| 0 | 38 | | if (asyncOpWebRequest != null) return asyncOpWebRequest.downloadHandler.data; |
| 0 | 39 | | Debug.LogWarning("The web request was disposed?" + asyncOp.isDisposed); |
| 0 | 40 | | return null; |
| | 41 | | } |
| | 42 | | } |
| | 43 | |
|
| 0 | 44 | | public string Text => asyncOp.webRequest.downloadHandler.text; |
| 0 | 45 | | public bool? IsBinary => IsGltfBinary(Data); |
| | 46 | |
|
| | 47 | | private static bool IsGltfBinary(byte[] data) |
| | 48 | | { |
| 0 | 49 | | var gltfBinarySignature = BitConverter.ToUInt32(data, 0); |
| 0 | 50 | | return gltfBinarySignature == GLB_SIGNATURE; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public bool MoveNext() => |
| 0 | 54 | | asyncOp.MoveNext(); |
| | 55 | |
|
| | 56 | | public void Dispose() |
| | 57 | | { |
| 0 | 58 | | isDisposed = true; |
| 0 | 59 | | cachedData = null; |
| 0 | 60 | | asyncOp.Dispose(); |
| 0 | 61 | | } |
| | 62 | | } |
| | 63 | | } |