nbt

package
v0.0.0-...-93368d4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 10, 2015 License: MIT Imports: 6 Imported by: 7

Documentation

Overview

Package nbt implements NBT read/write file support.

Its based heavily onthe io.ReaderFrom and io.WriteTo interfaces, so you can just use io.Copy to read and write NBT streams.

An NBT data structure can be created with code such as the following:

root := &Compound{
  map[string]Tag{
    "Data": &Compound{
      map[string]Tag{
        "Byte":   &Byte{1},
        "Short":  &Short{2},
        "Int":    &Int{3},
        "Long":   &Long{4},
        "Float":  &Float{5},
        "Double": &Double{6},
        "String": &String{"foo"},
        "List":   &List{TagByte, []Tag{&Byte{1}, &Byte{2}}},
      },
    },
  },
}

It is required that the root structure be a Compound for compatibility with existing NBT structures observed in the official server.

Many thanks to #mcdevs from Freenode and it's great documentation: http://wiki.vg/NBT

Index

Constants

View Source
const ArrayNum = 8

Sets the maximum number of elements to show in string representations of types: NBT_ByteArray and NBT_IntArray.

Variables

View Source
var (
	ErrEndTop     = errors.New("End tag found at top level.")
	ErrInvalidTop = errors.New("Expected compound at top level.")
)

Functions

func Pretty

func Pretty(s string) string

func Write

func Write(w io.Writer, c *Compound) (err error)

Write writes an NBT compound to a byte stream. It doesn't handle compression.

Types

type ByteArray

type ByteArray struct {
	Value []types.Int8
}

ByteArray holds a length-prefixed array of signed bytes. The prefix is a signed integer (4 bytes). TagType: 7, Size: 4 + elem * 1 bytes

func NewByteArray

func NewByteArray(s []int8) *ByteArray

func (ByteArray) Lookup

func (arr ByteArray) Lookup(path string) Tag

func (*ByteArray) ReadFrom

func (arr *ByteArray) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (ByteArray) Size

func (arr ByteArray) Size() int64

func (ByteArray) String

func (arr ByteArray) String() string

func (ByteArray) Type

func (arr ByteArray) Type() TagType

func (*ByteArray) WriteTo

func (arr *ByteArray) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface. TypeId is not encoded.

type Compound

type Compound struct {
	Name  string
	Value map[string]Tag
}

Compounds hold a list of a named tags. Order is not guaranteed. TagType: 10, Size: 1 + 4 + elem * id_size bytes

func NewCompound

func NewCompound(name string) *Compound

func Read

func Read(r io.Reader) (c *Compound, err error)

Read reads an NBT compound from a byte stream. It doesn't handle compression.

func (Compound) Lookup

func (c Compound) Lookup(path string) Tag

func (*Compound) ReadFrom

func (c *Compound) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (Compound) Size

func (c Compound) Size() (n int64)

func (*Compound) String

func (c *Compound) String() string

func (Compound) Type

func (c Compound) Type() TagType

func (*Compound) WriteTo

func (c *Compound) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface. TypeId is not encoded.

type End

type End struct{}

End marks end of a Compound. TagType: 0, Size: 1 byte

func (End) Lookup

func (e End) Lookup(path string) Tag

func (End) Name

func (e End) Name() string

func (*End) ReadFrom

func (e *End) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (End) Size

func (e End) Size() int64

func (End) String

func (e End) String() string

func (End) Type

func (e End) Type() TagType

func (*End) WriteTo

func (e *End) WriteTo(w io.Writer) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

type Float32

type Float32 struct {
	types.Float32
}

Float holds a single IEEE-754 single-precision floating point number. TagType: 5, Size: 4 bytes

func (Float32) Lookup

func (f Float32) Lookup(path string) Tag

func (Float32) Size

func (f Float32) Size() int64

func (Float32) String

func (f Float32) String() string

func (Float32) Type

func (f Float32) Type() TagType

type Float64

type Float64 struct {
	types.Float64
}

Double holds a single IEEE-754 double-precision floating point number. TagType: 6, Size: 8 bytes

func (Float64) Lookup

func (d Float64) Lookup(path string) Tag

func (Float64) Size

func (d Float64) Size() int64

func (Float64) String

func (d Float64) String() string

func (Float64) Type

func (d Float64) Type() TagType

type Int16

type Int16 struct {
	types.Int16
}

Short holds a single signed short. TagType: 2, Size: 2 bytes

func (Int16) Lookup

func (s Int16) Lookup(path string) Tag

func (Int16) Size

