/// <summary> /// Repairs corrupted values in an <see cref="MpqArchive"/>. /// </summary> /// <param name="sourceStream">The stream containing the archive that needs to be repaired.</param> /// <param name="leaveOpen">If false, the given <paramref name="sourceStream"/> will be disposed at the end of this method.</param> /// <returns>A stream containing the repaired archive.</returns> public static MemoryStream Restore(Stream sourceStream, bool leaveOpen = false) { if (sourceStream is null) { throw new ArgumentNullException(nameof(sourceStream)); } if (!TryLocateMpqHeader(sourceStream, out var mpqHeader, out var headerOffset)) { throw new MpqParserException($"Unable to locate MPQ header."); } if (mpqHeader.MpqVersion != 0) { throw new MpqParserException($"MPQ format version {mpqHeader.MpqVersion} is not supported"); } var memoryStream = new MemoryStream(); using (var writer = new BinaryWriter(memoryStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); var archiveSize = 0U; var hashTableEntries = mpqHeader.HashTableSize; var blockTableEntries = mpqHeader.BlockTableSize > MpqTable.MaxSize ? mpqHeader.IsArchiveAfterHeader() ? mpqHeader.BlockTablePosition < mpqHeader.HeaderOffset ? (mpqHeader.HeaderOffset - mpqHeader.BlockTablePosition) / MpqEntry.Size : (uint)(sourceStream.Length - sourceStream.Position) / MpqEntry.Size : throw new MpqParserException($"Unable to determine true BlockTable size.") : mpqHeader.BlockTableSize; var hashTable = (HashTable?)null; var blockTable = (BlockTable?)null; using (var reader = new BinaryReader(sourceStream, new UTF8Encoding(), true)) { // Load hash table sourceStream.Seek(mpqHeader.HashTablePosition, SeekOrigin.Begin); hashTable = new HashTable(reader, hashTableEntries); // Load entry table sourceStream.Seek(mpqHeader.BlockTablePosition, SeekOrigin.Begin); blockTable = new BlockTable(reader, blockTableEntries, (uint)headerOffset); // Load archive files for (var i = 0; i < blockTable.Size; i++) { var entry = blockTable[i]; if ((entry.Flags & MpqFileFlags.Garbage) == 0) { var size = entry.CompressedSize; var flags = entry.Flags; if (entry.IsEncrypted && entry.Flags.HasFlag(MpqFileFlags.BlockOffsetAdjustedKey)) { // To prevent encryption seed becoming incorrect, save file uncompressed and unencrypted. var pos = sourceStream.Position; using (var mpqStream = new MpqStream(entry, sourceStream, BlockSizeModifier << mpqHeader.BlockSize)) { mpqStream.CopyTo(memoryStream); } sourceStream.Position = pos + size; size = entry.FileSize; flags = entry.Flags & ~(MpqFileFlags.Compressed | MpqFileFlags.Encrypted | MpqFileFlags.BlockOffsetAdjustedKey); } else { sourceStream.Position = entry.FilePosition; writer.Write(reader.ReadBytes((int)size)); } blockTable[i] = new MpqEntry(null, 0, MpqHeader.Size + archiveSize, size, entry.FileSize, flags); archiveSize += size; } else { blockTable[i] = new MpqEntry(null, 0, MpqHeader.Size + archiveSize, 0, 0, 0); } } } // Fix invalid block indices and locales. for (var i = 0; i < hashTable.Size; i++) { var hash = hashTable[i]; if (!hash.IsEmpty && !hash.IsDeleted && hash.BlockIndex > BlockTable.MaxSize) { // TODO: don't force neutral locale if another MpqHash exists with the same Name1 and Name2, and that has the neutral locale hashTable[i] = new MpqHash(hash.Name, MpqLocale.Neutral /*hash.Locale & (MpqLocale)0x00000FFF*/, hash.BlockIndex & (BlockTable.MaxSize - 1), hash.Mask); } } hashTable.SerializeTo(memoryStream); blockTable.SerializeTo(memoryStream); writer.Seek(0, SeekOrigin.Begin); new MpqHeader(archiveSize, hashTableEntries, blockTableEntries, mpqHeader.BlockSize).WriteTo(writer); } if (!leaveOpen) { sourceStream.Dispose(); } memoryStream.Position = 0; return(memoryStream); }
/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> containing pre-archive data. Can be null.</param> /// <param name="mpqFiles">The <see cref="MpqFile"/>s that should be added to the archive.</param> /// <param name="hashTableSize">The desired size of the <see cref="BlockTable"/>. Larger size decreases the likelihood of hash collisions.</param> /// <param name="blockSize">The size of blocks in compressed files, which is used to enable seeking.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="mpqFiles"/> collection is null.</exception> public MpqArchive(Stream sourceStream, ICollection <MpqFile> mpqFiles, ushort?hashTableSize = null, ushort blockSize = 8) { // TODO: copy sourceStream contents to a new stream if CanWrite property is false (can do this in alignStream method) _baseStream = AlignStream(sourceStream ?? new MemoryStream()); _headerOffset = _baseStream.Position; _blockSize = BlockSizeModifier << blockSize; var fileCount = (uint)(mpqFiles ?? throw new ArgumentNullException(nameof(mpqFiles))).Count; _hashTable = new HashTable(Math.Max(hashTableSize ?? fileCount * 8, fileCount)); _blockTable = new BlockTable(fileCount); using (var writer = new BinaryWriter(_baseStream, new UTF8Encoding(false, true), true)) { // Skip the MPQ header, since its contents will be calculated afterwards. writer.Seek((int)MpqHeader.Size, SeekOrigin.Current); const bool archiveBeforeTables = true; uint hashTableEntries = 0; // Write Archive var fileIndex = 0U; var fileOffset = archiveBeforeTables ? MpqHeader.Size : throw new NotImplementedException(); var filePos = fileOffset; // TODO: add support for encryption of the archive files foreach (var mpqFile in mpqFiles) { var locale = MpqLocale.Neutral; mpqFile.AddToArchive((uint)_headerOffset, fileIndex, filePos, locale, _hashTable.Mask); if (archiveBeforeTables) { mpqFile.SerializeTo(writer, true); } hashTableEntries += _hashTable.Add(mpqFile.MpqHash, mpqFile.HashIndex, mpqFile.HashCollisions); _blockTable.Add(mpqFile.MpqEntry); filePos += mpqFile.MpqEntry.CompressedSize; fileIndex++; } // Match size of blocktable with amount of occupied entries in hashtable /* * for ( var i = blockTable.Size; i < hashTableEntries; i++ ) * { * var entry = MpqEntry.Dummy; * entry.SetPos( filePos ); * blockTable.Add( entry ); * } * blockTable.UpdateSize(); */ _hashTable.SerializeTo(writer); _blockTable.SerializeTo(writer); if (!archiveBeforeTables) { foreach (var mpqFile in mpqFiles) { mpqFile.SerializeTo(writer, true); } } writer.Seek((int)_headerOffset, SeekOrigin.Begin); _mpqHeader = new MpqHeader(filePos - fileOffset, _hashTable.Size, _blockTable.Size, blockSize, archiveBeforeTables); _mpqHeader.WriteToStream(writer); } }