-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdir_search.go
407 lines (320 loc) · 9.49 KB
/
dir_search.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package fnf
import (
"bufio"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"slices"
"strings"
)
// NOTE : This doesn't properly work on multibyte characters (eg: like koreans)
// and pretty slow, but for what we are doing, I think this is fine
func StringDistance(str1, str2 []byte) int {
matrix := make([]int, (len(str1)+1)*(len(str2)+1))
set := func(x, y, to int) {
matrix[x+y*(len(str1)+1)] = to
}
get := func(x, y int) int {
return matrix[x+y*(len(str1)+1)]
}
for i := 0; i <= len(str1); i++ {
set(i, len(str2), len(str1)-i)
}
for i := 0; i <= len(str2); i++ {
set(len(str1), i, len(str2)-i)
}
for i := len(str1) - 1; i >= 0; i-- {
for j := len(str2) - 1; j >= 0; j-- {
if str1[i] == str2[j] {
set(i, j, get(i+1, j+1))
} else {
n := min(
get(i+0, j+1),
get(i+1, j+0),
get(i+1, j+1),
) + 1
set(i, j, n)
}
}
}
return matrix[0]
}
// TODO : rather than dumping a log,
// I think this should really return grouped path
// like I walked these paths and parsed these paths and so on and so forth...
func TryToFindSongs(root string, logger *log.Logger) PathGroupCollection {
// ===============================================
// collect song json file and audio candidates
// ===============================================
failedDirectories := make(map[fs.FileInfo]error)
audioPaths := make([]string, 0)
jsonPaths := make([]string, 0)
onVisit := func(path string, f fs.FileInfo, err error) error {
logger.Printf("visited %v\n", path)
if err != nil {
failedDirectories[f] = err
} else {
if f.Mode().IsRegular() {
name := strings.ToLower(f.Name())
if strings.HasSuffix(name, ".ogg") || strings.HasSuffix(name, ".mp3") {
audioPaths = append(audioPaths, path)
} else if strings.HasSuffix(name, ".json") {
jsonPaths = append(jsonPaths, path)
}
}
}
return nil
}
err := filepath.Walk(root, onVisit)
_ = err
slices.Sort(audioPaths)
slices.Sort(jsonPaths)
// ==========================================================
// try to parse collected json files and see what sticks
// ==========================================================
pathToParseErrors := make(map[string]error)
pathToSong := make(map[string]FnfSong)
for _, path := range jsonPaths {
song, err := tryParseFile(path)
if err != nil {
pathToParseErrors[path] = err
} else {
pathToSong[path] = song
}
}
logger.Printf("%v of %v parsed\n", len(pathToSong), len(jsonPaths))
for path, song := range pathToSong {
logger.Printf("- path : %v\n", path)
logger.Printf("- name : %v\n", song.SongName)
}
logger.Printf("parse errors %v:\n", len(pathToParseErrors))
for path, err := range pathToParseErrors {
logger.Printf("- path : %v\n", path)
logger.Printf("- error : %v\n", err)
}
// ==========================================================
// collect song names form parsed jsons
// ==========================================================
var songNames []string
for _, song := range pathToSong {
if !slices.Contains(songNames, song.SongName) {
songNames = append(songNames, song.SongName)
}
}
logger.Printf("song names %v:\n", len(songNames))
for _, name := range songNames {
logger.Printf("- name : %v\n", name)
}
// ==========================================================
// try to group the songs
// ==========================================================
songPaths := make([]string, 0, len(pathToSong))
for path := range pathToSong {
songPaths = append(songPaths, path)
}
type Directory struct {
Path string
Children []string
}
var audioDirs []*Directory
for _, path := range audioPaths {
foundDir := false
pathDir := filepath.Dir(path)
for _, dir := range audioDirs {
if dir.Path == pathDir {
dir.Children = append(dir.Children, path)
foundDir = true
break
}
}
if !foundDir {
newDir := new(Directory)
newDir.Path = pathDir
newDir.Children = append(newDir.Children, path)
audioDirs = append(audioDirs, newDir)
}
}
dirSortFunc := func(dirA, dirB string, child string) int {
nameA := filepath.Base(dirA)
nameB := filepath.Base(dirB)
lowA := strings.ToLower(nameA)
lowB := strings.ToLower(nameB)
distA := StringDistance([]byte(lowA), []byte(child))
distB := StringDistance([]byte(lowB), []byte(child))
return distA - distB
}
type pathGroupAndSong struct {
Group FnfPathGroup
Songs [DifficultySize]FnfSong
}
var gsArray []pathGroupAndSong
songPathTaken := make(map[string]bool)
for _, songName := range songNames {
gAndS := pathGroupAndSong{}
gAndS.Group.SongName = songName
nameLow := strings.ToLower(songName)
var songPathsToCheck []string
for _, path := range songPaths {
if !songPathTaken[path] {
songPathsToCheck = append(songPathsToCheck, path)
}
}
slices.SortFunc(songPathsToCheck, func(a, b string) int {
aDir := strings.ToLower(filepath.Base(filepath.Dir(a)))
bDir := strings.ToLower(filepath.Base(filepath.Dir(b)))
aName := strings.ToLower(filepath.Base(a))
bName := strings.ToLower(filepath.Base(b))
distA := StringDistance([]byte(aDir), []byte(nameLow)) + StringDistance([]byte(aName), []byte(nameLow))
distB := StringDistance([]byte(bDir), []byte(nameLow)) + StringDistance([]byte(bName), []byte(nameLow))
return distA - distB
})
for _, path := range songPathsToCheck {
song := pathToSong[path]
if !songPathTaken[path] && song.SongName == songName {
//check the difficulty
difficulty := DifficultyNormal
pathLow := strings.ToLower(path)
if strings.HasSuffix(pathLow, "-hard.json") {
difficulty = DifficultyHard
} else if strings.HasSuffix(pathLow, "-easy.json") {
difficulty = DifficultyEasy
}
if !gAndS.Group.HasSong[difficulty] {
gAndS.Songs[difficulty] = song
gAndS.Group.SongPaths[difficulty] = path
gAndS.Group.HasSong[difficulty] = true
songPathTaken[path] = true
}
}
}
slices.SortFunc(audioDirs, func(a, b *Directory) int {
return dirSortFunc(a.Path, b.Path, nameLow)
})
audioDir := audioDirs[0]
for _, child := range audioDir.Children {
childName := strings.ToLower(filepath.Base(child))
if strings.HasSuffix(childName, ".ogg") {
if strings.Contains(childName, "inst") {
gAndS.Group.InstPath = child
} else if strings.Contains(childName, "voice") {
gAndS.Group.VoicePath = child
}
} else if strings.HasSuffix(childName, ".mp3") {
if strings.Contains(childName, "inst") && gAndS.Group.InstPath == "" {
gAndS.Group.InstPath = child
} else if strings.Contains(childName, "voice") && gAndS.Group.VoicePath == "" {
gAndS.Group.VoicePath = child
}
}
}
gsArray = append(gsArray, gAndS)
}
// check if pathgroup is good
{
var goodGsArray []pathGroupAndSong
for _, gAndS := range gsArray {
if err := isPathGroupGood(gAndS.Group, gAndS.Songs); err != nil {
logger.Printf("group %v is bad : %v\n", gAndS.Group.SongName, err)
} else {
goodGsArray = append(goodGsArray, gAndS)
}
}
gsArray = goodGsArray
}
// extract FnfPathGroup from pathGroupAndSong
var pathGroups []FnfPathGroup
for _, gAndS := range gsArray {
pathGroups = append(pathGroups, gAndS.Group)
}
printGroup := func(group FnfPathGroup) {
logger.Printf("%v :\n", group.SongName)
logger.Printf("difficulties : \n")
for difficulty := FnfDifficulty(0); difficulty < DifficultySize; difficulty++ {
if group.HasSong[difficulty] {
switch difficulty {
case DifficultyEasy:
logger.Printf(" easy - %v\n", group.SongPaths[difficulty])
case DifficultyNormal:
logger.Printf(" normal - %v\n", group.SongPaths[difficulty])
case DifficultyHard:
logger.Printf(" hard - %v\n", group.SongPaths[difficulty])
}
}
}
logger.Printf("inst path : %v\n", group.InstPath)
logger.Printf("voice path : %v\n", group.VoicePath)
}
for _, group := range pathGroups {
logger.Printf("\n")
printGroup(group)
}
// sort group to song name
slices.SortFunc(pathGroups, func(a, b FnfPathGroup) int {
return strings.Compare(a.SongName, b.SongName)
})
// give groups id
for i := range pathGroups {
pathGroups[i].id = NewFnfPathGroupId()
}
collection := PathGroupCollection{
BasePath: root,
PathGroups: pathGroups,
id: NewPathGroupCollectionId(),
}
return collection
}
func isPathGroupGood(group FnfPathGroup, songs [DifficultySize]FnfSong) error {
// first check if it has any song
hasSong := false
for i := range len(group.HasSong) {
if group.HasSong[i] {
hasSong = true
break
}
}
if !hasSong {
return fmt.Errorf("group has no song")
}
// check if song.SongName matches group.SongName
for i, song := range songs {
if group.HasSong[i] {
if song.SongName != group.SongName {
return fmt.Errorf("%v song name %v != group song name %v",
DifficultyStrs[i],
song.SongName,
group.SongName)
}
}
}
// if song usese voices, group needs a voice path
needsVoices := false
for i, song := range songs {
if group.HasSong[i] {
if song.NeedsVoices {
needsVoices = true
break
}
}
}
if needsVoices && group.VoicePath == "" {
return fmt.Errorf("group %v needs voice but has no voice path", group.SongName)
}
return nil
}
func tryParseFile(path string) (FnfSong, error) {
path = filepath.Clean(path)
jsonFile, err := os.Open(path)
defer jsonFile.Close()
var parsedSong FnfSong
if err != nil {
return parsedSong, err
}
reader := bufio.NewReader(jsonFile)
parsedSong, err = ParseJsonToFnfSong(reader)
if err != nil {
return parsedSong, err
}
return parsedSong, nil
}