static void Test(byte[] bytes, Method method) { var bytes2 = new byte[bytes.Length]; switch (method) { case Method.RLE: bytes2 = RLE.Decompress(new MemoryStream(RLE.Compress(new MemoryStream(bytes))), bytes.Length); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.Yaz0: bytes2 = Yaz0.Decompress(new MemoryStream(Yaz0.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; case Method.Yay0: bytes2 = Yay0.Decompress(new MemoryStream(Yay0.Compress(new MemoryStream(bytes)))); Assert.IsTrue(bytes.SequenceEqual(bytes2)); break; } }
public void Save(string filename = "") { if (!string.IsNullOrEmpty(filename)) { FileInfo = new FileInfo(filename); } // Save As... if (!string.IsNullOrEmpty(filename)) { var ms = new MemoryStream(); _sarc.Save(ms, true); _sarc.Close(); using (var bw = new BinaryWriterX(FileInfo.Create())) { bw.Write(Yaz0.Compress(ms, ByteOrder.BigEndian)); } } else { // Create the temp file var ms = new MemoryStream(); _sarc.Save(ms, true); _sarc.Close(); using (var bw = new BinaryWriterX(File.Create(FileInfo.FullName + ".tmp"))) { bw.Write(Yaz0.Compress(ms, ByteOrder.BigEndian)); } // Delete the original FileInfo.Delete(); // Rename the temporary file File.Move(FileInfo.FullName + ".tmp", FileInfo.FullName); } // Reload the new file to make sure everything is in order Load(FileInfo.FullName); }
public void InjectFile(int vrom, byte[] data) { var file = GetFile(vrom); if (file == null) { throw new Z64GameException("Invalid VROM"); } int oldSize = file.Compressed ? file.RomEnd - file.RomStart : file.Data.Length; var restStart = file.RomEnd != 0 ? file.RomEnd : file.RomStart + file.Data.Length; //find rom end Z64File last = null; for (int i = 0; i < GetFileCount(); i++) { var iter = GetFileFromIndex(i); if (iter.Valid() && !iter.Deleted && (last == null || iter.RomStart > last.RomStart)) { last = iter; } } if (last == null) { throw new Exception("?"); } var restEnd = last.RomEnd; //save rest byte[] rest = new byte[restEnd - restStart]; Buffer.BlockCopy(Rom.RawRom, restStart, rest, 0, rest.Length); var encData = file.Compressed ? Yaz0.Compress(data) : data; int off = encData.Length - oldSize; //copy new file in rom Buffer.BlockCopy(encData, 0, Rom.RawRom, file.RomStart, encData.Length); file.Data = data; file.VRomEnd = (file.VRomStart + data.Length) + 0xF & ~0xF; if (file.Compressed) { file.RomEnd = (file.RomStart + encData.Length) + 0xF & ~0xF; } //copy rest Buffer.BlockCopy(rest, 0, Rom.RawRom, file.RomEnd, rest.Length); //offset each file rom addresses for (int i = _files.IndexOf(file) + 1; i < _files.Count; i++) { _files[i].RomStart = (_files[i].RomStart + off) + 0xF & ~0xF; _files[i].RomEnd = (_files[i].RomEnd + off) + 0xF & ~0xF; } file.Data = data; file.Deleted = false; }
public static void Compress(object sender, EventArgs e) { var tsi = sender as ToolStripMenuItem; if (!Shared.PrepareFiles("Open a decompressed " + tsi?.Tag + "file...", "Save your compressed file...", ".decomp", out var openFile, out var saveFile, true)) { return; } try { using (openFile) using (var outFs = new BinaryWriterX(saveFile)) switch (tsi?.Tag) { case Compression.L5LZ10: outFs.Write(Level5.Compress(openFile, Level5.Method.LZ10)); break; case Compression.L5Huff4: outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman4Bit)); break; case Compression.L5Huff8: outFs.Write(Level5.Compress(openFile, Level5.Method.Huffman8Bit)); break; case Compression.L5RLE: outFs.Write(Level5.Compress(openFile, Level5.Method.RLE)); break; case Compression.NLZ10: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ10)); break; case Compression.NLZ11: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ11)); break; case Compression.NLZ60: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.LZ60)); break; case Compression.NHuff4: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff4)); break; case Compression.NHuff8: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.Huff8)); break; case Compression.NRLE: outFs.Write(Nintendo.Compress(openFile, Nintendo.Method.RLE)); break; case Compression.LZ77: outFs.Write(LZ77.Compress(openFile)); break; case Compression.RevLZ77: outFs.Write(RevLZ77.Compress(openFile)); break; case Compression.LZOvl: outFs.Write(LZOvl.Compress(openFile)); break; case Compression.LZ4: outFs.Write(Kontract.Compression.LZ4.Compress(openFile)); break; case Compression.MIO0LE: outFs.Write(MIO0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.MIO0BE: outFs.Write(MIO0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.Yay0LE: outFs.Write(Yay0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yay0BE: outFs.Write(Yay0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.Yaz0LE: outFs.Write(Yaz0.Compress(openFile, ByteOrder.LittleEndian)); break; case Compression.Yaz0BE: outFs.Write(Yaz0.Compress(openFile, ByteOrder.BigEndian)); break; case Compression.GZip: outFs.Write(GZip.Compress(openFile)); break; case Compression.ZLib: outFs.Write(ZLib.Compress(openFile)); break; case Compression.PSVSpikeChun: outFs.Write(PSVSpikeChun.Compress(openFile)); break; } } catch (Exception ex) { MessageBox.Show(ex.ToString(), tsi?.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } MessageBox.Show($"Successfully compressed {Path.GetFileName(openFile.Name)}.", tsi.Text, MessageBoxButtons.OK, MessageBoxIcon.Information); }