Class NbtFile
- Namespace
- fNbt
- Assembly
- fNbt.dll
Represents a complete NBT file.
public sealed class NbtFile
- Inheritance
-
NbtFile
- Inherited Members
Constructors
NbtFile()
Creates an empty NbtFile with the current defaults
(DefaultFlavor and the other NbtOptions defaults).
RootTag will be set to an empty NbtCompound with a blank name ("").
public NbtFile()
NbtFile(string)
Loads NBT data from a file using the most common settings.
Automatically detects compression, and reads with the current defaults
(DefaultFlavor and the other NbtOptions defaults).
public NbtFile(string fileName)
Parameters
fileNamestringName of the file from which data will be loaded.
Exceptions
- ArgumentNullException
fileNameisnull.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
NbtFile(NbtCompound)
Creates a new NBT file with the given root tag.
public NbtFile(NbtCompound rootTag)
Parameters
rootTagNbtCompoundCompound tag to set as the root tag. May be
null.
Exceptions
- ArgumentException
If given
rootTagis unnamed.
NbtFile(NbtCompound, NbtFlavor)
Creates a new NBT file with the given root tag and flavor, with the current
default policy settings. The tag is used directly, not cloned. NbtFile is not a
tag container, so a root tag may be shared between files: to re-save a loaded document
under a different flavor, pass its RootTag here.
public NbtFile(NbtCompound rootTag, NbtFlavor flavor)
Parameters
rootTagNbtCompoundCompound tag to set as the root tag. May not be
null.flavorNbtFlavorEncoding to read and write with.
Exceptions
- ArgumentNullException
rootTagorflavorisnull.- ArgumentException
If given
rootTagis unnamed; or the flavor has no root name (use NbtCodec for those).
NbtFile(NbtCompound, NbtOptions)
Creates a new NBT file with the given root tag and options.
public NbtFile(NbtCompound rootTag, NbtOptions options)
Parameters
rootTagNbtCompoundCompound tag to set as the root tag. May not be
null.optionsNbtOptionsSettings to use, resolved here. May not be
null.
Exceptions
- ArgumentNullException
rootTag,options, or the options'Flavorisnull.- ArgumentException
If given
rootTagis unnamed; or if the options' flavor has no root name.- ArgumentOutOfRangeException
MaxAllocationis zero or negative.
NbtFile(NbtFlavor)
Creates an empty NbtFile for the given flavor, with the current default
policy settings. RootTag will be set to an empty NbtCompound with a blank
name ("").
public NbtFile(NbtFlavor flavor)
Parameters
flavorNbtFlavorEncoding to read and write with.
Exceptions
- ArgumentNullException
flavorisnull.- ArgumentException
The flavor has no root name; use NbtCodec for those.
NbtFile(NbtOptions)
Creates an empty NbtFile with the given options.
RootTag will be set to an empty NbtCompound with a blank name ("").
public NbtFile(NbtOptions options)
Parameters
optionsNbtOptionsSettings to use, resolved here. May not be
null.
Exceptions
- ArgumentNullException
optionsor itsFlavorisnull.- ArgumentException
The options' flavor has no root name; use NbtCodec for those.
- ArgumentOutOfRangeException
MaxAllocationis zero or negative.
Properties
BigEndian
Whether this file's flavor is big-endian.
[Obsolete("Use Flavor instead. To change encodings, create a new NbtFile over the same RootTag with the target flavor.")]
public bool BigEndian { get; }
Property Value
BigEndianByDefault
Whether the current DefaultFlavor is big-endian.
[Obsolete("Use NbtOptions.DefaultFlavor instead. NbtFlavor.Java is big-endian, NbtFlavor.Bedrock little-endian.")]
public static bool BigEndianByDefault { get; }
Property Value
BufferSize
Gets or sets the size of internal buffer used for reading files and streams.
Initialized to value of DefaultBufferSize property.
public int BufferSize { get; set; }
Property Value
Exceptions
- ArgumentOutOfRangeException
value is negative.
DefaultBufferSize
Gets or sets the default value of BufferSize property. Default is 8192.
Set to 0 to disable buffering by default.
public static int DefaultBufferSize { get; set; }
Property Value
Exceptions
- ArgumentOutOfRangeException
value is negative.
FileCompression
Gets the compression method used for most recent loading/saving of this file. Defaults to AutoDetect.
public NbtCompression FileCompression { get; }
Property Value
FileName
Gets the file name used for most recent loading/saving of this file.
May be null, if this NbtFile instance has not been loaded from, or saved to, a file.
public string? FileName { get; }
Property Value
Flavor
The flavor this file reads and writes with, fixed at construction. To re-save a document under a different flavor, create a new NbtFile over the same RootTag: both files then share one tree.
public NbtFlavor Flavor { get; }
Property Value
RootTag
Root tag of this file. Must be a named CompoundTag. Defaults to an empty-named tag.
public NbtCompound RootTag { get; set; }
Property Value
Remarks
The assigned tag may already belong to another compound or list: NbtFile is not
a container and does not set or clear Parent. Saving then writes only this subtree,
as a standalone document.
Exceptions
- ArgumentException
If given tag is unnamed.
- ArgumentNullException
If value is
null.
Methods
LoadFromBuffer(byte[], int, int, NbtCompression)
Loads NBT data from a byte array. Existing RootTag will be replaced. FileName will be set to null.
public long LoadFromBuffer(byte[] buffer, int index, int length, NbtCompression compression)
Parameters
bufferbyte[]Byte array from which data will be loaded.
indexintThe index into
bufferat which the stream begins. Must not be negative.lengthintMaximum number of bytes to read from the given buffer. Must not be negative. An EndOfStreamException is thrown if NBT stream is longer than the given length.
compressionNbtCompressionCompression method to use for loading/saving this file.
Returns
- long
Number of bytes read from the buffer.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression; ifindexorlengthis less than zero; if the sum ofindexandlengthis greater than the length ofbuffer.- EndOfStreamException
If NBT stream extends beyond the given
length.- InvalidDataException
If file compression could not be detected or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
LoadFromBuffer(byte[], int, int, NbtCompression, TagSelector?)
Loads NBT data from a byte array. Existing RootTag will be replaced. FileName will be set to null.
public long LoadFromBuffer(byte[] buffer, int index, int length, NbtCompression compression, TagSelector? selector)
Parameters
bufferbyte[]Byte array from which data will be loaded.
indexintThe index into
bufferat which the stream begins. Must not be negative.lengthintMaximum number of bytes to read from the given buffer. Must not be negative. An EndOfStreamException is thrown if NBT stream is longer than the given length.
compressionNbtCompressionCompression method to use for loading/saving this file.
selectorTagSelectorOptional callback to select which tags to load into memory. Root may not be skipped. No reference is stored to this callback after loading (don't worry about implicitly captured closures). May be
null.
Returns
- long
Number of bytes read from the buffer.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression; ifindexorlengthis less than zero; if the sum ofindexandlengthis greater than the length ofbuffer.- EndOfStreamException
If NBT stream extends beyond the given
length.- InvalidDataException
If file compression could not be detected or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
LoadFromFile(string)
Loads NBT data from a file. Existing RootTag will be replaced. Compression will be auto-detected.
public long LoadFromFile(string fileName)
Parameters
fileNamestringName of the file from which data will be loaded.
Returns
- long
Number of bytes read from the file.
Exceptions
- ArgumentNullException
fileNameisnull.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
LoadFromFile(string, NbtCompression, TagSelector?)
Loads NBT data from a file. Existing RootTag will be replaced.
public long LoadFromFile(string fileName, NbtCompression compression, TagSelector? selector)
Parameters
fileNamestringName of the file from which data will be loaded.
compressionNbtCompressionCompression method to use for loading/saving this file.
selectorTagSelectorOptional callback to select which tags to load into memory. Root may not be skipped. No reference is stored to this callback after loading (don't worry about implicitly captured closures). May be
null.
Returns
- long
Number of bytes read from the file.
Exceptions
- ArgumentNullException
fileNameisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
LoadFromStream(Stream, NbtCompression)
Loads NBT data from a stream. Existing RootTag will be replaced
public long LoadFromStream(Stream stream, NbtCompression compression)
Parameters
streamStreamStream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking.
compressionNbtCompressionCompression method to use for loading/saving this file.
Returns
- long
Number of bytes read from the stream.
Exceptions
- ArgumentNullException
streamisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NotSupportedException
If
compressionis set to AutoDetect, but the stream is not seekable.- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
LoadFromStream(Stream, NbtCompression, TagSelector?)
Loads NBT data from a stream. Existing RootTag will be replaced
public long LoadFromStream(Stream stream, NbtCompression compression, TagSelector? selector)
Parameters
streamStreamStream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking.
compressionNbtCompressionCompression method to use for loading/saving this file.
selectorTagSelectorOptional callback to select which tags to load into memory. Root may not be skipped. No reference is stored to this callback after loading (don't worry about implicitly captured closures). May be
null.
Returns
- long
Number of bytes read from the stream.
Remarks
Compressed loads verify the container checksum on every kind of stream. On .NET 6 and later, a document cut off inside its trailer may still load without error. Seekable streams are left at their end, so the returned byte count is deterministic; non-seekable streams stay wherever decompression stopped, which can be past the document, since decompressors read ahead. Concatenated GZip members decompress as one document only on .NET Core and later and only from a seekable stream; otherwise the load reads the first member. Uncompressed loads stop exactly at the end of the document, leaving any trailing bytes in place.
Exceptions
- ArgumentNullException
streamisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NotSupportedException
If
compressionis set to AutoDetect, but the stream is not seekable.- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
ReadRootTagName(Stream, NbtCompression, bool, int)
Reads the root name from the given stream of NBT data.
[Obsolete("Use ReadRootTagName(Stream, NbtCompression, NbtFlavor) instead. true corresponds to NbtFlavor.Java, false to NbtFlavor.Bedrock.")]
public static string ReadRootTagName(Stream stream, NbtCompression compression, bool bigEndian, int bufferSize)
Parameters
streamStreamStream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking.
compressionNbtCompressionCompression method to use for loading this stream.
bigEndianboolWhether the stream uses big-endian (default) or little-endian encoding.
bufferSizeintNo longer used.
Returns
- string
Name of the root tag in the given stream.
Exceptions
- ArgumentNullException
streamisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NotSupportedException
If compression is set to AutoDetect, but the stream is not seekable.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
ReadRootTagName(Stream, NbtCompression, NbtFlavor)
Reads the root name from the given stream of NBT data. Root names longer than 65,535 bytes fail with NbtFormatException.
public static string ReadRootTagName(Stream stream, NbtCompression compression, NbtFlavor flavor)
Parameters
streamStreamStream from which data will be loaded. If compression is set to AutoDetect, this stream must support seeking.
compressionNbtCompressionCompression method to use for loading this stream.
flavorNbtFlavorEncoding to read with.
Returns
- string
Name of the root tag in the given stream.
Exceptions
- ArgumentNullException
streamorflavorisnull.- ArgumentException
The flavor has no root name; use NbtCodec for those.
- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NotSupportedException
If compression is set to AutoDetect, but the stream is not seekable.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
ReadRootTagName(string)
Reads the root name from the given NBT file. Automatically detects compression, and reads with DefaultFlavor.
public static string ReadRootTagName(string fileName)
Parameters
fileNamestringName of the file from which first tag will be read.
Returns
- string
Name of the root tag in the given NBT file.
Exceptions
- ArgumentNullException
fileNameisnull.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
ReadRootTagName(string, NbtCompression, bool, int)
Reads the root name from the given NBT file.
[Obsolete("Use ReadRootTagName(string, NbtCompression, NbtFlavor) instead. true corresponds to NbtFlavor.Java, false to NbtFlavor.Bedrock.")]
public static string ReadRootTagName(string fileName, NbtCompression compression, bool bigEndian, int bufferSize)
Parameters
fileNamestringName of the file from which data will be loaded.
compressionNbtCompressionFormat in which the given file is compressed.
bigEndianboolWhether the file uses big-endian (default) or little-endian encoding.
bufferSizeintNo longer used.
Returns
- string
Name of the root tag in the given NBT file.
Exceptions
- ArgumentNullException
fileNameisnull.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
ReadRootTagName(string, NbtCompression, NbtFlavor)
Reads the root name from the given NBT file. Root names longer than 65,535 bytes fail with NbtFormatException.
public static string ReadRootTagName(string fileName, NbtCompression compression, NbtFlavor flavor)
Parameters
fileNamestringName of the file from which data will be loaded.
compressionNbtCompressionFormat in which the given file is compressed.
flavorNbtFlavorEncoding to read with.
Returns
- string
Name of the root tag in the given NBT file.
Exceptions
- ArgumentNullException
fileNameorflavorisnull.- ArgumentException
The flavor has no root name; use NbtCodec for those.
- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- FileNotFoundException
If given file was not found.
- EndOfStreamException
If file ended earlier than expected.
- InvalidDataException
If file compression could not be detected, or decompressing failed.
- NbtFormatException
If an error occurred while parsing data in NBT format.
- IOException
If an I/O error occurred while reading the file.
SaveToBuffer(byte[], int, NbtCompression)
Saves this NBT file to a buffer.
public long SaveToBuffer(byte[] buffer, int index, NbtCompression compression)
Parameters
bufferbyte[]Buffer to write data to. May not be
null.indexintThe index into
bufferat which the stream should begin.compressionNbtCompressionCompression mode to use for saving. May not be AutoDetect.
Returns
- long
Number of bytes written to the buffer.
Exceptions
- ArgumentNullException
bufferisnull.- ArgumentException
If AutoDetect was given as the
compressionmode.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression; ifindexis less than zero; or ifindexis greater than the length ofbuffer.- NbtFormatException
If one of the NbtCompound tags contained unnamed tags; or if an NbtList tag had Unknown list type and no elements; or if a string is longer than the flavor's limit (65,535 bytes for the Java flavors); or if enabled validation rejects a tag type or string length for the flavor; or if tags are nested more than 512 levels deep.
SaveToBuffer(NbtCompression)
Saves this NBT file to a new byte array.
public byte[] SaveToBuffer(NbtCompression compression)
Parameters
compressionNbtCompressionCompression mode to use for saving. May not be AutoDetect.
Returns
- byte[]
Byte array containing the serialized NBT data.
Exceptions
- ArgumentException
If AutoDetect was given as the
compressionmode.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NotSupportedException
If the serialized document does not fit in a single array.
- NbtFormatException
If one of the NbtCompound tags contained unnamed tags; or if an NbtList tag had Unknown list type and no elements; or if a string is longer than the flavor's limit (65,535 bytes for the Java flavors); or if enabled validation rejects a tag type or string length for the flavor; or if tags are nested more than 512 levels deep.
SaveToFile(string, NbtCompression)
Saves this NBT file to a file.
public long SaveToFile(string fileName, NbtCompression compression)
Parameters
fileNamestringFile to write data to. May not be
null.compressionNbtCompressionCompression mode to use for saving. May not be AutoDetect.
Returns
- long
Number of bytes written to the file.
Remarks
The file is created or truncated up front, so a failed save can leave it
partially written. If you are overwriting an existing file, write to a temp file first
then use File.Replace to swap it with the original.
Exceptions
- ArgumentNullException
fileNameisnull.- ArgumentException
If AutoDetect was given as the
compressionmode.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- IOException
If an I/O error occurred while creating the file.
- UnauthorizedAccessException
Specified file is read-only, or a permission issue occurred.
- NbtFormatException
If one of the NbtCompound tags contained unnamed tags; or if an NbtList tag had Unknown list type and no elements; or if a string is longer than the flavor's limit (65,535 bytes for the Java flavors); or if enabled validation rejects a tag type or string length for the flavor; or if tags are nested more than 512 levels deep.
SaveToStream(Stream, NbtCompression)
Saves this NBT file to a stream.
public long SaveToStream(Stream stream, NbtCompression compression)
Parameters
streamStreamStream to write data to. May not be
null.compressionNbtCompressionCompression mode to use for saving. May not be AutoDetect.
Returns
- long
Number of bytes written to the stream.
Exceptions
- ArgumentNullException
streamisnull.- ArgumentException
If AutoDetect was given as the
compressionmode; or ifstreamdoes not support writing.- ArgumentOutOfRangeException
If an unrecognized/unsupported value was given for
compression.- NbtFormatException
If RootTag is unnamed; or if one of the NbtCompound tags contained unnamed tags; or if an NbtList tag had Unknown list type and no elements; or if a string is longer than the flavor's limit (65,535 bytes for the Java flavors); or if enabled validation rejects a tag type or string length for the flavor; or if tags are nested more than 512 levels deep.
ToString()
Prints contents of the root tag, and any child tags, to a string.
public override string ToString()
Returns
ToString(string)
Prints contents of the root tag, and any child tags, to a string. Indents the string using multiples of the given indentation string.
public string ToString(string indentString)
Parameters
indentStringstringString to be used for indentation.
Returns
- string
A string representing contents of this tag, and all child tags (if any).
Exceptions
- ArgumentNullException
indentStringisnull.