gonbt

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2021 License: MIT Imports: 9 Imported by: 1

README

GONBT

Go because this project is written in Golang and NBT because it is a nbt parser  ✌ 

Build status Codecov

GoDoc Sourcegraph GoReportCard Discord License

Description

The Named Binary Tag (NBT) format is used by Minecraft for the various files in which it saves data... the next on wiki  😎 

This gonbt package marshal and unmarshal nbt data in a readable format see tag.go

Installation

Easy to install like all go packages  😁 

<$ go get -u github.com/ymohl-cl/gonbt

Usage

// To read
func main() {
    var dataIn []byte // your nbt data
    var err error

    if tag, err = gonbt.Unmarshal(dataIn); err != nil {
      panic(err)
    }
}
// To write
func main() {
    var tag gonbt.Tag // your data to convert in nbt format
    var err error

    if tag, err = gonbt.Marshal(tag, gonbt.CompressNone); err != nil {
      panic(err)
    }
}

Roadmap

The next should be convert Tag to golang struct with specific field's tag like json or other parser stuf

Contributing

 ❕  Use issues for everything

  • For a small change, just send a PR.
  • For bigger changes open an issue for discussion before sending a PR.
  • PR should have:
    • Test case
    • Documentation
    • Example (If it makes sense)
  • You can also contribute by:
    • Reporting issues
    • Suggesting new features or enhancements
    • Improve/fix documentation

Thank you  🙏  👍 

Licence

MIT

Documentation

Overview

Example
package main

import (
	"fmt"
	"io/ioutil"
	"os"

	"github.com/ymohl-cl/gonbt"
)

const (
	exampleFilesPath = "./example"
)

func main() {
	var err error
	var files []os.FileInfo

	if files, err = ioutil.ReadDir(exampleFilesPath); err != nil {
		panic(err)
	}

	for _, file := range files {
		if file.IsDir() {
			continue
		}
		var dataIn []byte
		var tag gonbt.Tag

		if dataIn, err = ioutil.ReadFile(exampleFilesPath + "/" + file.Name()); err != nil {
			panic(err)
		}

		if tag, err = gonbt.Unmarshal(dataIn); err != nil {
			panic(err)
		}
		if _, err = gonbt.Marshal(tag, gonbt.CompressGZIP); err != nil {
			panic(err)
		}
		if _, err = gonbt.Marshal(tag, gonbt.CompressNone); err != nil {
			panic(err)
		}

		fmt.Printf("%s ok\n", file.Name())
	}
}
Output:

servers.dat ok
level.dat ok

Index

Examples

Constants

View Source
const (
	CompressGZIP = "gzip"
	CompressZLIB = "zlib"
	CompressNone = "none"
)

constant compression type

View Source
const (
	TagEnd byte = iota
	TagByte
	TagShort
	TagInt
	TagLong
	TagFloat
	TagDouble
	TagByteArray
	TagString
	TagList
	TagCompound
	TagIntArray
	TagLongArray
)

TagType values

Variables

This section is empty.

Functions

func Marshal

func Marshal(t Tag, compress string) ([]byte, error)

Marshal data

func TagType

func TagType(tag Tag) (byte, error)

TagType return the tag type from the Tag parameter

Types

type ByteArrayT

type ByteArrayT struct {
	Name  string
	Value []byte
}

ByteArrayT to byte array type: 7

func (*ByteArrayT) Read

func (t *ByteArrayT) Read(reader Reader) error

7 TAG_Byte_Array TAG_Int's payload size, then size TAG_Byte's payloads. [B;<byte>,<byte>,...] An array of bytes. Maximum number of elements ranges between (231 - 9) and (231 - 1) (2,147,483,639 and 2,147,483,647), depending on the specific JVM.

func (*ByteArrayT) Write

func (t *ByteArrayT) Write(writer Writer, printInfo bool) error

type ByteT

type ByteT struct {
	Name  string
	Value byte
}

ByteT to byte type: 1

func (*ByteT) Read

func (t *ByteT) Read(reader Reader) error

1 TAG_Byte 1 byte / 8 bits, signed <number>b or <number>B A signed integral type. Sometimes used for booleans. Full range of -(27) to (27 - 1) (-128 to 127)

func (*ByteT) Write

func (t *ByteT) Write(writer Writer, printInfo bool) error

type CompoundT

type CompoundT struct {
	Name  string
	Value map[string]interface{}
}

CompoundT to compound type: 10

func (*CompoundT) Read

func (t *CompoundT) Read(reader Reader) error

10 TAG_Compound Fully formed tags, followed by a TAG_End. {<tag name>:<value>,<tag name>:<value>,...} A list of fully formed tags, including their IDs, names, and payloads. No two tags may have the same name. Unlike lists, there is no hard limit to the number of tags within a Compound (of course, there is always the implicit limit of virtual memory). Note, however, that Compound and List tags may not be nested beyond a depth of 512.

func (*CompoundT) Write

func (t *CompoundT) Write(writer Writer, printInfo bool) error

type DoubleT

type DoubleT struct {
	Name  string
	Value float64
}

DoubleT to double type: 6

func (*DoubleT) Read

func (t *DoubleT) Read(reader Reader) error
6 		TAG_Double 	8 bytes / 64 bits, signed, big endian, IEEE 754-2008, binary64 	<decimal number>, <number>d or <number>D 	A signed floating point type. 	Precision varies throughout number line;

See Double-precision floating-point format. Maximum value about 1.8*10308

func (*DoubleT) Write

