private static void Compress(byte[] data, int offset, bool twice, int limit) { byte[] compData = Codec.Compress(data, twice, true); string info = CodecControl.FormatCompressedChunkInfo("New data", offset, compData.Length, data.Length); string oldInfo; try { int oldCompSize = Context.Game.GetCompressedChunkLength(offset); int oldUncompSize = Context.Game.Decompress(offset, twice).Length; oldInfo = CodecControl.FormatCompressedChunkInfo("Old data", offset, oldCompSize, oldUncompSize); } catch (InvalidDataException) { oldInfo = $"Cannot decompress data at {offset:X}." + Environment.NewLine; } info = oldInfo + Environment.NewLine + info; if (offset + compData.Length > limit) { info += Environment.NewLine + "Not enough room to fit the new data. Operation cancelled."; UITools.ShowError(info); } else if (UITools.ShowInfo(info, MessageBoxButtons.OKCancel) == DialogResult.OK) { Context.Game.InsertData(compData, offset); } }
private void WriteBlock() { if (_blockCount > 0) { byte[] dataToWrite = _blockStream.ToArray(); // write count _encoder.WriteLong(_blockCount); // write data _encoder.WriteBytes(_codec.Compress(dataToWrite)); // write sync marker _encoder.WriteFixed(_syncData); // reset / re-init block _blockCount = 0; _blockStream = new MemoryStream(); _blockEncoder = new Writer(_blockStream); } }
private void TestCodec(Region region, int offset, bool twice) { Game game = File.GetGame(region); byte[] romBuffer = File.ReadRom(region); byte[] originalCompressedData = Codec.GetCompressedChunk(romBuffer, offset); byte[] data = game.Decompress(offset, twice); byte[] newCompressedData = Codec.Compress(data, twice, true); // The new compressed data has to be different from the original compressed data, // since both didn't use the same compressor. Assert.AreNotEqual(originalCompressedData, newCompressedData); byte[] originalDecompressedData = Codec.Decompress(originalCompressedData); byte[] newDecompressedData = Codec.Decompress(newCompressedData); if (twice) { originalDecompressedData = Codec.Decompress(originalDecompressedData); newDecompressedData = Codec.Decompress(newDecompressedData); } // The decompressed data has to be the same, otherwise this implies that // simply recompressing unmodified data somehow modified it. Assert.AreEqual(originalDecompressedData, newDecompressedData); game.InsertData(newCompressedData, offset); string filePath = File.GetOutputPath($"SMK_{region}_{offset:X}_{twice}.sfc"); game.SaveRom(filePath); byte[] newRomBuffer = System.IO.File.ReadAllBytes(filePath); byte[] newResavedCompressedData = Codec.GetCompressedChunk(newRomBuffer, offset); // Ensure the resaved ROM contains the new compressed data Assert.AreEqual(newCompressedData, newResavedCompressedData); }