/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> from which to load an <see cref="MpqArchive"/>.</param> /// <param name="loadListfile">If true, automatically execute <see cref="AddListfileFilenames()"/> after the <see cref="MpqArchive"/> is initialized.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="sourceStream"/> is null.</exception> /// <exception cref="MpqParserException">Thrown when the <see cref="MpqHeader"/> could not be found, or when the MPQ format version is not 0.</exception> public MpqArchive(Stream sourceStream, bool loadListfile = false) { _baseStream = sourceStream ?? throw new ArgumentNullException(nameof(sourceStream)); if (!TryLocateMpqHeader(_baseStream, out var mpqHeader, out _headerOffset)) { throw new MpqParserException("Unable to locate MPQ header."); } _mpqHeader = mpqHeader; _blockSize = BlockSizeModifier << _mpqHeader.BlockSize; _archiveFollowsHeader = _mpqHeader.IsArchiveAfterHeader(); if (_mpqHeader.MpqVersion != 0) { throw new MpqParserException($"MPQ format version {_mpqHeader.MpqVersion} is not supported"); } using (var reader = new BinaryReader(_baseStream, new UTF8Encoding(), true)) { // Load hash table _baseStream.Seek(_mpqHeader.HashTablePosition, SeekOrigin.Begin); _hashTable = new HashTable(reader, _mpqHeader.HashTableSize); // Load entry table _baseStream.Seek(_mpqHeader.BlockTablePosition, SeekOrigin.Begin); _blockTable = new BlockTable(reader, _mpqHeader.BlockTableSize, (uint)_headerOffset); } if (loadListfile) { AddListfileFilenames(); } }
/// <summary> /// Initializes a new instance of the <see cref="MpqArchive"/> class. /// </summary> /// <param name="sourceStream">The <see cref="Stream"/> from which to load an <see cref="MpqArchive"/>.</param> /// <param name="loadListFile">If <see langword="true"/>, automatically add filenames from the <see cref="MpqArchive"/>'s <see cref="ListFile"/> after the archive is initialized.</param> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="sourceStream"/> is <see langword="null"/>.</exception> /// <exception cref="MpqParserException">Thrown when the <see cref="MpqHeader"/> could not be found, or when the MPQ format version is not 0.</exception> public MpqArchive(Stream sourceStream, bool loadListFile = false) { _isStreamOwner = true; _baseStream = sourceStream ?? throw new ArgumentNullException(nameof(sourceStream)); if (!TryLocateMpqHeader(_baseStream, out var mpqHeader, out _headerOffset)) { throw new MpqParserException("Unable to locate MPQ header."); } _mpqHeader = mpqHeader; _blockSize = BlockSizeModifier << _mpqHeader.BlockSize; _archiveFollowsHeader = _mpqHeader.IsArchiveAfterHeader(); using (var reader = new BinaryReader(_baseStream, new UTF8Encoding(), true)) { // Load hash table _baseStream.Seek(_mpqHeader.HashTablePosition, SeekOrigin.Begin); _hashTable = new HashTable(reader, _mpqHeader.HashTableSize); // Load entry table _baseStream.Seek(_mpqHeader.BlockTablePosition, SeekOrigin.Begin); _blockTable = new BlockTable(reader, _mpqHeader.BlockTableSize, (uint)_headerOffset); } if (loadListFile) { if (TryOpenFile(ListFile.FileName, out var listFileStream)) { using var listFileReader = new StreamReader(listFileStream, leaveOpen: false); AddFileNames(listFileReader.ReadListFile().FileNames); } } }