mapserver/server/mapblockparser/parse.go

106 lines
1.9 KiB
Go
Raw Normal View History

2019-01-05 23:35:20 +03:00
package mapblockparser
import (
"errors"
2019-02-15 11:08:22 +03:00
"github.com/prometheus/client_golang/prometheus"
2019-01-21 13:31:50 +03:00
"mapserver/coords"
2019-01-21 15:45:35 +03:00
"strconv"
2019-01-05 23:35:20 +03:00
)
2019-02-06 10:38:08 +03:00
func Parse(data []byte, mtime int64, pos *coords.MapBlockCoords) (*MapBlock, error) {
2019-01-05 23:35:20 +03:00
if len(data) == 0 {
return nil, errors.New("no data")
}
2019-02-15 10:36:48 +03:00
timer := prometheus.NewTimer(parseDuration)
2019-02-15 11:08:22 +03:00
defer timer.ObserveDuration()
2019-02-15 10:36:48 +03:00
2019-01-18 17:18:42 +03:00
mapblock := NewMapblock()
mapblock.Mtime = mtime
2019-01-21 15:45:35 +03:00
mapblock.Pos = pos
2019-01-18 17:18:42 +03:00
mapblock.Size = len(data)
2019-01-05 23:35:20 +03:00
offset := 0
// version
mapblock.Version = data[0]
if mapblock.Version < 25 || mapblock.Version > 28 {
2019-02-01 16:50:39 +03:00
return nil, errors.New("mapblock-version not supported: " + strconv.Itoa(int(mapblock.Version)))
}
2019-01-05 23:35:20 +03:00
//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)
if mapblock.Version >= 27 {
offset = 6
} else {
offset = 4
2019-02-01 16:50:39 +03:00
}
2019-01-05 23:35:20 +03:00
//metadata
2019-01-22 15:17:30 +03:00
count, err := parseMapdata(mapblock, data[offset:])
2019-01-05 23:35:20 +03:00
if err != nil {
return nil, err
}
offset += count
2019-01-22 15:17:30 +03:00
count, err = parseMetadata(mapblock, data[offset:])
2019-01-05 23:35:20 +03:00
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-02-15 10:36:48 +03:00
parsedMapBlocks.Inc()
2019-01-22 15:17:30 +03:00
return mapblock, nil
2019-01-05 23:35:20 +03:00
}