-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
246 lines (208 loc) · 5.73 KB
/
parser.go
File metadata and controls
246 lines (208 loc) · 5.73 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type MapInfo struct {
ID string `json:"id"`
Name string `json:"name"`
PosX float64 `json:"pos_x"`
PosY float64 `json:"pos_y"`
Scale float64 `json:"scale"`
Rotate float64 `json:"rotate"`
VerticalSections map[string]VerticalSection `json:"verticalSections,omitempty"`
}
type VerticalSection struct {
AltitudeMax float64 `json:"altitudeMax"`
AltitudeMin float64 `json:"altitudeMin"`
}
func parseMapConfig(mapName string) (MapInfo, error) {
filePath := fmt.Sprintf("static/cs2-radar-images/%s.txt", mapName)
data, err := os.ReadFile(filePath)
if err != nil {
return MapInfo{}, err
}
content := string(data)
mapInfo := MapInfo{
ID: mapName,
Name: formatMapName(mapName),
PosX: 0,
PosY: 0,
Scale: 1.0,
Rotate: 0,
VerticalSections: make(map[string]VerticalSection),
}
lines := strings.Split(content, "\n")
for i := 0; i < len(lines); i++ {
line := strings.TrimSpace(lines[i])
if line == "" || strings.HasPrefix(line, "//") {
continue
}
if strings.Contains(line, "\"pos_x\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
mapInfo.PosX = val
}
} else if strings.Contains(line, "\"pos_y\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
mapInfo.PosY = val
}
} else if strings.Contains(line, "\"scale\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
mapInfo.Scale = val
}
} else if strings.Contains(line, "\"rotate\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
mapInfo.Rotate = val
}
} else if strings.Contains(line, "\"verticalsections\"") {
i = parseVerticalSections(lines, i, &mapInfo)
}
}
return mapInfo, nil
}
func extractQuotedValue(line string) string {
parts := strings.Split(line, "\"")
if len(parts) >= 4 {
return parts[3]
}
return ""
}
func parseVerticalSections(lines []string, startLine int, mapInfo *MapInfo) int {
lineIndex := startLine
for lineIndex < len(lines) {
line := strings.TrimSpace(lines[lineIndex])
if strings.Contains(line, "{") {
break
}
lineIndex++
}
if lineIndex >= len(lines) {
return lineIndex
}
lineIndex++
braceCount := 1
var currentSection string
var currentAltMin, currentAltMax float64
var inSection bool
for lineIndex < len(lines) && braceCount > 0 {
line := strings.TrimSpace(lines[lineIndex])
if line == "" || strings.HasPrefix(line, "//") {
lineIndex++
continue
}
if strings.Contains(line, "{") {
braceCount++
} else if strings.Contains(line, "}") {
braceCount--
if inSection && braceCount == 1 {
mapInfo.VerticalSections[currentSection] = VerticalSection{
AltitudeMin: currentAltMin,
AltitudeMax: currentAltMax,
}
inSection = false
}
if braceCount == 0 {
return lineIndex
}
}
if !inSection && braceCount == 1 && strings.HasPrefix(line, "\"") {
quoteParts := strings.Split(line, "\"")
if len(quoteParts) >= 2 {
currentSection = quoteParts[1]
inSection = true
currentAltMin = 0
currentAltMax = 0
}
} else if inSection {
if strings.Contains(line, "\"AltitudeMin\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
currentAltMin = val
}
} else if strings.Contains(line, "\"AltitudeMax\"") {
value := extractQuotedValue(line)
if val, err := strconv.ParseFloat(value, 64); err == nil {
currentAltMax = val
}
}
}
lineIndex++
}
return lineIndex
}
func formatMapName(mapID string) string {
displayName := mapID
prefixes := []string{"de_", "cs_", "ar_"}
for _, prefix := range prefixes {
if strings.HasPrefix(mapID, prefix) {
displayName = strings.TrimPrefix(mapID, prefix)
displayName = cases.Title(language.English).String(displayName)
return displayName
}
}
return cases.Title(language.English).String(mapID)
}
func parseManifest(filePath string) ([]string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
lines := strings.Split(string(data), "\n")
var mapIDs []string
for _, line := range lines {
if strings.HasPrefix(line, "//") {
continue
}
if strings.TrimSpace(line) == "" {
continue
}
mapIDs = append(mapIDs, strings.TrimSpace(line))
}
return mapIDs, nil
}
func getAvailableMaps() ([]MapInfo, error) {
var maps []MapInfo
processedMapIDs := make(map[string]bool)
manifestFile := "static/cs2-radar-images/maps"
configFiles, err := parseManifest(manifestFile)
if err != nil {
log.Printf("Error reading manifest file: %v", err)
return nil, err
}
for _, configFile := range configFiles {
baseName := filepath.Base(configFile)
mapID := strings.TrimSuffix(baseName, ".txt")
if processedMapIDs[mapID] {
continue
}
pngPath := fmt.Sprintf("static/cs2-radar-images/%s.png", mapID)
if _, err = os.Stat(pngPath); os.IsNotExist(err) {
log.Printf("Warning: Config file %s exists but no corresponding .png found", baseName)
continue
}
mapInfo, err := parseMapConfig(mapID)
if err != nil {
log.Printf("Warning: Could not parse config for %s: %v", mapID, err)
mapInfo = MapInfo{
ID: mapID,
Name: formatMapName(mapID),
PosX: 0,
PosY: 0,
Scale: 1.0,
}
}
maps = append(maps, mapInfo)
processedMapIDs[mapID] = true
}
return maps, nil
}