Example #1
0
        public BinaryTagReader(Stream stream, bool autoDetectCompression)
        {
            if (stream.CanSeek && autoDetectCompression)
            {
                if (stream.IsGzipCompressed())
                {
                    _originalStream = stream;
                    _stream         = new GZipStream(_originalStream, CompressionMode.Decompress);
                }
                else if (stream.IsDeflateCompressed())
                {
                    _originalStream = stream;
                    _stream         = new DeflateStream(_originalStream, CompressionMode.Decompress);
                }
                else
                {
                    _stream = stream;
                }
            }
            else
            {
                _stream = stream;
            }

            _state = new TagState(FileAccess.Read);
            _state.Start();
        }
    public virtual bool IsNbtDocument(Stream stream)
    {
      bool result;
      long position;

      position = stream.Position;

      try
      {
        if (stream.IsGzipCompressed())
        {
          using (Stream decompressionStream = new GZipStream(stream, CompressionMode.Decompress, true))
          {
            result = decompressionStream.PeekNextByte() == (int)TagType.Compound;
          }
        }
        else if (stream.IsDeflateCompressed())
        {
          using (Stream decompressionStream = new DeflateStream(stream, CompressionMode.Decompress, true))
          {
            result = decompressionStream.PeekNextByte() == (int)TagType.Compound;
          }
        }
        else if (stream.PeekNextByte() == (int)TagType.Compound)
        {
          result = true;
        }
        else
        {
          result = false;
        }
      }
      catch
      {
        result = false;
      }

      stream.Position = position;

      return result;
    }
    public virtual TagCompound ReadDocument(Stream stream, ReadTagOptions options)
    {
      TagCompound tag;

      if (stream.IsGzipCompressed())
      {
        using (Stream decompressionStream = new GZipStream(stream, CompressionMode.Decompress))
        {
          _stream = decompressionStream;
          tag = (TagCompound)this.ReadTag(options);
        }
      }
      else if (stream.IsDeflateCompressed())
      {
        using (Stream decompressionStream = new DeflateStream(stream, CompressionMode.Decompress))
        {
          _stream = decompressionStream;
          tag = (TagCompound)this.ReadTag(options);
        }
      }
      else if (stream.PeekNextByte() == (int)TagType.Compound)
      {
        _stream = stream;
        tag = (TagCompound)this.ReadTag(options);
      }
      else
      {
        throw new InvalidDataException("Source stream does not contain a NBT document.");
      }

      return tag;
    }