From e9436211924b03993c6272069d10a917578b1a52 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Wed, 7 Aug 2013 11:19:57 +0200 Subject: Stringer interface implemented for Tag. Also added a small test tool which reads NBT data from stdin and put the stringified data to stdout. --- nbt/nbt.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'nbt/nbt.go') diff --git a/nbt/nbt.go b/nbt/nbt.go index 5363d1b..12afb5d 100644 --- a/nbt/nbt.go +++ b/nbt/nbt.go @@ -2,9 +2,12 @@ package nbt import ( "encoding/binary" + "encoding/hex" "errors" + "fmt" "github.com/kch42/kagus" "io" + "strconv" ) type Tag struct { @@ -17,6 +20,51 @@ type TagList struct { Elems []interface{} } +func (t Tag) String() string { + s := t.Type.String() + switch t.Type { + case TAG_Byte: + s += fmt.Sprintf(": 0x%02x", t.Payload.(byte)) + case TAG_Short: + s += fmt.Sprintf(": %d", t.Payload.(int16)) + case TAG_Int: + s += fmt.Sprintf(": %d", t.Payload.(int32)) + case TAG_Long: + s += fmt.Sprintf(": %d", t.Payload.(int64)) + case TAG_Float: + s += fmt.Sprintf(": %f", t.Payload.(float32)) + case TAG_Double: + s += fmt.Sprintf(": %f", t.Payload.(float64)) + case TAG_Byte_Array: + s += "\n" + kagus.Indent(hex.Dump(t.Payload.([]byte)), " ") + case TAG_String: + s += ": " + strconv.Quote(t.Payload.(string)) + case TAG_List: + l := t.Payload.(TagList) + s += " of " + l.Type.String() + ":" + for _, elem := range l.Elems { + s += "\n" + kagus.Indent(Tag{l.Type, elem}.String(), " ") + } + case TAG_Compound: + s += ":" + comp := t.Payload.(TagCompound) + for k, v := range comp { + s += "\n" + kagus.Indent(strconv.Quote(k)+" ->"+kagus.Indent(v.String(), " "), " ") + } + return s + case TAG_Int_Array: + l := t.Payload.([]int32) + s += ": " + sep := "" + for _, elem := range l { + s += sep + fmt.Sprintf("%d", elem) + sep = ", " + } + } + + return s +} + type TagCompound map[string]Tag func readTagData(r io.Reader, tt TagType) (interface{}, error) { -- cgit v1.2.3-54-g00ecf