diff --git a/go.mod b/go.mod index b7a48ff..42f2bc7 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module mapserver + +require github.com/sirupsen/logrus v1.3.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..4c48ee4 --- /dev/null +++ b/go.sum @@ -0,0 +1,11 @@ +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/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/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= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33 h1:I6FyU15t786LL7oL/hn43zqTuEGr4PN7F4XJ1p4E3Y8= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/mapblockparser/parse.go b/mapblockparser/parse.go new file mode 100644 index 0000000..092c93b --- /dev/null +++ b/mapblockparser/parse.go @@ -0,0 +1,125 @@ +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){ +} + +type CountedReader struct { + reader io.Reader + Count int +} + +func (r *CountedReader) Read(p []byte) (int, error){ + i,err := r.reader.Read(p) + r.Count += i + return i,err +} + +func (r *CountedReader) ReadByte() (byte, error){ + r.Count++; + return r.reader.ReadByte() +} + +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()) + + return cr.Count, nil +} + +func Parse(data []byte) (*MapBlock, error) { + mapblock := MapBlock{} + if len(data) == 0 { + return nil, errors.New("no data") + } + + log.Println("data-length: ", len(data)) + + offset := 0 + + // version + mapblock.Version = data[0] + + //flags + flags := data[1] + mapblock.Underground = (flags & 0x01) == 0x01 + + //mapdata (blocks) + offset = 6 + + //metadata + count, err := parseMapdata(&mapblock, data[offset:]) + if err != nil { + 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 + } + + return &mapblock, nil +} + diff --git a/mapblockparser/parse_test.go b/mapblockparser/parse_test.go new file mode 100644 index 0000000..77800c4 --- /dev/null +++ b/mapblockparser/parse_test.go @@ -0,0 +1,33 @@ +package mapblockparser + +import ( + "testing" + "io/ioutil" + "strconv" + mapblockparser "mapserver/mapblockparser" +) + +func TestParse(t *testing.T){ + data, err := ioutil.ReadFile("testdata/0.0.0") + if err != nil { + t.Error(err) + } + + mapblock, err := mapblockparser.Parse(data) + + if err != nil { + t.Error(err) + } + + if mapblock.Version != 28 { + t.Error("wrong mapblock version: " + strconv.Itoa(int(mapblock.Version))) + } + + if !mapblock.Underground { + t.Error("Underground flag") + } + + if len(mapblock.Mapdata) != 16384 { + t.Error("Mapdata length wrong") + } +} \ No newline at end of file diff --git a/mapblockparser/testdata/0.-1.0 b/mapblockparser/testdata/0.-1.0 new file mode 100644 index 0000000..c3dbcdf Binary files /dev/null and b/mapblockparser/testdata/0.-1.0 differ diff --git a/mapblockparser/testdata/0.0.0 b/mapblockparser/testdata/0.0.0 new file mode 100644 index 0000000..350dc8f Binary files /dev/null and b/mapblockparser/testdata/0.0.0 differ diff --git a/mapblockparser/testdata/0.1.0 b/mapblockparser/testdata/0.1.0 new file mode 100644 index 0000000..af71256 Binary files /dev/null and b/mapblockparser/testdata/0.1.0 differ diff --git a/mapblockparser/testdata/0.10.0 b/mapblockparser/testdata/0.10.0 new file mode 100644 index 0000000..c898a29 Binary files /dev/null and b/mapblockparser/testdata/0.10.0 differ diff --git a/mapblockparser/testdata/0.2.0 b/mapblockparser/testdata/0.2.0 new file mode 100644 index 0000000..84b5a1c Binary files /dev/null and b/mapblockparser/testdata/0.2.0 differ diff --git a/mapblockparser/testdata/0.3.0 b/mapblockparser/testdata/0.3.0 new file mode 100644 index 0000000..64b3bc5 Binary files /dev/null and b/mapblockparser/testdata/0.3.0 differ diff --git a/mapblockparser/testdata/0.4.0 b/mapblockparser/testdata/0.4.0 new file mode 100644 index 0000000..05dc431 Binary files /dev/null and b/mapblockparser/testdata/0.4.0 differ diff --git a/mapblockparser/testdata/0.5.0 b/mapblockparser/testdata/0.5.0 new file mode 100644 index 0000000..92cd4f9 Binary files /dev/null and b/mapblockparser/testdata/0.5.0 differ diff --git a/mapblockparser/testdata/0.6.0 b/mapblockparser/testdata/0.6.0 new file mode 100644 index 0000000..ae4e38e Binary files /dev/null and b/mapblockparser/testdata/0.6.0 differ diff --git a/mapblockparser/testdata/0.7.0 b/mapblockparser/testdata/0.7.0 new file mode 100644 index 0000000..f82c4a5 Binary files /dev/null and b/mapblockparser/testdata/0.7.0 differ diff --git a/mapblockparser/testdata/0.8.0 b/mapblockparser/testdata/0.8.0 new file mode 100644 index 0000000..1dc9d49 Binary files /dev/null and b/mapblockparser/testdata/0.8.0 differ diff --git a/mapblockparser/testdata/0.9.0 b/mapblockparser/testdata/0.9.0 new file mode 100644 index 0000000..fe215a7 Binary files /dev/null and b/mapblockparser/testdata/0.9.0 differ