CrystalMpq.MpqFileStream.ReadBlock C# (CSharp) Method

ReadBlock() private method

private ReadBlock ( int block ) : void
block int
return void
        private unsafe void ReadBlock(int block)
        {
            int length = (int)(fileHeader[block + 1] - fileHeader[block]);
            bool last = block == fileHeader.Length - 2;
            bool compressed = !(length == file.Archive.BlockSize || last && (uint)length == lastBlockLength);
            var buffer = compressed ? compressedBuffer : blockBuffer;

            file.Archive.ReadArchiveData(buffer, 0, offset + fileHeader[block], length);

            if (file.IsEncrypted)
            {
                // If last bytes don't fit in an uint, then they won't be encrypted/decrypted
                // Therefore we just leave "length" here as a parameter and bits 0..1 will be cut
                CommonMethods.Decrypt(buffer, seed + (uint)block, length);
            }

            if (compressed)
            {
                int byteCount;

                // Check the advanced compression scheme first, as it is the only used in modern games.
                if ((file.Flags & MpqFileFlags.MultiCompressed) != 0)
                    byteCount = CommonMethods.DecompressBlock(compressedBuffer, length, blockBuffer, true);
                else /*if ((file.Flags & MpqFileFlags.DclCompressed) != 0)*/
                    byteCount = CommonMethods.DecompressBlock(compressedBuffer, length, blockBuffer, false);

                if (byteCount != (last ? lastBlockLength : (uint)blockBuffer.Length)) throw new InvalidDataException();
            }

            // As an added bonus, clear the reference to the compressed data buffer once we don't need it anymore
            if (this.blockBuffer.Length == this.length) this.compressedBuffer = null;
        }