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 (errorReason != null)
{ {
yield return new IssueTemplateFileError(this).Create(filename, errorReason);
continue;
}
if (tagFile.Properties.AudioChannels == 0) if (tagFile.Properties.AudioChannels == 0)
continue; continue;
}
yield return new IssueTemplateHasAudioTrack(this).Create(filename); issue = new IssueTemplateHasAudioTrack(this).Create(filename);
}
catch (CorruptFileException)
{
issue = new IssueTemplateFileError(this).Create(filename, "Corrupt file");
}
catch (UnsupportedFormatException)
{
issue = new IssueTemplateFileError(this).Create(filename, "Unsupported format");
}
yield return issue;
} }
} }

View File

@ -31,7 +31,8 @@ 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) if (data == null)
continue; continue;
@ -56,6 +57,7 @@ namespace osu.Game.Rulesets.Edit.Checks
yield return new IssueTemplateTooShort(this).Create(file.Filename, ms); yield return new IssueTemplateTooShort(this).Create(file.Filename, ms);
} }
} }
}
private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLower().EndsWith); private bool hasAudioExtension(string filename) => audioExtensions.Any(filename.ToLower().EndsWith);
private bool probablyHasAudioData(Stream data) => data.Length > min_bytes_threshold; private bool probablyHasAudioData(Stream data) => data.Length > min_bytes_threshold;

View File

@ -22,11 +22,13 @@ 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) if (data?.Length == 0)
yield return new IssueTemplateZeroBytes(this).Create(file.Filename); yield return new IssueTemplateZeroBytes(this).Create(file.Filename);
} }
} }
}
public class IssueTemplateZeroBytes : IssueTemplate public class IssueTemplateZeroBytes : IssueTemplate
{ {