bug fix
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using System.IO;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public class FileLoader : IDataLoader2
|
||||
{
|
||||
private readonly string _rootDirectoryPath;
|
||||
|
||||
public FileLoader(string rootDirectoryPath)
|
||||
{
|
||||
_rootDirectoryPath = rootDirectoryPath;
|
||||
}
|
||||
|
||||
public Task<Stream> LoadStreamAsync(string relativeFilePath)
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
string path = Path.Combine(_rootDirectoryPath, relativeFilePath).Replace("\\", "/");
|
||||
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
path = Path.Combine(_rootDirectoryPath, Uri.UnescapeDataString(relativeFilePath)).Replace("\\", "/");
|
||||
}
|
||||
|
||||
if (UnityEditor.AssetDatabase.GetMainAssetTypeAtPath(path) == typeof(UnityEngine.Texture2D))
|
||||
{
|
||||
var stream = new GLTFSceneImporter.AssetDatabaseStream(path);
|
||||
return Task.FromResult((Stream) stream);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !WINDOWS_UWP && !UNITY_WEBGL
|
||||
// seems the Editor locks up in some cases when directly using Task.Run(() => {})
|
||||
if (UnityEngine.Application.isPlaying)
|
||||
{
|
||||
return Task.Run(() => LoadStream(relativeFilePath));
|
||||
}
|
||||
#endif
|
||||
return Task.FromResult(LoadStream(relativeFilePath));
|
||||
}
|
||||
|
||||
public Stream LoadStream(string relativeFilePath)
|
||||
{
|
||||
if (relativeFilePath == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(relativeFilePath));
|
||||
}
|
||||
|
||||
if (File.Exists(relativeFilePath))
|
||||
return File.OpenRead(relativeFilePath);
|
||||
|
||||
string pathToLoad = Path.Combine(_rootDirectoryPath, relativeFilePath);
|
||||
if (!File.Exists(pathToLoad))
|
||||
{
|
||||
pathToLoad = Path.Combine(_rootDirectoryPath, Uri.UnescapeDataString(relativeFilePath));
|
||||
}
|
||||
|
||||
if (!File.Exists(pathToLoad))
|
||||
{
|
||||
if (relativeFilePath.ToLowerInvariant().EndsWith(".bin"))
|
||||
throw new FileNotFoundException("Buffer file " + relativeFilePath + " not found in " + _rootDirectoryPath + ", complete path: " + pathToLoad, relativeFilePath);
|
||||
|
||||
UnityEngine.Debug.LogError("Buffer file " + relativeFilePath + " not found in " + _rootDirectoryPath + ", complete path: " + pathToLoad);
|
||||
return InvalidStream;
|
||||
}
|
||||
|
||||
return File.OpenRead(pathToLoad);
|
||||
}
|
||||
|
||||
internal static readonly Stream InvalidStream = new MemoryStream();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e1980a22c6cc4ce4e920022d7d9e941d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public interface IDataLoader
|
||||
{
|
||||
Task<Stream> LoadStreamAsync(string relativeFilePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fdb6a96a5ee34824685713dbad74cf51
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
using System.IO;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public interface IDataLoader2 : IDataLoader
|
||||
{
|
||||
Stream LoadStream(string relativeFilePath);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 88e0d77167dc3af489e57a17abcde939
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
[Obsolete("Please switch to IDataLoader. This interface is deprecated and will be removed in a future release.")]
|
||||
public interface ILoader
|
||||
{
|
||||
Task LoadStream(string relativeFilePath);
|
||||
|
||||
void LoadStreamSync(string jsonFilePath);
|
||||
|
||||
Stream LoadedStream { get; }
|
||||
|
||||
bool HasSyncLoadMethod { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba15ab25bba758f4e8867413d5011747
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,56 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
/// <summary>
|
||||
/// Wrapper utility for exposing an <see cref="ILoader"/> as a <see cref="IDataLoader"/> (and if appropriate, <see cref="IDataLoader2"/>).
|
||||
/// </summary>
|
||||
public static class LegacyLoaderWrapper
|
||||
{
|
||||
public static IDataLoader Wrap(ILoader loader)
|
||||
{
|
||||
if (loader == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (loader.HasSyncLoadMethod)
|
||||
{
|
||||
return new LegacyLoader2(loader);
|
||||
}
|
||||
return new LegacyLoader(loader);
|
||||
}
|
||||
|
||||
private class LegacyLoader : IDataLoader
|
||||
{
|
||||
protected readonly ILoader _loader;
|
||||
|
||||
public LegacyLoader(ILoader loader)
|
||||
{
|
||||
_loader = loader;
|
||||
}
|
||||
|
||||
public Task<Stream> LoadStreamAsync(string relativeFilePath)
|
||||
{
|
||||
_loader.LoadStream(relativeFilePath);
|
||||
return Task.FromResult(_loader.LoadedStream);
|
||||
}
|
||||
}
|
||||
|
||||
private class LegacyLoader2 : LegacyLoader, IDataLoader2
|
||||
{
|
||||
public LegacyLoader2(ILoader loader) :
|
||||
base(loader)
|
||||
{
|
||||
}
|
||||
|
||||
public Stream LoadStream(string relativeFilePath)
|
||||
{
|
||||
_loader.LoadStreamSync(relativeFilePath);
|
||||
return _loader.LoadedStream;
|
||||
}
|
||||
}
|
||||
}
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0ca569386b974e44d8f914fdd0781b2a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,39 @@
|
||||
#if WINDOWS_UWP
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Windows.Storage;
|
||||
using System;
|
||||
using System.Collections;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public class StorageFolderLoader : IDataLoader
|
||||
{
|
||||
private StorageFolder _rootFolder;
|
||||
|
||||
public StorageFolderLoader(StorageFolder rootFolder)
|
||||
{
|
||||
_rootFolder = rootFolder;
|
||||
}
|
||||
|
||||
public async Task<Stream> LoadStreamAsync(string relativeFilePath)
|
||||
{
|
||||
if (relativeFilePath == null)
|
||||
{
|
||||
throw new ArgumentNullException("relativeFilePath");
|
||||
}
|
||||
|
||||
StorageFolder parentFolder = _rootFolder;
|
||||
string fileName = Path.GetFileName(relativeFilePath);
|
||||
if (relativeFilePath != fileName)
|
||||
{
|
||||
string folderToLoad = relativeFilePath.Substring(0, relativeFilePath.Length - fileName.Length);
|
||||
parentFolder = await _rootFolder.GetFolderAsync(folderToLoad);
|
||||
}
|
||||
|
||||
StorageFile bufferFile = await parentFolder.GetFileAsync(fileName);
|
||||
return await bufferFile.OpenStreamForReadAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8e7fd7d6556ddd446903db465957f23d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,97 @@
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public class UnityWebRequestLoader : IDataLoader
|
||||
{
|
||||
private string dir;
|
||||
|
||||
public UnityWebRequestLoader(string dir)
|
||||
{
|
||||
this.dir = dir;
|
||||
}
|
||||
#if UNITY_WEBREQUEST
|
||||
public async Task<Stream> LoadStreamAsync(string relativeFilePath)
|
||||
{
|
||||
var path = Path.Combine(dir, relativeFilePath).Replace("\\","/");
|
||||
if (File.Exists(path))
|
||||
path = "file://" + Path.GetFullPath(path);
|
||||
var request = UnityWebRequest.Get(path);
|
||||
// request.downloadHandler = new DownloadStreamHandler(new byte[1024 * 1024]);
|
||||
var asyncOperation = request.SendWebRequest();
|
||||
|
||||
while (!asyncOperation.isDone) {
|
||||
await Task.Yield();
|
||||
}
|
||||
|
||||
#if UNITY_2020_1_OR_NEWER
|
||||
if (request.result != UnityWebRequest.Result.Success)
|
||||
#else
|
||||
if (request.isNetworkError || request.isHttpError)
|
||||
#endif
|
||||
{
|
||||
Debug.LogError($"Error when loading {relativeFilePath} ({path}): {request.error}");
|
||||
return null;
|
||||
}
|
||||
|
||||
var results = request.downloadHandler.data;
|
||||
var stream = new MemoryStream(results, 0, results.Length, false, true);
|
||||
return stream;
|
||||
}
|
||||
#else
|
||||
public async Task<Stream> LoadStreamAsync(string relativeFilePath)
|
||||
{
|
||||
await Task.CompletedTask;
|
||||
throw new System.ApplicationException("The module com.unity.modules.unitywebrequest is required for this functionality. Please install it in your project.");
|
||||
}
|
||||
#endif
|
||||
|
||||
// TODO: figure out how to do this correctly in a streaming fashion.
|
||||
// private class DownloadStreamHandler : DownloadHandlerScript
|
||||
// {
|
||||
// private ulong expectedTotalBytes = 0;
|
||||
// private Stream stream;
|
||||
// public Stream GetStream()
|
||||
// {
|
||||
// return stream;
|
||||
// }
|
||||
//
|
||||
// public DownloadStreamHandler(byte[] buffer) : base(buffer)
|
||||
// {
|
||||
// stream = new MemoryStream(buffer, true);
|
||||
// }
|
||||
//
|
||||
// protected override bool ReceiveData(byte[] data, int dataLength)
|
||||
// {
|
||||
// if(data == null || data.Length < 1)
|
||||
// {
|
||||
// Debug.Log("LoggingDownloadHandler :: ReceiveData - received a null/empty buffer");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// stream.Write(data, 0, dataLength);
|
||||
// return true;
|
||||
// }
|
||||
//
|
||||
// protected override byte[] GetData()
|
||||
// {
|
||||
// throw new System.NotSupportedException("This is a stream, can't get all bytes");
|
||||
// }
|
||||
//
|
||||
// protected override void CompleteContent()
|
||||
// {
|
||||
// Debug.Log("LoggingDownloadHandler :: CompleteContent - DOWNLOAD COMPLETE!");
|
||||
// }
|
||||
//
|
||||
// protected override void ReceiveContentLengthHeader(ulong contentLength)
|
||||
// {
|
||||
// Debug.Log(string.Format("LoggingDownloadHandler :: ReceiveContentLength - length {0}", contentLength));
|
||||
// expectedTotalBytes = contentLength;
|
||||
// stream.SetLength((long) contentLength);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f570680f27e849d59aeaa47194bfa5e4
|
||||
timeCreated: 1650878357
|
||||
@@ -0,0 +1,101 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
#if WINDOWS_UWP
|
||||
using Windows.Web.Http;
|
||||
using Windows.Security;
|
||||
using Windows.Storage.Streams;
|
||||
#else
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
#endif
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UnityGLTF.Loader
|
||||
{
|
||||
public class WebRequestLoader : IDataLoader
|
||||
{
|
||||
private readonly HttpClient httpClient = new HttpClient();
|
||||
private readonly Uri baseAddress;
|
||||
|
||||
public WebRequestLoader(string rootUri)
|
||||
{
|
||||
#if !WINDOWS_UWP
|
||||
ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
|
||||
#endif
|
||||
baseAddress = new Uri(rootUri);
|
||||
}
|
||||
|
||||
public async Task<Stream> LoadStreamAsync(string gltfFilePath)
|
||||
{
|
||||
if (gltfFilePath == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(gltfFilePath));
|
||||
}
|
||||
|
||||
HttpResponseMessage response;
|
||||
try
|
||||
{
|
||||
#if WINDOWS_UWP
|
||||
response = await httpClient.GetAsync(new Uri(baseAddress, gltfFilePath));
|
||||
#else
|
||||
var tokenSource = new CancellationTokenSource(30000);
|
||||
response = await httpClient.GetAsync(new Uri(baseAddress, gltfFilePath), tokenSource.Token);
|
||||
#endif
|
||||
}
|
||||
catch (TaskCanceledException)
|
||||
{
|
||||
#if WINDOWS_UWP
|
||||
throw new Exception("Connection timeout");
|
||||
#else
|
||||
throw new HttpRequestException("Connection timeout");
|
||||
#endif
|
||||
}
|
||||
|
||||
response.EnsureSuccessStatusCode();
|
||||
|
||||
// HACK: Download the whole file before returning the stream
|
||||
// Ideally the parsers would wait for data to be available, but they don't.
|
||||
var result = new MemoryStream((int?)response.Content.Headers.ContentLength ?? 5000);
|
||||
#if WINDOWS_UWP
|
||||
await response.Content.WriteToStreamAsync(result.AsOutputStream());
|
||||
#else
|
||||
await response.Content.CopyToAsync(result);
|
||||
#endif
|
||||
response.Dispose();
|
||||
return result;
|
||||
}
|
||||
|
||||
#if !WINDOWS_UWP
|
||||
// enables HTTPS support
|
||||
// https://answers.unity.com/questions/50013/httpwebrequestgetrequeststream-https-certificate-e.html
|
||||
private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||||
{
|
||||
bool isOk = true;
|
||||
// If there are errors in the certificate chain, look at each error to determine the cause.
|
||||
if (errors != SslPolicyErrors.None)
|
||||
{
|
||||
for (int i = 0; i<chain.ChainStatus.Length; i++)
|
||||
{
|
||||
if (chain.ChainStatus[i].Status != X509ChainStatusFlags.RevocationStatusUnknown)
|
||||
{
|
||||
chain.ChainPolicy.RevocationFlag = X509RevocationFlag.EntireChain;
|
||||
chain.ChainPolicy.RevocationMode = X509RevocationMode.Online;
|
||||
chain.ChainPolicy.UrlRetrievalTimeout = new TimeSpan(0, 1, 0);
|
||||
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
|
||||
bool chainIsValid = chain.Build((X509Certificate2)certificate);
|
||||
if (!chainIsValid)
|
||||
{
|
||||
isOk = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return isOk;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 23ded4d709de5b543811a96389760fc9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user