示例#1
0
    public static void ReadChunk( this BinaryReader self, ReadChunkDelegate readAction )
    {
        // self.ReadGuardBytes ( StartBytes );

        uint chunckSize = self.ReadUInt32 ();
        if ( chunckSize > 0 )
        {
            long position = self.BaseStream.Position;

            byte[] bytes = self.ReadBytes ( (int)chunckSize );
            MemoryStream ms = new MemoryStream ( bytes );

            try { readAction.Invoke ( new BinaryReader ( ms ) ); }
            catch ( System.Exception ex )
            {
                Debug.LogError ( "Error reading chunk stream. Got an exception." );
                Debug.LogException ( ex );
            }

            long positionToBe = ( position + chunckSize );
            if ( self.BaseStream.Position != positionToBe )
            {
                Debug.LogError ( "Error reading stream, chunk size mismatch. Seeking in stream ( position is " + self.BaseStream.Position + ", should be : " + positionToBe + "." );
                self.BaseStream.Seek ( positionToBe, SeekOrigin.Begin );
            }

        }

        // self.ReadGuardBytes ( EndBytes );
    }
示例#2
0
        public void ReadChunks(ReadChunkDelegate tryParseChunk)
        {
            do
            {
                ChunkId chunkID = ChunkId.FromStream(_reader);
                if (chunkID == ChunkId.Empty) // End reached
                {
                    break;
                }

                // Check if chunk is known, and if not, mark it
                if (_knownChunkList != null && !UnknownChunksFound &&
                    !_knownChunkList.Contains(chunkID))
                {
                    UnknownChunksFound = true;
                }

                // Read up to a 64 bit number for the chunk size
                long chunkSize = LEB128.ReadLong(_reader);

                // Try loading chunk content
                long      chunkStart      = _reader.BaseStream.Position;
                bool      chunkRecognized = false;
                Exception chunkException  = null;
                try
                {
                    chunkRecognized = tryParseChunk(chunkID, chunkSize);
                }
                catch (OperationCanceledException)
                { // Don't actually keep going if it's an 'OperationCanceledException'
                    throw;
                }
                catch (Exception exc)
                {
                    chunkException = exc;
                }
                long readDataCount = _reader.BaseStream.Position - chunkStart;

                // Print messages for various problems that might have occurred while loading
                if (chunkException != null)
                {
                    logger.Error(chunkException, "Chunk loading raised an exception" + GetLocoationStr(chunkStart, chunkSize, chunkID));
                }
                else if (!chunkRecognized)
                {
                    logger.Warn("Chunk not recognized" + GetLocoationStr(chunkStart, chunkSize, chunkID));
                }
                else if (readDataCount > chunkSize)
                {
                    logger.Error("More data was read than available (Read: " + readDataCount + " Available: " + chunkSize + ")" + GetLocoationStr(chunkStart, chunkSize, chunkID));
                }
                else if (readDataCount < chunkSize)
                {
                    logger.Warn("Not all the available data was read (Read: " + readDataCount + " Available: " + chunkSize + ")" + GetLocoationStr(chunkStart, chunkSize, chunkID));
                }

                // Adjust _stream position if necessaary
                if (readDataCount != chunkSize)
                {
                    _reader.BaseStream.Position = chunkStart + chunkSize;
                }
            } while (true);
        }