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 BrowseButtonClick(object sender, EventArgs e) { int offset = (int)this.offsetNumericUpDown.Value; bool twice = this.twiceCheckBox.Checked; if (this.compressRadioButton.Checked) { byte[] data = null; if (UITools.ShowImportDataDialog(fileName => data = File.ReadAllBytes(fileName), FileDialogFilters.Binary)) { int limit = (int)this.offsetNumericUpDown.Maximum; CodecControl.Compress(data, offset, twice, limit); } } else { UITools.ShowExportDataDialog(fileName => File.WriteAllBytes(fileName, Context.Game.Decompress(offset, twice)), string.Empty, FileDialogFilters.Binary); } }