public static string GetFileExtension(Stream data) { // Check to see if the file is an archive Archive archive = new Archive(data, null); if (archive.Format != ArchiveFormat.NULL) return archive.FileExtension; // Check to see if the file is an image Images images = new Images(data, null); if (images.Format != GraphicFormat.NULL) return images.FileExtension; // Check to see if the file is an ADX (special case) if (data.Length > 4 && data.ReadUShort(0x00) == 0x0080 && data.Length > data.ReadUShort(0x02).SwapEndian() + 4 && data.ReadString(data.ReadUShort(0x02).SwapEndian() - 0x02, 6, true) == "(c)CRI") return ".adx"; // Unknown return String.Empty; }
/* Load Palette File */ private Stream LoadPaletteFile(string filename, Images imageClass) { // See if the palette file exists if (File.Exists(filename)) { using (FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read)) return input.Copy(); } return null; }
private Bitmap LoadImage(ref Stream data, string filename, Stream palette) { try { /* Get and return the image */ Images imageClass = new Images(data, filename); if (imageClass.Format != GraphicFormat.NULL) { imageClass.Decoder.PaletteData = palette; return imageClass.Unpack(); } throw new Exception(); } catch { return null; } }
private Bitmap LoadImage(ref Stream data, string filename) { try { /* Get and return the image */ Images imageClass = new Images(data, filename); try { return imageClass.Unpack(); } catch (GraphicFormatNeedsPalette) { throw new GraphicFormatNeedsPalette(); } } catch (GraphicFormatNeedsPalette) { throw new GraphicFormatNeedsPalette(); } catch { return null; } }
private Bitmap LoadImage(string filename) { try { /* Open the file */ Images imageClass; Stream file; using (file = new FileStream(filename, FileMode.Open, FileAccess.Read)) { imageClass = new Images(file, filename); /* Get the image */ if (imageClass.Format == GraphicFormat.NULL) { /* Decompress the file and see if we can get anything */ Compression compression = new Compression(file, filename); if (compression.Format == CompressionFormat.NULL) throw new GraphicFormatNotSupported(); file = compression.Decompress(); imageClass = new Images(file, filename); if (imageClass.Format == GraphicFormat.NULL) throw new GraphicFormatNotSupported(); } try { return imageClass.Unpack(); } catch (GraphicFormatNeedsPalette) { // Load palette data and then unpack again if (Path.GetDirectoryName(filename) != String.Empty) imageClass.Decoder.PaletteData = LoadPaletteFile(Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar + imageClass.PaletteFilename, imageClass); else imageClass.Decoder.PaletteData = LoadPaletteFile(imageClass.PaletteFilename, imageClass); return imageClass.Unpack(); } } } catch { return null; } }
public override MemoryStream CreateHeader(string[] files, string[] archiveFilenames, int blockSize, bool[] settings, out uint[] offsetList) { try { /* Let's get out settings now */ blockSize = 24; bool addFilename = settings[0]; bool addPixelFormat = settings[1]; bool addDimensions = settings[2]; bool addGlobalIndex = settings[3]; /* Let's figure out the metadata size, so we can create the header properly */ int metaDataSize = 2; if (addFilename) metaDataSize += 28; if (addPixelFormat) metaDataSize += 2; if (addDimensions) metaDataSize += 2; if (addGlobalIndex) metaDataSize += 4; /* Create the header now */ offsetList = new uint[files.Length]; MemoryStream header = new MemoryStream(Number.RoundUp(0xC + (files.Length * metaDataSize), blockSize)); header.Write(ArchiveHeader.GVM, 4); header.Write(header.Capacity); /* Set up format type */ byte formatType = 0x0; if (addFilename) formatType |= (1 << 3); if (addPixelFormat) formatType |= (1 << 2); if (addDimensions) formatType |= (1 << 1); if (addGlobalIndex) formatType |= (1 << 0); header.WriteByte(0x0); header.WriteByte(formatType); /* Write number of files */ header.Write(((ushort)files.Length).SwapEndian()); uint offset = (uint)header.Capacity + 8; /* Start writing the information in the header */ for (int i = 0; i < files.Length; i++) { using (FileStream data = new FileStream(files[i], FileMode.Open, FileAccess.Read)) { /* Make sure this is a GVR */ Images images = new Images(data, files[i]); if (images.Format != GraphicFormat.GVR) throw new IncorrectGraphicFormat(); /* Get the header offset */ int headerOffset = (data.ReadString(0x0, 4) == GraphicHeader.GVRT ? 0x0 : 0x10); offsetList[i] = offset; header.Write(((ushort)i).SwapEndian()); if (addFilename) header.Write(Path.GetFileNameWithoutExtension(archiveFilenames[i]), 27, 28); if (addPixelFormat) header.Write(data, headerOffset + 0xA, 2); if (addDimensions) { /* Get the width and height */ int width = (int)Math.Min(Math.Log(data.ReadUShort(headerOffset + 0xC).SwapEndian(), 2) - 2, 9); int height = (int)Math.Min(Math.Log(data.ReadUShort(headerOffset + 0xE).SwapEndian(), 2) - 2, 9); header.WriteByte(0x0); header.WriteByte((byte)((width << 4) | height)); } if (addGlobalIndex) { if (headerOffset == 0x0) header.Write(new byte[] {0x0, 0x0, 0x0, 0x0}); else header.Write(data, 0x8, 4); } offset += Number.RoundUp((uint)(data.Length - headerOffset), blockSize); } } return header; } catch (IncorrectGraphicFormat) { offsetList = null; return null; } catch { offsetList = null; return null; } }
/* Format File */ public override Stream FormatFileToAdd(ref Stream data) { /* Check to see if this is a GVR */ Images images = new Images(data, null); if (images.Format == GraphicFormat.GVR) { /* Does the file start with GVRT? */ if (data.ReadString(0x0, 4) == GraphicHeader.GVRT) return data; /* Otherwise strip off the first 16 bytes */ else return data.Copy(0x10, (int)data.Length - 0x10); } /* Can't add this file! */ return null; }
/* Decompress the files */ private void run(object sender, DoWorkEventArgs e) { /* Create the file list */ List<string> fileList = new List<string>(); foreach (string i in files) fileList.Add(i); for (int i = 0; i < files.Length; i++) { /* Set the current file */ status.CurrentFile = i; try { /* Open up the file */ MemoryStream data; string outputDirectory, outputFilename; using (FileStream inputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read)) { /* Set up the decompressor */ Compression compression = new Compression(inputStream, Path.GetFileName(fileList[i])); if (compression.Format == CompressionFormat.NULL) continue; /* Set up the output directories and file names */ outputDirectory = Path.GetDirectoryName(fileList[i]) + (decompressSameDir.Checked ? String.Empty : Path.DirectorySeparatorChar + compression.OutputDirectory); outputFilename = (useStoredFilename.Checked ? compression.DecompressFilename : Path.GetFileName(fileList[i])); /* Decompress data */ MemoryStream decompressedData = compression.Decompress(); /* Check to make sure the decompression was successful */ if (decompressedData == null) continue; else data = decompressedData; } /* Create the output directory if it does not exist */ if (!Directory.Exists(outputDirectory)) Directory.CreateDirectory(outputDirectory); /* Write file data */ using (FileStream outputStream = new FileStream(outputDirectory + Path.DirectorySeparatorChar + outputFilename, FileMode.Create, FileAccess.Write)) outputStream.Write(data); /* Delete source file? */ if (deleteSourceFile.Checked && File.Exists(fileList[i])) File.Delete(fileList[i]); /* Unpack image? */ if (unpackImage.Checked) { /* Create Image object and make sure the format is supported */ Images images = new Images(data, outputFilename); if (images.Format != GraphicFormat.NULL) { /* Set up our input and output image */ string inputImage = outputDirectory + Path.DirectorySeparatorChar + outputFilename; string outputImage = outputDirectory + Path.DirectorySeparatorChar + (convertSameDir.Checked ? String.Empty : images.OutputDirectory + Path.DirectorySeparatorChar) + Path.GetFileNameWithoutExtension(outputFilename) + ".png"; // Convert image Bitmap imageData = null; try { imageData = images.Unpack(); } catch (GraphicFormatNeedsPalette) { // See if the palette file exists if (File.Exists(outputDirectory + Path.DirectorySeparatorChar + images.PaletteFilename)) { using (FileStream paletteData = new FileStream(outputDirectory + Path.DirectorySeparatorChar + images.PaletteFilename, FileMode.Open, FileAccess.Read)) { images.Decoder.PaletteData = paletteData; imageData = images.Unpack(); } } } /* Make sure an image was written */ if (imageData != null) { data = new MemoryStream(); imageData.Save(data, ImageFormat.Png); /* Create the output directory if it does not exist */ if (!Directory.Exists(Path.GetDirectoryName(outputImage))) Directory.CreateDirectory(Path.GetDirectoryName(outputImage)); /* Output the image */ using (FileStream outputStream = new FileStream(outputImage, FileMode.Create, FileAccess.Write)) outputStream.Write(data); /* Delete the source image if we want to */ if (deleteSourceImage.Checked && File.Exists(inputImage) && File.Exists(outputImage)) File.Delete(inputImage); } } } } catch { /* Something went wrong. Continue please. */ continue; } } /* Close the status box now */ status.Close(); this.Close(); }
// Import PVR settings private void ImportPvrSettings(object sender, EventArgs e) { /* Select Pvr */ string pvrFile = FileSelectionDialog.OpenFile("Select PVR file", "PVR Files (*.pvr;*.pvz)|*.pvr;*.pvz|" + "All Files (*.*)|*.*"); if (pvrFile == null || pvrFile == String.Empty) return; /* Load pvr file */ using (Stream file = new FileStream(pvrFile, FileMode.Open, FileAccess.Read)) { Stream fileData = file; /* Is the file compressed? */ Compression compression = new Compression(fileData, pvrFile); if (compression.Format != CompressionFormat.NULL) { MemoryStream decompressedData = compression.Decompress(); if (decompressedData != null) fileData = decompressedData; } /* Is this a PVR? */ Images images = new Images(fileData, pvrFile); if (images.Format == GraphicFormat.PVR) { /* Get file offset */ int fileOffset = (fileData.ReadString(0x0, 4) == GraphicHeader.GBIX ? 0x10 : 0x0); /* Get Palette Format, Data Format, and Global Index */ byte pvrPaletteFormat = fileData.ReadByte(fileOffset + 0x8); byte pvrDataFormat = fileData.ReadByte(fileOffset + 0x9); uint pvrGlobalIndex = 0; if (fileData.ReadString(0x0, 4) == GraphicHeader.GBIX) pvrGlobalIndex = fileData.ReadUInt(0x8); /* Set palette format */ switch (pvrPaletteFormat) { case 0x0: paletteFormat.SelectedIndex = 0; break; case 0x1: paletteFormat.SelectedIndex = 1; break; case 0x2: paletteFormat.SelectedIndex = 2; break; } /* Set data format */ switch (pvrDataFormat) { case 0x1: dataFormat.SelectedIndex = 0; break; case 0x9: dataFormat.SelectedIndex = 1; break; case 0xD: dataFormat.SelectedIndex = 2; break; } /* Set global index */ addGbix.Checked = (fileData.ReadString(0x0, 4) == "GBIX"); globalIndex.Text = pvrGlobalIndex.ToString(); } } }
private void loadEmbeddedImage() { try { // Get the selected item and the data and filename int item = fileListView.SelectedIndices[0] - (level == 0 ? 0 : 1); MemoryStream imageData = ArchiveData[level].Copy(FileList[level].Entries[item].Offset, FileList[level].Entries[item].Length); string filename = FileList[level].Entries[item].Filename; // Check to see if the image is compressed Compression compression = new Compression(imageData, filename); if (compression.Format != CompressionFormat.NULL) { // Decompress MemoryStream decompressedData = compression.Decompress(); if (decompressedData != null) imageData = decompressedData; } // Check to see if this is an image Images image = new Images(imageData, filename); if (image.Format == GraphicFormat.NULL) throw new GraphicFormatNotSupported(); // Try to open this image if we can try { new Image_Viewer(imageData, filename); } catch (GraphicFormatNeedsPalette) { int index = indexOfFile(image.PaletteFilename); if (index == -1) throw new Exception(); else new Image_Viewer(imageData, filename, ArchiveData[level].Copy(FileList[level].Entries[index].Offset, FileList[level].Entries[index].Length)); } } catch { } }
/* Extract files */ private void extract(object sender, EventArgs e) { /* Set the files to extract */ List<int> files = new List<int>(); if (sender == extractSelectedFiles) // Select only the files chosen { for (int i = 0; i < fileListView.SelectedIndices.Count; i++) { if (level == 0 || fileListView.SelectedIndices[i] > 0) files.Add(fileListView.SelectedIndices[i]); } } else // Select all the files { for (int i = (level == 0 ? 0 : 1); i < fileListView.Items.Count; i++) files.Add(i); } /* Don't continue if no files were selected */ if (files.Count == 0) return; /* Display the save file dialog or folder dialog */ string output_directory = String.Empty; string output_filename = String.Empty; /* Directory selection if there are more than one file */ if (files.Count > 1) { output_directory = FileSelectionDialog.SaveDirectory("Select a directory to output the files to"); /* Make sure a directory was selected */ if (output_directory == null || output_directory == String.Empty) return; } for (int i = 0; i < files.Count; i++) { try { string filename = FileList[level].Entries[files[i] - (level > 0 ? 1 : 0)].Filename; int num = files[i] - (level > 0 ? 1 : 0); output_filename = (filename == String.Empty ? num.ToString().PadLeft(FileList.Count.Digits(), '0') : (addFileNumberExtracted.Checked ? Path.GetFileNameWithoutExtension(filename) + '_' + num.ToString().PadLeft(FileList.Count.Digits(), '0') + Path.GetExtension(filename) : filename)); /* Which selection dialog do we want? */ if (files.Count == 1) { output_filename = FileSelectionDialog.SaveFile("Extract File", output_filename, (Path.GetExtension(filename) == String.Empty ? "All Files (*.*)|*.*" : Path.GetExtension(filename).Substring(1).ToUpper() + " File (*" + Path.GetExtension(filename) + ")|*" + Path.GetExtension(filename))); /* Make sure we selected a file */ if (output_filename == null || output_filename == String.Empty) return; output_directory = Path.GetDirectoryName(output_filename); output_filename = Path.GetFileName(output_filename); } /* If for some reason the output directory doesn't exist, create it */ if (!Directory.Exists(output_directory)) Directory.CreateDirectory(output_directory); /* Copy the data to a new stream */ MemoryStream outputData = ArchiveData[level].Copy(FileList[level].Entries[num].Offset, FileList[level].Entries[num].Length); /* Do we want to decompress the file? */ if (decompressExtracted.Checked) { Compression compression = new Compression(outputData, output_filename); if (compression.Format != CompressionFormat.NULL) { MemoryStream decompressedData = compression.Decompress(); if (decompressedData != null) { outputData = decompressedData; output_filename = compression.DecompressFilename; } } } // Convert the file to a PNG? if (extractImageAsPng.Checked) { Images images = new Images(outputData, output_filename); if (images.Format != GraphicFormat.NULL) { Bitmap imageData; try { imageData = images.Unpack(); } catch (GraphicFormatNeedsPalette) { // See if the file exists in the archive int index = indexOfFile(images.PaletteFilename); if (index != -1) images.Decoder.PaletteData = ArchiveData[level].Copy(FileList[level].Entries[index].Offset, FileList[level].Entries[index].Length); imageData = images.Unpack(); } // Now output the image if it was written if (imageData != null) { outputData = new MemoryStream(); imageData.Save(outputData, ImageFormat.Png); output_filename = Path.GetFileNameWithoutExtension(output_filename) + ".png"; } } } /* Write the file */ using (FileStream outputStream = new FileStream(output_directory + Path.DirectorySeparatorChar + output_filename, FileMode.Create, FileAccess.Write)) outputStream.Write(outputData); } catch { continue; } } }
// Extract the files private void run(object sender, DoWorkEventArgs e) { // Create the file list List<string> fileList = new List<string>(); foreach (string i in files) fileList.Add(i); for (int i = 0; i < fileList.Count; i++) { // Set the current file in the status status.CurrentFile = i; status.CurrentFileLocal = 0; // Set up the image file list for conversion List<string> imageFileList = new List<string>(); try { // Set up the file paths and open up the file string InFile = Path.GetFileName(fileList[i]); string InDirectory = Path.GetDirectoryName(fileList[i]); string OutFile = InFile; string OutDirectory = InDirectory; using (FileStream InputStream = new FileStream(fileList[i], FileMode.Open, FileAccess.Read)) { Stream InputData = InputStream; // Decompress the file if (decompressSourceFile.Checked) { Compression compression = new Compression(InputData, InFile); if (compression.Format != CompressionFormat.NULL) { MemoryStream DecompressedData = compression.Decompress(); if (DecompressedData != null) { InputData = DecompressedData; if (useStoredFilename.Checked) InFile = compression.DecompressFilename; } } } // Open up the archive Archive archive = new Archive(InputData, InFile); if (archive.Format == ArchiveFormat.NULL) continue; ArchiveFileList ArchiveFileList = archive.GetFileList(); if (ArchiveFileList == null || ArchiveFileList.Entries.Length == 0) continue; status.TotalFilesLocal = ArchiveFileList.Entries.Length; // Create the directory where we will extract the files to if (extractDirSameFilename.Checked && deleteSourceArchive.Checked) OutDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); // Create a temporary place to store the files for now else if (extractSameDir.Checked) OutDirectory = InDirectory; else OutDirectory = InDirectory + Path.DirectorySeparatorChar + archive.OutputDirectory + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(InFile); if (!Directory.Exists(OutDirectory)) Directory.CreateDirectory(OutDirectory); // Extract the data from the archive for (int j = 0; j < ArchiveFileList.Entries.Length; j++) { // Set the current file and get the data status.CurrentFileLocal = j; MemoryStream OutData = archive.GetData().Copy(ArchiveFileList.Entries[j].Offset, ArchiveFileList.Entries[j].Length); // Get the filename that the file will be extracted to if (extractFilenames.Checked && ArchiveFileList.Entries[j].Filename != String.Empty) OutFile = ArchiveFileList.Entries[j].Filename; else OutFile = j.ToString().PadLeft(ArchiveFileList.Entries.Length.Digits(), '0') + FileTypeInfo.GetFileExtension(OutData); // Decompress the data if (decompressExtractedFile.Checked) { Compression compression = new Compression(OutData, OutFile); if (compression.Format != CompressionFormat.NULL) { MemoryStream DecompressedData = compression.Decompress(); if (decompressExtractedDir.Checked) { // Write this to a different directory string TempOutFile = OutFile; // We will change it temporarily if (useStoredFilename.Checked) OutFile = compression.DecompressFilename; if (!Directory.Exists(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory)) Directory.CreateDirectory(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory); using (FileStream OutputStream = new FileStream(OutDirectory + Path.DirectorySeparatorChar + compression.OutputDirectory + Path.DirectorySeparatorChar + OutFile, FileMode.Create, FileAccess.Write)) OutputStream.Write(DecompressedData); OutFile = TempOutFile; // Reset the output file now } else OutData = DecompressedData; } } // See if we want to extract subarchives if (extractExtracted.Checked) { Archive archiveTemp = new Archive(OutData, OutFile); if (archiveTemp.Format != ArchiveFormat.NULL) { string AddFile = String.Empty; if (extractDirSameFilename.Checked && deleteSourceArchive.Checked) AddFile = fileList[i] + Path.DirectorySeparatorChar + OutFile; else AddFile = OutDirectory + Path.DirectorySeparatorChar + OutFile; fileList.Add(AddFile); status.AddFile(AddFile); } } // Output an image if (unpackImage.Checked) { Images images = new Images(OutData, OutFile); if (images.Format != GraphicFormat.NULL) imageFileList.Add(OutDirectory + Path.DirectorySeparatorChar + OutFile); // Add this to the image conversion list (in case we need a palette file) } // Write the file using (FileStream OutputStream = new FileStream(OutDirectory + Path.DirectorySeparatorChar + OutFile, FileMode.Create, FileAccess.Write)) OutputStream.Write(OutData); } } // Process image conversion now if (unpackImage.Checked && imageFileList.Count > 0) { // Reset the local file count status.CurrentFileLocal = 0; status.TotalFilesLocal = imageFileList.Count; for (int j = 0; j < imageFileList.Count; j++) { status.CurrentFileLocal = j; string InFileBitmap = Path.GetFileName(imageFileList[j]); string InDirectoryBitmap = Path.GetDirectoryName(imageFileList[j]); string OutFileBitmap = Path.GetFileNameWithoutExtension(InFileBitmap) + ".png"; string OutDirectoryBitmap = InDirectoryBitmap; // Load the file using (FileStream InputStream = new FileStream(imageFileList[j], FileMode.Open, FileAccess.Read)) { Images images = new Images(InputStream, Path.GetFileName(imageFileList[j])); if (images.Format != GraphicFormat.NULL) { if (!convertSameDir.Checked) OutDirectoryBitmap = InDirectoryBitmap + Path.DirectorySeparatorChar + images.OutputDirectory; // Start the conversion Bitmap ImageData = null; try { ImageData = images.Unpack(); } catch (GraphicFormatNeedsPalette) { // We need a palette file if (File.Exists(InDirectoryBitmap + Path.DirectorySeparatorChar + images.PaletteFilename)) { using (FileStream InStreamPalette = new FileStream(InDirectoryBitmap + Path.DirectorySeparatorChar + images.PaletteFilename, FileMode.Open, FileAccess.Read)) { images.Decoder.PaletteData = InStreamPalette; ImageData = images.Unpack(); } } } catch { continue; } // Output the image if (ImageData != null) { if (!Directory.Exists(OutDirectoryBitmap)) Directory.CreateDirectory(OutDirectoryBitmap); ImageData.Save(OutDirectoryBitmap + Path.DirectorySeparatorChar + OutFileBitmap, ImageFormat.Png); } } } if (deleteSourceImage.Checked && File.Exists(InDirectoryBitmap + Path.DirectorySeparatorChar + InFileBitmap) && File.Exists(OutDirectoryBitmap + Path.DirectorySeparatorChar + OutFileBitmap)) File.Delete(InDirectoryBitmap + Path.DirectorySeparatorChar + InFileBitmap); } } // Delete the source archive now if (deleteSourceArchive.Checked && File.Exists(fileList[i])) { File.Delete(fileList[i]); if (extractDirSameFilename.Checked) { // If the source and destination directory are on the same volume we can just move the directory if (Directory.GetDirectoryRoot(OutDirectory) == Directory.GetDirectoryRoot(fileList[i])) Directory.Move(OutDirectory, fileList[i]); // Otherwise we have to do a series of complicated file moves else MoveDirectory(new DirectoryInfo(OutDirectory), new DirectoryInfo(fileList[i])); } } } catch { // Something went wrong. Continue please. continue; } } // Close the status box now status.Close(); this.Close(); }