diff --git a/mapblockparser/mapblock.go b/mapblockparser/mapblock.go index 3aa3f84..c75e727 100644 --- a/mapblockparser/mapblock.go +++ b/mapblockparser/mapblock.go @@ -11,17 +11,17 @@ type MapBlock struct { type Metadata struct { Inventories map[int]map[string]*Inventory - Pairs map[int]map[string]*string + Pairs map[int]map[string]string } -func (md *Metadata) GetPairsMap(pos int) map[string]*string { +func (md *Metadata) GetPairsMap(pos int) map[string]string { if md.Pairs == nil { - md.Pairs = make(map[int]map[string]*string) + md.Pairs = make(map[int]map[string]string) } pairsMap := md.Pairs[pos] if pairsMap == nil { - pairsMap = make(map[string]*string) + pairsMap = make(map[string]string) md.Pairs[pos] = pairsMap } diff --git a/mapblockparser/metadata.go b/mapblockparser/metadata.go index 35aeb26..58c6cbb 100644 --- a/mapblockparser/metadata.go +++ b/mapblockparser/metadata.go @@ -49,6 +49,10 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) { buf := new(bytes.Buffer) io.Copy(buf, z) + if cr.Count == 0 { + return 0, errors.New("no data") + } + metadata := buf.Bytes() offset := 0 @@ -86,17 +90,14 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) { offset+=keyLength valueLength := readU32(metadata, offset) - offset+=4; + offset+=4 - value := string(metadata[offset:keyLength+offset]) + value := string(metadata[offset:valueLength+offset]) offset+=valueLength - pairsMap[key] = &value + pairsMap[key] = value offset++ - - log.Println("MD item", i, offset, position, valuecount, valueLength, keyLength, pairsMap)//XXX - } var currentInventoryName *string = nil @@ -108,7 +109,7 @@ func parseMetadata(mapblock *MapBlock, data []byte) (int, error) { txt := scanner.Text() offset += len(txt) + 1; - log.Println("inv", txt) + log.Println("inv", txt)//XXX if txt == INVENTORY_END { currentInventoryName = nil diff --git a/mapblockparser/parse_test.go b/mapblockparser/parse_test.go index e519d79..ae5f97d 100644 --- a/mapblockparser/parse_test.go +++ b/mapblockparser/parse_test.go @@ -1,6 +1,7 @@ package mapblockparser import ( + "fmt" "testing" "io/ioutil" "strconv" @@ -40,6 +41,7 @@ func TestParse(t *testing.T){ } mapblock, err := Parse(data) + fmt.Println("mapblock.Metadata", mapblock.Metadata) if err != nil { t.Error(err) @@ -56,4 +58,26 @@ func TestParse(t *testing.T){ if len(mapblock.Mapdata) != 16384 { t.Error("Mapdata length wrong") } -} \ No newline at end of file + + pairs := mapblock.Metadata.GetPairsMap(0) + if pairs["owner"] != "pipo" { + t.Error(pairs["owner"]) + } +} + + +func TestParse2(t *testing.T){ + log.SetLevel(log.DebugLevel) + + data, err := ioutil.ReadFile("testdata/0.9.0") + if err != nil { + t.Error(err) + } + + _, err = Parse(data) + //fmt.Println("mapblock.Metadata", mapblock.Metadata) + + if err != nil { + t.Error(err) + } +}