Refactor to use scoped using (and also correctly dispose TagLib portion)

This commit is contained in:
Dean Herbert 2021-10-12 10:46:26 +09:00
parent f2f97602f2
commit 82f7f99f37
3 changed files with 46 additions and 41 deletions

View File

@ -6,6 +6,8 @@ using System.IO;
using osu.Game.IO.FileAbstraction; using osu.Game.IO.FileAbstraction;
using osu.Game.Rulesets.Edit.Checks.Components; using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Storyboards; using osu.Game.Storyboards;
using TagLib;
using File = TagLib.File;
namespace osu.Game.Rulesets.Edit.Checks namespace osu.Game.Rulesets.Edit.Checks
{ {
@ -50,31 +52,30 @@ namespace osu.Game.Rulesets.Edit.Checks
continue; continue;
} }
using Stream data = context.WorkingBeatmap.GetStream(storagePath); Issue issue;
var fileAbstraction = new StreamFileAbstraction(filename, data);
// We use TagLib here for platform invariance; BASS cannot detect audio presence on Linux.
TagLib.File tagFile = null;
string errorReason = null;
try try
{ {
tagFile = TagLib.File.Create(fileAbstraction); // We use TagLib here for platform invariance; BASS cannot detect audio presence on Linux.
} using (Stream data = context.WorkingBeatmap.GetStream(storagePath))
catch (TagLib.CorruptFileException) { errorReason = "Corrupt file"; } using (File tagFile = File.Create(new StreamFileAbstraction(filename, data)))
catch (TagLib.UnsupportedFormatException) { errorReason = "Unsupported format"; } {
if (tagFile.Properties.AudioChannels == 0)
continue;
}
if (errorReason != null) issue = new IssueTemplateHasAudioTrack(this).Create(filename);
}
catch (CorruptFileException)
{ {
yield return new IssueTemplateFileError(this).Create(filename, errorReason); issue = new IssueTemplateFileError(this).Create(filename, "Corrupt file");
}
continue; catch (UnsupportedFormatException)
{
issue = new IssueTemplateFileError(this).Create(filename, "Unsupported format");
} }
if (tagFile.Properties.AudioChannels == 0) yield return issue;
continue;
yield return new IssueTemplateHasAudioTrack(this).Create(filename);
} }
} }

View File

@ -31,29 +31,31 @@ namespace osu.Game.Rulesets.Edit.Checks
foreach (var file in beatmapSet.Files) foreach (var file in beatmapSet.Files)
{ {
using Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath); using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath))
if (data == null)
continue;
var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle);
if (decodeStream == 0)
{ {
// If the file is not likely to be properly parsed by Bass, we don't produce Error issues about it. if (data == null)
// Image files and audio files devoid of audio data both fail, for example, but neither would be issues in this check. continue;
if (hasAudioExtension(file.Filename) && probablyHasAudioData(data))
yield return new IssueTemplateBadFormat(this).Create(file.Filename);
continue; var fileCallbacks = new FileCallbacks(new DataStreamFileProcedures(data));
int decodeStream = Bass.CreateStream(StreamSystem.NoBuffer, BassFlags.Decode | BassFlags.Prescan, fileCallbacks.Callbacks, fileCallbacks.Handle);
if (decodeStream == 0)
{
// If the file is not likely to be properly parsed by Bass, we don't produce Error issues about it.
// Image files and audio files devoid of audio data both fail, for example, but neither would be issues in this check.
if (hasAudioExtension(file.Filename) && probablyHasAudioData(data))
yield return new IssueTemplateBadFormat(this).Create(file.Filename);
continue;
}
long length = Bass.ChannelGetLength(decodeStream);
double ms = Bass.ChannelBytes2Seconds(decodeStream, length) * 1000;
// Extremely short audio files do not play on some soundcards, resulting in nothing being heard in-game for some users.
if (ms > 0 && ms < ms_threshold)
yield return new IssueTemplateTooShort(this).Create(file.Filename, ms);
} }
long length = Bass.ChannelGetLength(decodeStream);
double ms = Bass.ChannelBytes2Seconds(decodeStream, length) * 1000;
// Extremely short audio files do not play on some soundcards, resulting in nothing being heard in-game for some users.
if (ms > 0 && ms < ms_threshold)
yield return new IssueTemplateTooShort(this).Create(file.Filename, ms);
} }
} }

View File

@ -22,9 +22,11 @@ namespace osu.Game.Rulesets.Edit.Checks
foreach (var file in beatmapSet.Files) foreach (var file in beatmapSet.Files)
{ {
using Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath); using (Stream data = context.WorkingBeatmap.GetStream(file.FileInfo.StoragePath))
if (data?.Length == 0) {
yield return new IssueTemplateZeroBytes(this).Create(file.Filename); if (data?.Length == 0)
yield return new IssueTemplateZeroBytes(this).Create(file.Filename);
}
} }
} }