| | 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 UnityWebRequest webRequest; |
| | 19 | | private byte[] cachedData; |
| | 20 | | private bool isDisposed; |
| | 21 | |
|
| 100 | 22 | | public GltfDownloaderWrapper(UnityWebRequest webRequest) |
| | 23 | | { |
| 100 | 24 | | this.webRequest = webRequest; |
| 100 | 25 | | } |
| | 26 | |
|
| 200 | 27 | | public bool Success => webRequest.result == UnityWebRequest.Result.Success; |
| 0 | 28 | | public string Error => webRequest.error; |
| | 29 | |
|
| | 30 | | public byte[] Data |
| | 31 | | { |
| | 32 | | get |
| | 33 | | { |
| 182 | 34 | | if (isDisposed) return null; |
| 182 | 35 | | if (cachedData != null) return cachedData; |
| | 36 | |
|
| 364 | 37 | | if (webRequest != null) return webRequest.downloadHandler.data; |
| 0 | 38 | | return null; |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| 12 | 42 | | public string Text => webRequest.downloadHandler.text; |
| 97 | 43 | | public bool? IsBinary => IsGltfBinary(Data); |
| | 44 | |
|
| | 45 | | private static bool IsGltfBinary(byte[] data) |
| | 46 | | { |
| 97 | 47 | | if (data == null) return false; |
| 97 | 48 | | var gltfBinarySignature = BitConverter.ToUInt32(data, 0); |
| 97 | 49 | | return gltfBinarySignature == GLB_SIGNATURE; |
| | 50 | | } |
| | 51 | |
|
| | 52 | | public void Dispose() |
| | 53 | | { |
| 194 | 54 | | isDisposed = true; |
| 194 | 55 | | cachedData = null; |
| 194 | 56 | | webRequest.Dispose(); |
| 194 | 57 | | } |
| | 58 | | } |
| | 59 | | } |