partially working metadata parser

This commit is contained in:
Thomas Rudin 2019-01-06 21:00:24 +01:00
parent 730fb15248
commit 38787bcca2
6 changed files with 158 additions and 65 deletions

3
go.sum
View File

@ -1,9 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.3.0 h1:hI/7Q+DtNZ2kINb6qt/lS+IyXnHQe9e90POfeewL/ME=
github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793 h1:u+LnwYTOOW7Ukr/fppxEb1Nwz0AtPflrblfvUudpo+I=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

View File

@ -0,0 +1,9 @@
package mapblockparser
type MapBlock struct {
Version byte
Underground bool
Mapdata []byte
Metadata Metadata
}

42
mapblockparser/mapdata.go Normal file
View File

@ -0,0 +1,42 @@
package mapblockparser
import (
"errors"
"compress/zlib"
"bytes"
"io"
"strconv"
log "github.com/sirupsen/logrus"
)
//TODO: mapdata struct with accessors
func parseMapdata(mapblock *MapBlock, data []byte) (int, error) {
log.WithFields(log.Fields{
"data-length": len(data),
}).Debug("Parsing map-data")
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.Reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
if buf.Len() != 16384 {
return 0, errors.New("Mapdata length invalid: " + strconv.Itoa(buf.Len()))
}
mapblock.Mapdata = buf.Bytes()
return cr.Count, nil
}

View File

@ -0,0 +1,87 @@
package mapblockparser
import (
"compress/zlib"
"bytes"
"errors"
"strconv"
"io"
log "github.com/sirupsen/logrus"
)
type Metadata struct {
Inventories map[int]map[string]Inventory
Pairs map[int]map[string]string
}
type Item struct {
Name string
Count int
Wear int
}
type Inventory struct {
Size int
Items []Item
}
func readU16(data []byte, offset int) int {
return (int(data[offset]) << 8) | int(data[offset + 1])
}
func readU32(data []byte, offset int){
}
func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
log.WithFields(log.Fields{
"data-length": len(data),
}).Debug("Parsing metadata")
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.Reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
metadata := buf.Bytes()
offset := 0
version := metadata[offset]
if version != 2 {
return 0, errors.New("Wrong metadata version: " + strconv.Itoa(int(version)))
}
offset++;
count := readU16(metadata, offset)
log.WithFields(log.Fields{
"count": count,
"version": version,
}).Debug("Parsed metadata-header")
offset+=2
log.Println("Metadata", buf.String())//XXX
for i := 0; i < count; i++ {
position := readU16(metadata, offset);
log.Println("MD item", i, position)//XXX
offset+=2
//TODO
}
return cr.Count, nil
}

View File

@ -2,71 +2,11 @@ package mapblockparser
import (
"errors"
"compress/zlib"
"bytes"
"io"
"strconv"
log "github.com/sirupsen/logrus"
)
type MapBlock struct {
Version byte
Underground bool
Mapdata []byte
}
func readU16(data []byte, offset int){
}
func readU32(data []byte, offset int){
}
func parseMapdata(mapblock *MapBlock, data []byte) (int, error) {
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.Reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
if buf.Len() != 16384 {
return 0, errors.New("Mapdata length invalid: " + strconv.Itoa(buf.Len()))
}
mapblock.Mapdata = buf.Bytes()
return cr.Count, nil
}
func parseMetadata(mapblock *MapBlock, data []byte) (int, error) {
r := bytes.NewReader(data)
cr := new(CountedReader)
cr.Reader = r
z, err := zlib.NewReader(cr)
if err != nil {
return 0, err
}
defer z.Close()
buf := new(bytes.Buffer)
io.Copy(buf, z)
log.Println("Metadata length ", buf.Len(), buf.String())
return cr.Count, nil
}
func Parse(data []byte) (*MapBlock, error) {
mapblock := MapBlock{}
@ -74,7 +14,9 @@ func Parse(data []byte) (*MapBlock, error) {
return nil, errors.New("no data")
}
log.Println("data-length: ", len(data))
log.WithFields(log.Fields{
"data-length": len(data),
}).Debug("Parsing mapblock")
offset := 0
@ -85,6 +27,17 @@ func Parse(data []byte) (*MapBlock, error) {
flags := data[1]
mapblock.Underground = (flags & 0x01) == 0x01
content_width := data[4]
params_width := data[4]
if content_width != 2 {
return nil, errors.New("content_width = " + strconv.Itoa(int(content_width)))
}
if params_width != 2 {
return nil, errors.New("params_width = " + strconv.Itoa(int(params_width)))
}
//mapdata (blocks)
offset = 6
@ -94,12 +47,8 @@ func Parse(data []byte) (*MapBlock, error) {
return nil, err
}
log.Println("Mapdata length: ", count)
offset += count
log.Println("New offset: ", offset)
count, err = parseMetadata(&mapblock, data[offset:])
if err != nil {
return nil, err

View File

@ -5,9 +5,12 @@ import (
"io/ioutil"
"strconv"
mapblockparser "mapserver/mapblockparser"
log "github.com/sirupsen/logrus"
)
func TestParse(t *testing.T){
log.SetLevel(log.DebugLevel)
data, err := ioutil.ReadFile("testdata/0.0.0")
if err != nil {
t.Error(err)