Skip to content

Commit

Permalink
Handle some exceptions with chart preparsing (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
RileyTheFox authored May 11, 2023
1 parent 28a1f97 commit 98e7348
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
20 changes: 16 additions & 4 deletions Assets/Script/Song/Preparsers/ChartPreparser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,29 @@ public static class ChartPreparser {
// { Instrument.GHLiveCoop, "GHLCoop" }
};

public static ulong GetAvailableTracks(byte[] chartData) {
public static bool GetAvailableTracks(byte[] chartData, out ulong tracks) {
using var stream = new MemoryStream(chartData);

using var reader = new StreamReader(stream);
return ReadStream(reader);
try {
tracks = ReadStream(reader);
return true;
} catch {
tracks = 0;
return false;
}
}

public static ulong GetAvailableTracks(SongEntry song) {
public static bool GetAvailableTracks(SongEntry song, out ulong tracks) {
using var reader = File.OpenText(Path.Combine(song.Location, song.NotesFile));

return ReadStream(reader);
try {
tracks = ReadStream(reader);
return true;
} catch {
tracks = 0;
return false;
}
}

private static ulong ReadStream(StreamReader reader) {
Expand Down
25 changes: 13 additions & 12 deletions Assets/Script/Song/Preparsers/MidPreparser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.IO;
using Melanchall.DryWetMidi.Core;
using MoonscraperChartEditor.Song.IO;
using UnityEngine;
using YARG.Data;

namespace YARG.Song.Preparsers {
Expand Down Expand Up @@ -31,26 +30,28 @@ public static class MidPreparser {

};

public static ulong GetAvailableTracks(byte[] chartData) {
public static bool GetAvailableTracks(byte[] chartData, out ulong tracks) {
using var stream = new MemoryStream(chartData);
try {
var midi = MidiFile.Read(stream, ReadSettings);
return ReadStream(midi);
tracks = ReadStream(midi);
return true;
} catch (Exception e) {
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
return ulong.MaxValue;
// Debug.LogError(e.Message);
// Debug.LogError(e.StackTrace);
tracks = 0;
return false;
}
}

public static ulong GetAvailableTracks(SongEntry song) {
public static bool GetAvailableTracks(SongEntry song, out ulong tracks) {
try {
var midi = MidiFile.Read(Path.Combine(song.Location, song.NotesFile), ReadSettings);
return ReadStream(midi);
} catch (Exception e) {
Debug.LogError(e.Message);
Debug.LogError(e.StackTrace);
return ulong.MaxValue;
tracks = ReadStream(midi);
return true;
} catch {
tracks = 0;
return false;
}
}

Expand Down
3 changes: 3 additions & 0 deletions Assets/Script/Song/ScanHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ public static class ScanHelpers {

public static ScanResult ParseSongIni(string iniFile, IniSongEntry entry) {
var file = new IniFile(iniFile);

// Had some reports that ini parsing might throw an exception, leaving this in for now
// in as I don't know the cause just yet and I want to investigate it further.
file.Parse();

string sectionName = file.ContainsSection("song") ? "song" : "Song";
Expand Down
20 changes: 15 additions & 5 deletions Assets/Script/Song/Scanning/SongScanThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void ScanSubDirectory(string cacheFolder, string subDir, ICollection<Son
_errorsEncountered++;
errorsEncountered = _errorsEncountered;
_songErrors[cacheFolder].Add(new SongError(subDir, result));
Debug.LogWarning($"Error encountered with {subDir}");
Debug.LogWarning($"Error encountered with {subDir}: {result}");
return;
}

Expand Down Expand Up @@ -251,9 +251,15 @@ private static ScanResult ScanIniSong(string cache, string directory, out SongEn
var tracks = ulong.MaxValue;

if (notesFile.EndsWith(".mid")) {
tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong availTracks)) {
return ScanResult.CorruptedNotesFile;
}
tracks = availTracks;
} else if (notesFile.EndsWith(".chart")) {
tracks = ChartPreparser.GetAvailableTracks(bytes);
if (!ChartPreparser.GetAvailableTracks(bytes, out ulong availTracks)) {
return ScanResult.CorruptedNotesFile;
}
tracks = availTracks;
}

// We have a song.ini, notes file and audio. The song is scannable.
Expand Down Expand Up @@ -289,7 +295,9 @@ private static ScanResult ScanExConSong(string cache, ExtractedConSongEntry file

string checksum = BitConverter.ToString(SHA1.Create().ComputeHash(bytes)).Replace("-", "");

ulong tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong tracks)) {
return ScanResult.CorruptedNotesFile;
}

file.CacheRoot = cache;
file.Checksum = checksum;
Expand Down Expand Up @@ -321,7 +329,9 @@ private static ScanResult ScanConSong(string cache, ConSongEntry file) {

string checksum = BitConverter.ToString(SHA1.Create().ComputeHash(bytes)).Replace("-", "");

ulong tracks = MidPreparser.GetAvailableTracks(bytes);
if (!MidPreparser.GetAvailableTracks(bytes, out ulong tracks)) {
return ScanResult.CorruptedNotesFile;
}

file.CacheRoot = cache;
file.Checksum = checksum;
Expand Down
7 changes: 6 additions & 1 deletion Assets/Script/Song/Scanning/SongScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public enum ScanResult {
NotASong,
NoNotesFile,
NoAudioFile,
EncryptedMogg
EncryptedMogg,
CorruptedNotesFile,
CorruptedMetadataFile
}

public readonly struct SongError {
Expand Down Expand Up @@ -231,6 +233,9 @@ await writer.WriteLineAsync(
case ScanResult.EncryptedMogg:
await writer.WriteLineAsync("These songs contain encrypted moggs!");
break;
case ScanResult.CorruptedNotesFile:
await writer.WriteLineAsync("These songs contain a corrupted notes.chart/notes.mid file!");
break;
}
lastResult = error.Result;
}
Expand Down

0 comments on commit 98e7348

Please sign in to comment.