mapserver/mapblockparser/parse.go

88 lines
1.5 KiB
Go
Raw Normal View History

2019-01-05 23:35:20 +03:00
package mapblockparser
import (
"errors"
"strconv"
)
2019-01-18 15:07:01 +03:00
func Parse(data []byte, mtime int64) (*MapBlock, error) {
2019-01-07 21:38:53 +03:00
mapblock := NewMapblock()
2019-01-18 15:07:01 +03:00
mapblock.Mtime = mtime
2019-01-05 23:35:20 +03:00
if len(data) == 0 {
return nil, errors.New("no data")
}
offset := 0
// version
mapblock.Version = data[0]
//flags
flags := data[1]
mapblock.Underground = (flags & 0x01) == 0x01
2019-01-06 23:00:24 +03:00
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)))
}
2019-01-05 23:35:20 +03:00
//mapdata (blocks)
offset = 6
//metadata
count, err := parseMapdata(&mapblock, data[offset:])
if err != nil {
return nil, err
}
offset += count
count, err = parseMetadata(&mapblock, data[offset:])
if err != nil {
return nil, err
}
2019-01-07 21:53:05 +03:00
offset += count
//static objects
offset++ //static objects version
staticObjectsCount := readU16(data, offset)
offset += 2
for i := 0; i < staticObjectsCount; i++ {
offset += 13
dataSize := readU16(data, offset)
offset += dataSize + 2
}
//timestamp
offset += 4
//mapping version
offset++
numMappings := readU16(data, offset)
offset += 2
for i := 0; i < numMappings; i++ {
nodeId := readU16(data, offset)
offset += 2
nameLen := readU16(data, offset)
offset += 2
blockName := string(data[offset : offset+nameLen])
offset += nameLen
mapblock.BlockMapping[nodeId] = blockName
}
2019-01-05 23:35:20 +03:00
return &mapblock, nil
}