mapserver/server/mapblockparser/parse.go

91 lines
1.6 KiB
Go
Raw Normal View History

2019-01-05 23:35:20 +03:00
package mapblockparser
import (
"errors"
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-01-21 13:31:50 +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-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]
//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
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-01-22 15:17:30 +03:00
return mapblock, nil
2019-01-05 23:35:20 +03:00
}