Simplify taglib interactions further

This commit is contained in:
Dean Herbert
2025-04-28 15:52:31 +09:00
parent c2efda20d7
commit 40faa3fc99
5 changed files with 37 additions and 44 deletions

View File

@@ -1,29 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.IO;
namespace osu.Game.IO.FileAbstraction
{
public class StreamFileAbstraction : TagLib.File.IFileAbstraction
{
public StreamFileAbstraction(string filename, Stream fileStream)
{
ReadStream = fileStream;
Name = filename;
}
public string Name { get; }
public Stream ReadStream { get; }
public Stream WriteStream => ReadStream;
public void CloseStream(Stream stream)
{
ArgumentNullException.ThrowIfNull(stream);
stream.Close();
}
}
}

View File

@@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.IO;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.IO.FileAbstraction;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Storyboards;
using osu.Game.Utils;
@@ -62,7 +61,7 @@ namespace osu.Game.Rulesets.Edit.Checks
{
// We use TagLib here for platform invariance; BASS cannot detect audio presence on Linux.
using (Stream data = context.WorkingBeatmap.GetStream(storagePath))
using (File tagFile = TagLibUtils.CreateFile(new StreamFileAbstraction(filename, data)))
using (File tagFile = TagLibUtils.GetTagLibFile(filename, data))
{
if (tagFile.Properties.AudioChannels == 0)
continue;

View File

@@ -6,7 +6,6 @@ using System.Collections.Generic;
using System.IO;
using osu.Framework.Logging;
using osu.Game.Beatmaps;
using osu.Game.IO.FileAbstraction;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Storyboards;
using osu.Game.Utils;
@@ -46,7 +45,7 @@ namespace osu.Game.Rulesets.Edit.Checks
try
{
using (Stream data = context.WorkingBeatmap.GetStream(storagePath))
using (File tagFile = TagLibUtils.CreateFile(new StreamFileAbstraction(filename, data)))
using (File tagFile = TagLibUtils.GetTagLibFile(filename, data))
{
int height = tagFile.Properties.VideoHeight;
int width = tagFile.Properties.VideoWidth;

View File

@@ -102,7 +102,7 @@ namespace osu.Game.Screens.Edit.Setup
try
{
tagSource = TagLibUtils.CreateFile(source.FullName);
tagSource = TagLibUtils.GetTagLibFile(source.FullName);
}
catch (Exception e)
{

View File

@@ -1,32 +1,56 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.IO;
using osu.Game.IO.FileAbstraction;
using TagLib;
using File = TagLib.File;
namespace osu.Game.Utils
{
public class TagLibUtils
{
/// <summary>
/// Creates a <see cref="TagLib.File"/> with culture-invariant MIME type detection.
/// Creates a <see cref="TagLib.File"/> with culture-invariant MIME type detection, based on stream data.
/// </summary>
/// <param name="fileAbstraction">The file abstraction of the file to be created.</param>
/// <returns>The <see cref="TagLib.File"/> created.</returns>
public static TagLib.File CreateFile(StreamFileAbstraction fileAbstraction) =>
TagLib.File.Create(fileAbstraction, getMimeType(fileAbstraction.Name), ReadStyle.Average);
public static File GetTagLibFile(string filename, Stream stream)
{
var fileAbstraction = new StreamFileAbstraction(filename, stream);
return File.Create(fileAbstraction, getMimeType(fileAbstraction.Name), ReadStyle.Average | ReadStyle.PictureLazy);
}
/// <summary>
/// Creates a <see cref="TagLib.File"/> with culture-invariant MIME type detection.
/// Creates a <see cref="TagLib.File"/> with culture-invariant MIME type detection based on a file on disk.
/// </summary>
/// <param name="filePath">The full path of the file to be created.</param>
/// <returns>The <see cref="TagLib.File"/> created.</returns>
public static TagLib.File CreateFile(string filePath) =>
TagLib.File.Create(filePath, getMimeType(filePath), ReadStyle.Average);
public static File GetTagLibFile(string filePath) =>
File.Create(filePath, getMimeType(filePath), ReadStyle.Average | ReadStyle.PictureLazy);
// Manual MIME type resolution to avoid culture variance (ie. https://github.com/ppy/osu/issues/32962)
private static string getMimeType(string fileName) =>
@"taglib/" + Path.GetExtension(fileName).TrimStart('.');
private static string getMimeType(string fileName) => @"taglib/" + Path.GetExtension(fileName).TrimStart('.');
private class StreamFileAbstraction : File.IFileAbstraction
{
public StreamFileAbstraction(string filename, Stream fileStream)
{
ReadStream = fileStream;
Name = filename;
}
public string Name { get; }
public Stream ReadStream { get; }
public Stream WriteStream => ReadStream;
public void CloseStream(Stream stream)
{
ArgumentNullException.ThrowIfNull(stream);
stream.Close();
}
}
}
}