Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try/catch added to con reading to prevent crashing #312

Merged
merged 7 commits into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 35 additions & 34 deletions Assets/Script/Song/Scanning/SongScanThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,45 +190,46 @@ private void ScanSubDirectory(string cacheFolder, string subDir, ICollection<Son

return;
}

// Iterate through the files in this current directory to look for CON files
foreach (var file in Directory.EnumerateFiles(subDir)) {
// 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") {
List<ConSongEntry> SongsInsideCON = XboxCONFileBrowser.BrowseCON(file, _updateFolderPath, _updatableSongs);
// for each CON song that was found (assuming some WERE found)
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) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;
songs.Add(SongInsideCON);
break;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, CONResult, SongInsideCON.Name));
Debug.LogWarning($"Error encountered with {subDir}");
break;

try{ // try-catch to prevent crash if user doesn't have permission to access a folder
foreach (var file in Directory.EnumerateFiles(subDir)) {
// 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") {
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) {
// validate that the song is good to add in-game
var CONResult = ScanConSong(cacheFolder, SongInsideCON);
switch (CONResult) {
case ScanResult.Ok:
_songsScanned++;
songsScanned = _songsScanned;
songs.Add(SongInsideCON);
break;
case ScanResult.NotASong:
break;
default:
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, CONResult, SongInsideCON.Name));
Debug.LogWarning($"Error encountered with {subDir}");
break;
}
}
}
}
}
}

string[] subdirectories = Directory.GetDirectories(subDir);

foreach (string subdirectory in subdirectories) {
if(subdirectory != Path.Combine(subDir, "songs_updates"))
string[] subdirectories = Directory.GetDirectories(subDir);
foreach (string subdirectory in subdirectories) {
ScanSubDirectory(cacheFolder, subdirectory, songs);
}
}catch(Exception e){
Debug.LogException(e);
}
}

Expand Down Expand Up @@ -376,4 +377,4 @@ private static ScanResult ScanConSong(string cache, ConSongEntry file) {
}
}

}
}
32 changes: 14 additions & 18 deletions Assets/Script/ThirdParty/TwitchController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ public static TwitchController Instance {

// Creates .TXT file witth current song information
public string TextFilePath => Path.Combine(GameManager.PersistentDataPath, "currentSong.txt");
// Creates .JSON file with current song information
public string JsonFilePath => Path.Combine(GameManager.PersistentDataPath, "currentSong.json");

private void Start() {
Instance = this;

// While YARG should delete the file on exit, you never know if a crash or something prevented that.
DeleteCurrentSongFile();
CreateEmptySongFile();
// While YARG should blank the file on exit, you never know if a crash or something prevented that.
BlankSongFile();

// Listen to the changing of songs
Play.OnSongStart += OnSongStart;
Expand All @@ -36,29 +37,24 @@ private void Start() {
Play.OnPauseToggle += OnPauseToggle;
}

private void CreateEmptySongFile() {
// Open the text file for appending
using var writer = new StreamWriter(TextFilePath, false);

// Make the file blank (Avoid errors in OBS)
writer.Write("");
}

private void DeleteCurrentSongFile() {
private void BlankSongFile() {
// Open the text file for appending
using var writer = new StreamWriter(TextFilePath, false);
using var jsonWriter = new StreamWriter(JsonFilePath, false);

// Make the file blank (Avoid errors in OBS)
writer.Write("");
jsonWriter.Write("");
}

private void OnApplicationQuit() {
DeleteCurrentSongFile();
BlankSongFile();
}

void OnSongStart(SongEntry song) {
// Open the text file for appending
using var writer = new StreamWriter(TextFilePath, false);
using var jsonWriter = new StreamWriter(JsonFilePath, false);

// Get the input
string str = $"{song.Name}\n{song.Artist}\n{song.Album}\n{song.Genre}\n" +
Expand All @@ -68,17 +64,17 @@ void OnSongStart(SongEntry song) {
if (TagRegex.IsMatch(str)) {
str = TagRegex.Replace(str, string.Empty);
}

// Convert to JSON
string json = JsonUtility.ToJson(song);

// Write text to the file
writer.Write(str);
jsonWriter.Write(json);
}

void OnSongEnd(SongEntry song) {
// Open the text file for appending
using var writer = new StreamWriter(TextFilePath, false);

// Make the file blank (Avoid errors in OBS)
writer.Write("");
BlankSongFile();
}

private void OnInstrumentSelection(PlayerManager.Player playerInfo) {
Expand Down
1 change: 1 addition & 0 deletions Assets/Script/ThirdParty/UpdateChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ private async void CheckForUpdates() {
IsOutOfDate = true;

Debug.Log($"Update available! New version: {releaseTag}");
ToastManager.ToastInformation($"Update available! New version: {releaseTag}");
} else {
Debug.Log("Game is up to date.");
ToastManager.ToastMessage("Game is up to date.");
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/UI/MusicLibrary/SongSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private set {
_selectedIndex -= _songs.Count;
}

if (_songs[value] is SongViewType song) {
if (_songs[_selectedIndex] is SongViewType song) {
EliteAsian123 marked this conversation as resolved.
Show resolved Hide resolved
GameManager.Instance.SelectedSong = song.SongEntry;
}

Expand Down