public MTV(Stream input) { _stream = input; // Offsets and Sizes using (var br = new BinaryReaderX(input, true)) { int index = 0; while (br.BaseStream.Position < br.BaseStream.Length) { byte[] fileData; int extraNameID = -1; var magic = br.ReadString(4); if (magic == "TIM2") { br.BaseStream.Position += 0xc; var size = br.ReadInt32() + 0x10; br.BaseStream.Position -= 0x14; fileData = br.ReadBytes(size); } else if (magic == "SShd") { var origOffset = br.BaseStream.Position - 4; //Header var headerSize = br.ReadInt32(); br.BaseStream.Position += headerSize - 8; br.BaseStream.Position = (br.BaseStream.Position + 15) & ~15; headerSize = (headerSize + 15) & ~15; //Body br.ReadString(4); //SSbd var bodySize = br.ReadInt32(); br.BaseStream.Position += bodySize - 8; br.BaseStream.Position = (br.BaseStream.Position + 16) & ~15; bodySize = (bodySize + 16) & ~15; br.BaseStream.Position = origOffset; fileData = br.ReadBytes(headerSize + bodySize); } else { compressed = true; br.BaseStream.Position -= 4; var data = LZSSVLE.Decompress(br.BaseStream, true); using (var br2 = new BinaryReaderX(new MemoryStream(data))) { var nr = br2.ReadUInt32(); if (nr > 0 && nr < 0x10) { br2.BaseStream.Position -= 4; var header = new VarHeader(br2.BaseStream); for (int i = 0; i < header.fileEntries - 1; i++) { br2.BaseStream.Position = header.entries[i].offset; fileData = br2.ReadBytes(header.entries[i].size); var Afi = new MtvArchiveFileInfo { FileData = new MemoryStream(fileData), State = ArchiveFileState.Archived, id = i }; Files.Add(Afi); // Filename var brP = new BinaryReaderX(Afi.FileData, true); var matchedP = brP.ReadString(4); var extensionP = _knownFiles.ContainsKey(matchedP) ? _knownFiles[matchedP] : ".bin"; brP.BaseStream.Position = 0; Afi.FileName = index.ToString("000000") + "_" + i.ToString("00") + extensionP; } extraNameID = header.fileEntries - 1; br2.BaseStream.Position = header.entries.Last().offset; fileData = br2.ReadBytes(header.entries.Last().size); List <byte> byteTmp = new List <byte>(); var bk = br.BaseStream.Position; while (br.BaseStream.Position % 0x10 != 0) { byteTmp.Add(br.ReadByte()); } if (!byteTmp.ToArray().Equals(new byte[byteTmp.Count])) { br.BaseStream.Position = bk; } } else { fileData = data.ToArray(); } } } var afi = new MtvArchiveFileInfo { FileData = new MemoryStream(fileData), State = ArchiveFileState.Archived, id = extraNameID }; Files.Add(afi); // Filename var br3 = new BinaryReaderX(afi.FileData, true); var matched = br3.ReadString(4); var extension = _knownFiles.ContainsKey(matched) ? _knownFiles[matched] : ".bin"; br3.BaseStream.Position = 0; string extra = (extraNameID > -1) ? "_" + extraNameID.ToString("00") : ""; afi.FileName = index.ToString("000000") + extra + extension; br.BaseStream.Position = (br.BaseStream.Position + 15) & ~15; index++; } } }
public static void Decompress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!Shared.PrepareFiles("Open a " + tsi?.Tag + " compressed file...", "Save your decompressed file...", ".decomp", out var openFile, out var saveFile)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi?.Tag) { case Compression.Level5: outFs.Write(Level5.Decompress(openFile)); break; case Compression.Nintendo: outFs.Write(Nintendo.Decompress(openFile)); break; case Compression.LZ77: outFs.Write(LZ77.Decompress(openFile)); break; case Compression.RevLZ77: outFs.Write(RevLZ77.Decompress(openFile)); break; case Compression.LZOvl: outFs.Write(LZOvl.Decompress(openFile)); break; case Compression.LZ4: outFs.Write(Kontract.Compression.LZ4.Decompress(openFile)); break; case Compression.MIO0LE: outFs.Write(MIO0.Decompress(openFile, ByteOrder.LittleEndian)); break; case Compression.MIO0BE: outFs.Write(MIO0.Decompress(openFile, ByteOrder.BigEndian)); break; case Compression.Yay0LE: outFs.Write(Yay0.Decompress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yay0BE: outFs.Write(Yay0.Decompress(openFile, ByteOrder.BigEndian)); break; case Compression.Yaz0LE: outFs.Write(Yaz0.Decompress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yaz0BE: outFs.Write(Yaz0.Decompress(openFile, ByteOrder.BigEndian)); break; case Compression.LZECD: outFs.Write(LZECD.Decompress(openFile)); break; case Compression.LZ10VLE: outFs.Write(LZSSVLE.Decompress(openFile)); break; case Compression.GZip: outFs.Write(GZip.Decompress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Decompress(openFile)); break; case Compression.PSVSpikeChun: outFs.Write(PSVSpikeChun.Decompress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show($"Successfully decompressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static void Decompress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!PrepareFiles("Open a " + tsi.Tag.ToString() + " compressed file...", "Save your decompressed file...", ".decomp", out FileStream openFile, out FileStream saveFile)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi.Tag) { case Compression.Level5: outFs.Write(Level5.Decompress(openFile)); break; case Compression.GZip: outFs.Write(GZip.Decompress(openFile)); break; case Compression.Huff4: outFs.Write(Huffman.Decompress(openFile, 4)); break; case Compression.Huff8: outFs.Write(Huffman.Decompress(openFile, 8)); break; case Compression.LZ10: outFs.Write(LZ10.Decompress(openFile)); break; case Compression.LZ11: outFs.Write(LZ11.Decompress(openFile)); break; case Compression.LZ77: outFs.Write(LZ77.Decompress(openFile)); break; case Compression.LZSS: outFs.Write(LZSS.Decompress(openFile, LZSS.GetDecompressedSize(openFile))); break; case Compression.LZSSVLE: outFs.Write(LZSSVLE.Decompress(openFile)); break; case Compression.RevLZ77: outFs.Write(RevLZ77.Decompress(openFile)); break; case Compression.LZECD: outFs.Write(LZECD.Decompress(openFile)); break; case Compression.RLE: outFs.Write(RLE.Decompress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Decompress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } MessageBox.Show($"Successfully decompressed {Path.GetFileName(openFile.Name)}.", tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }