2021-07-13 10:17:41 +08:00
|
|
|
// 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.
|
|
|
|
|
2024-03-13 20:55:49 +08:00
|
|
|
using System;
|
2021-07-13 10:17:41 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2024-03-13 20:55:49 +08:00
|
|
|
using osu.Framework.Logging;
|
2022-08-10 14:48:38 +08:00
|
|
|
using osu.Game.Beatmaps;
|
2021-10-11 19:56:26 +08:00
|
|
|
using osu.Game.IO.FileAbstraction;
|
2021-07-13 10:17:41 +08:00
|
|
|
using osu.Game.Rulesets.Edit.Checks.Components;
|
|
|
|
using osu.Game.Storyboards;
|
2021-10-12 09:46:26 +08:00
|
|
|
using TagLib;
|
|
|
|
using File = TagLib.File;
|
2021-07-13 10:17:41 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Rulesets.Edit.Checks
|
|
|
|
{
|
|
|
|
public class CheckAudioInVideo : ICheck
|
|
|
|
{
|
|
|
|
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Audio track in video files");
|
|
|
|
|
|
|
|
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
|
|
|
|
{
|
|
|
|
new IssueTemplateHasAudioTrack(this),
|
2021-10-11 19:53:23 +08:00
|
|
|
new IssueTemplateMissingFile(this),
|
|
|
|
new IssueTemplateFileError(this)
|
2021-07-13 10:17:41 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
|
|
|
|
{
|
|
|
|
var beatmapSet = context.Beatmap.BeatmapInfo.BeatmapSet;
|
2021-10-11 19:55:50 +08:00
|
|
|
var videoPaths = new List<string>();
|
2021-07-13 10:17:41 +08:00
|
|
|
|
|
|
|
foreach (var layer in context.WorkingBeatmap.Storyboard.Layers)
|
|
|
|
{
|
|
|
|
foreach (var element in layer.Elements)
|
|
|
|
{
|
|
|
|
if (!(element is StoryboardVideo video))
|
|
|
|
continue;
|
|
|
|
|
2021-10-11 19:55:50 +08:00
|
|
|
// Ensures we don't check the same video file multiple times in case of multiple elements using it.
|
|
|
|
if (!videoPaths.Contains(video.Path))
|
|
|
|
videoPaths.Add(video.Path);
|
|
|
|
}
|
|
|
|
}
|
2021-07-13 10:17:41 +08:00
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
foreach (string filename in videoPaths)
|
2021-10-11 19:55:50 +08:00
|
|
|
{
|
2022-10-30 16:28:49 +08:00
|
|
|
string? storagePath = beatmapSet?.GetPathForFile(filename);
|
2021-07-13 10:17:41 +08:00
|
|
|
|
2021-10-11 19:55:50 +08:00
|
|
|
if (storagePath == null)
|
|
|
|
{
|
|
|
|
// There's an element in the storyboard that requires this resource, so it being missing is worth warning about.
|
|
|
|
yield return new IssueTemplateMissingFile(this).Create(filename);
|
2021-07-13 10:17:41 +08:00
|
|
|
|
2021-10-11 19:55:50 +08:00
|
|
|
continue;
|
2021-07-13 10:17:41 +08:00
|
|
|
}
|
2021-10-11 19:55:50 +08:00
|
|
|
|
2021-10-12 09:46:26 +08:00
|
|
|
Issue issue;
|
2021-10-11 19:56:26 +08:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2021-10-12 09:46:26 +08:00
|
|
|
// We use TagLib here for platform invariance; BASS cannot detect audio presence on Linux.
|
|
|
|
using (Stream data = context.WorkingBeatmap.GetStream(storagePath))
|
|
|
|
using (File tagFile = File.Create(new StreamFileAbstraction(filename, data)))
|
|
|
|
{
|
|
|
|
if (tagFile.Properties.AudioChannels == 0)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
issue = new IssueTemplateHasAudioTrack(this).Create(filename);
|
2021-10-11 19:56:26 +08:00
|
|
|
}
|
2021-10-12 09:46:26 +08:00
|
|
|
catch (CorruptFileException)
|
2021-10-11 19:56:26 +08:00
|
|
|
{
|
2021-10-12 09:46:26 +08:00
|
|
|
issue = new IssueTemplateFileError(this).Create(filename, "Corrupt file");
|
|
|
|
}
|
|
|
|
catch (UnsupportedFormatException)
|
|
|
|
{
|
|
|
|
issue = new IssueTemplateFileError(this).Create(filename, "Unsupported format");
|
2021-10-11 19:56:26 +08:00
|
|
|
}
|
2024-03-13 20:55:49 +08:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
issue = new IssueTemplateFileError(this).Create(filename, "Internal failure - see logs for more info");
|
|
|
|
Logger.Log($"Failed when running {nameof(CheckAudioInVideo)}: {ex}");
|
|
|
|
}
|
2021-10-11 19:56:26 +08:00
|
|
|
|
2021-10-12 09:46:26 +08:00
|
|
|
yield return issue;
|
2021-07-13 10:17:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class IssueTemplateHasAudioTrack : IssueTemplate
|
|
|
|
{
|
|
|
|
public IssueTemplateHasAudioTrack(ICheck check)
|
|
|
|
: base(check, IssueType.Problem, "\"{0}\" has an audio track.")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public Issue Create(string filename) => new Issue(this, filename);
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:53:23 +08:00
|
|
|
public class IssueTemplateFileError : IssueTemplate
|
2021-07-13 10:17:41 +08:00
|
|
|
{
|
2021-10-11 19:53:23 +08:00
|
|
|
public IssueTemplateFileError(ICheck check)
|
|
|
|
: base(check, IssueType.Error, "Could not check whether \"{0}\" has an audio track ({1}).")
|
2021-07-13 10:17:41 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-10-11 19:53:23 +08:00
|
|
|
public Issue Create(string filename, string errorReason) => new Issue(this, filename, errorReason);
|
2021-07-13 10:17:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class IssueTemplateMissingFile : IssueTemplate
|
|
|
|
{
|
|
|
|
public IssueTemplateMissingFile(ICheck check)
|
|
|
|
: base(check, IssueType.Warning, "Could not check whether \"{0}\" has an audio track, because it is missing.")
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public Issue Create(string filename) => new Issue(this, filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|