func (s Int16) Size() int64

func (Int16) String

func (s Int16) String() string

func (Int16) Type

func (s Int16) Type() TagType

type Int32

type Int32 struct {
	types.Int32
}

Int holds a single signed integer. TagType: 3, Size: 4 bytes

func (Int32) Lookup

func (i Int32) Lookup(path string) Tag

func (Int32) Size

func (i Int32) Size() int64

func (Int32) String

func (i Int32) String() string

func (Int32) Type

func (i Int32) Type() TagType

type Int64

type Int64 struct {
	types.Int64
}

Long holds a single signed long. TagType: 4, Size: 8 bytes

func (Int64) Lookup

func (l Int64) Lookup(path string) Tag

func (Int64) Size

func (l Int64) Size() int64

func (Int64) String

func (l Int64) String() string

func (Int64) Type

func (l Int64) Type() TagType

type Int8

type Int8 struct {
	types.Int8
}

Byte holds a single signed byte. TagType: 1, Size: 1 byte

func (Int8) Lookup

func (b Int8) Lookup(path string) Tag

func (Int8) Size

func (b Int8) Size() int64

func (Int8) String

func (b Int8) String() string

func (Int8) Type

func (b Int8) Type() TagType

type IntArray

type IntArray struct {
	Value []types.Int32
}

IntArray holds a length-prefixed array of signed integers. The prefix is a signed integer (4 bytes) and indicates the number of 4 byte integers. TagType: 11, Size: 4 + 4 * elem

func NewIntArray

func NewIntArray(s []int32) *IntArray

func (IntArray) Lookup

func (arr IntArray) Lookup(path string) Tag

func (*IntArray) ReadFrom

func (arr *IntArray) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (IntArray) Size

func (arr IntArray) Size() int64

func (IntArray) String

func (arr IntArray) String() string

func (IntArray) Type

func (arr IntArray) Type() TagType

func (*IntArray) WriteTo

func (arr *IntArray) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface. TypeId is not encoded.

type List

type List struct {
	Typ   TagType
	Value []Tag
}

List holds a list of nameless tags, all of the same type. The list is prefixed with the Type ID of the items it contains (1 byte), and the length of the list as a signed integer (4 bytes). TagType: 9, Size: 1 + 4 + elem * id_size bytes

func (List) Lookup

func (l List) Lookup(path string) Tag

func (*List) ReadFrom

func (l *List) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (List) Size

func (l List) Size() (n int64)

func (List) String

func (l List) String() string

func (List) Type

func (l List) Type() TagType

func (*List) WriteTo

func (l *List) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface. TypeId is not encoded.

type String

type String struct {
	Value string
}

String holds a length-prefixed UTF-8 string. The prefix is an unsigned short (2 bytes). TagType: 8, Size: 1 + (2 + elem) + (2 + elem)

func (String) Lookup

func (s String) Lookup(path string) Tag

func (*String) ReadFrom

func (s *String) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface. TypeId is not decoded.

func (String) Size

func (s String) Size() int64

func (String) String

func (s String) String() string

func (String) Type

func (s String) Type() TagType

func (*String) WriteTo

func (s *String) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface. TypeId is not encoded.

type Tag

type Tag interface {
	io.ReaderFrom
	io.WriterTo
	Type() TagType
	Size() int64
	Lookup(path string) Tag
}

Tag is the interface for all tags that can be represented in an NBT tree.

type TagType

type TagType int8

TagType is the header byte value that identifies the type of tag(s). List & Compound types send TagType over the wire as a signed byte, using a int8 as underlying type allows us to assign TagType to Byte.

const (
	// Tag types. All these can be used to create a new tag.
	TagEnd       TagType = iota // Size: 0
	TagByte                     // Size: 1
	TagShort                    // Size: 2
	TagInt                      // Size: 4
	TagLong                     // Size: 8
	TagFloat                    // Size: 4
	TagDouble                   // Size: 8
	TagByteArray                // Size: 4 + 1*elem
	TagString                   // Size: 2 + 4*elem
	TagList                     // Size: 1 + 4 + elem*len
	TagCompound                 // Size: varies
	TagIntArray                 // Size: 4 + 4*elem
)

func (TagType) New

func (tt TagType) New() (t Tag)

func (*TagType) ReadFrom

func (tt *TagType) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom satifies io.ReaderFrom interface.

func (TagType) String

func (tt TagType) String() string

func (TagType) WriteTo

func (tt TagType) WriteTo(w io.Writer) (n int64, err error)

WriteTo satifies io.WriterTo interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL