private void runButton_Click(object sender, EventArgs e) { // Disable the form this.Enabled = false; // Set up the settings we will be using for this Settings settings = new Settings(); settings.OverwriteSourceFile = overwriteSourceFileCheckbox.Checked; settings.DeleteSourceFile = deleteSourceFileCheckbox.Checked; // Set up the process dialog and then run the tool ProgressDialog dialog = new ProgressDialog(); dialog.WindowTitle = "Processing"; dialog.Title = "Decompressing Files"; dialog.DoWork += delegate(object sender2, DoWorkEventArgs e2) { Run(settings, dialog); }; dialog.RunWorkerCompleted += delegate(object sender2, RunWorkerCompletedEventArgs e2) { // The tool is finished doing what it needs to do. We can close it now. this.Close(); }; dialog.RunWorkerAsync(); }
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 if (settings.WriterSettingsControl != null) { settings.WriterSettingsControl.SetModuleSettings(texture); } 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. } } }
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 { CompressionFormat format; MemoryStream buffer = new MemoryStream(); using (FileStream source = File.OpenRead(file)) { // Just run it through the decompressor. // No need to check the format beforehand. format = Compression.Decompress(source, buffer, Path.GetFileName(file)); } // If the compression format is unknown, then nothing happened. // Just continue on with the next file if (format == CompressionFormat.Unknown) continue; // Now that we have a decompressed file (we hope!), let's see what we need to do with it. if (settings.OverwriteSourceFile) { // Overwrite the source file. Ok, we can do that! using (FileStream destination = File.Create(file)) { buffer.WriteTo(destination); // We are done here. Continue on with the next file continue; } } // Get the output path and create it if it does not exist. string outPath = Path.Combine(Path.GetDirectoryName(file), "Decompressed Files"); if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } // Time to write out the file using (FileStream destination = File.Create(Path.Combine(outPath, Path.GetFileName(file)))) { buffer.WriteTo(destination); } // Delete the source file if the user chose to if (settings.DeleteSourceFile) { File.Delete(file); } } catch { // Meh, just ignore the error. } } }
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. } } }
private void runButton_Click(object sender, EventArgs e) { // Disable the form this.Enabled = false; // Get the format of the texture the user wants to create TextureFormat textureFormat = textureFormats[textureFormatBox.SelectedIndex - 1]; // Set the settings for the tool Settings settings = new Settings(); settings.TextureFormat = textureFormat; settings.OutputToSourceDirectory = outputToSourceDirButton.Checked; settings.DeleteSource = deleteSourceButton.Checked; if (compressionFormatBox.SelectedIndex != 0) { settings.CompressionFormat = compressionFormats[compressionFormatBox.SelectedIndex - 1]; } else { settings.CompressionFormat = CompressionFormat.Unknown; } settings.WriterSettingsControl = writerSettingsControls[textureFormatBox.SelectedIndex - 1]; // Set up the process dialog and then run the tool ProgressDialog dialog = new ProgressDialog(); dialog.WindowTitle = "Processing"; dialog.Title = "Encoding Textures"; dialog.DoWork += delegate(object sender2, DoWorkEventArgs e2) { Run(settings, dialog); }; dialog.RunWorkerCompleted += delegate(object sender2, RunWorkerCompletedEventArgs e2) { // The tool is finished doing what it needs to do. We can close it now. this.Close(); }; dialog.RunWorkerAsync(); }
private void Run(Settings settings, ProgressDialog dialog) { for (int i = 0; i < fileList.Count; i++) { string file = fileList[i]; string description; if (fileList.Count == 1) { description = String.Format("Processing {0}", Path.GetFileName(file)); } else { description = String.Format("Processing {0} ({1:N0} of {2:N0})", Path.GetFileName(file), i + 1, fileList.Count); } dialog.ReportProgress(i * 100 / fileList.Count, description); // Let's open the file. // But, we're going to do this in a try catch in case any errors happen. try { ArchiveFormat format; string outPath, outName; Queue<TextureEntry> textureFileQueue = null; using (FileStream inStream = File.OpenRead(file)) { // Set source to inStream // The reason we do it like this is because source does not always equal inStream. // You'll see why very soon. Stream source = inStream; // Get the format of the archive format = Archive.GetFormat(source, Path.GetFileName(file)); if (format == ArchiveFormat.Unknown) { // Maybe it's compressed? Let's check. // But first, we need to make sure we want to check if (settings.DecompressSourceArchive) { // Get the compression format, if it is compressed that is. CompressionFormat compressionFormat = Compression.GetFormat(source, Path.GetFileName(file)); if (compressionFormat != CompressionFormat.Unknown) { // Ok, it appears to be compressed. Let's decompress it, and then check the format again source = new MemoryStream(); Compression.Decompress(inStream, source, compressionFormat); source.Position = 0; format = Archive.GetFormat(source, Path.GetFileName(file)); } } // If we still don't know what the archive format is, just skip the file. if (format == ArchiveFormat.Unknown) { continue; } } // Now that we know its format, let's open it and start working with it. ArchiveReader archive = Archive.Open(source, format); // Get the appropiate output directory if (settings.ExtractToSourceDirectory) { // Extract to the same directory as the source archive outPath = Path.GetDirectoryName(file); } else if (settings.ExtractToSameNameDirectory) { // Extract to a directory of the same name outPath = file + "." + Path.GetRandomFileName(); } else { // Just the standard output path outPath = Path.Combine(Path.Combine(Path.GetDirectoryName(file), "Extracted Files"), Path.GetFileNameWithoutExtension(file)); } // Create the output directory if it does not exist if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } // Now we can start extracting the files for (int j = 0; j < archive.Entries.Count; j++) { if (fileList.Count == 1) { // If there is just one file in the file list, then the progress will be // based on how many files are being extracted from the archive, not // how many archives we are extracting. dialog.ReportProgress(j * 100 / archive.Entries.Count, description + "\n\n" + String.Format("{0:N0} of {1:N0} extracted", j + 1, archive.Entries.Count)); } else { dialog.Description = description + "\n\n" + String.Format("{0:N0} of {1:N0} extracted", j + 1, archive.Entries.Count); } ArchiveEntry entry = archive.Entries[j]; Stream entryData = entry.Open(); // Get the output name for this file if (settings.FileNumberAsFilename || entry.Name == String.Empty) { // Use the file number as its filename outName = j.ToString("D" + archive.Entries.Count.ToString().Length) + Path.GetExtension(entry.Name); } else if (settings.AppendFileNumber) { // Append the file number to its filename outName = Path.GetFileNameWithoutExtension(entry.Name) + j.ToString("D" + archive.Entries.Count.ToString().Length) + Path.GetExtension(entry.Name); } else { // Just use the filename as defined in the archive outName = entry.Name; } // What we're going to do here may seem a tiny bit hackish, but it'll make my job much simplier. // First, let's check to see if the file is compressed, and if we want to decompress it. if (settings.DecompressExtractedFiles) { // Get the compression format, if it is compressed that is. CompressionFormat compressionFormat = Compression.GetFormat(entryData, entry.Name); if (compressionFormat != CompressionFormat.Unknown) { // Ok, it appears to be compressed. Let's decompress it, and then edit the entry MemoryStream decompressedData = new MemoryStream(); Compression.Decompress(entryData, decompressedData, compressionFormat); entryData = decompressedData; entryData.Position = 0; } } // Check to see if this file is a texture. If so, let's convert it to a PNG and then edit the entry if (settings.ConvertExtractedTextures) { // Get the texture format, if it is a texture that is. TextureFormat textureFormat = Texture.GetFormat(entryData, entry.Name); if (textureFormat != TextureFormat.Unknown) { // Ok, it appears to be a texture. We're going to attempt to convert it here. // If we get a TextureNeedsPalette exception, we'll wait until after we extract // all the files in this archive before we process it. try { MemoryStream textureData = new MemoryStream(); Texture.Read(entryData, textureData, textureFormat); // If no exception was thrown, then we are all good doing what we need to do entryData = textureData; entryData.Position = 0; outName = Path.GetFileNameWithoutExtension(outName) + ".png"; } catch (TextureNeedsPaletteException) { // Uh oh, looks like we need a palette. // What we are going to do is add it to textureFileQueue, then convert it // after we extract all of the files. if (textureFileQueue == null) { textureFileQueue = new Queue<TextureEntry>(); } TextureEntry textureEntry = new TextureEntry(); textureEntry.Format = textureFormat; textureEntry.Filename = Path.Combine(outPath, outName); textureFileQueue.Enqueue(textureEntry); } } } // Time to write out the file using (FileStream destination = File.Create(Path.Combine(outPath, outName))) { PTStream.CopyTo(entryData, destination, (int)entryData.Length); } // Let's check to see if this is an archive. If it is an archive, add it to the file list. entryData.Position = 0; if (settings.ExtractExtractedArchives) { ArchiveFormat archiveFormat = Archive.GetFormat(entryData, entry.Name); if (archiveFormat != ArchiveFormat.Unknown) { // It appears to be an archive. Let's add it to the file list if (settings.ExtractToSameNameDirectory) { // If we're adding to a directory of the same name, the outPath will be different. // We should remember that. fileList.Add(Path.Combine(file, outName)); } else { fileList.Add(Path.Combine(outPath, outName)); } if (fileList.Count == 2) { // If there was one archive in the file list, and now there is more, // adjust the progress bar and the description description = String.Format("Processing {0} ({1:N0} of {2:N0})", Path.GetFileName(file), i + 1, fileList.Count); dialog.ReportProgress(i * 100 / fileList.Count, description + "\n\n" + String.Format("{0:N0} of {1:N0} extracted", j + 1, archive.Entries.Count)); } } } } } // Let's see if we have any textures we still need to convert. if (settings.ConvertExtractedTextures && textureFileQueue != null) { // Ok, it appears we do. So, let's loop through the queue until it is empty. while (textureFileQueue.Count > 0) { TextureEntry textureEntry = textureFileQueue.Dequeue(); // Get the palette file name, and the out file name string paletteName = Path.Combine(Path.GetDirectoryName(textureEntry.Filename), Path.GetFileNameWithoutExtension(textureEntry.Filename)) + Texture.Formats[textureEntry.Format].PaletteFileExtension; string textureOutName = Path.Combine(Path.GetDirectoryName(textureEntry.Filename), Path.GetFileNameWithoutExtension(textureEntry.Filename)) + ".png"; // Make sure the two files exist before we attempt to open them. // Wrap the whole thing in a try catch in case for some reason the texture file was modifed. // That way, it'll fail peacefully and not screw over last minute things that need to be done to the archive. // Open up the archive and test to make sure it's still a valid texture. // You know, in case somehow it was edited or not extracted properly. if (File.Exists(textureEntry.Filename) && File.Exists(paletteName)) { try { using (FileStream inTextureStream = File.OpenRead(textureEntry.Filename)) { if (!Texture.Formats[textureEntry.Format].Is(inTextureStream, (int)inTextureStream.Length, textureEntry.Filename)) { // Oh dear, somehow this isn't a texture anymore. Just skip over it continue; } // Ok, now we can load the palette data and try to convert it. using (FileStream inPaletteStream = File.OpenRead(paletteName), outTextureStream = File.Create(textureOutName)) { TextureBase texture = Texture.Formats[textureEntry.Format]; texture.PaletteStream = inPaletteStream; texture.PaletteLength = (int)inPaletteStream.Length; texture.Read(inTextureStream, outTextureStream, (int)inTextureStream.Length); } } // Now we can delete those two files File.Delete(textureEntry.Filename); File.Delete(paletteName); } catch { // Something happened! But we'll just ignore it. } } } } // Delete the source archive if the user chose to if (settings.DeleteSourceArchive) { File.Delete(file); } // If we're extracting to a directory of the same name, we can now rename the directory if (settings.ExtractToSameNameDirectory) { Directory.Move(outPath, file); } } catch { // Meh, just ignore the error. } } }
private void runButton_Click(object sender, EventArgs e) { // Disable the form this.Enabled = false; // Set up the settings we will be using for this Settings settings = new Settings(); settings.DecompressSourceArchive = decompressSourceArchiveCheckbox.Checked; settings.ExtractToSourceDirectory = extractToSourceDirCheckbox.Checked; settings.ExtractToSameNameDirectory = extractToSameNameDirCheckbox.Checked && !settings.ExtractToSourceDirectory; settings.DeleteSourceArchive = deleteSourceArchiveCheckbox.Checked || settings.ExtractToSameNameDirectory; settings.DecompressExtractedFiles = decompressExtractedFilesCheckbox.Checked; settings.FileNumberAsFilename = fileNumberAsFilenameCheckbox.Checked; settings.AppendFileNumber = appendFileNumberCheckbox.Checked && !settings.FileNumberAsFilename; settings.ExtractExtractedArchives = extractExtractedArchivesCheckbox.Checked; settings.ConvertExtractedTextures = convertExtractedTexturesCheckbox.Checked; // Set up the process dialog and then run the tool ProgressDialog dialog = new ProgressDialog(); dialog.WindowTitle = "Processing"; dialog.Title = "Extracting Archives"; dialog.DoWork += delegate(object sender2, DoWorkEventArgs e2) { Run(settings, dialog); }; dialog.RunWorkerCompleted += delegate(object sender2, RunWorkerCompletedEventArgs e2) { // The tool is finished doing what it needs to do. We can close it now. this.Close(); }; dialog.RunWorkerAsync(); }
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 { CompressionFormat format; MemoryStream buffer = new MemoryStream(); using (FileStream source = File.OpenRead(file)) { // Just run it through the decompressor. // No need to check the format beforehand. format = Compression.Decompress(source, buffer, Path.GetFileName(file)); } // If the compression format is unknown, then nothing happened. // Just continue on with the next file if (format == CompressionFormat.Unknown) { continue; } // Now that we have a decompressed file (we hope!), let's see what we need to do with it. if (settings.OverwriteSourceFile) { // Overwrite the source file. Ok, we can do that! using (FileStream destination = File.Create(file)) { buffer.WriteTo(destination); // We are done here. Continue on with the next file continue; } } // Get the output path and create it if it does not exist. string outPath = Path.Combine(Path.GetDirectoryName(file), "Decompressed Files"); if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } // Time to write out the file using (FileStream destination = File.Create(Path.Combine(outPath, Path.GetFileName(file)))) { buffer.WriteTo(destination); } // Delete the source file if the user chose to if (settings.DeleteSourceFile) { File.Delete(file); } } catch { // Meh, just ignore the error. } } }
private void runButton_Click(object sender, EventArgs e) { // Get the format of the archive the user wants to create ArchiveFormat archiveFormat = archiveFormats[archiveFormatBox.SelectedIndex - 1]; string fileExtension = (Archive.Formats[archiveFormat].FileExtension != String.Empty ? Archive.Formats[archiveFormat].FileExtension : ".*"); // Prompt the user to save the archive SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "Save Archive"; sfd.Filter = Archive.Formats[archiveFormat].Name + " Archive (*" + fileExtension + ")|*" + fileExtension + "|All Files (*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { // Disable the form this.Enabled = false; Settings settings = new Settings(); settings.ArchiveFormat = archiveFormat; settings.OutFilename = sfd.FileName; if (compressionFormatBox.SelectedIndex != 0) { settings.CompressionFormat = compressionFormats[compressionFormatBox.SelectedIndex - 1]; } else { settings.CompressionFormat = CompressionFormat.Unknown; } settings.WriterSettingsControl = writerSettingsControls[archiveFormatBox.SelectedIndex - 1]; settings.FileEntries = new List <FileEntry>(); foreach (ListViewItem item in listView.Items) { FileEntry listViewEntry = (FileEntry)item.Tag; FileEntry entry = new FileEntry(); entry.Filename = listViewEntry.Filename; entry.FilenameInArchive = listViewEntry.FilenameInArchive; entry.SourceFile = listViewEntry.SourceFile; settings.FileEntries.Add(entry); } // Set up the process dialog and then run the tool ProgressDialog dialog = new ProgressDialog(); dialog.WindowTitle = "Processing"; dialog.Title = "Creating Archive"; dialog.DoWork += delegate(object sender2, DoWorkEventArgs e2) { Run(settings, dialog); }; dialog.RunWorkerCompleted += delegate(object sender2, RunWorkerCompletedEventArgs e2) { // The tool is finished doing what it needs to do. We can close it now. this.Close(); }; dialog.RunWorkerAsync(); } }
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 { TextureFormat format; MemoryStream textureData = new MemoryStream(); using (FileStream inStream = File.OpenRead(file)) { // Set source to inStream // The reason we do it like this is because source does not always equal inStream. // You'll see why very soon. Stream source = inStream; // Get the format of the texture format = Texture.GetFormat(source, Path.GetFileName(file)); if (format == TextureFormat.Unknown) { // Maybe it's compressed? Let's check. // But first, we need to make sure we want to check if (settings.DecodeCompressedTextures) { // Get the compression format, if it is compressed that is. CompressionFormat compressionFormat = Compression.GetFormat(source, Path.GetFileName(file)); if (compressionFormat != CompressionFormat.Unknown) { // Ok, it appears to be compressed. Let's decompress it, and then check the format again source = new MemoryStream(); Compression.Decompress(inStream, source, compressionFormat); source.Position = 0; format = Texture.GetFormat(source, Path.GetFileName(file)); } } // If we still don't know what the texture format is, just skip the file. if (format == TextureFormat.Unknown) { continue; } } // Alright, let's decode the texture now TextureBase texture = Texture.GetModule(format); try { texture.Read(source, textureData, (int)source.Length); //Texture.Read(source, textureData, (int)source.Length, format); } catch (TextureNeedsPaletteException) { // It appears that we need to load an external palette. // Let's get the filename for this palette file, see if it exists, and load it in string paletteName = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)) + Texture.GetModule(format).PaletteFileExtension; if (!File.Exists(paletteName)) { // If the palette file doesn't exist, just skip over this texture. continue; } source.Position = 0; // We need to reset the position textureData = new MemoryStream(); // Just incase some data was written using (FileStream paletteData = File.OpenRead(paletteName)) { texture.PaletteStream = paletteData; texture.PaletteLength = (int)paletteData.Length; texture.Read(source, textureData, (int)source.Length); } // Delete the palette file if the user chose to delete the source texture if (settings.DeleteSource) { File.Delete(paletteName); } } } // Get the output path and create it if it does not exist. string outPath; if (settings.OutputToSourceDirectory) { outPath = Path.GetDirectoryName(file); } else { outPath = Path.Combine(Path.GetDirectoryName(file), "Decoded Textures"); } if (!Directory.Exists(outPath)) { Directory.CreateDirectory(outPath); } // Time to write out the file using (FileStream destination = File.Create(Path.Combine(outPath, Path.GetFileNameWithoutExtension(file) + ".png"))) { textureData.WriteTo(destination); } // Delete the source if the user chose to if (settings.DeleteSource) { File.Delete(file); } } catch { // Meh, just ignore the error. } } }