NBT RawMessage fully support
This commit is contained in:
@ -10,31 +10,32 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Unmarshal(data []byte, v interface{}) error {
|
func Unmarshal(data []byte, v interface{}) error {
|
||||||
return NewDecoder(bytes.NewReader(data)).Decode(v)
|
_, err := NewDecoder(bytes.NewReader(data)).Decode(v)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Decoder) Decode(v interface{}) error {
|
func (d *Decoder) Decode(v interface{}) (string, error) {
|
||||||
val := reflect.ValueOf(v)
|
val := reflect.ValueOf(v)
|
||||||
if val.Kind() != reflect.Ptr {
|
if val.Kind() != reflect.Ptr {
|
||||||
return errors.New("nbt: non-pointer passed to Unmarshal")
|
return "", errors.New("nbt: non-pointer passed to Decode")
|
||||||
}
|
}
|
||||||
//start read NBT
|
//start read NBT
|
||||||
tagType, tagName, err := d.readTag()
|
tagType, tagName, err := d.readTag()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("nbt: %w", err)
|
return tagName, fmt.Errorf("nbt: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c := d.checkCompressed(tagType); c != "" {
|
if c := d.checkCompressed(tagType); c != "" {
|
||||||
return fmt.Errorf("nbt: unknown Tag, maybe need %s", c)
|
return tagName, fmt.Errorf("nbt: unknown Tag, maybe need %s", c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// We decode val not val.Elem because the Unmarshaler interface
|
// We decode val not val.Elem because the NBTDecoder interface
|
||||||
// test must be applied at the top level of the value.
|
// test must be applied at the top level of the value.
|
||||||
err = d.unmarshal(val, tagType, tagName)
|
err = d.unmarshal(val, tagType, tagName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("nbt: fail to decode tag %q: %w", tagName, err)
|
return tagName, fmt.Errorf("nbt: fail to decode tag %q: %w", tagName, err)
|
||||||
}
|
}
|
||||||
return nil
|
return tagName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// check the first byte and return if it use compress
|
// check the first byte and return if it use compress
|
||||||
@ -53,7 +54,7 @@ var ErrEND = errors.New("unexpected TAG_End")
|
|||||||
func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) error {
|
func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) error {
|
||||||
u, val := indirect(val, tagType == TagEnd)
|
u, val := indirect(val, tagType == TagEnd)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
return u.Unmarshal(tagType, tagName, d.r)
|
return u.Decode(tagType, d.r)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch tagType {
|
switch tagType {
|
||||||
@ -357,12 +358,12 @@ func (d *Decoder) unmarshal(val reflect.Value, tagType byte, tagName string) err
|
|||||||
|
|
||||||
// indirect walks down v allocating pointers as needed,
|
// indirect walks down v allocating pointers as needed,
|
||||||
// until it gets to a non-pointer.
|
// until it gets to a non-pointer.
|
||||||
// If it encounters an Unmarshaler, indirect stops and returns that.
|
// If it encounters an NBTDecoder, indirect stops and returns that.
|
||||||
// If decodingNull is true, indirect stops at the first settable pointer so it
|
// If decodingNull is true, indirect stops at the first settable pointer so it
|
||||||
// can be set to nil.
|
// can be set to nil.
|
||||||
//
|
//
|
||||||
// This function is copied and modified from encoding/json
|
// This function is copied and modified from encoding/json
|
||||||
func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
|
func indirect(v reflect.Value, decodingNull bool) (NBTDecoder, reflect.Value) {
|
||||||
v0 := v
|
v0 := v
|
||||||
haveAddr := false
|
haveAddr := false
|
||||||
|
|
||||||
@ -404,7 +405,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, reflect.Value) {
|
|||||||
v.Set(reflect.New(v.Type().Elem()))
|
v.Set(reflect.New(v.Type().Elem()))
|
||||||
}
|
}
|
||||||
if v.Type().NumMethod() > 0 && v.CanInterface() {
|
if v.Type().NumMethod() > 0 && v.CanInterface() {
|
||||||
if u, ok := v.Interface().(Unmarshaler); ok {
|
if u, ok := v.Interface().(NBTDecoder); ok {
|
||||||
return u, reflect.Value{}
|
return u, reflect.Value{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,14 +182,14 @@ func MakeBigTestStruct() BigTestStruct {
|
|||||||
return want
|
return want
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_bigTest(t *testing.T) {
|
func TestDecoder_Decode_bigTest(t *testing.T) {
|
||||||
//test parse
|
//test parse
|
||||||
var value BigTestStruct
|
var value BigTestStruct
|
||||||
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
|
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := NewDecoder(r).Decode(&value); err != nil {
|
if _, err := NewDecoder(r).Decode(&value); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,7 +204,7 @@ func TestUnmarshal_bigTest(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := NewDecoder(r).Decode(&empty); err != nil {
|
if _, err := NewDecoder(r).Decode(&empty); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -213,20 +213,20 @@ func TestUnmarshal_bigTest(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := NewDecoder(r).Decode(&inf); err != nil {
|
if _, err := NewDecoder(r).Decode(&inf); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// t.Log(inf)
|
// t.Log(inf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkUnmarshal_bigTest(b *testing.B) {
|
func BenchmarkDecoder_Decode_bigTest(b *testing.B) {
|
||||||
var value BigTestStruct
|
var value BigTestStruct
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
|
r, err := gzip.NewReader(bytes.NewReader(bigTestData[:]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := NewDecoder(r).Decode(&value); err != nil {
|
if _, err := NewDecoder(r).Decode(&value); err != nil {
|
||||||
b.Fatal(err)
|
b.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -256,7 +256,7 @@ func TestDecoder_overRead(t *testing.T) {
|
|||||||
r := bytes.NewReader(enc)
|
r := bytes.NewReader(enc)
|
||||||
// Count read bytes by using io.LimitReader
|
// Count read bytes by using io.LimitReader
|
||||||
rr := io.LimitReader(r, math.MaxInt64).(*io.LimitedReader)
|
rr := io.LimitReader(r, math.MaxInt64).(*io.LimitedReader)
|
||||||
if err := NewDecoder(rr).Decode(&value); err != nil {
|
if _, err := NewDecoder(rr).Decode(&value); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -266,7 +266,7 @@ func TestDecoder_overRead(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_IntArray(t *testing.T) {
|
func TestDecoder_Decode_IntArray(t *testing.T) {
|
||||||
data := []byte{
|
data := []byte{
|
||||||
TagIntArray, 0, 0,
|
TagIntArray, 0, 0,
|
||||||
0, 0, 0, 3,
|
0, 0, 0, 3,
|
||||||
@ -300,7 +300,7 @@ func TestUnmarshal_IntArray(t *testing.T) {
|
|||||||
// t.Log(value, value2)
|
// t.Log(value, value2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_LongArray(t *testing.T) {
|
func TestDecoder_Decode_LongArray(t *testing.T) {
|
||||||
data := []byte{
|
data := []byte{
|
||||||
TagLongArray, 0, 0,
|
TagLongArray, 0, 0,
|
||||||
0, 0, 0, 3,
|
0, 0, 0, 3,
|
||||||
@ -332,7 +332,7 @@ func TestUnmarshal_LongArray(t *testing.T) {
|
|||||||
// t.Log(infValue)
|
// t.Log(infValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_ByteArray(t *testing.T) {
|
func TestDecoder_Decode_ByteArray(t *testing.T) {
|
||||||
data := []byte{
|
data := []byte{
|
||||||
TagByteArray, 0, 0,
|
TagByteArray, 0, 0,
|
||||||
0, 0, 0, 7,
|
0, 0, 0, 7,
|
||||||
@ -363,7 +363,7 @@ func TestUnmarshal_ByteArray(t *testing.T) {
|
|||||||
// t.Log(infValue)
|
// t.Log(infValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshal_ErrorString(t *testing.T) {
|
func TestDecoder_Decode_ErrorString(t *testing.T) {
|
||||||
var data = []byte{
|
var data = []byte{
|
||||||
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0xFF, 0xFE,
|
0x08, 0x00, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0xFF, 0xFE,
|
||||||
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
|
0x42, 0x61, 0x6e, 0x61, 0x6e, 0x72, 0x61, 0x6d, 0x61,
|
||||||
@ -379,3 +379,32 @@ func TestUnmarshal_ErrorString(t *testing.T) {
|
|||||||
t.Log(err)
|
t.Log(err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDecoder_Decode_rawMessage(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
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,9 +11,9 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Marshal(v interface{}, optionalTagName ...string) ([]byte, error) {
|
func Marshal(v interface{}) ([]byte, error) {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
err := NewEncoder(&buf).Encode(v, optionalTagName...)
|
err := NewEncoder(&buf).Encode(v, "")
|
||||||
return buf.Bytes(), err
|
return buf.Bytes(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,25 +25,31 @@ func NewEncoder(w io.Writer) *Encoder {
|
|||||||
return &Encoder{w: w}
|
return &Encoder{w: w}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) Encode(v interface{}, optionalTagName ...string) error {
|
func (e *Encoder) Encode(v interface{}, tagName string) error {
|
||||||
val := reflect.ValueOf(v)
|
val := reflect.ValueOf(v)
|
||||||
var tagName string
|
return e.marshal(val, getTagType(val), tagName)
|
||||||
if len(optionalTagName) > 0 {
|
|
||||||
tagName = optionalTagName[0]
|
|
||||||
}
|
|
||||||
return e.marshal(val, getTagType(val.Type()), tagName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) marshal(val reflect.Value, tagType byte, tagName string) error {
|
func (e *Encoder) marshal(val reflect.Value, tagType byte, tagName string) error {
|
||||||
if err := e.writeHeader(val, tagType, tagName); err != nil {
|
if err := e.writeHeader(val, tagType, tagName); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
if val.CanInterface() {
|
||||||
|
if encoder, ok := val.Interface().(NBTEncoder); ok {
|
||||||
|
return encoder.Encode(e.w)
|
||||||
|
}
|
||||||
|
}
|
||||||
return e.writeValue(val, tagType)
|
return e.writeValue(val, tagType)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Encoder) writeHeader(val reflect.Value, tagType byte, tagName string) (err error) {
|
func (e *Encoder) writeHeader(val reflect.Value, tagType byte, tagName string) (err error) {
|
||||||
if tagType == TagList {
|
if tagType == TagList {
|
||||||
eleType := getTagType(val.Type().Elem())
|
var eleType byte
|
||||||
|
if val.Len() > 0 {
|
||||||
|
eleType = getTagType(val.Index(0))
|
||||||
|
} else {
|
||||||
|
eleType = getTagTypeByType(val.Type().Elem())
|
||||||
|
}
|
||||||
err = e.writeListHeader(eleType, tagName, val.Len(), true)
|
err = e.writeListHeader(eleType, tagName, val.Len(), true)
|
||||||
} else {
|
} else {
|
||||||
err = e.writeTag(tagType, tagName)
|
err = e.writeTag(tagType, tagName)
|
||||||
@ -96,7 +102,7 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
|
|||||||
case TagList:
|
case TagList:
|
||||||
for i := 0; i < val.Len(); i++ {
|
for i := 0; i < val.Len(); i++ {
|
||||||
arrVal := val.Index(i)
|
arrVal := val.Index(i)
|
||||||
err := e.writeValue(arrVal, getTagType(arrVal.Type()))
|
err := e.writeValue(arrVal, getTagType(arrVal))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -119,12 +125,13 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
|
|||||||
n := val.NumField()
|
n := val.NumField()
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
f := val.Type().Field(i)
|
f := val.Type().Field(i)
|
||||||
|
v := val.Field(i)
|
||||||
tag := f.Tag.Get("nbt")
|
tag := f.Tag.Get("nbt")
|
||||||
if (f.PkgPath != "" && !f.Anonymous) || tag == "-" {
|
if (f.PkgPath != "" && !f.Anonymous) || tag == "-" {
|
||||||
continue // Private field
|
continue // Private field
|
||||||
}
|
}
|
||||||
|
|
||||||
tagProps := parseTag(f, tag)
|
tagProps := parseTag(f, v, tag)
|
||||||
if err := e.marshal(val.Field(i), tagProps.Type, tagProps.Name); err != nil {
|
if err := e.marshal(val.Field(i), tagProps.Type, tagProps.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -139,7 +146,7 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
|
|||||||
tagName = r.Key().String()
|
tagName = r.Key().String()
|
||||||
}
|
}
|
||||||
tagValue := r.Value()
|
tagValue := r.Value()
|
||||||
tagType := getTagType(tagValue.Type())
|
tagType := getTagType(tagValue)
|
||||||
if tagType == TagNone {
|
if tagType == TagNone {
|
||||||
return errors.New("unsupported value " + tagValue.String())
|
return errors.New("unsupported value " + tagValue.String())
|
||||||
}
|
}
|
||||||
@ -156,7 +163,36 @@ func (e *Encoder) writeValue(val reflect.Value, tagType byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getTagType(vk reflect.Type) byte {
|
func getTagType(v reflect.Value) byte {
|
||||||
|
if v.CanInterface() {
|
||||||
|
if encoder, ok := v.Interface().(NBTEncoder); ok {
|
||||||
|
return encoder.TagType()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch v.Kind() {
|
||||||
|
case reflect.Array, reflect.Slice:
|
||||||
|
var elemType byte
|
||||||
|
if v.Len() > 0 {
|
||||||
|
elemType = getTagType(v.Index(0))
|
||||||
|
} else {
|
||||||
|
elemType = getTagTypeByType(v.Type().Elem())
|
||||||
|
}
|
||||||
|
switch elemType {
|
||||||
|
case TagByte: // Special types for these values
|
||||||
|
return TagByteArray
|
||||||
|
case TagInt:
|
||||||
|
return TagIntArray
|
||||||
|
case TagLong:
|
||||||
|
return TagLongArray
|
||||||
|
default:
|
||||||
|
return TagList
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return getTagTypeByType(v.Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTagTypeByType(vk reflect.Type) byte {
|
||||||
switch vk.Kind() {
|
switch vk.Kind() {
|
||||||
case reflect.Uint8:
|
case reflect.Uint8:
|
||||||
return TagByte
|
return TagByte
|
||||||
@ -174,17 +210,6 @@ func getTagType(vk reflect.Type) byte {
|
|||||||
return TagString
|
return TagString
|
||||||
case reflect.Struct, reflect.Interface, reflect.Map:
|
case reflect.Struct, reflect.Interface, reflect.Map:
|
||||||
return TagCompound
|
return TagCompound
|
||||||
case reflect.Array, reflect.Slice:
|
|
||||||
switch vk.Elem().Kind() {
|
|
||||||
case reflect.Uint8: // Special types for these values
|
|
||||||
return TagByteArray
|
|
||||||
case reflect.Int32:
|
|
||||||
return TagIntArray
|
|
||||||
case reflect.Int64:
|
|
||||||
return TagLongArray
|
|
||||||
default:
|
|
||||||
return TagList
|
|
||||||
}
|
|
||||||
default:
|
default:
|
||||||
return TagNone
|
return TagNone
|
||||||
}
|
}
|
||||||
@ -195,7 +220,7 @@ type tagProps struct {
|
|||||||
Type byte
|
Type byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseTag(f reflect.StructField, tagName string) tagProps {
|
func parseTag(f reflect.StructField, v reflect.Value, tagName string) tagProps {
|
||||||
result := tagProps{}
|
result := tagProps{}
|
||||||
result.Name = tagName
|
result.Name = tagName
|
||||||
if result.Name == "" {
|
if result.Name == "" {
|
||||||
@ -203,7 +228,7 @@ func parseTag(f reflect.StructField, tagName string) tagProps {
|
|||||||
}
|
}
|
||||||
|
|
||||||
nbtType := f.Tag.Get("nbt_type")
|
nbtType := f.Tag.Get("nbt_type")
|
||||||
result.Type = getTagType(f.Type)
|
result.Type = getTagType(v)
|
||||||
if strings.Contains(nbtType, "list") {
|
if strings.Contains(nbtType, "list") {
|
||||||
if IsArrayTag(result.Type) {
|
if IsArrayTag(result.Type) {
|
||||||
result.Type = TagList // for expanding the array to a standard list
|
result.Type = TagList // for expanding the array to a standard list
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMarshal_IntArray(t *testing.T) {
|
func TestEncoder_Encode_intArray(t *testing.T) {
|
||||||
// Test marshal pure Int array
|
// Test marshal pure Int array
|
||||||
v := []int32{0, -10, 3}
|
v := []int32{0, -10, 3}
|
||||||
out := []byte{TagIntArray, 0x00, 0x00, 0, 0, 0, 3,
|
out := []byte{TagIntArray, 0x00, 0x00, 0, 0, 0, 3,
|
||||||
@ -41,7 +41,7 @@ func TestMarshal_IntArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_FloatArray(t *testing.T) {
|
func TestEncoder_Encode_floatArray(t *testing.T) {
|
||||||
// Test marshal pure Int array
|
// Test marshal pure Int array
|
||||||
v := []float32{0.3, -100, float32(math.NaN())}
|
v := []float32{0.3, -100, float32(math.NaN())}
|
||||||
out := []byte{TagList, 0x00, 0x00, TagFloat, 0, 0, 0, 3,
|
out := []byte{TagList, 0x00, 0x00, TagFloat, 0, 0, 0, 3,
|
||||||
@ -56,7 +56,7 @@ func TestMarshal_FloatArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_String(t *testing.T) {
|
func TestEncoder_Encode_string(t *testing.T) {
|
||||||
v := "Test"
|
v := "Test"
|
||||||
out := []byte{TagString, 0x00, 0x00, 0, 4,
|
out := []byte{TagString, 0x00, 0x00, 0, 4,
|
||||||
'T', 'e', 's', 't'}
|
'T', 'e', 's', 't'}
|
||||||
@ -68,7 +68,7 @@ func TestMarshal_String(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_InterfaceArray(t *testing.T) {
|
func TestEncoder_Encode_interfaceArray(t *testing.T) {
|
||||||
type Struct1 struct {
|
type Struct1 struct {
|
||||||
Val int32
|
Val int32
|
||||||
}
|
}
|
||||||
@ -110,7 +110,7 @@ func TestMarshal_InterfaceArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_StructArray(t *testing.T) {
|
func TestEncoder_Encode_structArray(t *testing.T) {
|
||||||
type Struct1 struct {
|
type Struct1 struct {
|
||||||
Val int32
|
Val int32
|
||||||
}
|
}
|
||||||
@ -164,9 +164,9 @@ func TestMarshal_StructArray(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_bigTest(t *testing.T) {
|
func TestEncoder_Encode_bigTest(t *testing.T) {
|
||||||
data, err := Marshal(MakeBigTestStruct(), "Level")
|
var buf bytes.Buffer
|
||||||
if err != nil {
|
if err := NewEncoder(&buf).Encode(MakeBigTestStruct(), "Level"); err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,12 +176,12 @@ func TestMarshal_bigTest(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(data, want) {
|
if !bytes.Equal(buf.Bytes(), want) {
|
||||||
t.Errorf("got:\n[% 2x]\nwant:\n[% 2x]", data, want)
|
t.Errorf("got:\n[% 2x]\nwant:\n[% 2x]", buf.Bytes(), want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMarshal_map(t *testing.T) {
|
func TestEncoder_Encode_map(t *testing.T) {
|
||||||
v := map[string][]int32{
|
v := map[string][]int32{
|
||||||
"Tnze": {1, 2, 3, 4, 5},
|
"Tnze": {1, 2, 3, 4, 5},
|
||||||
"Xi_Xi_Mi": {0, 0, 4, 7, 2},
|
"Xi_Xi_Mi": {0, 0, 4, 7, 2},
|
||||||
@ -197,7 +197,7 @@ func TestMarshal_map(t *testing.T) {
|
|||||||
XXM []int32 `nbt:"Xi_Xi_Mi"`
|
XXM []int32 `nbt:"Xi_Xi_Mi"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := NewDecoder(bytes.NewReader(b)).Decode(&data); err != nil {
|
if _, err := NewDecoder(bytes.NewReader(b)).Decode(&data); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(data.Tnze, v["Tnze"]) {
|
if !reflect.DeepEqual(data.Tnze, v["Tnze"]) {
|
||||||
@ -207,3 +207,26 @@ func TestMarshal_map(t *testing.T) {
|
|||||||
t.Fatalf("Marshal map error: got: %#v, want %#v", data.XXM, v["Xi_Xi_Mi"])
|
t.Fatalf("Marshal map error: got: %#v, want %#v", data.XXM, v["Xi_Xi_Mi"])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEncoder_Encode_rawMessage(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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
package nbt
|
package nbt
|
||||||
|
|
||||||
type Unmarshaler interface {
|
import "io"
|
||||||
Unmarshal(tagType byte, tagName string, r DecoderReader) error
|
|
||||||
|
type NBTDecoder interface {
|
||||||
|
Decode(tagType byte, r DecoderReader) error
|
||||||
}
|
}
|
||||||
|
|
||||||
//type Marshaller interface{
|
type NBTEncoder interface {
|
||||||
// Marshal()
|
TagType() byte
|
||||||
//}
|
Encode(w io.Writer) error
|
||||||
|
}
|
||||||
|
23
nbt/raw.go
23
nbt/raw.go
@ -1,23 +0,0 @@
|
|||||||
package nbt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
type RawMessage []byte
|
|
||||||
|
|
||||||
func (m *RawMessage) Unmarshal(tagType byte, _ string, r DecoderReader) error {
|
|
||||||
if tagType == TagEnd {
|
|
||||||
return ErrEND
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := bytes.NewBuffer((*m)[:0])
|
|
||||||
tee := io.TeeReader(r, buf)
|
|
||||||
err := NewDecoder(tee).rawRead(tagType)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*m = buf.Bytes()
|
|
||||||
return nil
|
|
||||||
}
|
|
34
nbt/rawmsg.go
Normal file
34
nbt/rawmsg.go
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
package nbt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RawMessage struct {
|
||||||
|
Type byte
|
||||||
|
Data []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m RawMessage) TagType() byte {
|
||||||
|
return m.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m RawMessage) Encode(w io.Writer) error {
|
||||||
|
_, err := w.Write(m.Data)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *RawMessage) Decode(tagType byte, r DecoderReader) error {
|
||||||
|
if tagType == TagEnd {
|
||||||
|
return ErrEND
|
||||||
|
}
|
||||||
|
buf := bytes.NewBuffer(m.Data[:0])
|
||||||
|
tee := io.TeeReader(r, buf)
|
||||||
|
err := NewDecoder(tee).rawRead(tagType)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
m.Data = buf.Bytes()
|
||||||
|
return nil
|
||||||
|
}
|
10
nbt/snbt.go
Normal file
10
nbt/snbt.go
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package nbt
|
||||||
|
|
||||||
|
type StringifiedNBT struct {
|
||||||
|
Name string
|
||||||
|
Content string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *StringifiedNBT) Decode(tagType byte, tagName string, r DecoderReader) error {
|
||||||
|
panic("unimplemented")
|
||||||
|
}
|
@ -454,7 +454,7 @@ func (n nbtField) WriteTo(w io.Writer) (int64, error) {
|
|||||||
func (n nbtField) ReadFrom(r io.Reader) (int64, error) {
|
func (n nbtField) ReadFrom(r io.Reader) (int64, error) {
|
||||||
// LimitReader is used to count reader length
|
// LimitReader is used to count reader length
|
||||||
lr := &io.LimitedReader{R: r, N: math.MaxInt64}
|
lr := &io.LimitedReader{R: r, N: math.MaxInt64}
|
||||||
err := nbt.NewDecoder(lr).Decode(n.V)
|
_, err := nbt.NewDecoder(lr).Decode(n.V)
|
||||||
if err != nil && errors.Is(err, nbt.ErrEND) {
|
if err != nil && errors.Is(err, nbt.ErrEND) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
@ -66,6 +66,6 @@ func (c *Column) Load(data []byte) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = nbt.NewDecoder(r).Decode(c)
|
_, err = nbt.NewDecoder(r).Decode(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,6 @@ type Level struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadLevel(r io.Reader) (data Level, err error) {
|
func ReadLevel(r io.Reader) (data Level, err error) {
|
||||||
err = nbt.NewDecoder(r).Decode(&data)
|
_, err = nbt.NewDecoder(r).Decode(&data)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,7 @@ type Item struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadPlayerData(r io.Reader) (data PlayerData, err error) {
|
func ReadPlayerData(r io.Reader) (data PlayerData, err error) {
|
||||||
err = nbt.NewDecoder(r).Decode(&data)
|
_, err = nbt.NewDecoder(r).Decode(&data)
|
||||||
//parse UUID from two int64s
|
//parse UUID from two int64s
|
||||||
binary.BigEndian.PutUint64(data.UUID[:], uint64(data.UUIDMost))
|
binary.BigEndian.PutUint64(data.UUID[:], uint64(data.UUIDMost))
|
||||||
binary.BigEndian.PutUint64(data.UUID[8:], uint64(data.UUIDLeast))
|
binary.BigEndian.PutUint64(data.UUID[8:], uint64(data.UUIDLeast))
|
||||||
|
@ -40,7 +40,7 @@ func TestReadRegion(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
var b interface{}
|
var b interface{}
|
||||||
err = nbt.NewDecoder(r).Decode(&b)
|
_, err = nbt.NewDecoder(r).Decode(&b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user