Repository navigation
Expand file tree
/
Copy pathparse_netwalk.go
More file actions
104 lines (92 loc) · 2.57 KB
/
Copy pathparse_netwalk.go
File metadata and controls
104 lines (92 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pdfexport
import (
"encoding/json"
"fmt"
"strings"
)
type netwalkSave struct {
Size int `json:"size"`
Masks string `json:"masks"`
InitialRotations string `json:"initial_rotations"`
Kinds string `json:"kinds"`
}
func ParseNetwalkPrintData(saveData []byte) (*NetwalkData, error) {
if len(strings.TrimSpace(string(saveData))) == 0 {
return nil, nil
}
var save netwalkSave
if err := json.Unmarshal(saveData, &save); err != nil {
return nil, fmt.Errorf("decode netwalk save: %w", err)
}
if save.Size <= 0 {
return nil, nil
}
maskRows, err := parseNetwalkRows(save.Size, save.Masks)
if err != nil {
return nil, fmt.Errorf("parse netwalk masks: %w", err)
}
rotationRows, err := parseNetwalkRows(save.Size, save.InitialRotations)
if err != nil {
return nil, fmt.Errorf("parse netwalk rotations: %w", err)
}
kindRows, err := parseNetwalkRows(save.Size, save.Kinds)
if err != nil {
return nil, fmt.Errorf("parse netwalk kinds: %w", err)
}
data := &NetwalkData{
Size: save.Size,
Masks: make([][]uint8, save.Size),
Rotations: make([][]uint8, save.Size),
RootX: -1,
RootY: -1,
}
for y := 0; y < save.Size; y++ {
data.Masks[y] = make([]uint8, save.Size)
data.Rotations[y] = make([]uint8, save.Size)
for x := 0; x < save.Size; x++ {
mask, ok := parseHexNibble(maskRows[y][x])
if !ok {
return nil, fmt.Errorf("invalid mask value %q at (%d,%d)", maskRows[y][x], x, y)
}
if rotationRows[y][x] < '0' || rotationRows[y][x] > '3' {
return nil, fmt.Errorf("invalid rotation value %q at (%d,%d)", rotationRows[y][x], x, y)
}
data.Masks[y][x] = mask
data.Rotations[y][x] = rotationRows[y][x] - '0'
if kindRows[y][x] == 'S' {
data.RootX = x
data.RootY = y
}
}
}
if data.RootX < 0 || data.RootY < 0 {
return nil, fmt.Errorf("netwalk print data missing root")
}
return data, nil
}
func parseNetwalkRows(size int, raw string) ([][]byte, error) {
rows := splitNormalizedLines(raw)
if len(rows) != size {
return nil, fmt.Errorf("row count = %d, want %d", len(rows), size)
}
result := make([][]byte, size)
for y, row := range rows {
if len(row) != size {
return nil, fmt.Errorf("row %d width = %d, want %d", y, len(row), size)
}
result[y] = []byte(row)
}
return result, nil
}
func parseHexNibble(value byte) (uint8, bool) {
switch {
case value >= '0' && value <= '9':
return value - '0', true
case value >= 'a' && value <= 'f':
return 10 + value - 'a', true
case value >= 'A' && value <= 'F':
return 10 + value - 'A', true
default:
return 0, false
}
}