public void LoadHeader() { if (SyncTask(TaskType.LoadHeader)) { return; } // Read and construct header. currentTask = Task.Run(() => { Stream stream = TitleContainer.OpenStream(AppRelativeDirectory); BinaryReader reader = new BinaryReader(stream); try { Header = SEFileHeader.ReadFromStream(reader); } finally { reader.Close(); UpdateState(); curTaskType = TaskType.None; } }); curTaskType = TaskType.LoadHeader; }
public static void Setup() { HashSet <string> needsProcessing = new HashSet <string>(); HashSet <string> processed = new HashSet <string>(); QuickList <ValueTuple <Task, string> > processing = new QuickList <(Task, string)>(); // Locate files which need processing, or are already processed. foreach (string dir in Directory.GetDirectories(FileIO.DataDirectory)) { foreach (string file in FileIO.GetAllFiles(dir, excludedExtensions: excludedExtensions)) { string extension = Path.GetExtension(file); if (string.IsNullOrEmpty(extension)) { continue; } if (extension != _DATA_EXTENSION) { needsProcessing.Add(file); } else { processed.Add(file); } } } // Process un-processed files (e.g .png, .jpg, .wav, etc). foreach (string file in needsProcessing) { byte[] bytes = IOFile.ReadAllBytes(file); string extension = Path.GetExtension(file); string pathNoExtension = Path.ChangeExtension(file, null); string newFile = pathNoExtension + _DATA_EXTENSION; // Create write stream. FileStream stream = new FileStream(newFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, bytes.Length, FileOptions.Asynchronous); BinaryWriter writer = new BinaryWriter(stream); stream.SetLength(0); // Construct header. SEFileHeader header = new SEFileHeader(SEFileHeaderFlags.None, 1, extension, new byte[0], (uint)bytes.Length); // Write to the processed file. header.WriteToStream(writer); Task writeTask = stream.WriteAsync(bytes).AsTask().ContinueWith(_ => stream.Dispose()); processing.Add((writeTask, newFile)); // Delete the old file IOFile.Delete(file); } // Await pending process tasks. foreach ((Task task, string newFileName) in processing) { task.Wait(); processed.Add(newFileName); } // Load headers of processed files. foreach (string file in processed) { string fileName = Path.ChangeExtension(FileIO.GetRelativePath(FileIO.DataDirectory, file), null); files.Add(fileName, new File(file)); } }