rename fastnbt -> dynbt. add some NewXXX api
This commit is contained in:
@ -428,6 +428,22 @@ func TestDecoder_Decode_textUnmarshaler(t *testing.T) {
|
||||
if b != true {
|
||||
t.Errorf("b should be true")
|
||||
}
|
||||
|
||||
var s struct {
|
||||
A TextBool
|
||||
}
|
||||
data = []byte{
|
||||
TagCompound, 0, 0,
|
||||
TagString, 0, 1, 'A', 0, 4, 't', 'r', 'u', 'e',
|
||||
TagEnd,
|
||||
}
|
||||
_, err = NewDecoder(bytes.NewReader(data)).Decode(&s)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if s.A != true {
|
||||
t.Errorf("s.A should be true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecoder_Decode_ErrorUnknownField(t *testing.T) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package fastnbt
|
||||
package dynbt
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
@ -1,4 +1,4 @@
|
||||
package fastnbt
|
||||
package dynbt
|
||||
|
||||
import (
|
||||
"bytes"
|
@ -1,4 +1,4 @@
|
||||
package fastnbt
|
||||
package dynbt
|
||||
|
||||
import (
|
||||
"errors"
|
@ -1,7 +1,9 @@
|
||||
package fastnbt
|
||||
package dynbt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/Tnze/go-mc/nbt"
|
||||
@ -37,13 +39,29 @@ func TestValue_new(t *testing.T) {
|
||||
}
|
||||
|
||||
byteArray := make([]byte, 1000)
|
||||
for n := 0; n < 1000; n++ {
|
||||
for n := range byteArray {
|
||||
byteArray[n] = byte((n*n*255 + n*7) % 100)
|
||||
}
|
||||
if val := NewByteArray(byteArray); !bytes.Equal(byteArray, val.ByteArray()) {
|
||||
t.Error("encode byteArray error")
|
||||
}
|
||||
|
||||
intArray := make([]int32, 250)
|
||||
for n := range intArray {
|
||||
intArray[n] = rand.Int31()
|
||||
}
|
||||
if val := NewIntArray(intArray); !reflect.DeepEqual(intArray, val.IntArray()) {
|
||||
t.Error("encode intArray error")
|
||||
}
|
||||
|
||||
longArray := make([]int64, 125)
|
||||
for n := range longArray {
|
||||
longArray[n] = rand.Int63()
|
||||
}
|
||||
if val := NewLongArray(longArray); !reflect.DeepEqual(longArray, val.LongArray()) {
|
||||
t.Error("encode longArray error")
|
||||
}
|
||||
|
||||
val := NewCompound()
|
||||
val.Set("a", NewString("tnze"))
|
||||
if val.Get("a").String() != "tnze" || val.Compound().Len() != 1 {
|
@ -1,4 +1,21 @@
|
||||
package fastnbt
|
||||
// Package dynbt is a library that provides dynamic NBT operation APIs.
|
||||
//
|
||||
// Dynamically represented NBT value is useful in many cases, for example,
|
||||
// - You want to store custom structural values at runtime.
|
||||
// - You want to query or modify the data later. (Otherwise use the [nbt.RawMessage])
|
||||
// - You don't know what type the data is at compile time. (Otherwise use the [nbt.RawMessage] too)
|
||||
//
|
||||
// The [*Value] provides a group of APIs on top of the [nbt] package.
|
||||
// [*Value] implements [nbt.Marshaler] and [nbt.Unmarshaler] interfaces.
|
||||
// It can be used as a field of struct, or element of slice, map, etc.
|
||||
// The pointer type should always be used, unless used as fields for structures
|
||||
//
|
||||
// Notice that querying Tags in Compound use a linear search, so it's not recommended to use it in a large Compound.
|
||||
// The better choice is map[string]*Value for dynamic accessing a large Compound.
|
||||
//
|
||||
// This package tries its best to not copy data if possible.
|
||||
// It returns the underlying data in some cases. Don't modify them!
|
||||
package dynbt
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
@ -62,6 +79,24 @@ func NewByteArray(v []byte) *Value {
|
||||
return &Value{tag: nbt.TagByteArray, data: append(data, v...)}
|
||||
}
|
||||
|
||||
func NewIntArray(v []int32) *Value {
|
||||
data := make([]byte, 4+len(v)*4)
|
||||
binary.BigEndian.PutUint32(data, uint32(len(v)))
|
||||
for i, j := 0, 4; i < len(v); i, j = i+1, j+4 {
|
||||
binary.BigEndian.PutUint32(data[j:], uint32(v[i]))
|
||||
}
|
||||
return &Value{tag: nbt.TagIntArray, data: data}
|
||||
}
|
||||
|
||||
func NewLongArray(v []int64) *Value {
|
||||
data := make([]byte, 4+len(v)*8)
|
||||
binary.BigEndian.PutUint32(data, uint32(len(v)))
|
||||
for i, j := 0, 4; i < len(v); i, j = i+1, j+8 {
|
||||
binary.BigEndian.PutUint64(data[j:], uint64(v[i]))
|
||||
}
|
||||
return &Value{tag: nbt.TagLongArray, data: data}
|
||||
}
|
||||
|
||||
func NewString(str string) *Value {
|
||||
data := make([]byte, 2, 2+len(str))
|
||||
binary.BigEndian.PutUint16(data, uint16(len(str)))
|
||||
@ -126,10 +161,16 @@ func (v *Value) Double() float64 {
|
||||
}
|
||||
|
||||
func (v *Value) List() []*Value {
|
||||
if v.tag != nbt.TagList {
|
||||
return nil
|
||||
}
|
||||
return v.list
|
||||
}
|
||||
|
||||
func (v *Value) Compound() *Compound {
|
||||
if v.tag != nbt.TagCompound {
|
||||
return nil
|
||||
}
|
||||
return &v.comp
|
||||
}
|
||||
|
||||
@ -141,10 +182,25 @@ func (v *Value) ByteArray() []byte {
|
||||
}
|
||||
|
||||
func (v *Value) IntArray() []int32 {
|
||||
if v.tag != nbt.TagIntArray {
|
||||
return nil
|
||||
}
|
||||
length := binary.BigEndian.Uint32(v.data)
|
||||
ret := make([]int32, length)
|
||||
for i := uint32(0); i < length; i += 4 {
|
||||
ret[i] = int32(binary.BigEndian.Uint32(v.data[i:]))
|
||||
for i, j := uint32(0), 4; i < length; i, j = i+1, j+4 {
|
||||
ret[i] = int32(binary.BigEndian.Uint32(v.data[j:]))
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (v *Value) LongArray() []int64 {
|
||||
if v.tag != nbt.TagLongArray {
|
||||
return nil
|
||||
}
|
||||
length := binary.BigEndian.Uint32(v.data)
|
||||
ret := make([]int64, length)
|
||||
for i, j := uint32(0), 4; i < length; i, j = i+1, j+8 {
|
||||
ret[i] = int64(binary.BigEndian.Uint64(v.data[j:]))
|
||||
}
|
||||
return ret
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package fastnbt
|
||||
package dynbt
|
||||
|
||||
import "github.com/Tnze/go-mc/nbt"
|
||||
|
Reference in New Issue
Block a user