All block states' properties are parsed and all enums represented as byte

This commit is contained in:
Tnze
2022-03-11 14:02:21 +08:00
parent 442993d3b1
commit a05f8e0a33
17 changed files with 1954 additions and 760 deletions

View File

@ -23,6 +23,9 @@ func Unmarshal(data []byte, v interface{}) error {
// For example, you can decode an NBT value which root tag is TagCompound(0x0a)
// into a struct or map, but not a string.
//
// If v implement Unmarshaler, the method will be called and override the default behavior.
// Else if v implement encoding.TextUnmarshaler, the value will be encoded as TagString.
//
// This method also return tag name of the root tag.
// In real world, it is often empty, but the API should allow you to get it when ever you want.
func (d *Decoder) Decode(v interface{}) (string, error) {

View File

@ -405,72 +405,3 @@ func TestDecoder_Decode_textUnmarshaler(t *testing.T) {
t.Errorf("b should be true")
}
}
func TestRawMessage_Decode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 4, 'T', 'n', 'z', 'e',
TagList, 0, 4, 'L', 'i', 's', 't', TagCompound, 0, 0, 0, 2, 0, 0,
TagEnd,
}
var container struct {
Key int32
Value RawMessage
List RawMessage
}
if tag, err := NewDecoder(bytes.NewReader(data)).Decode(&container); err != nil {
t.Fatal(tag)
} else {
if tag != "ab" {
t.Fatalf("Decode tag name error: want %s, get: %s", "ab", tag)
}
if container.Key != 12 {
t.Fatalf("Decode Key error: want %v, get: %v", 12, container.Key)
}
if !bytes.Equal(container.Value.Data, []byte{
0, 4, 'T', 'n', 'z', 'e',
}) {
t.Fatalf("Decode Key error: get: %v", container.Value)
}
if !bytes.Equal(container.List.Data, []byte{
TagCompound, 0, 0, 0, 2,
0, 0,
}) {
t.Fatalf("Decode List error: get: %v", container.List)
}
}
}
func TestStringifiedMessage_Decode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 5, 'T', 'n', ' ', 'z', 'e',
TagList, 0, 4, 'L', 'i', 's', 't', TagCompound, 0, 0, 0, 2, 0, 0,
TagEnd,
}
var container struct {
Key int32
Value StringifiedMessage
List StringifiedMessage
}
if tag, err := NewDecoder(bytes.NewReader(data)).Decode(&container); err != nil {
t.Fatal(tag, err)
} else {
if tag != "ab" {
t.Fatalf("UnmarshalNBT tag name error: want %s, get: %s", "ab", tag)
}
if container.Key != 12 {
t.Fatalf("UnmarshalNBT Key error: want %v, get: %v", 12, container.Key)
}
if container.Value != `"Tn ze"` {
t.Fatalf("UnmarshalNBT Key error: get: %v", container.Value)
}
if container.List != "[{},{}]" {
t.Fatalf("UnmarshalNBT List error: get: %v", container.List)
}
}
}

View File

