Exemplo n.º 1
0
        /// <summary>
        /// Parse the header of a chunk, with that we know the <see cref="ChunkType"/> and the length of the chunk
        /// Took extra caution because there is no <see cref="SubStream"/> to protect the reading.
        /// When I discover the length of the replay, I immediatly create a SubStream so i can protect the rest of the replay.
        /// </summary>
        /// <param name="replayInfo"></param>
        /// <returns></returns>
        public virtual async ValueTask <ChunkHeader> ParseChunkHeader()
        {
            if (SubStreamFactory.CanReadLength && SubStreamFactory.CanReadPosition &&
                SubStreamFactory.BaseStream.Position == SubStreamFactory.BaseStream.Length)
            {
                return(new ChunkHeader {
                    ChunkType = ChunkType.EndOfStream, ChunkSize = 0
                });
            }

            int       chunkSize;
            ChunkType chunkType;

            await using (SubStream chunkHeader = SubStreamFactory.CreateSubstream(8))
                await using (CustomBinaryReaderAsync customReader = new CustomBinaryReaderAsync(chunkHeader, true))
                {
                    try //TODO add case when you can seek.
                    {
                        chunkType = (ChunkType)await customReader.ReadUInt32Async();
                    }
                    catch (EndOfStreamException)
                    {
                        chunkHeader.CancelSelfRepositioning();
                        return(new ChunkHeader {
                            ChunkType = ChunkType.EndOfStream, ChunkSize = 0
                        });
                    }
                    chunkSize = await customReader.ReadInt32Async();

                    if ((uint)chunkType > 3)
                    {
                        return(new ChunkHeader {
                            ChunkType = ChunkType.Unknown, ChunkSize = 0
                        });
                    }
                }
            return(new ChunkHeader {
                ChunkType = chunkType, ChunkSize = chunkSize
            });
        }