Skip to content

Commit

Permalink
use minetest-go types
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Jun 21, 2024
1 parent d0bad6f commit 8bfd732
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 144 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
20 changes: 0 additions & 20 deletions inventory.go

This file was deleted.

12 changes: 0 additions & 12 deletions item.go

This file was deleted.

38 changes: 0 additions & 38 deletions mapblock.go

This file was deleted.

7 changes: 0 additions & 7 deletions mapdata.go

This file was deleted.

52 changes: 0 additions & 52 deletions metadata.go

This file was deleted.

6 changes: 4 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions parse_block_mapping.go
Original file line number Diff line number Diff line change
@@ -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:]))
Expand Down
6 changes: 4 additions & 2 deletions parse_mapdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 5 additions & 3 deletions parse_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"errors"
"strconv"
"strings"

"github.com/minetest-go/types"
)

/*
Expand All @@ -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]

Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand Down
12 changes: 7 additions & 5 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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")
}

Expand Down

0 comments on commit 8bfd732

Please sign in to comment.