@ -2,6 +2,7 @@ package nbt
import (
"bytes"
"encoding"
"errors"
"fmt"
"io"
@ -37,8 +38,8 @@ func NewEncoder(w io.Writer) *Encoder {
// expect `[]int8`, `[]int32`, `[]int64`, `[]uint8`, `[]uint32` and `[]uint64`,
// which TagByteArray, TagIntArray and TagLongArray.
// To force encode them as TagList, add a struct field tag.
// You haven't ability to encode them as TagList as root element at this time,
// issue or pull-request is welcome.
//
//
func (e *Encoder) Encode(v interface{}, tagName string) error {
t, val := getTagType(reflect.ValueOf(v))
return e.marshal(val, t, tagName)
@ -129,10 +130,22 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
}
case TagString:
if err := e.writeInt16(int16(val.Len())); err != nil {
var str []byte
if val.NumMethod() > 0 && val.CanInterface() {
if t, ok := val.Interface().(encoding.TextMarshaler); ok {
var err error
str, err = t.MarshalText()
if err != nil {
return err
}
}
} else {
str = []byte(val.String())
}
if err := e.writeInt16(int16(len(str))); err != nil {
return err
}
_, err := e.w.Write([]byte(val.String()))
_, err := e.w.Write(str)
return err
case TagCompound:
@ -205,17 +218,23 @@ func getTagType(v reflect.Value) (byte, reflect.Value) {
v.Set(reflect.New(v.Type().Elem()))
}
if v.Type().NumMethod() > 0 && v.CanInterface() {
if u, ok := v.Interface().(Marshaler); ok {
i := v.Interface()
if u, ok := i.(Marshaler); ok {
return u.TagType(), v
} else if _, ok := i.(encoding.TextMarshaler); ok {
return TagString, v
}
}
v = v.Elem()
}
if v.CanInterface() {
if encoder, ok := v.Interface().(Marshaler); ok {
return encoder.TagType(), v
if v.Type().NumMethod() > 0 && v.CanInterface() {
i := v.Interface()
if u, ok := i.(Marshaler); ok {
return u.TagType(), v
} else if _, ok := i.(encoding.TextMarshaler); ok {
return TagString, v
}
}

View File

@ -208,29 +208,6 @@ func TestEncoder_Encode_map(t *testing.T) {
}
}
func TestRawMessage_Encode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 4, 'T', 'n', 'z', 'e',
TagEnd,
}
var container struct {
Key int32
Value RawMessage
}
container.Key = 12
container.Value.Type = TagString
container.Value.Data = []byte{0, 4, 'T', 'n', 'z', 'e'}
var buf bytes.Buffer
if err := NewEncoder(&buf).Encode(container, "ab"); err != nil {
t.Fatalf("Encode error: %v", err)
} else if !bytes.Equal(data, buf.Bytes()) {
t.Fatalf("Encode error: want %v, get: %v", data, buf.Bytes())
}
}
func TestEncoder_Encode_interface(t *testing.T) {
data := map[string]interface{}{
"Key": int32(12),
@ -253,3 +230,18 @@ func TestEncoder_Encode_interface(t *testing.T) {
t.Fatalf("want: (%v, %v), but got (%v, %v)", 12, "Tnze", container.Key, container.Value)
}
}
func TestEncoder_Encode_textMarshaler(t *testing.T) {
var b TextBool = true
data, err := Marshal(&b)
if err != nil {
t.Fatal(err)
}
wants := []byte{
TagString, 0, 0,
0, 4, 't', 'r', 'u', 'e',
}
if !bytes.Equal(data, wants) {
t.Errorf("get %v, want %v", data, wants)
}
}

66
nbt/rawmsg_test.go Normal file
View File

@ -0,0 +1,66 @@
package nbt
import (
"bytes"
"testing"
)
func TestRawMessage_Encode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 4, 'T', 'n', 'z', 'e',
TagEnd,
}
var container struct {
Key int32
Value RawMessage
}
container.Key = 12
container.Value.Type = TagString
container.Value.Data = []byte{0, 4, 'T', 'n', 'z', 'e'}
var buf bytes.Buffer
if err := NewEncoder(&buf).Encode(container, "ab"); err != nil {
t.Fatalf("Encode error: %v", err)
} else if !bytes.Equal(data, buf.Bytes()) {
t.Fatalf("Encode error: want %v, get: %v", data, buf.Bytes())
}
}
func TestRawMessage_Decode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 4, 'T', 'n', 'z', 'e',
TagList, 0, 4, 'L', 'i', 's', 't', TagCompound, 0, 0, 0, 2, 0, 0,
TagEnd,
}
var container struct {
Key int32
Value RawMessage
List RawMessage
}
if tag, err := NewDecoder(bytes.NewReader(data)).Decode(&container); err != nil {
t.Fatal(tag)
} else {
if tag != "ab" {
t.Fatalf("Decode tag name error: want %s, get: %s", "ab", tag)
}
if container.Key != 12 {
t.Fatalf("Decode Key error: want %v, get: %v", 12, container.Key)
}
if !bytes.Equal(container.Value.Data, []byte{
0, 4, 'T', 'n', 'z', 'e',
}) {
t.Fatalf("Decode Key error: get: %v", container.Value)
}
if !bytes.Equal(container.List.Data, []byte{
TagCompound, 0, 0, 0, 2,
0, 0,
}) {
t.Fatalf("Decode List error: get: %v", container.List)
}
}
}

View File

@ -1,3 +1,38 @@
package nbt
//TODO: Test SNBT encode
import (
"bytes"
"testing"
)
func TestStringifiedMessage_Decode(t *testing.T) {
data := []byte{
TagCompound, 0, 2, 'a', 'b',
TagInt, 0, 3, 'K', 'e', 'y', 0, 0, 0, 12,
TagString, 0, 5, 'V', 'a', 'l', 'u', 'e', 0, 5, 'T', 'n', ' ', 'z', 'e',
TagList, 0, 4, 'L', 'i', 's', 't', TagCompound, 0, 0, 0, 2, 0, 0,
TagEnd,
}
var container struct {
Key int32
Value StringifiedMessage
List StringifiedMessage
}
if tag, err := NewDecoder(bytes.NewReader(data)).Decode(&container); err != nil {
t.Fatal(tag, err)
} else {
if tag != "ab" {
t.Fatalf("UnmarshalNBT tag name error: want %s, get: %s", "ab", tag)
}
if container.Key != 12 {
t.Fatalf("UnmarshalNBT Key error: want %v, get: %v", 12, container.Key)
}
if container.Value != `"Tn ze"` {
t.Fatalf("UnmarshalNBT Key error: get: %v", container.Value)
}
if container.List != "[{},{}]" {
t.Fatalf("UnmarshalNBT List error: get: %v", container.List)
}
}
}