func (t *DoubleT) Write(writer Writer, printInfo bool) error

type EndT

type EndT int

EndT to end type: 0

type FloatT

type FloatT struct {
	Name  string
	Value float32
}

FloatT to float type: 5

func (*FloatT) Read

func (t *FloatT) Read(reader Reader) error

5 TAG_Float 4 bytes / 32 bits, signed, big endian, IEEE 754-2008, binary32 <number>f or <number>F A signed floating point type. Precision varies throughout number line; See Single-precision floating-point format. Maximum value about 3.4*1038

func (*FloatT) Write

func (t *FloatT) Write(writer Writer, printInfo bool) error

type IntArrayT

type IntArrayT struct {
	Name  string
	Value []int32
}

IntArrayT to int array type: 11

func (*IntArrayT) Read

func (t *IntArrayT) Read(reader Reader) error

11 TAG_Int_Array TAG_Int's payload size, then size TAG_Int's payloads. [I;<integer>,<integer>,...] An array of TAG_Int's payloads. Maximum number of elements ranges between (231 - 9) and (231 - 1) (2,147,483,639 and 2,147,483,647), depending on the specific JVM.

func (*IntArrayT) Write

func (t *IntArrayT) Write(writer Writer, printInfo bool) error

type IntT

type IntT struct {
	Name  string
	Value int32
}

IntT to int type: 3

func (*IntT) Read

func (t *IntT) Read(reader Reader) error

3 TAG_Int 4 bytes / 32 bits, signed, big endian <number> A signed integral type. Full range of -(231) to (231 - 1) (-2,147,483,648 to 2,147,483,647)

func (*IntT) Write

func (t *IntT) Write(writer Writer, printInfo bool) error

type ListT

type ListT struct {
	Name  string
	Value []interface{}
}

ListT to list type: 9

func (*ListT) Read

func (t *ListT) Read(reader Reader) error

9 TAG_List TAG_Byte's payload tagId, then TAG_Int's payload size, then size tags' payloads, all of type tagId. [<value>,<value>,...] A list of tag payloads, without repeated tag IDs or any tag names. Due to JVM limitations and the implementation of ArrayList, the maximum number of list elements is (231 - 9), or 2,147,483,639. Also note that List and Compound tags may not be nested beyond a depth of 512.

func (*ListT) Write

func (t *ListT) Write(writer Writer, printInfo bool) error

type LongArrayT

type LongArrayT struct {
	Name  string
	Value []int64
}

LongArrayT to long array type: 12

func (*LongArrayT) Read

func (t *LongArrayT) Read(reader Reader) error

12 TAG_Long_Array TAG_

func (*LongArrayT) Write

func (t *LongArrayT) Write(writer Writer, printInfo bool) error

type LongT

type LongT struct {
	Name  string
	Value int64
}

LongT to long type: 4

func (*LongT) Read

func (t *LongT) Read(reader Reader) error

4 TAG_Long 8 bytes / 64 bits, signed, big endian <number>l or <number>L A signed integral type. Full range of -(263) to (263 - 1) (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)

func (*LongT) Write

func (t *LongT) Write(writer Writer, printInfo bool) error

type Reader

type Reader interface {
	String() (string, error)
	Byte() (byte, error)
	Short() (int16, error)
	Int() (int32, error)
	Long() (int64, error)
	Float() (float32, error)
	Double() (float64, error)
	Bytes() ([]byte, error)
	IntArray() ([]int32, error)
	LongArray() ([]int64, error)
}

Reader nbt

func NewReader

func NewReader(r io.Reader) Reader

NewReader nbt

type ShortT

type ShortT struct {
	Name  string
	Value int16
}

ShortT to short type: 2

func (*ShortT) Read

func (t *ShortT) Read(reader Reader) error

2 TAG_Short 2 bytes / 16 bits, signed, big endian <number>s or <number>S A signed integral type. Full range of -(215) to (215 - 1) (-32,768 to 32,767)

func (*ShortT) Write

func (t *ShortT) Write(writer Writer, printInfo bool) error

type StringT

type StringT struct {
	Name  string
	Value string
}

StringT to string type: 8

func (*StringT) Read

func (t *StringT) Read(reader Reader) error

8 TAG_String A TAG_Short-like, but instead unsigned[2] payload length, then a UTF-8 string resembled by length bytes. <a-zA-Z0-9 text>, "<text>" (" within needs to be escaped to \"), or '<text>' (' within needs to be escaped to \') A UTF-8 string. It has a size, rather than being null terminated. 65,535 bytes interpretable as UTF-8 (see modified UTF-8 format; most commonly-used characters are a single byte).

func (*StringT) Write

func (t *StringT) Write(writer Writer, printInfo bool) error

type Tag

type Tag interface {
	Read(reader Reader) error
	Write(writer Writer, printInfo bool) error
}

Tag interface to provide a nbt reader / writer

func NewTag

func NewTag(tagT byte, name string) (Tag, error)

NewTag instance

func Unmarshal

func Unmarshal(data []byte) (Tag, error)

Unmarshal data

type Writer

type Writer interface {
	String(string) error
	Byte(byte) error
	Short(int16) error
	Int(int32) error
	Long(int64) error
	Float(float32) error
	Double(float64) error
	Bytes([]byte) error
	IntArray([]int32) error
	LongArray([]int64) error
}

Writer nbt

func NewWriter

func NewWriter(driver io.Writer) Writer

NewWriter nbt

Jump to

Keyboard shortcuts

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