-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_file_data.go
220 lines (200 loc) · 7.16 KB
/
read_file_data.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
package main
import (
"github.com/fatih/color"
"regexp"
"sort"
"strings"
)
var (
// ignore mines....for now.
// Quaver might implement this in the future
//mineRegex = regexp.MustCompile("[d][1-9]")
noteRegex = regexp.MustCompile("[1][1-9]")
player2NoteRegex = regexp.MustCompile("[2][1-9]")
lnRegex = regexp.MustCompile("[5][1-z]")
player2LnRegex = regexp.MustCompile("[6][1-9]")
)
// ReadFileData converts from BMS to a ConvertedFile. Returns a ConvertedFile, whether file was skipped or not, and an error if it errored.
func (conf *ProgramConfig) ReadFileData(inputPath string, bmsFileName string) (*BMSFileData, error) {
// What time (ms) the current track will start at.
var startTrackAt float64
// What BPM the current track will start at.
var startTrackWithBPM float64
// Tracks long notes in channels 51-59 when they start.
// Normal LNs (that use #LNOBJ) will not need this.
longNoteTracker := map[int]float64{}
longNoteSoundEffectTracker := map[int]*KeySound{}
fileData, e := conf.CompileBMSToStruct(inputPath, bmsFileName)
if e != nil {
return nil, e
}
if fileData == nil {
return nil, nil
}
startTrackWithBPM = fileData.StartingBPM
fileData.TimingPoints[0.0] = fileData.StartingBPM
// Sort all tracks in ascending order
keys := make([]int, 0)
for k := range fileData.TrackLines {
keys = append(keys, k)
}
sort.Ints(keys)
for _, trackInt := range keys {
localTrackData, e := conf.ReadTrackData(trackInt, fileData.TrackLines[trackInt], fileData.Indices.BPMChanges, fileData.Indices.Stops)
if e != nil {
return nil, e
}
if localTrackData == nil {
return nil, nil
}
for _, line := range fileData.TrackLines[trackInt] {
if len(line.Message)%2 != 0 {
continue
}
// Cancel parsing if notes are found in P2 side.
if player2NoteRegex.MatchString(line.Channel) || player2LnRegex.MatchString(line.Channel) {
if conf.Verbose {
color.HiYellow("* This map has notes in player 2's side, which would overlap player 1. Not going to process this map.")
return nil, nil
}
}
if !(noteRegex.MatchString(line.Channel) || lnRegex.MatchString(line.Channel) || line.Channel == "01" || line.Channel == "04" || line.Channel == "07") {
continue
}
for i := 0; i < len(line.Message)/2; i++ {
if (i*2)+2 > len(line.Message) {
break
}
target := getHexadecimalPair(i, line.Message)
if target == "00" {
continue
}
localOffset := GetOffsetFromStartingTime(localTrackData, i, line.Message, startTrackWithBPM)
sfx := conf.GetCorrespondingHitSound(fileData.Audio.HexadecimalArray, target)
laneInt := strings.Index(Base36Range, line.Channel[1:])
// maybe you should get among some bitches
if noteRegex.MatchString(line.Channel) || lnRegex.MatchString(line.Channel) {
if (laneInt == 6 && !conf.NoScratchLane) || laneInt != 6 {
if laneInt == 6 {
// Uses the channel for the scratch lane, manually adjust to lane 8
laneInt = 8
} else if laneInt >= 8 {
// Compensate for notes past 8th key (6th and 7th lane)
laneInt -= 2
}
if laneInt > 8 {
color.HiRed("* File wants more than 8 keys, skipping")
return nil, nil
}
hitObject := HitObject{
StartTime: startTrackAt + localOffset,
}
// Closes the long note
if target == fileData.LNObject {
if len(fileData.HitObjects[laneInt]) == 0 {
// Why is there an LN tail as the first object??
continue
}
back := len(fileData.HitObjects[laneInt]) - 1
//if fileData.HitObjects[back].KeySounds == nil {
// // Previous hit object didn't have key sounds.
// // That means the previous value is a LN object, so we don't add a new one.
// continue
//}
// If the LN is too short don't actually use it.
if hitObject.StartTime-fileData.HitObjects[laneInt][back].StartTime < 2.0 {
continue
}
fileData.HitObjects[laneInt][back].IsLongNote = true
fileData.HitObjects[laneInt][back].EndTime = hitObject.StartTime
continue
}
if sfx != nil {
hitObject.KeySounds = sfx
}
// This is a long note existing in channels 51-59. We save it to a map storing these values.
if lnRegex.MatchString(line.Channel) {
// This is the end of a long note. Now, we can place the note.
if longNoteTracker[laneInt] != 0.0 {
// haha funny end time joke
hitObject.EndTime = hitObject.StartTime
hitObject.StartTime = longNoteTracker[laneInt]
hitObject.IsLongNote = true
if longNoteSoundEffectTracker[laneInt] != nil {
hitObject.KeySounds = &KeySound{
Sample: longNoteSoundEffectTracker[laneInt].Sample,
Volume: longNoteSoundEffectTracker[laneInt].Volume,
}
}
// Reset values
longNoteTracker[laneInt] = 0.0
longNoteSoundEffectTracker[laneInt] = nil
// Invalid long note because it ends either before or exactly at the position it ends.
// In other words, do not process it.
if hitObject.EndTime <= hitObject.StartTime {
continue
}
} else {
// This is the head of a long note, so we store its start time and key sounds for later.
longNoteTracker[laneInt] = hitObject.StartTime
longNoteSoundEffectTracker[laneInt] = hitObject.KeySounds
continue
}
}
fileData.HitObjects[laneInt] = append(fileData.HitObjects[laneInt], hitObject)
continue
}
}
if line.Channel == "01" || laneInt == 6 && conf.NoScratchLane {
// Sound effect (channel 01)
soundEffect := SoundEffect{
StartTime: startTrackAt + localOffset,
}
if sfx != nil {
soundEffect.Sample = sfx.Sample
soundEffect.Volume = sfx.Volume
} else {
// No sound effect corresponding to this address?
continue
}
fileData.SoundEffects = append(fileData.SoundEffects, soundEffect)
}
if (line.Channel == "04" || line.Channel == "07") && conf.FileType == Osu && !conf.NoStoryboard {
t := fileData.Indices.BGA[target]
l := Back
if line.Channel == "07" {
l = Front
}
if len(t) > 0 {
fileData.BGAFrames = append(fileData.BGAFrames, BGAFrame{
StartTime: startTrackAt + localOffset,
File: t,
Layer: l,
})
}
}
}
}
// Get the full length of the track.
fullLengthOfTrack := GetTotalTrackDuration(startTrackWithBPM, *localTrackData)
// Calculate all timing points.
if !conf.NoTimingPoints {
timingPoints := CalculateTimingPoints(startTrackAt, startTrackWithBPM, *localTrackData)
for k, v := range timingPoints {
fileData.TimingPoints[k] = v
}
}
if len(localTrackData.BPMChanges) > 0 {
startTrackWithBPM = localTrackData.BPMChanges[len(localTrackData.BPMChanges)-1].Bpm
}
// Add the length of the track onto the current time.
startTrackAt += fullLengthOfTrack
if !conf.NoMeasureLines && !conf.NoTimingPoints {
fileData.TimingPoints[startTrackAt] = startTrackWithBPM
}
}
sort.Slice(fileData.BGAFrames, func(i, j int) bool {
return fileData.BGAFrames[i].StartTime < fileData.BGAFrames[j].StartTime
})
return fileData, nil
}