diff --git a/go.mod b/go.mod index 0610360..9fd523a 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,7 @@ module github.com/minetest-go/mapparser -go 1.22 +go 1.22.4 require github.com/klauspost/compress v1.16.0 + +require github.com/minetest-go/types v1.0.2 diff --git a/go.sum b/go.sum index 5ee70e3..3ddf8d4 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +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/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4= github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/minetest-go/types v1.0.2 h1:lHU9frnlgtWO3XFIVxO5X3Gi+y07gV+guREglqnwDJM= +github.com/minetest-go/types v1.0.2/go.mod h1:QS2q8tKAiuBe2Rin880ARFt9VokYIJNZV4Z4kHAChU0= +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/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/inventory.go b/inventory.go deleted file mode 100644 index b275895..0000000 --- a/inventory.go +++ /dev/null @@ -1,20 +0,0 @@ -package mapparser - -type Inventory struct { - Size int `json:"size"` - Items []*Item `json:"items"` -} - -func (inv *Inventory) IsEmpty() bool { - if len(inv.Items) == 0 { - return true - } - - for _, item := range inv.Items { - if item.Name != "" && item.Count > 0 { - return false - } - } - - return true -} diff --git a/item.go b/item.go deleted file mode 100644 index 2aa3f40..0000000 --- a/item.go +++ /dev/null @@ -1,12 +0,0 @@ -package mapparser - -type Item struct { - Name string `json:"name"` - Count int `json:"count"` - Wear int `json:"wear"` - //TODO: metadata -} - -func (i *Item) IsEmpty() bool { - return i.Name == "" && i.Count == 0 -} diff --git a/mapblock.go b/mapblock.go deleted file mode 100644 index ed975ec..0000000 --- a/mapblock.go +++ /dev/null @@ -1,38 +0,0 @@ -package mapparser - -type MapBlock struct { - Size int `json:"size"` - Version byte `json:"version"` - Underground bool `json:"underground"` - Timestamp uint32 `json:"timestamp"` - Mapdata *MapData `json:"mapdata"` - Metadata *Metadata `json:"metadata"` - BlockMapping map[int]string `json:"blockmapping"` -} - -func NewMapblock() *MapBlock { - mb := MapBlock{} - mb.Metadata = NewMetadata() - mb.BlockMapping = make(map[int]string) - return &mb -} - -// returns true if the mapblock is empty (air-only) -func (mb *MapBlock) IsEmpty() bool { - return len(mb.BlockMapping) == 0 -} - -func (mb *MapBlock) GetNodeId(x, y, z int) int { - pos := GetNodePos(x, y, z) - return mb.Mapdata.ContentId[pos] -} - -func (mb *MapBlock) GetParam2(x, y, z int) int { - pos := GetNodePos(x, y, z) - return mb.Mapdata.Param2[pos] -} - -func (mb *MapBlock) GetNodeName(x, y, z int) string { - id := mb.GetNodeId(x, y, z) - return mb.BlockMapping[id] -} diff --git a/mapdata.go b/mapdata.go deleted file mode 100644 index c1eab55..0000000 --- a/mapdata.go +++ /dev/null @@ -1,7 +0,0 @@ -package mapparser - -type MapData struct { - ContentId []int `json:"contentid"` - Param1 []int `json:"param1"` - Param2 []int `json:"param2"` -} diff --git a/metadata.go b/metadata.go deleted file mode 100644 index 325a538..0000000 --- a/metadata.go +++ /dev/null @@ -1,52 +0,0 @@ -package mapparser - -type Metadata struct { - Inventories map[int]map[string]*Inventory `json:"inventories"` - Pairs map[int]map[string]string `json:"pairs"` -} - -func NewMetadata() *Metadata { - md := Metadata{} - md.Inventories = make(map[int]map[string]*Inventory) - md.Pairs = make(map[int]map[string]string) - return &md -} - -func (md *Metadata) GetMetadata(x, y, z int) map[string]string { - return md.GetPairsMap(GetNodePos(x, y, z)) -} - -func (md *Metadata) GetPairsMap(pos int) map[string]string { - pairsMap := md.Pairs[pos] - if pairsMap == nil { - pairsMap = make(map[string]string) - md.Pairs[pos] = pairsMap - } - - return pairsMap -} - -func (md *Metadata) GetInventoryMap(pos int) map[string]*Inventory { - invMap := md.Inventories[pos] - if invMap == nil { - invMap = make(map[string]*Inventory) - md.Inventories[pos] = invMap - } - - return invMap -} - -func (md *Metadata) GetInventoryMapAtPos(x, y, z int) map[string]*Inventory { - return md.GetInventoryMap(GetNodePos(x, y, z)) -} - -func (md *Metadata) GetInventory(pos int, name string) *Inventory { - m := md.GetInventoryMap(pos) - inv := m[name] - if inv == nil { - inv = &Inventory{} - m[name] = inv - } - - return inv -} diff --git a/parse.go b/parse.go index 350805b..446d4f4 100644 --- a/parse.go +++ b/parse.go @@ -4,17 +4,19 @@ import ( "encoding/binary" "errors" "strconv" + + "github.com/minetest-go/types" ) var ErrNoData = errors.New("no data") var ErrMapblockVersion = errors.New("mapblock version unsupported") -func Parse(data []byte) (*MapBlock, error) { +func Parse(data []byte) (*types.MapBlock, error) { if len(data) == 0 { return nil, ErrNoData } - mapblock := NewMapblock() + mapblock := types.NewMapblock() mapblock.Size = len(data) // version diff --git a/parse_block_mapping.go b/parse_block_mapping.go index 7cc8a70..5415fbf 100644 --- a/parse_block_mapping.go +++ b/parse_block_mapping.go @@ -1,8 +1,12 @@ package mapparser -import "encoding/binary" +import ( + "encoding/binary" -func parseBlockMapping(data []byte, offset *int, mapblock *MapBlock) { + "github.com/minetest-go/types" +) + +func parseBlockMapping(data []byte, offset *int, mapblock *types.MapBlock) { if len(data) > (*offset + 2) { // disk-data has per-block mapping, network-data has a global mapping numMappings := int(binary.BigEndian.Uint16(data[*offset:])) diff --git a/parse_mapdata.go b/parse_mapdata.go index 9ffd64a..61e331c 100644 --- a/parse_mapdata.go +++ b/parse_mapdata.go @@ -3,16 +3,18 @@ package mapparser import ( "encoding/binary" "fmt" + + "github.com/minetest-go/types" ) const MapDataSize = 16384 -func parseMapdata(rawdata []byte, mapblock *MapBlock) error { +func parseMapdata(rawdata []byte, mapblock *types.MapBlock) error { if len(rawdata) < MapDataSize { return fmt.Errorf("mapdata length invalid: %d", len(rawdata)) } - mapd := MapData{ + mapd := types.MapData{ ContentId: make([]int, 4096), Param1: make([]int, 4096), Param2: make([]int, 4096), diff --git a/parse_metadata.go b/parse_metadata.go index 7700b4e..c61b24e 100644 --- a/parse_metadata.go +++ b/parse_metadata.go @@ -7,6 +7,8 @@ import ( "errors" "strconv" "strings" + + "github.com/minetest-go/types" ) /* @@ -19,7 +21,7 @@ const ( INVENTORY_START = "List" ) -func parseMetadata(metadata []byte, mapblock *MapBlock) error { +func parseMetadata(metadata []byte, mapblock *types.MapBlock) error { offset := 0 version := metadata[offset] @@ -67,7 +69,7 @@ func parseMetadata(metadata []byte, mapblock *MapBlock) error { } var currentInventoryName *string - var currentInventory *Inventory + var currentInventory *types.Inventory scanner := bufio.NewScanner(bytes.NewReader(metadata[offset:])) for scanner.Scan() { @@ -86,7 +88,7 @@ func parseMetadata(metadata []byte, mapblock *MapBlock) error { } else if currentInventory != nil { //content if strings.HasPrefix(txt, "Item") { - item := Item{} + item := types.Item{} parts := strings.Split(txt, " ") if len(parts) >= 2 { diff --git a/parse_test.go b/parse_test.go index b8d9c6a..ae995db 100644 --- a/parse_test.go +++ b/parse_test.go @@ -6,9 +6,11 @@ import ( "io/ioutil" "strconv" "testing" + + "github.com/minetest-go/types" ) -func validateMapblock(t *testing.T, mapblock *MapBlock) { +func validateMapblock(t *testing.T, mapblock *types.MapBlock) { if mapblock == nil { t.Error("no data") return @@ -49,7 +51,7 @@ func validateMapblock(t *testing.T, mapblock *MapBlock) { for _, nodeid := range mapblock.Mapdata.ContentId { nodename := mapblock.BlockMapping[nodeid] if nodename == "" { - t.Error(fmt.Sprintf("Nodename not found for id: %d", nodeid)) + t.Errorf("Nodename not found for id: %d", nodeid) } } @@ -107,15 +109,15 @@ func TestParse(t *testing.T) { t.Error(pairs["owner"]) } - if mapblock.GetNodeId(0, 0, 0) != 0 { + if mapblock.GetNodeId(types.NewPos(0, 0, 0)) != 0 { t.Error("nodeid mismatch") } - if mapblock.GetNodeName(0, 0, 0) != "travelnet:travelnet" { + if mapblock.GetNodeName(types.NewPos(0, 0, 0)) != "travelnet:travelnet" { t.Error("nodename mismatch") } - if mapblock.GetParam2(0, 0, 0) != 0 { + if mapblock.GetParam2(types.NewPos(0, 0, 0)) != 0 { t.Error("param2 mismatch") }