diff options
-rw-r--r-- | nbt/nbt.go | 48 | ||||
-rw-r--r-- | nbt/nbtdump/main.go | 18 |
2 files changed, 66 insertions, 0 deletions
@@ -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) { diff --git a/nbt/nbtdump/main.go b/nbt/nbtdump/main.go new file mode 100644 index 0000000..bf9dd9b --- /dev/null +++ b/nbt/nbtdump/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "strconv" + "fmt" + "os" + "github.com/kch42/gonbt/nbt" +) + +func main() { + tag, name, err := nbt.ReadNamedTag(os.Stdin) + if err != nil { + fmt.Fprintf(os.Stderr, "Could not read NBT data: %s", err) + os.Exit(1) + } + + fmt.Printf("Tag Name:\n%s\n\nData:\n%s\n", strconv.Quote(name), tag) +} |