// The directory for the asset must have already been created IEnumerator <Null> PrecacheCoroutine(AssetGetter request) { string assetId = request.Asset.Id; var cr = request.GetAssetCoroutine(); while (true) { try { bool result = cr.MoveNext(); if (!result) { break; } } catch (VrAssetServiceException e) { ControllerConsoleScript.m_Instance.AddNewLine(e.Message); Debug.LogException(e); yield break; } yield return(cr.Current); } while (!request.IsReady) { yield return(null); } request.Asset.WriteToDisk(); string path = Path.Combine(GetCacheDirectoryForAsset(assetId), request.Asset.RootFilePath); m_ModelsByAssetId[assetId] = new Model(Model.Location.PolyAsset(assetId, path)); }
public void UpdateCatalog() { // Walk backwards so removal doesn't mess up our indexing. for (int i = m_ActiveRequests.Count - 1; i >= 0; --i) { AssetGetter request = m_ActiveRequests[i]; if (request.IsReady || request.IsCanceled) { if (request.Asset.ValidAsset) { if (request.Asset.WriteToDisk()) { string assetId = request.Asset.Id; string path = Path.Combine(GetCacheDirectoryForAsset(assetId), request.Asset.RootFilePath); // TODO: This assumes PolyRawAssets are models. This may not be true in the // future and should the VrAssetProtos.ElementType request parameter to // VrAssetService.m_Instance.GetAsset to decide how to store and index the asset. // Populate map entry for this new model. m_ModelsByAssetId[assetId] = new Model(Model.Location.PolyAsset(assetId, path)); // After download the model should be loaded too, unless the request was canceled. // TODO: this seems a littttle suspect. Just because it finished downloading, // does that mean we still want to bring it into memory? if (!request.IsCanceled) { m_RequestLoadQueue.Add( new ModelLoadRequest( m_ModelsByAssetId[assetId], $"{request.Reason} fetched")); } else { // Just reset, in case asset is on one of the other queues. m_IsLoadingMemo = null; } } } else { Debug.LogWarning("Downloaded asset is empty " + request.Asset.RootFilePath); } m_ActiveRequests.RemoveAt(i); m_NotifyListeners = true; } } if (m_RequestLoadQueue.Count > 0 && m_LoadQueue.Count == 0) { // Move a single item from "request load" to "load". Too many items on the load queue // causes bad stuttering (at least for heavy models). var toMove = m_RequestLoadQueue[0]; // TODO: how is it possible for m_RequestLoadQueue to contain duplicates? m_RequestLoadQueue = m_RequestLoadQueue .Where(elt => elt.AssetId != toMove.AssetId) .ToList(); m_LoadQueue.Add(toMove); } // Always call this to poll the async loader. LoadModelsInQueueAsync(); // Shout from the hills. if (m_NotifyListeners) { if (CatalogChanged != null) { CatalogChanged(); } m_NotifyListeners = false; } }
/// Request loading a model with a given Poly Asset ID. /// Pass the reason the Model is being pulled into memory, for logging purposes. /// /// Upon completion, the asset will: /// - be in "failed download" state (don't know how to check for this) /// - be in "download succeeded, load failed" state (check Model.Error != null) /// - be in "download succeeded, load succeeded" state (check Model.m_Valid) /// /// The intent is that this method will ignore previous failures and try again. /// If you don't want to retry a failed load-into-memory, you should check Model.Error first. /// If you aren't trying to do a hot-reload, you should check Model.m_Valid first. public void RequestModelLoad(string assetId, string reason) { // Don't attempt to load models which are already loading. if (IsLoading(assetId)) { return; } if (m_ModelsByAssetId.ContainsKey(assetId)) { // Already downloaded. // It may be in memory already, but it's safe to ask for it to be brought in again. // That way we get the behavior of "ignore a failed load-into-memory" m_RequestLoadQueue.Add(new ModelLoadRequest(m_ModelsByAssetId[assetId], reason)); m_IsLoadingMemo.Add(assetId); } else { // Not downloaded yet. // Kick off a download; when done the load will and arrange for the download-complete to kick off the // load-into-memory work. string assetDir = GetCacheDirectoryForAsset(assetId); try { // For the case that the folder exists, but the files were removed. if (!Directory.Exists(assetDir)) { Directory.CreateDirectory(assetDir); } } catch (UnauthorizedAccessException) { Debug.LogError("Cannot create directory for online asset download."); } // Then request the asset from Poly. AssetGetter request = VrAssetService.m_Instance.GetAsset( assetId, VrAssetFormat.GLTF2, reason); StartCoroutine(request.GetAssetCoroutine()); m_ActiveRequests.Add(request); m_IsLoadingMemo.Add(assetId); } }
private static string ToString(AssetGetter ag) { return($"{ag.Asset.Id} {(ag.IsCanceled ? 'c' : '-')} {(ag.IsReady ? 'r' : '-')}"); }