nbt2json

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2021 License: MIT Imports: 9 Imported by: 2

README

nbt2json

A command line utility and Go module that reads NBT data and converts it to JSON or YAML for editing and then back to NBT.

Features

  • nbt2json executable will auto-detect and decompress gzipped files
  • nbt2json executable has option to gzip output
  • Can read and write both Minecraft Bedrock Edition and Java Edition NBT data
    • Does not auto-detect which
    • nbt2json executable defaults to Bedrock Edition / little endian
  • Can import to other Go projects
  • Can use either JSON or YAML
  • Can include comment in JSON/YAML output (which is ignored when converting back to NBT)

Help screen

By defualt, the nbt2json executable waits for input from stdin, so you need to nbt2json -h to see the help screen.

NAME:
   NBT to JSON - Converts NBT-encoded data to JSON | https://github.com/midnightfreddie/nbt2json

USAGE:
   nbt2json.exe [global options] command [command options] [arguments...]

VERSION:
   0.4.0

AUTHOR:
   Jim Nelson <jim@jimnelson.us>

COMMANDS:
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --reverse, -r                  Convert JSON to NBT instead (default: false)
   --gzip, -z                     Compress output with gzip (default: false)
   --comment COMMENT, -c COMMENT  Add COMMENT to json or yaml output, use quotes if contains white space
   --big-endian, --java, -b       Use for Minecraft Java Edition (like most other NBT tools) (default: false)
   --in FILE, -i FILE             Input FILE path (default: "-")
   --out FILE, -o FILE            Output FILE path (default: "-")
   --yaml, --yml, -y              Use YAML instead of JSON (default: false)
   --long-as-string, -l           If set, nbt long values will be a string instead of uint32 pair (default: false)
   --skip NUM                     Skip NUM bytes of NBT input. For Bedrock's level.dat, use --skip 8 to bypass header (default: 0)
   --help, -h                     show help (default: false)
   --version, -v                  print the version (default: false)

COPYRIGHT:
   (c) 2018, 2019, 2020 Jim Nelson

JSON format notes

The JSON document should be an object with an "nbt" field which is an array. Objects in the array represent one tag and will typically have tagType, name, and value fields. A typical Minecraft NBT will have one compound tag with a number of other tags inside that one. This converter needs at least one tag.

Many JSON libraries cannot properly handle a 64-bit integer, so nbt2json handles the long tag in one of two special ways for portability and compatibility.

By default it presents valueLeast and valueMost unsigned 32-bit integers. valueMost is the most significant 32 bits, so it should be rolled bitwise to the left 32 bits in a 64-bit space and added (or bitwise 'or'ed) to valueLeast to reassemble the 64-bit long singed integer. Here is an example:

{
  "nbt": [
    {
      "tagType": 4,
      "name": "LongAsUint32PairExample",
      "value": {
        "valueLeast": 4294967295,
        "valueMost": 2147483647
      }
    }
  ]
}

Alternately it can turn the 64-bit integer into a string. This would be easier to edit by hand in a text editor. Example:

{
  "nbt": [
    {
      "tagType": 4,
      "name": "LongAsStringExample",
      "value": "9223372036854775807"
    }
  ]
}

Dev notes

  • Client Go code needs to import "github.com/midnightfreddie/nbt2json"
  • Defaults to little-endian encoding for Bedrock Edition. Call nbt2json.UseJavaEncoding() and nbt2json.UseBedrockEncoding() to change encoding mode for as long as the module is open.
  • The functions use byte arrays where you might expect strings. Convert as such: var myString = someByteArray[:] or var myByteArray = []byte(someStringValue)
  • All errors should bubble up through the error part of the result and should describe where the problem was
  • The Json2Nbt function uses an interface{} and encodes based on the tagType fields. I had originally hoped to Marshal and Unmarshal to and from JSON and NBT, but my goal was to export to JSON, edit and then reencode. This way the struct doesn't have to match the data schema.
  • My main motivation for this project is to convert to/from JSON and use any JSON editor to modify Minecraft PE data with McpeTool, and to keep the read/write primitives in Go code while letting a client browser manage any validation to avoid having to re-release the read/write tools every time Minecraft changes formats.
