/// <summary> /// Write an MTPA file to disk. /// </summary> /// <param name="output">A writable stream of an MTPA file.</param> public void Save(Stream output) { using (var bw = new BinaryWriterX(output)) { var metadataSize = _mtpaHeader.MetadataSize; // Move passed the headers. bw.BaseStream.Position = Common.PacketHeaderXSize + MtpaHeaderSize + _mtpaHeaderData.Count * sizeof(int); // Write the metadata pointers which don't change (add not supported). foreach (var pointer in _mtpaTextMetadataPointers) { bw.Write(pointer); } // Move passed the metadata. var metadataStart = bw.BaseStream.Position; bw.BaseStream.Position += _mtpaHeader.MetadataCount * sizeof(int) * metadataSize; // Write out the text strings. var textStart = (int)bw.BaseStream.Position; for (var i = 0; i < Entries.Count; i++) { var text = Encoding.GetEncoding("Shift-JIS").GetBytes(Entries[i].EditedText); _mtpaTextMetadata[i].Offset = (int)bw.BaseStream.Position - textStart; bw.WriteROTnInt32(text.Length); bw.WriteROTnBytes(text); bw.Write((byte)0x01); bw.WriteAlignment(4, 0x01); } bw.WriteAlignment(8); // Update Packet Header _packetHeader.DataSize = (int)bw.BaseStream.Position - Common.PacketHeaderXSize; // ENRS bw.WriteType(_enrs); bw.Write(_enrsData); bw.WriteType(_enrsFooter); // Update Packet Header _packetHeader.PacketSize = (int)bw.BaseStream.Position - Common.PacketHeaderXSize; // Footer bw.WriteType(_mtpaFooter); // Write Text Metadata bw.BaseStream.Position = metadataStart; foreach (var pointer in _mtpaTextMetadata) { bw.WriteType(pointer); } // Write Packer Header bw.BaseStream.Position = 0; bw.WriteType(_packetHeader); // Write MTPA Header bw.WriteType(_mtpaHeader); // Write Unknown MTPA Data foreach (var data in _mtpaHeaderData) { bw.Write(data); } } }