mirror of
https://github.com/SK-la/Ez2Lazer.git
synced 2026-03-13 11:20:28 +00:00
Simplify taglib interactions further
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace osu.Game.Screens.Edit.Setup
|
||||
|
||||
try
|
||||
{
|
||||
tagSource = TagLibUtils.CreateFile(source.FullName);
|
||||
tagSource = TagLibUtils.GetTagLibFile(source.FullName);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user