Skip to content

Commit

Permalink
simplify inventory/meta parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Sep 3, 2024
1 parent 4869e10 commit a150fe5
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 58 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ go 1.22.4

require github.com/klauspost/compress v1.16.0

require github.com/minetest-go/types v1.0.4
require github.com/minetest-go/types v1.0.7
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/minetest-go/types v1.0.2 h1:lHU9frnlgtWO3XFIVxO5X3Gi+y07gV+guREglqnwD
github.com/minetest-go/types v1.0.2/go.mod h1:QS2q8tKAiuBe2Rin880ARFt9VokYIJNZV4Z4kHAChU0=
github.com/minetest-go/types v1.0.4 h1:SuM1UPXM2ocngJqZwlxZjnQVJEFxeMyk4bqlEw1BAj4=
github.com/minetest-go/types v1.0.4/go.mod h1:QS2q8tKAiuBe2Rin880ARFt9VokYIJNZV4Z4kHAChU0=
github.com/minetest-go/types v1.0.7 h1:McQVnHQysqXegJbHnVl66TOS/172yi5b70sosWRCvuk=
github.com/minetest-go/types v1.0.7/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=
Expand Down
15 changes: 6 additions & 9 deletions parse_mapdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ func parseMapdata(rawdata []byte, mapblock *types.MapBlock) error {
return fmt.Errorf("mapdata length invalid: %d", len(rawdata))
}

mapd := types.MapData{
ContentId: make([]int, 4096),
Param1: make([]int, 4096),
Param2: make([]int, 4096),
}
mapblock.Mapdata = &mapd
mapblock.ContentId = make([]int, 4096)
mapblock.Param1 = make([]int, 4096)
mapblock.Param2 = make([]int, 4096)

for i := 0; i < 4096; i++ {
mapd.ContentId[i] = int(binary.BigEndian.Uint16(rawdata[i*2:]))
mapd.Param1[i] = int(rawdata[(4096*2)+i])
mapd.Param2[i] = int(rawdata[(4096*3)+i])
mapblock.ContentId[i] = int(binary.BigEndian.Uint16(rawdata[i*2:]))
mapblock.Param1[i] = int(rawdata[(4096*2)+i])
mapblock.Param2[i] = int(rawdata[(4096*3)+i])
}

return nil
Expand Down
52 changes: 20 additions & 32 deletions parse_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
INVENTORY_TERMINATOR = "EndInventory"
INVENTORY_END = "EndInventoryList"
INVENTORY_START = "List"
ITEM_PREFIX = "Item"
ITEM_EMPTY = "Empty"
)

func parseMetadata(metadata []byte, mapblock *types.MapBlock) error {
Expand All @@ -37,7 +39,8 @@ func parseMetadata(metadata []byte, mapblock *types.MapBlock) error {

for i := 0; i < count; i++ {
position := int(binary.BigEndian.Uint16(metadata[offset:]))
pairsMap := mapblock.Metadata.GetPairsMap(position)
fields := map[string]string{}
mapblock.Fields[position] = fields

offset += 2
valuecount := int(binary.BigEndian.Uint32(metadata[offset:]))
Expand All @@ -61,15 +64,15 @@ func parseMetadata(metadata []byte, mapblock *types.MapBlock) error {
value := string(metadata[offset : valueLength+offset])
offset += valueLength

pairsMap[key] = value
fields[key] = value

if version >= 2 { /* private tag doesn't exist in version=1 */
offset++
}
}

var currentInventoryName *string
var currentInventory *types.Inventory
var currentInventory []string

scanner := bufio.NewScanner(bytes.NewReader(metadata[offset:]))
for scanner.Scan() {
Expand All @@ -79,42 +82,27 @@ func parseMetadata(metadata []byte, mapblock *types.MapBlock) error {
if strings.HasPrefix(txt, INVENTORY_START) {
pairs := strings.Split(txt, " ")
currentInventoryName = &pairs[1]
currentInventory = mapblock.Metadata.GetInventory(position, *currentInventoryName)
currentInventory.Size = 0
currentInventory = make([]string, 0)
inv := mapblock.Inventory[position]
if inv == nil {
inv = map[string][]string{}
mapblock.Inventory[position] = inv
}
inv[*currentInventoryName] = currentInventory

} else if txt == INVENTORY_END {
currentInventoryName = nil
currentInventory = nil
} else if currentInventory != nil {
//content
if strings.HasPrefix(txt, "Item") {
item := types.Item{}
parts := strings.Split(txt, " ")

if len(parts) >= 2 {
item.Name = parts[1]
}

if len(parts) >= 3 {
val, err := strconv.ParseInt(parts[2], 10, 32)
if err != nil {
return err
}
item.Count = int(val)
}

if len(parts) >= 4 {
val, err := strconv.ParseInt(parts[3], 10, 32)
if err != nil {
return err
}
item.Count = int(val)
}

currentInventory.Items = append(currentInventory.Items, &item)
currentInventory.Size += 1

inv := mapblock.Inventory[position]
if strings.HasPrefix(txt, ITEM_PREFIX) {
item := txt[len(ITEM_PREFIX)+1:]
currentInventory = append(currentInventory, item)
} else if txt == ITEM_EMPTY {
currentInventory = append(currentInventory, "")
}
inv[*currentInventoryName] = currentInventory

} else if txt == INVENTORY_TERMINATOR {
break
Expand Down
27 changes: 11 additions & 16 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,34 @@ func validateMapblock(t *testing.T, mapblock *types.MapBlock) {
return
}

if mapblock.Mapdata == nil {
t.Error("mapdata is nil")
return
}

if mapblock.Mapdata.ContentId == nil {
if mapblock.ContentId == nil {
t.Error("contentid is nil")
return
}

if mapblock.Mapdata.Param1 == nil {
if mapblock.Param1 == nil {
t.Error("param1 is nil")
return
}

if mapblock.Mapdata.Param2 == nil {
if mapblock.Param2 == nil {
t.Error("param2 is nil")
return
}

if len(mapblock.Mapdata.ContentId) != 4096 {
if len(mapblock.ContentId) != 4096 {
t.Error("invalid contentid size")
}

if len(mapblock.Mapdata.Param1) != 4096 {
if len(mapblock.Param1) != 4096 {
t.Error("invalid param1 size")
}

if len(mapblock.Mapdata.Param2) != 4096 {
if len(mapblock.Param2) != 4096 {
t.Error("invalid param2 size")
}

for _, nodeid := range mapblock.Mapdata.ContentId {
for _, nodeid := range mapblock.ContentId {
nodename := mapblock.BlockMapping[nodeid]
if nodename == "" {
t.Errorf("Nodename not found for id: %d", nodeid)
Expand Down Expand Up @@ -92,19 +87,19 @@ func TestParse(t *testing.T) {
t.Error("Underground flag")
}

if len(mapblock.Mapdata.ContentId) != 4096 {
if len(mapblock.ContentId) != 4096 {
t.Error("Mapdata length wrong")
}

if len(mapblock.Mapdata.Param2) != 4096 {
if len(mapblock.Param2) != 4096 {
t.Error("Mapdata length wrong")
}

if len(mapblock.Mapdata.Param1) != 4096 {
if len(mapblock.Param1) != 4096 {
t.Error("Mapdata length wrong")
}

pairs := mapblock.Metadata.GetPairsMap(0)
pairs := mapblock.Fields[0]
if pairs["owner"] != "pipo" {
t.Error(pairs["owner"])
}
Expand Down

0 comments on commit a150fe5

Please sign in to comment.