Documentation ¶
Index ¶
- Variables
- func Marshal(v interface{}) ([]byte, error)
- func MarshalVariant(v interface{}, variant Variant) ([]byte, error)
- func Unmarshal(data []byte, v interface{}) error
- func UnmarshalVariant(data []byte, v interface{}, variant Variant) error
- type BufferOverrunError
- type Decoder
- type Encoder
- type FailedWriteError
- type IncompatibleTypeError
- type InvalidArraySizeError
- type InvalidStringError
- type InvalidTypeError
- type MaximumBytesReadError
- type MaximumDepthReachedError
- type NonPointerTypeError
- type UnexpectedNamedTagError
- type UnexpectedTagError
- type UnknownTagError
- type Variant
Constants ¶
This section is empty.
Variables ¶
var BigEndian bigEndian
BigEndian is the fixed size big endian implementation of NBT. It is the original implementation, and is used only on Minecraft Java Edition.
var LittleEndian littleEndian
LittleEndian is the fixed size little endian implementation of NBT. It is the format typically used for writing Minecraft (Bedrock Edition) world saves.
var NetworkLittleEndian networkLittleEndian
NetworkLittleEndian is the variable sized integer implementation of NBT. It is otherwise the same as the normal little endian NBT. The NetworkLittleEndian format limits the total bytes of NBT that may be read. If the limit is hit, the reading operation will fail immediately.
Functions ¶
func Marshal ¶
Marshal encodes an object to its NBT representation and returns it as a byte slice. It uses the NetworkLittleEndian NBT format. To use a specific format, use MarshalVariant.
The Go value passed must be one of the values below, and structs, maps and slices may only have types that are found in the list below.
The following Go types are converted to tags as such:
byte/uint8: TAG_Byte int16: TAG_Short int32: TAG_Int int64: TAG_Long float32: TAG_Float float64: TAG_Double [...]byte: TAG_ByteArray [...]int32: TAG_IntArray [...]int64: TAG_LongArray string: TAG_String []<type>: TAG_List struct{...}: TAG_Compound map[string]<type/interface{}>: TAG_Compound
Marshal accepts struct fields with the 'nbt' struct tag. The 'nbt' struct tag allows setting the name of a field that some tag should be decoded in. Setting the struct tag to '-' means that field will never be filled by the decoding of the data passed. Suffixing the 'nbt' struct tag with ',omitempty' will prevent the field from being encoded if it is equal to its default value.
func MarshalVariant ¶ added in v0.3.0
MarshalVariant encodes an object to its NBT representation and returns it as a byte slice. Its functionality is identical to that of Marshal, except it accepts any NBT variant.
func Unmarshal ¶
Unmarshal decodes a slice of NBT data into a pointer to a Go values passed. Marshal will use the NetworkLittleEndian NBT format by default. To use a specific format, use UnmarshalVariant.
The Go value passed must be a pointer to a value. Anything else will return an error before decoding. The following NBT tags are decoded in the Go value passed as such:
TAG_Byte: byte/uint8(/interface{}) TAG_Short: int16(/interface{}) TAG_Int: int32(/interface{}) TAG_Long: int64(/interface{}) TAG_Float: float32(/interface{}) TAG_Double: float64(/interface{}) TAG_ByteArray: [...]byte(/interface{}) (The value must be a byte array, not a slice) TAG_String: string(/interface{}) TAG_List: []interface{}(/interface{}) (The value type of the slice may vary. Depending on the type of values in the List tag, it might be of the type of any of the other tags, such as []int64.
TAG_Compound: struct{...}/map[string]interface{}(/interface{}) TAG_IntArray: [...]int32(/interface{}) (The value must be an int32 array, not a slice) TAG_LongArray: [...]int64(/interface{}) (The value must be an int64 array, not a slice)
Unmarshal returns an error if the data is decoded into a struct and the struct does not have all fields that the matching TAG_Compound in the NBT has, in order to prevent the loss of data. For varying data, the data should be decoded into a map. Nil maps and slices are initialised and filled out automatically by Unmarshal.
Unmarshal accepts struct fields with the 'nbt' struct tag. The 'nbt' struct tag allows setting the name of a field that some tag should be decoded in. Setting the struct tag to '-' means that field will never be filled by the decoding of the data passed.
func UnmarshalVariant ¶ added in v0.3.0
UnmarshalVariant decodes a slice of NBT data into a pointer to a Go values passed using the NBT variant passed. Its functionality is identical to that of Unmarshal.
Types ¶
type BufferOverrunError ¶
type BufferOverrunError struct {
Op string
}
BufferOverrunError is returned when the data buffer passed in when reading is overrun, meaning one of the reading operations extended beyond the end of the slice.
type Decoder ¶
type Decoder struct { // Variant is the variant to use for decoding the NBT passed. By default, the variant is set to // NetworkLittleEndian, which is the variant used for network NBT. Variant Variant // contains filtered or unexported fields }
Decoder reads NBT objects from an NBT input stream.
func NewDecoder ¶
NewDecoder returns a new Decoder for the input stream reader passed.
func NewDecoderVariant ¶ added in v1.3.0
NewDecoderVariant returns a new Decoder for the input stream reader passed with a specific variant.
type Encoder ¶
type Encoder struct { // Variant is the variant to use for encoding the objects passed. By default, the variant is set to // NetworkLittleEndian, which is the variant used for network NBT. Variant Variant // contains filtered or unexported fields }
Encoder writes NBT objects to an NBT output stream.
func NewEncoder ¶
NewEncoder returns a new encoder for the output stream writer passed.
func NewEncoderVariant ¶ added in v1.3.0
NewEncoderVariant returns a new encoder for the output stream writer passed using a specific variant.
type FailedWriteError ¶
FailedWriteError is returned if a Write operation failed on an offsetWriter, meaning some of the data could not be written to the io.Writer.
type IncompatibleTypeError ¶
IncompatibleTypeError is returned if a value is attempted to be written to an io.Writer, but its type can- not be translated to an NBT tag.
type InvalidArraySizeError ¶
InvalidArraySizeError is returned when an array read from the NBT (that includes byte arrays, int32 arrays and int64 arrays) does not have the same size as the Go representation.
type InvalidStringError ¶
InvalidStringError is returned if a string read is not valid, meaning it does not exist exclusively out of utf8 characters, or if it is longer than the length prefix can carry.
type InvalidTypeError ¶
InvalidTypeError is returned when the type of a tag read is not equal to the struct field with the name of that tag.
type MaximumBytesReadError ¶
type MaximumBytesReadError struct { }
MaximumBytesReadError is returned if the maximum amount of bytes has been read for NetworkLittleEndian format. It is returned if the offset hits maximumNetworkOffset.
type MaximumDepthReachedError ¶
type MaximumDepthReachedError struct { }
MaximumDepthReachedError is returned if the maximum depth of 512 compound/list tags has been reached while reading or writing NBT.
func (MaximumDepthReachedError) Error ¶
func (err MaximumDepthReachedError) Error() string
Error ...
type NonPointerTypeError ¶
NonPointerTypeError is returned when the type of a value passed in Decoder.Decode or Unmarshal is not a pointer.
type UnexpectedNamedTagError ¶
UnexpectedNamedTagError is returned when a named tag is read from a compound which is not present in the struct it is decoded into.
type UnexpectedTagError ¶
UnexpectedTagError is returned when a tag type encountered was not expected, and thus valid in its context.
type UnknownTagError ¶
UnknownTagError is returned when the type of a tag read is not known, meaning it is not found in the tag.go file.
type Variant ¶ added in v0.3.0
type Variant interface { Int16(r *offsetReader) (int16, error) Int32(r *offsetReader) (int32, error) Int64(r *offsetReader) (int64, error) Float32(r *offsetReader) (float32, error) Float64(r *offsetReader) (float64, error) String(r *offsetReader) (string, error) WriteInt16(w *offsetWriter, x int16) error WriteInt32(w *offsetWriter, x int32) error WriteInt64(w *offsetWriter, x int64) error WriteFloat32(w *offsetWriter, x float32) error WriteFloat64(w *offsetWriter, x float64) error WriteString(w *offsetWriter, x string) error }
Variant is a specific variant of NBT. In general, there are three variants of NBT, which are all the same except for the way basic types are written.