/// <summary> /// Reads an NBT tag from a stream. /// </summary> /// <param name="stream">The stream to load from.</param> /// <param name="name">Will be set to the name of the root tag.</param> /// <param name="compression">The compression to use, null or default to auto-detect.</param> /// <param name="encoding">The encoding to use for strings. Uses Java's weird MUTF8 encoding by default.</param> /// <param name="endianness">The endianness to use. PC = Big, Pocket Edition = Little.</param> /// <param name="ensureCompound">If true, throws an exception if the root tag isn't a compound.</param> public static TagBase ReadFrom(Stream stream, out string name, NbtCompression compression = null, Encoding encoding = null, Endianness endianness = Endianness.Big, bool ensureCompound = true) { if (stream == null) { throw new ArgumentNullException("stream"); } compression = compression ?? NbtCompression.AutoDetect(stream); encoding = encoding ?? MUTF8Encoding.Instance; using (stream = compression.Decompress(stream)) { var reader = new NBTStreamReader(stream, encoding, endianness); var type = reader.ReadTagType(); if (ensureCompound && (type != TagType.Compound)) { throw new InvalidDataException("Root tag is not a compound"); } return(reader.ReadTag(type, out name)); } }