summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--nbt/bigtest_test.go64
-rw-r--r--nbt/compressed.go19
-rw-r--r--nbt/create.go14
-rw-r--r--nbt/nbt.go2
4 files changed, 93 insertions, 6 deletions
diff --git a/nbt/bigtest_test.go b/nbt/bigtest_test.go
index 079141e..af6c573 100644
--- a/nbt/bigtest_test.go
+++ b/nbt/bigtest_test.go
@@ -3,6 +3,7 @@ package nbt
import (
"bytes"
"fmt"
+ "io"
"testing"
)
@@ -55,6 +56,8 @@ func bigtest() []byte {
}
}
+func byteArrayTestSeries(n int) byte { return byte((n*n*255 + n*7) % 100) }
+
func getKey(t *testing.T, comp TagCompound, key string, tt TagType) (tag Tag, ok bool) {
tag, ok = comp[key]
if !ok {
@@ -131,7 +134,10 @@ func nestedCompoundHelper(t *testing.T, comp TagCompound, id, name string, value
}
func TestBigtest(t *testing.T) {
- r := bytes.NewReader(bigtest())
+ testBigtest(bytes.NewReader(bigtest()), t)
+}
+
+func testBigtest(r io.Reader, t *testing.T) {
root, name, err := ReadGzipdNamedTag(r)
if err != nil {
t.Fatalf("Could not read NBT data: %s", err)
@@ -214,7 +220,7 @@ func TestBigtest(t *testing.T) {
t.Errorf("byteArrayTest data has length %d, expected 1000", len(data))
} else {
for n := 0; n < 1000; n++ {
- want := byte((n*n*255 + n*7) % 100)
+ want := byteArrayTestSeries(n)
if data[n] != want {
t.Errorf("Wrong byteArrayTest data at index %d: 0x%02x", n, data[n])
break
@@ -223,3 +229,57 @@ func TestBigtest(t *testing.T) {
}
}
}
+
+func makeNested(name string, value float32) Tag {
+ comp := make(TagCompound)
+ comp["name"] = NewStringTag(name)
+ comp["value"] = NewFloatTag(value)
+ return Tag{TAG_Compound, comp}
+}
+
+func TestCreateBigtest(t *testing.T) {
+ rootcomp := make(TagCompound)
+
+ rootcomp["shortTest"] = NewShortTag(32767)
+ rootcomp["longTest"] = NewLongTag(9223372036854775807)
+ rootcomp["floatTest"] = NewFloatTag(0.49823147)
+ rootcomp["stringTest"] = NewStringTag("HELLO WORLD THIS IS A TEST STRING ÅÄÖ!")
+ rootcomp["intTest"] = NewIntTag(2147483647)
+ rootcomp["byteTest"] = NewByteTag(127)
+ rootcomp["doubleTest"] = NewDoubleTag(0.4931287132182315)
+
+ comp := make(TagCompound)
+ comp["ham"] = makeNested("Hampus", 0.75)
+ comp["egg"] = makeNested("Eggbert", 0.5)
+ rootcomp["nested compound test"] = Tag{TAG_Compound, comp}
+
+ listlong := make([]interface{}, 5)
+ for i := 0; i < 5; i++ {
+ listlong[i] = int64(i + 11)
+ }
+ rootcomp["listTest (long)"] = Tag{TAG_List, TagList{TAG_Long, listlong}}
+
+ listcomp := make([]interface{}, 2)
+ for i := 0; i < 2; i++ {
+ comp := make(TagCompound)
+ comp["name"] = NewStringTag(fmt.Sprintf("Compound tag #%d", i))
+ comp["created-on"] = NewLongTag(1264099775885)
+ listcomp[i] = comp
+ }
+ rootcomp["listTest (compound)"] = Tag{TAG_List, TagList{TAG_Compound, listcomp}}
+
+ data := make([]byte, 1000)
+ for n := 0; n < 1000; n++ {
+ data[n] = byteArrayTestSeries(n)
+ }
+ rootcomp["byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"] = NewByteArrayTag(data)
+
+ tag := Tag{TAG_Compound, rootcomp}
+ buf := new(bytes.Buffer)
+
+ if err := WriteGzipdNamedTag(buf, "Level", tag); err != nil {
+ t.Fatalf("Could not write NBT data: %s", err)
+ }
+
+ testBigtest(buf, t)
+}
diff --git a/nbt/compressed.go b/nbt/compressed.go
index a16308f..406c299 100644
--- a/nbt/compressed.go
+++ b/nbt/compressed.go
@@ -19,9 +19,16 @@ func ReadGzipdNamedTag(r io.Reader) (Tag, string, error) {
}
// WriteGzipdNamedTag writes a gzip compressed named tag. See WriteNamedTag for more info.
-func WriteGzipdNamedTag(w io.Writer, name string, tag Tag) error {
+func WriteGzipdNamedTag(w io.Writer, name string, tag Tag) (outerr error) {
comp := gzip.NewWriter(w)
- return WriteNamedTag(comp, name, tag)
+ defer func(){
+ err := comp.Close()
+ if outerr != nil {
+ outerr = err
+ }
+ }()
+ outerr = WriteNamedTag(comp, name, tag)
+ return
}
// ReadZlibdNamedTag reads a zlib compressed named tag. See ReadNamedTags for more info.
@@ -35,7 +42,13 @@ func ReadZlibdNamedTag(r io.Reader) (Tag, string, error) {
}
// WriteZlibdNamedTag writes a zlib compressed named tag. See WriteNamedTag for more info.
-func WriteZlibdNamedTag(w io.Writer, name string, tag Tag) error {
+func WriteZlibdNamedTag(w io.Writer, name string, tag Tag) (outerr error) {
comp := zlib.NewWriter(w)
+ defer func(){
+ err := comp.Close()
+ if outerr != nil {
+ outerr = err
+ }
+ }()
return WriteNamedTag(comp, name, tag)
}
diff --git a/nbt/create.go b/nbt/create.go
new file mode 100644
index 0000000..d515c1b
--- /dev/null
+++ b/nbt/create.go
@@ -0,0 +1,14 @@
+package nbt
+
+func NewByteTag(v byte) Tag { return Tag{TAG_Byte, v} }
+func NewShortTag(v int16) Tag { return Tag{TAG_Short, v} }
+func NewIntTag(v int32) Tag { return Tag{TAG_Int, v} }
+func NewLongTag(v int64) Tag { return Tag{TAG_Long, v} }
+func NewFloatTag(v float32) Tag { return Tag{TAG_Float, v} }
+func NewDoubleTag(v float64) Tag { return Tag{TAG_Double, v} }
+func NewByteArrayTag(v []byte) Tag { return Tag{TAG_Byte_Array, v} }
+func NewStringTag(v string) Tag { return Tag{TAG_String, v} }
+func NewIntArrayTag(v []int32) Tag { return Tag{TAG_Int_Array, v} }
+
+// func NewCompoundTag() Tag {return Tag{TAG_Compound, make(TagCompound)}}
+// func NewListTag
diff --git a/nbt/nbt.go b/nbt/nbt.go
index fe9f7a4..dd8db1d 100644
--- a/nbt/nbt.go
+++ b/nbt/nbt.go
@@ -82,7 +82,7 @@ func (t Tag) String() string {
return s
}
-// TagCompund is the payload of a TAG_Compound.
+// TagCompund is the payload of a TAG_Compound. Initialize with make.
type TagCompound map[string]Tag
func readTagData(r io.Reader, tt TagType) (interface{}, error) {