-
Notifications
You must be signed in to change notification settings - Fork 5
/
parser.go
235 lines (202 loc) · 4.55 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
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
package oppai
import (
"bufio"
"io"
"strings"
)
/* ------------------------------------------------------------- */
/* beatmap parser */
// Parser for storing the status of parsing
type Parser struct {
lastline string // last lines touched
Beatmap *Map
section string
}
func trimSpace(s string) string { return strings.TrimSpace(s) }
func (p *Parser) property() []string {
split := strings.SplitN(p.lastline, ":", 2)
split[0] = trimSpace(split[0])
if len(split) > 1 {
split[1] = trimSpace(split[1])
}
return split
}
func (p *Parser) metadata() {
pr := p.property()
switch pr[0] {
case "Title":
p.Beatmap.Title = pr[1]
case "TitleUnicode":
p.Beatmap.TitleUnicode = pr[1]
case "Artist":
p.Beatmap.Artist = pr[1]
case "ArtistUnicode":
p.Beatmap.ArtistUnicode = pr[1]
case "Creator":
p.Beatmap.Creator = pr[1]
case "Version":
p.Beatmap.Version = pr[1]
}
}
func (p *Parser) general() {
pr := p.property()
if pr[0] == "Mode" {
p.Beatmap.Mode = parseInt(pr[1])
if p.Beatmap.Mode != ModeStd {
panic("this gamemode is not yet supported")
}
}
}
func (p *Parser) difficulty() {
pr := p.property()
switch pr[0] {
case "CircleSize":
p.Beatmap.CS = parseFloat(trimSpace(pr[1]))
case "OverallDifficulty":
p.Beatmap.OD = parseFloat(trimSpace(pr[1]))
case "ApproachRate":
p.Beatmap.AR = parseFloat(trimSpace(pr[1]))
case "HPDrainRate":
p.Beatmap.HP = parseFloat(trimSpace(pr[1]))
case "SliderMultiplier":
p.Beatmap.SV = parseFloat(trimSpace(pr[1]))
case "SliderTickRate":
p.Beatmap.TickRate = parseFloat(trimSpace(pr[1]))
}
}
func (p *Parser) timing() {
s := strings.Split(p.lastline, ",")
if len(s) > 8 {
info("timing point with trailing values")
}
t := Timing{
Time: parseDouble(s[0]),
MsPerBeat: parseDouble(s[1]),
}
if len(s) >= 7 {
t.Change = !(s[6] == "0")
}
p.Beatmap.TPoints = append(p.Beatmap.TPoints, &t)
}
func (p *Parser) objects(s *ObjScanner) {
t0 := parseDouble(unsafeByteToStr(s.GetField()))
t1 := parseDouble(unsafeByteToStr(s.GetField()))
t2 := parseDouble(unsafeByteToStr(s.GetField()))
t3 := parseInt(unsafeByteToStr(s.GetField()))
obj := HitObject{
Time: t2,
Type: t3,
Strains: []float64{0.0, 0.0},
}
if (obj.Type & ObjCircle) != 0 {
p.Beatmap.NCircles++
obj.Data = Circle{
pos: Vector2{
X: t0,
Y: t1,
},
}
} else if (obj.Type & ObjSpinner) != 0 {
p.Beatmap.NSpinners++
} else if (obj.Type & ObjSlider) != 0 {
s.GetField()
s.GetField()
p.Beatmap.NSliders++
obj.Data = Slider{
pos: Vector2{
X: t0,
Y: t1,
},
repetitions: parseInt(unsafeByteToStr(s.GetField())),
distance: parseDouble(unsafeByteToStr(s.GetField())),
}
}
p.Beatmap.Objects = append(p.Beatmap.Objects, &obj)
}
// Map returns the beatmap info
func (p *Parser) Map(reader io.Reader) *Map {
var line string
objScanner := &ObjScanner{}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line = scanner.Text()
p.lastline = line
if strings.HasPrefix(line, " ") ||
strings.HasPrefix(line, "_") {
continue
}
p.lastline = strings.TrimSpace(line)
line = p.lastline
if len(line) <= 0 {
continue
}
if strings.HasPrefix(line, "//") {
continue
}
// [SectionName]
if strings.HasPrefix(line, "[") {
p.section = line[1 : len(line)-1]
continue
}
switch p.section {
case "Metadata":
p.metadata()
case "General":
p.general()
case "Difficulty":
p.difficulty()
case "TimingPoints":
p.timing()
case "HitObjects":
objScanner.SetSource(scanner.Bytes())
p.objects(objScanner)
}
}
return p.Beatmap
}
// Map returns the beatmap info with a specified number of objects parsed
func (p *Parser) MapbyNum(reader io.Reader, objnum int) *Map {
num := objnum
var line string
objScanner := &ObjScanner{}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line = scanner.Text()
p.lastline = line
if strings.HasPrefix(line, " ") ||
strings.HasPrefix(line, "_") {
continue
}
p.lastline = strings.TrimSpace(line)
line = p.lastline
if len(line) <= 0 {
continue
}
if strings.HasPrefix(line, "//") {
continue
}
// [SectionName]
if strings.HasPrefix(line, "[") {
p.section = line[1 : len(line)-1]
continue
}
switch p.section {
case "Metadata":
p.metadata()
case "General":
p.general()
case "Difficulty":
p.difficulty()
case "TimingPoints":
p.timing()
case "HitObjects":
objScanner.SetSource(scanner.Bytes())
p.objects(objScanner)
num -= 1
if num == 0 {
return p.Beatmap
}
}
}
return p.Beatmap
}