Exported Go functions
  • Nbt2Yaml converts uncompressed NBT byte array to YAML byte array

      func Nbt2Yaml(b []byte, comment string) ([]byte, error)
    
  • Nbt2Json converts uncompressed NBT byte array to JSON byte array

      func Nbt2Json(b []byte, comment string) ([]byte, error)
    
  • Yaml2Nbt converts JSON byte array to uncompressed NBT byte array (Hint: You can just use this for both JSON and YAML if you like since JSON is a valid subeset of YAML)

      func Yaml2Nbt(b []byte) ([]byte, error)
    
  • Json2Nbt converts JSON byte array to uncompressed NBT byte array

      func Json2Nbt(b []byte) ([]byte, error)
    
  • UseJavaEndoding sets any nbt encoding/decoding to big-endian to match Minecraft Java Edition

      func UseJavaEncoding()
    
  • UseBedrockEncoding sets nbt encoding/decoding to little-endian to match Minecraft Bedrock Edition (default)

      func UseBedrockEncoding()
    
  • UseLongAsString sets json output for nbt long tags to numbers in strings

      func UseLongAsString()
    
  • UseLongAsUint32Pair sets json output for nbt long tags to a uint32 pair (default, needed for json portability)

      func UseLongAsUint32Pair()
    

Other exports of possible interest are in common.go.

Documentation

Index

Constants

View Source
const Nbt2JsonUrl = "https://github.com/midnightfreddie/nbt2json"

Nbt2JsonUrl is inserted in the json document as nbt2JsonUrl

View Source
const Version = "0.4.0"

Version is the json document's nbt2JsonVersion:

Variables

View Source
var Name = "Named Binary Tag to JSON"

Name is the json document's name:

Functions

func Json2Nbt

func Json2Nbt(b []byte) ([]byte, error)

Json2Nbt converts JSON byte array to uncompressed NBT byte array

func Nbt2Json

func Nbt2Json(b []byte, comment string) ([]byte, error)

Nbt2Json converts uncompressed NBT byte array to JSON byte array

func Nbt2Yaml

func Nbt2Yaml(b []byte, comment string) ([]byte, error)

Nbt2Yaml converts uncompressed NBT byte array to YAML byte array

func ReadNbt2Json

func ReadNbt2Json(r *bytes.Reader, comment string, tagCount int) ([]byte, error)

ReadNbt2Json reads the given count of top level NBT tags from r and converts it to JSON byte array

func ReadNbt2Yaml

func ReadNbt2Yaml(r *bytes.Reader, comment string, tagCount int) ([]byte, error)

ReadNbt2Yaml reads the given count of top level NBT tags from r and converts it to YAML byte array

func UseBedrockEncoding

func UseBedrockEncoding()

UseBedrockEncoding sets the module to decode/encode from/to little endian NBT for Minecraft Bedrock Edition

func UseJavaEncoding

func UseJavaEncoding()

UseJavaEncoding sets the module to decode/encode from/to big endian NBT for Minecraft Java Edition

func UseLongAsString

func UseLongAsString()

UseLongAsString will make nbt long values as string numbers in the json/yaml

func UseLongAsUint32Pair

func UseLongAsUint32Pair()

UseLongAsUint32Pair will make nbt long values as valueLeast/valueMost uint32 pairs in the json

func Yaml2Nbt

func Yaml2Nbt(b []byte) ([]byte, error)

Yaml2Nbt converts JSON byte array to uncompressed NBT byte array

Types

type JsonParseError

type JsonParseError struct {
	// contains filtered or unexported fields
}

JsonParseError is when the json data does not match an expected pattern. Pass it message string and downstream error

func (JsonParseError) Error

func (e JsonParseError) Error() string

type NbtJson

type NbtJson struct {
	Name           string             `json:"name"`
	Version        string             `json:"version"`
	Nbt2JsonUrl    string             `json:"nbt2JsonUrl"`
	ConversionTime string             `json:"conversionTime,omitempty"`
	Comment        string             `json:"comment,omitempty"`
	Nbt            []*json.RawMessage `json:"nbt"`
}

NbtJson is the top-level JSON document; it is exported for reflect, and client code shouldn't use it

type NbtLong

type NbtLong struct {
	ValueLeast uint32 `json:"valueLeast"`
	ValueMost  uint32 `json:"valueMost"`
}

NbtLong stores a 64-bit int into two 32-bit values for json portability. ValueMost are the high 32 bits and ValueLeast are the low 32 bits.

using uint32s to avoid Go trying to outsmart us on "negative" int32s

type NbtParseError

type NbtParseError struct {
	// contains filtered or unexported fields
}

NbtParseError is when the nbt data does not match an expected pattern. Pass it message string and downstream error

func (NbtParseError) Error

func (e NbtParseError) Error() string

type NbtTag

type NbtTag struct {
	TagType byte        `json:"tagType"`
	Name    string      `json:"name"`
	Value   interface{} `json:"value,omitempty"`
}

NbtTag represents one NBT tag for each struct; it is exported for reflect, and client code shouldn't use it

type NbtTagList

type NbtTagList struct {
	TagListType byte          `json:"tagListType"`
	List        []interface{} `json:"list"`
}

NbtTagList represents an NBT tag list; it is exported for reflect, and client code shouldn't use it

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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