private void Run(Settings settings, ProgressDialog dialog) { // Setup some stuff for the progress dialog int numFilesAdded = 0; string description = String.Format("Processing {0}", Path.GetFileName(settings.OutFilename)); dialog.ReportProgress(0, description); // For some archives, the file needs to be a specific format. As such, // they may be rejected when trying to add them. We'll store such files in // this list to let the user know they could not be added. List <string> FilesNotAdded = new List <string>(); // Create the stream we are going to write the archive to Stream destination; if (settings.CompressionFormat == CompressionFormat.Unknown) { // We are not compression the archive. Write directly to the destination destination = File.Create(settings.OutFilename); } else { // We are compressing the archive. Write to a memory stream first. destination = new MemoryStream(); } // Create the archive ArchiveWriter archive = Archive.Create(destination, settings.ArchiveFormat); // Set archive settings ModuleSettingsControl settingsControl = settings.WriterSettingsControl; if (settingsControl != null) { Action moduleSettingsAction = () => settingsControl.SetModuleSettings(archive); settingsControl.Invoke(moduleSettingsAction); } // Add the file added event handler the archive archive.FileAdded += delegate(object sender, EventArgs e) { numFilesAdded++; //if (numFilesAdded == archive.NumberOfFiles) if (numFilesAdded == archive.Entries.Count) { dialog.ReportProgress(100, "Finishing up"); } else { dialog.ReportProgress(numFilesAdded * 100 / archive.Entries.Count, description + "\n\n" + String.Format("Adding {0} ({1:N0} of {2:N0})", Path.GetFileName(settings.FileEntries[numFilesAdded].SourceFile), numFilesAdded + 1, archive.Entries.Count)); } }; // Add the files to the archive. We're going to do this in a try catch since // sometimes an exception may be thrown (namely if the archive cannot contain // the file the user is trying to add) foreach (FileEntry entry in settings.FileEntries) { try { archive.CreateEntryFromFile(entry.SourceFile, entry.FilenameInArchive); } catch (CannotAddFileToArchiveException) { FilesNotAdded.Add(entry.SourceFile); } } // If filesNotAdded is not empty, then show a message to the user // and ask them if they want to continue if (FilesNotAdded.Count > 0) { if (new FilesNotAddedDialog(FilesNotAdded).ShowDialog() != DialogResult.Yes) { destination.Close(); return; } } if (archive.Entries.Count == 1) { dialog.Description = description + "\n\n" + String.Format("Adding {0}", Path.GetFileName(settings.FileEntries[numFilesAdded].SourceFile)); } else { dialog.Description = description + "\n\n" + String.Format("Adding {0} ({1:N0} of {2:N0})", Path.GetFileName(settings.FileEntries[numFilesAdded].SourceFile), numFilesAdded + 1, archive.Entries.Count); } archive.Flush(); // Do we want to compress this archive? if (settings.CompressionFormat != CompressionFormat.Unknown) { destination.Position = 0; using (FileStream outStream = File.Create(settings.OutFilename)) { Compression.Compress(destination, outStream, settings.CompressionFormat); } } destination.Close(); }
private void Run(Settings settings, ProgressDialog dialog) { for (int i = 0; i < fileList.Count; i++) { string file = fileList[i]; // Report progress. If we only have one file to process, no need to display (x of n). if (fileList.Count == 1) { dialog.ReportProgress(i * 100 / fileList.Count, String.Format("Processing {0}", Path.GetFileName(file))); } else { dialog.ReportProgress(i * 100 / fileList.Count, String.Format("Processing {0} ({1:N0} of {2:N0})", Path.GetFileName(file), i + 1, fileList.Count)); } // Let's open the file. // But, we're going to do this in a try catch in case any errors happen. try { // Set the output path and filename string outPath; if (settings.OutputToSourceDirectory) { outPath = Path.GetDirectoryName(file); } else { outPath = Path.Combine(Path.GetDirectoryName(file), "Encoded Textures"); } string outFname = Path.ChangeExtension(Path.GetFileName(file), Texture.Formats[settings.TextureFormat].FileExtension); MemoryStream buffer = new MemoryStream(); // Run it through the texture encoder. TextureBase texture = Texture.Formats[settings.TextureFormat]; using (FileStream source = File.OpenRead(file)) { // Set the source path (really only used for GIM textures at the current moment). texture.SourcePath = file; // Set texture settings ModuleSettingsControl settingsControl = settings.WriterSettingsControl; if (settingsControl != null) { Action moduleSettingsAction = () => settingsControl.SetModuleSettings(texture); settingsControl.Invoke(moduleSettingsAction); } texture.Write(source, buffer, (int)source.Length); } // Do we want to compress this texture? if (settings.CompressionFormat != CompressionFormat.Unknown) { MemoryStream tempBuffer = new MemoryStream(); buffer.Position = 0; Compression.Compress(buffer, tempBuffer, settings.CompressionFormat); buffer = tempBuffer; } // Create the output path it if it does not exist. if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } // Time to write out the file using (FileStream destination = File.Create(Path.Combine(outPath, outFname))) { buffer.WriteTo(destination); } // Write out the palette file (if one was created along with the texture). if (texture.PaletteStream != null) { using (FileStream destination = File.Create(Path.Combine(outPath, Path.ChangeExtension(outFname, Texture.Formats[settings.TextureFormat].PaletteFileExtension)))) { texture.PaletteStream.Position = 0; PTStream.CopyTo(texture.PaletteStream, destination); } } // Delete the source if the user chose to if (settings.DeleteSource) { File.Delete(file); } } catch { // Meh, just ignore the error. } } }