-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
169 lines (148 loc) · 4.1 KB
/
parser.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package gogogadget_navmesh
import (
"errors"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"sync"
)
type NavMeshLoader struct {
id int
navMeshes sync.Map
}
// Loads a NavMesh from a Path. This should point to a Godot Scene file
// If no settings for the NavMesh are passed, default values are used.
func (nml *NavMeshLoader) LoadFromPath(path string, settings *NavMeshSettings) error {
nm, err := nml.loadAndParse(path)
if err != nil {
return err
}
var findErr error = nil
nml.navMeshes.Range(func(key any, value any) bool {
n := value.(NavMesh)
if n.FileName == nm.FileName {
findErr = errors.New("navmesh with filename " + nm.FileName + " already exists")
return false
}
return true
})
if findErr != nil {
return findErr
}
if err := nm.initialize(settings); err != nil {
return err
}
nml.navMeshes.Store(nm.FileName, nm)
return nil
}
// Loads a NavMesh from a Path with a name. This should point to a Godot Scene file
// If no settings for the NavMesh are passed, default values are used.
func (nml *NavMeshLoader) LoadFromPathName(path string, settings *NavMeshSettings, name string) error {
nm, err := nml.loadAndParse(path)
if err != nil {
return err
}
var findErr error = nil
nml.navMeshes.Range(func(key any, value any) bool {
n := value.(NavMesh)
if n.FileName == nm.FileName {
findErr = errors.New("navmesh with filename " + nm.FileName + " already exists")
return false
}
return true
})
if findErr != nil {
return findErr
}
if err := nm.initialize(settings); err != nil {
return err
}
nm.ShortName = name
nml.navMeshes.Store(nm.ShortName, nm)
return nil
}
func (nml *NavMeshLoader) GetByName(name string) (*NavMesh, error) {
if len(name) == 0 {
return nil, errors.New("name can't be empty")
}
var nm *NavMesh
nml.navMeshes.Range(func(key any, value any) bool {
n := value.(NavMesh)
if n.ShortName == name {
nm = &n
return false
}
return true
})
if nm == nil {
return nil, errors.New("navmesh " + name + " not found")
}
return nm, nil
}
func (nml *NavMeshLoader) GetById(id int) (*NavMesh, error) {
if id <= 0 {
return nil, errors.New("id can't be <= 0")
}
var nm *NavMesh
nml.navMeshes.Range(func(key any, value any) bool {
n := value.(NavMesh)
if n.ID == id {
nm = &n
return false
}
return true
})
if nm == nil {
return nil, errors.New("navmesh " + fmt.Sprint(id) + " not found")
}
return nm, nil
}
func (nml *NavMeshLoader) loadAndParse(path string) (NavMesh, error) {
file, err := os.Open(path)
if err != nil {
return NavMesh{}, err
}
defer file.Close()
data, err := io.ReadAll(file)
if err != nil {
return NavMesh{}, err
}
sceneData := string(data)
// Define regular expressions to match the relevant portion
navMeshRegex := regexp.MustCompile(`\[sub_resource type="NavigationMesh" id="NavigationMesh_unj38"\]\n(\s*)vertices = PackedVector3Array\(([\s\S]*?)\)\n(\s*)polygons = \[([\s\S]*?)\]`)
match := navMeshRegex.FindStringSubmatch(sceneData)
if len(match) != 5 {
return NavMesh{}, errors.New("navigationMesh sub-resource not found")
}
// Parse vertices
vertexStr := strings.TrimSpace(match[2])
vertexValues := strings.Split(vertexStr, ", ")
vertices := make([]Vertex, len(vertexValues)/3)
for i := 0; i < len(vertexValues); i += 3 {
x, _ := strconv.ParseFloat(vertexValues[i], 64)
y, _ := strconv.ParseFloat(vertexValues[i+1], 64)
z, _ := strconv.ParseFloat(vertexValues[i+2], 64)
vertices[i/3] = Vertex{X: x, Y: y, Z: z}
}
// Parse triangles
polygonStr := strings.TrimSpace(match[4])
polygonValues := strings.Split(polygonStr, "), ")
triangles := make([][3]int, 0)
for index, ele := range polygonValues {
cleaned := ele[17:]
if index == len(polygonValues)-1 {
cleaned = cleaned[:len(cleaned)-1]
}
vals := strings.Split(cleaned, ", ")
x, _ := strconv.ParseInt(vals[0], 10, 64)
y, _ := strconv.ParseInt(vals[1], 10, 64)
z, _ := strconv.ParseInt(vals[2], 10, 64)
triangle := [3]int{int(x), int(y), int(z)}
triangles = append(triangles, triangle)
}
nml.id++
return NavMesh{Vertices: vertices, Triangles: triangles, FileName: path, IsInitialized: false, ID: nml.id}, nil
}