/// Waits for the json data to be read on a background thread, and then executes a precache /// coroutine for each found asset. private IEnumerator<Null> PrecacheModelsCoroutine(SceneFileInfo sceneFileInfo, string reason) { var getIdsFuture = new Future<List<string>>(() => GetModelIds(sceneFileInfo)); List<string> ids; while (true) { try { if (getIdsFuture.TryGetResult(out ids)) { break; } } catch (FutureFailed e) { throw new Exception($"While reading {sceneFileInfo}", e); } yield return null; } if (ids == null) { yield break; } List<IEnumerator<Null>> precacheCoroutines = new List<IEnumerator<Null>>(); // Only trigger off one precache routine per frame. foreach (string id in ids) { if (m_ModelsByAssetId.ContainsKey(id)) { // Already cached continue; } if (! FileUtils.InitializeDirectory(GetCacheDirectoryForAsset(id))) { continue; } precacheCoroutines.Add(PrecacheCoroutine( VrAssetService.m_Instance.GetAsset(id, VrAssetFormat.GLTF2, reason))); yield return null; } var cr = CoroutineUtil.CompleteAllCoroutines(precacheCoroutines); while (cr.MoveNext()) { yield return cr.Current; } }
// Returns a log filename that can be written to, or null if logging is disabled. // For internal use. private static string RequestDebugGetLogFile( int id, string description, string ext, int retryIndex = 0) { if (!App.Config.m_DebugWebRequest) { return(null); } #if UNITY_EDITOR string dir = "Requests"; #else string dir = Path.Combine(Application.temporaryCachePath, "Requests"); #endif FileUtils.InitializeDirectory(dir); string niceDescription = description?.Replace("/", "_").Replace("\\", "_").Replace(":", ""); if (retryIndex > 0) { return(Path.Combine(dir, $"{id}_r{retryIndex}_{niceDescription}.{ext}")); } else { return(Path.Combine(dir, $"{id}_{niceDescription}.{ext}")); } }
/// The directory must already have been created public bool WriteToDisk() { // First, iterate over everything that needs to be written to disk and verify it's valid. // If any invalid elements are found, it's likely due to a download failure and we will abort // the entire process. if (m_RootElement.assetBytes == null) { return(false); } ulong requiredDiskSpace = (ulong)m_RootElement.assetBytes.Length; for (int j = 0; j < m_ResourceElements.Count; ++j) { if (m_ResourceElements[j].assetBytes == null) { // Download failed on one of the elements. return(false); } else { requiredDiskSpace += (ulong)m_ResourceElements[j].assetBytes.Length; } } // Next, check to see if we have enough disk space to write all the files. string assetDir = App.PolyAssetCatalog.GetCacheDirectoryForAsset(m_AssetId); if (!FileUtils.HasFreeSpace(assetDir, requiredDiskSpace / (1024 * 1024))) { OutputWindowScript.Error(String.Format("Out of disk space! {0} {1}", requiredDiskSpace, m_AssetId)); return(false); } // // Next, begin writing to disk, remembering each file written to make the operation atomic. // var written = new List <string>(); if (!Directory.Exists(assetDir)) { UnityEngine.Debug.LogErrorFormat("Caller did not create directory for me: {0}", assetDir); } string rootFilePath = Path.Combine(assetDir, GetPolySanitizedFilePath(m_RootElement.filePath)); if (!FileUtils.InitializeDirectory(Path.GetDirectoryName(rootFilePath)) || !FileUtils.WriteBytesIgnoreExceptions(m_RootElement.assetBytes, rootFilePath)) { return(false); } written.Add(rootFilePath); // Write all resources to disk for (int j = 0; j < m_ResourceElements.Count; ++j) { string filePath = Path.Combine(assetDir, GetPolySanitizedFilePath(m_ResourceElements[j].filePath)); if (!FileUtils.InitializeDirectory(Path.GetDirectoryName(filePath)) || !FileUtils.WriteBytesIgnoreExceptions(m_ResourceElements[j].assetBytes, filePath)) { RemoveFiles(written); return(false); } written.Add(filePath); } return(true); }