Exemplo n.º 1
0
 static public bool CheckDiskSpaceWithError(string path, string error = null)
 {
     if (error == null)
     {
         error = "Out of disk space!";
     }
     if (!FileUtils.HasFreeSpace(path))
     {
         OutputWindowScript.ReportFileSaved(error, null,
                                            OutputWindowScript.InfoCardSpawnPos.Brush);
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        /// 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);
        }