Skip to content

Commit

Permalink
Speeded up standard song scanning
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed May 9, 2023
1 parent bd68d2f commit 9cc8679
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
86 changes: 44 additions & 42 deletions Assets/Script/Song/Scanning/SongScanThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Threading;
using UnityEngine;
using YARG.Serialization;
using YARG.Song;
using YARG.Song.Preparsers;

namespace YARG.Song {
Expand Down Expand Up @@ -133,64 +132,85 @@ private void ScanSubDirectory(string cacheFolder, string subDir, ICollection<Son
_foldersScanned++;
foldersScanned = _foldersScanned;

// Check if it is a song folder
var result = ScanIniSong(cacheFolder, subDir, out var song);
switch (result) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;

songs.Add(song);
return;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, result));
Debug.LogWarning($"Error encountered with {subDir}");
return;
}

// Raw CON folder, so don't scan anymore subdirectories here
string songsPath = Path.Combine(subDir, "songs");
if (File.Exists(Path.Combine(songsPath, "songs.dta"))) {
List<ExtractedConSongEntry> files = ExCONBrowser.BrowseFolder(songsPath);

foreach(ExtractedConSongEntry file in files){
foreach (ExtractedConSongEntry file in files) {
// validate that the song is good to add in-game
var ExCONResult = ScanExConSong(cacheFolder, file);
switch(ExCONResult){
switch (ExCONResult) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;
songs.Add((ExtractedConSongEntry)file);
break;
songs.Add(file);
return;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, ExCONResult));
Debug.LogWarning($"Error encountered with {subDir}");
break;
return;
}
}

return;
}

// iterate through the files in this current directory
foreach(var file in Directory.EnumerateFiles(subDir)){
// Iterate through the files in this current directory to look for CON files
foreach (var file in Directory.EnumerateFiles(subDir)) {
Debug.Log($"Scanning file {file}");

// for each file found, read first 4 bytes and check for "CON " or "LIVE"
using var fs = new FileStream(file, FileMode.Open, FileAccess.Read);
using var br = new BinaryReader(fs);
string fHeader = Encoding.UTF8.GetString(br.ReadBytes(4));
if(fHeader == "CON " || fHeader == "LIVE"){
if (fHeader == "CON " || fHeader == "LIVE") {
List<ConSongEntry> SongsInsideCON = XboxCONFileBrowser.BrowseCON(file);
// for each CON song that was found (assuming some WERE found)
if(SongsInsideCON != null){
foreach(ConSongEntry SongInsideCON in SongsInsideCON){
if (SongsInsideCON != null) {
foreach (ConSongEntry SongInsideCON in SongsInsideCON) {
// validate that the song is good to add in-game
var CONResult = ScanConSong(cacheFolder, SongInsideCON);
switch(CONResult){
switch (CONResult) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;
songs.Add((ConSongEntry)SongInsideCON);
break;
songs.Add(SongInsideCON);
return;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, CONResult));
Debug.LogWarning($"Error encountered with {subDir}");
break;
return;
}
}
}
}
}
}

Expand All @@ -199,24 +219,6 @@ private void ScanSubDirectory(string cacheFolder, string subDir, ICollection<Son
foreach (string subdirectory in subdirectories) {
ScanSubDirectory(cacheFolder, subdirectory, songs);
}

var result = ScanIniSong(cacheFolder, subDir, out var song);
switch (result) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;

songs.Add(song);
break;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, result));
Debug.LogWarning($"Error encountered with {subDir}");
break;
}
}

private static ScanResult ScanIniSong(string cache, string directory, out SongEntry song) {
Expand Down Expand Up @@ -268,19 +270,19 @@ private static ScanResult ScanIniSong(string cache, string directory, out SongEn
return ScanHelpers.ParseSongIni(Path.Combine(directory, "song.ini"), (IniSongEntry) song);
}

private static ScanResult ScanExConSong(string cache, ExtractedConSongEntry file){
private static ScanResult ScanExConSong(string cache, ExtractedConSongEntry file) {
// Skip if the song doesn't have notes
if(!File.Exists(file.NotesFile)){
if (!File.Exists(file.NotesFile)) {
return ScanResult.NoNotesFile;
}

// Skip if this is a "fake song" (tutorials, etc.)
if(file.IsFake){
if (file.IsFake) {
return ScanResult.NotASong;
}

// Skip if the mogg is encrypted
if(file.MoggHeader != 0xA){
if (file.MoggHeader != 0xA) {
return ScanResult.EncryptedMogg;
}

Expand All @@ -298,19 +300,19 @@ private static ScanResult ScanExConSong(string cache, ExtractedConSongEntry file
return ScanResult.Ok;
}

private static ScanResult ScanConSong(string cache, ConSongEntry file){
private static ScanResult ScanConSong(string cache, ConSongEntry file) {
// Skip if the song doesn't have notes
if(file.MidiFileSize == 0){
if (file.MidiFileSize == 0) {
return ScanResult.NoNotesFile;
}

// Skip if this is a "fake song" (tutorials, etc.)
if(file.IsFake){
if (file.IsFake) {
return ScanResult.NotASong;
}

// Skip if the mogg is encrypted
if(file.MoggHeader != 0xA){
if (file.MoggHeader != 0xA) {
return ScanResult.EncryptedMogg;
}

Expand Down
16 changes: 8 additions & 8 deletions Assets/Script/Song/Scanning/SongScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,27 @@ private void AssignFoldersToThreads() {
}

private async UniTask WriteBadSongs() {
#if UNITY_EDITOR
#if UNITY_EDITOR
string badSongsPath = Path.Combine(GameManager.PersistentDataPath, "badsongs.txt");
#else
#else
string badSongsPath = Path.Combine(GameManager.ExecutablePath, "badsongs.txt");
#endif
#endif

await using var stream = new FileStream(badSongsPath, FileMode.Create, FileAccess.Write);
await using var writer = new StreamWriter(stream);

foreach (var thread in _scanThreads) {
foreach(var folder in thread.SongErrors) {
foreach (var folder in thread.SongErrors) {
if (folder.Value.Count == 0) {
continue;
}

await writer.WriteLineAsync(folder.Key);
folder.Value.Sort((x, y) => x.Result.CompareTo(y.Result));

var lastResult = ScanResult.Ok;
foreach (var error in folder.Value) {
if(error.Result != lastResult) {
if (error.Result != lastResult) {
switch (error.Result) {
case ScanResult.InvalidDirectory:
await writer.WriteLineAsync(
Expand Down

0 comments on commit 9cc8679

Please sign in to comment.