public IEnumerator <Null> NextPage(List <PolyAssetCatalog.AssetDetails> files, string thumbnailSuffix) { string uri = m_PageToken == null ? m_Uri : String.Format("{0}&page_token={1}", m_Uri, m_PageToken); WebRequest request = new WebRequest(uri, App.GoogleIdentity, UnityWebRequest.kHttpVerbGET); using (var cr = request.SendAsync().AsIeNull()) { while (!request.Done) { try { cr.MoveNext(); } catch (VrAssetServiceException e) { e.UserFriendly = m_ErrorMessage; throw; } yield return(cr.Current); } } Future <JObject> f = new Future <JObject>(() => JObject.Parse(request.Result)); JObject json; while (!f.TryGetResult(out json)) { yield return(null); } if (json.Count == 0) { yield break; } JToken lastAsset = null; var assets = json["assets"] ?? json["userAssets"]; foreach (JToken possibleAsset in assets) { try { // User assets are nested in an 'asset' node. JToken asset = possibleAsset["asset"] ?? possibleAsset; if (asset["visibility"].ToString() == "PRIVATE") { continue; } // We now don't filter the liked Poly objects, but we don't want to return liked Tilt Brush // sketches so in this section we filter out anything with a Tilt file in it. // Also, although currently all Poly objects have a GLTF representation we should probably // not rely on that continuing, so we discard anything that doesn't have a GLTF (1) // representation. We look for PGLTF and GLTF as for a lot of objects Poly is returning // PGLTF without GLTF. bool skipObject = false; foreach (var format in asset["formats"]) { var formatType = format["formatType"].ToString(); if (formatType == "TILT") { skipObject = true; break; } } if (skipObject) { continue; } lastAsset = asset; string accountName = asset["authorName"]?.ToString() ?? "Unknown"; files.Add(new PolyAssetCatalog.AssetDetails(asset, accountName, thumbnailSuffix)); } catch (NullReferenceException) { UnityEngine.Debug.LogErrorFormat("Failed to load asset: {0}", lastAsset == null ? "NULL" : lastAsset.ToString()); } yield return(null); } JToken jPageToken = json["nextPageToken"]; m_PageToken = jPageToken != null?jPageToken.ToString() : null; }
public Awaiter(Future <T> future) { m_future = future; }
// Initiates the contact with Poly. public IEnumerator <Null> GetAssetCoroutine() { OAuth2Identity identity = null; if (!m_URI.StartsWith(VrAssetService.ApiHost)) { m_Asset.SetRootElement(UnityWebRequest.EscapeURL(m_URI), m_URI); } else { identity = App.GoogleIdentity; if (string.IsNullOrEmpty(App.Config.GoogleSecrets?.ApiKey)) { IsCanceled = true; yield break; } m_Ready = false; WebRequest initialRequest = new WebRequest(m_URI, App.GoogleIdentity, UnityWebRequest.kHttpVerbGET); using (var cr = initialRequest.SendAsync().AsIeNull()) { while (!initialRequest.Done) { try { cr.MoveNext(); } catch (VrAssetServiceException e) { Debug.LogException(e); IsCanceled = true; yield break; } yield return(cr.Current); } } // Deserialize request string in to an Asset class. MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(initialRequest.Result)); using (var jsonReader = new JsonTextReader(new StreamReader(memStream))) { Future <JObject> f = new Future <JObject>(() => JObject.Parse(initialRequest.Result)); JObject json; while (!f.TryGetResult(out json)) { yield return(null); } if (json.Count == 0) { Debug.LogErrorFormat("Failed to deserialize response for {0}", m_URI); yield break; } // Find the asset by looking through the format list for the specified type. VrAssetFormat requestedType = m_Asset.DesiredType; while (true) { var format = json["formats"]?.FirstOrDefault(x => x["formatType"]?.ToString() == requestedType.ToString()); if (format != null) { string internalRootFilePath = format["root"]?["relativePath"].ToString(); // If we successfully get a gltf2 format file, internally change the extension to // "gltf2" so that the cache knows that it is a gltf2 file. if (requestedType == VrAssetFormat.GLTF2) { internalRootFilePath = Path.ChangeExtension(internalRootFilePath, "gltf2"); } // Get root element info. m_Asset.SetRootElement( internalRootFilePath, format["root"]?["url"].ToString()); // Get all resource infos. There may be zero. foreach (var r in format["resources"]) { string path = r["relativePath"].ToString(); m_Asset.AddResourceElement(path, r["url"].ToString()); // The root element should be the only gltf file. Debug.Assert(!path.EndsWith(".gltf") && !path.EndsWith(".gltf2"), string.Format("Found extra gltf resource: {0}", path)); } break; } else { // We asked for an asset in a format that it doesn't have. // In some cases, we should look for a different format as backup. if (requestedType == VrAssetFormat.GLTF2) { requestedType = VrAssetFormat.GLTF; } else { // In other cases, we should fail and get out. Debug.LogErrorFormat("Can't download {0} in {1} format.", m_Asset.Id, m_Asset.DesiredType); yield break; } } } } } // Download root asset. var request = new WebRequest(m_Asset.RootDataURL, identity); using (var cr = request.SendAsync().AsIeNull()) { while (!request.Done) { try { cr.MoveNext(); } catch (VrAssetServiceException e) { Debug.LogErrorFormat("Error downloading {0} at {1}\n{2}", m_Asset.Id, m_Asset.RootDataURL, e); yield break; } yield return(cr.Current); } } m_Asset.CopyBytesToRootElement(request.ResultBytes); // Download all resource assets. foreach (var e in m_Asset.ResourceElements) { request = new WebRequest(e.dataURL, App.GoogleIdentity); using (var cr = request.SendAsync().AsIeNull()) { while (!request.Done) { try { cr.MoveNext(); } catch (VrAssetServiceException ex) { Debug.LogErrorFormat("Error downloading {0} at {1}\n{2}", m_Asset.Id, m_Asset.RootDataURL, ex); e.assetBytes = null; yield break; } yield return(cr.Current); } } e.assetBytes = request.ResultBytes; } m_Ready = true; }