using System; using System.IO; using UnityEngine; namespace UnityEditor.Build.CacheServer { /// /// IDownloadItem implementation for downloading to a file specified by path. /// public class FileDownloadItem : IDownloadItem { private Stream m_writeStream; private string m_tmpPath; /// /// The FileId for the FileDownloadItem. FileId consists of an assets guid and hash code. /// public FileId Id { get; private set; } /// /// The type of the FileDownloadItems desired item. /// public FileType Type { get; private set; } /// /// File path where downloaded file data is saved. Data is first written to a temporary file location, and moved /// into place when the Finish() method is called by the Cache Server Client. /// public string FilePath { get; private set; } /// /// Gets the write stream for a given FileDownloadItem. If one does not exist, it will be created. /// /// /// public Stream GetWriteStream(long size) { if (m_writeStream == null) { m_tmpPath = Path.GetTempFileName(); m_writeStream = new FileStream(m_tmpPath, FileMode.Create, FileAccess.Write); } return m_writeStream; } /// /// Create a new FileDownloadItem /// /// The FileId of the desired item. /// The FileType of the desired item. /// The path of the desired item. public FileDownloadItem(FileId fileId, FileType fileType, string path) { Id = fileId; Type = fileType; FilePath = path; } /// /// Dispose the FileDownloadItems write stream and move the data from the temporary path to its final destination. /// public void Finish() { if (m_writeStream == null) return; m_writeStream.Dispose(); try { if (File.Exists(FilePath)) File.Delete(FilePath); File.Move(m_tmpPath, FilePath); } catch (Exception e) { Debug.LogError(e); } } } }