public void LoadLocalPageInternal() { serverConnectionError.SetActive(false); videoContainer.SetActive(false); spinner.enabled = true; spinner.rectTransform.rotation = Quaternion.identity; var di = new DirectoryInfo(Application.persistentDataPath); //Note(Simon): Regex to match Guids var localVideos = di.GetDirectories("*-*-*-*-*"); if (loadedVideos == null) { loadedVideos = new VideoResponseSerialize(); } loadedVideos.videos = new List <VideoSerialize>(); loadedVideos.totalcount = localVideos.Length; for (var i = (page - 1) * videosPerPage; i < Mathf.Min(page * videosPerPage, localVideos.Length); i++) { var path = localVideos[i].FullName; var data = SaveFile.OpenFile(Path.Combine(path, SaveFile.metaFilename)); var folderInfo = new DirectoryInfo(path); loadedVideos.videos.Add(new VideoSerialize { title = data.meta.title, description = data.meta.description, downloadsize = SaveFile.DirectorySize(folderInfo), realTimestamp = folderInfo.LastWriteTime, id = localVideos[i].Name, compatibleVersion = !(data.meta.version > VersionManager.VERSION) }); } videoContainer.SetActive(true); spinner.enabled = false; noVideos.enabled = loadedVideos.videos.Count == 0; totalVideos = loadedVideos.totalcount; numPages = Mathf.Max(1, Mathf.CeilToInt(totalVideos / (float)videosPerPage)); //Note(Simon): Videos { StartCoroutine(BuildVideoGameObjects()); } }
//NOTE(Simon): First unpack to random destination. Then read savefile, and copy to correct folder (or overwrite if existing) public void UnpackFile(string zipPath) { unpacking = true; long bytesUnpacked = 0; long bytesToUnpack = 0; long bytesCopied = 0; long bytesToCopy = 0; string tempDestFolder = Path.Combine(persistentDataPath, Guid.NewGuid().ToString()); bool corrupt = false; bool fullImport = false; bool isCurrentProject = false; using (var source = new ZipArchive(File.OpenRead(zipPath), ZipArchiveMode.Read)) { var filenames = new List <string>(); foreach (var entry in source.Entries) { filenames.Add(entry.Name); } if (!filenames.Contains(SaveFile.metaFilename)) { corrupt = true; } if (filenames.Contains(SaveFile.videoFilename)) { fullImport = true; } if (fullImport && !filenames.Contains(SaveFile.thumbFilename)) { corrupt = true; } if (!corrupt) { foreach (var entry in source.Entries) { bytesToUnpack += entry.Length; } foreach (var entry in source.Entries) { string destDir = Path.Combine(tempDestFolder, Path.GetDirectoryName(entry.FullName)); string destFile = Path.Combine(tempDestFolder, entry.FullName); bool isFile = !String.IsNullOrEmpty(entry.Name); Directory.CreateDirectory(destDir); if (isFile) { using (var writer = File.Create(destFile)) using (var entryStream = entry.Open()) { //NOTE(Simon): 80kB is the buffer size used in .NET's CopyTo() var buffer = new byte[80 * 1024]; int read; do { read = entryStream.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, read); bytesUnpacked += read; progress = (float)(bytesUnpacked) / bytesToUnpack / 2; } while (read > 0); } } } } } if (!corrupt) { var savefileData = SaveFile.OpenFile(tempDestFolder); var realGuid = savefileData.meta.guid; var realDestFolder = Path.Combine(persistentDataPath, realGuid.ToString()); if (Editor.Instance != null && realGuid == Editor.Instance.currentProjectGuid) { isCurrentProject = true; } if (!isCurrentProject) { bytesToCopy += SaveFile.DirectorySize(new DirectoryInfo(tempDestFolder)); if (!Directory.Exists(realDestFolder)) { Directory.Move(tempDestFolder, realDestFolder); } else { var files = Directory.GetFiles(tempDestFolder); foreach (var file in files) { var filename = Path.GetFileName(file); var fileInRealDest = Path.Combine(realDestFolder, filename); var fileInTempDest = Path.Combine(tempDestFolder, filename); //NOTE(Simon): If file already exists in real destination, first delete. So Move() is safe. File.Delete(fileInRealDest); File.Move(fileInTempDest, fileInRealDest); bytesCopied += new FileInfo(fileInRealDest).Length; progress = (float)(bytesUnpacked + bytesCopied) / (bytesToUnpack + bytesToCopy); } var dirs = Directory.GetDirectories(tempDestFolder); foreach (var dir in dirs) { var dirname = new DirectoryInfo(dir).Name; //NOTE(Simon): Make sure directory exists Directory.CreateDirectory(Path.Combine(realDestFolder, dirname)); var dirFiles = Directory.GetFiles(Path.Combine(tempDestFolder, dir)); foreach (var file in dirFiles) { var filename = Path.GetFileName(file); var fileInRealDest = Path.Combine(realDestFolder, dirname, filename); var fileInTempDest = Path.Combine(tempDestFolder, dirname, filename); File.Delete(fileInRealDest); File.Move(fileInTempDest, fileInRealDest); bytesCopied += new FileInfo(fileInRealDest).Length; progress = (float)(bytesUnpacked + bytesCopied) / (bytesToUnpack + bytesToCopy); } } } } if (Directory.Exists(tempDestFolder)) { Directory.Delete(tempDestFolder, true); } } if (corrupt) { Toasts.AddToast(10.0f, "Import failed. File is corrupt"); } else if (isCurrentProject) { Toasts.AddToast(10.0f, "Import failed. Same as currently opened project."); } progress = 1f; unpacking = false; answered = true; }
public void LoadLocalPageInternal() { serverConnectionError.SetActive(false); videoContainer.SetActive(false); spinner.enabled = true; spinner.rectTransform.rotation = Quaternion.identity; var di = new DirectoryInfo(Application.persistentDataPath); //Note(Simon): Regex to match Guids var localVideos = di.GetDirectories("*-*-*-*-*"); if (loadedVideos == null) { loadedVideos = new VideoResponseSerialize(); } loadedVideos.videos = new List <VideoSerialize>(); loadedVideos.totalcount = localVideos.Length; foreach (var v in localVideos) { var path = v.FullName; var data = SaveFile.OpenFile(Path.Combine(path, SaveFile.metaFilename)); var folderInfo = new DirectoryInfo(path); loadedVideos.videos.Add(new VideoSerialize { title = data.meta.title, description = data.meta.description, downloadsize = SaveFile.DirectorySize(folderInfo), realTimestamp = folderInfo.LastWriteTime, uuid = v.Name, }); } videoContainer.SetActive(true); spinner.enabled = false; noVideos.enabled = loadedVideos.videos.Count == 0; totalVideos = loadedVideos.totalcount; numPages = Mathf.Max(1, Mathf.CeilToInt(totalVideos / (float)loadedVideos.count)); page = loadedVideos.page; //Note(Simon): Videos { var videosThisPage = loadedVideos.videos ?? new List <VideoSerialize>(); while (videos.Count < Math.Min(videosPerPage, videosThisPage.Count)) { var video = Instantiate(videoPrefab); video.transform.SetParent(videoContainer.transform, false); videos.Add(video); } while (videos.Count > Math.Min(videosPerPage, videosThisPage.Count)) { var video = videos[videos.Count - 1]; videos.RemoveAt(videos.Count - 1); Destroy(video); } for (int i = 0; i < videosThisPage.Count; i++) { var v = videosThisPage[i]; videos[i].GetComponent <IndexPanelVideo>().SetData(v, true); } } }