Skip to content

Commit b1296b0

Browse files
authored
Merge pull request #34666 from Hiviexd/verify/check-inconsistent-audio
Add verify check for inconsistent audio usage
2 parents 7c3249c + 14530fe commit b1296b0

File tree

3 files changed

+205
-0
lines changed

3 files changed

+205
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System.Linq;
5+
using NUnit.Framework;
6+
using osu.Game.Beatmaps;
7+
using osu.Game.Models;
8+
using osu.Game.Rulesets.Edit;
9+
using osu.Game.Rulesets.Edit.Checks;
10+
using osu.Game.Rulesets.Objects;
11+
using osu.Game.Tests.Beatmaps;
12+
13+
namespace osu.Game.Tests.Editing.Checks
14+
{
15+
[TestFixture]
16+
public class CheckInconsistentAudioTest
17+
{
18+
private CheckInconsistentAudio check = null!;
19+
20+
[SetUp]
21+
public void Setup()
22+
{
23+
check = new CheckInconsistentAudio();
24+
}
25+
26+
[Test]
27+
public void TestConsistentAudio()
28+
{
29+
var beatmaps = createBeatmapSetWithAudio("audio.mp3", "audio.mp3");
30+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
31+
32+
Assert.That(check.Run(context), Is.Empty);
33+
}
34+
35+
[Test]
36+
public void TestInconsistentAudio()
37+
{
38+
var beatmaps = createBeatmapSetWithAudio("audio1.mp3", "audio2.mp3");
39+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
40+
41+
var issues = check.Run(context).ToList();
42+
43+
Assert.That(issues, Has.Count.EqualTo(1));
44+
Assert.That(issues.Single().Template is CheckInconsistentAudio.IssueTemplateInconsistentAudio);
45+
Assert.That(issues.Single().ToString(), Contains.Substring("audio1.mp3"));
46+
Assert.That(issues.Single().ToString(), Contains.Substring("audio2.mp3"));
47+
}
48+
49+
[Test]
50+
public void TestInconsistentAudioWithNull()
51+
{
52+
var beatmaps = createBeatmapSetWithAudio("audio.mp3", null);
53+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
54+
55+
var issues = check.Run(context).ToList();
56+
57+
Assert.That(issues, Has.Count.EqualTo(1));
58+
Assert.That(issues.Single().Template is CheckInconsistentAudio.IssueTemplateInconsistentAudio);
59+
Assert.That(issues.Single().ToString(), Contains.Substring("audio.mp3"));
60+
Assert.That(issues.Single().ToString(), Contains.Substring("not set"));
61+
}
62+
63+
[Test]
64+
public void TestInconsistentAudioWithEmptyString()
65+
{
66+
var beatmaps = createBeatmapSetWithAudio("audio.mp3", "");
67+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
68+
69+
var issues = check.Run(context).ToList();
70+
71+
Assert.That(issues, Has.Count.EqualTo(1));
72+
Assert.That(issues.Single().Template is CheckInconsistentAudio.IssueTemplateInconsistentAudio);
73+
Assert.That(issues.Single().ToString(), Contains.Substring("audio.mp3"));
74+
Assert.That(issues.Single().ToString(), Contains.Substring("not set"));
75+
}
76+
77+
[Test]
78+
public void TestBothAudioNotSet()
79+
{
80+
var beatmaps = createBeatmapSetWithAudio("", "");
81+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
82+
83+
Assert.That(check.Run(context), Is.Empty);
84+
}
85+
86+
[Test]
87+
public void TestMultipleInconsistencies()
88+
{
89+
var beatmaps = createBeatmapSetWithAudio("audio1.mp3", "audio2.mp3", "audio3.mp3");
90+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
91+
92+
var issues = check.Run(context).ToList();
93+
94+
Assert.That(issues, Has.Count.EqualTo(2));
95+
Assert.That(issues.All(issue => issue.Template is CheckInconsistentAudio.IssueTemplateInconsistentAudio));
96+
}
97+
98+
[Test]
99+
public void TestSingleDifficulty()
100+
{
101+
var beatmaps = createBeatmapSetWithAudio("audio.mp3");
102+
var context = createContextWithMultipleDifficulties(beatmaps.First(), beatmaps);
103+
104+
Assert.That(check.Run(context), Is.Empty);
105+
}
106+
107+
private IBeatmap createBeatmapWithAudio(string audioFile, RealmNamedFileUsage? file)
108+
{
109+
var beatmap = new Beatmap<HitObject>
110+
{
111+
BeatmapInfo = new BeatmapInfo
112+
{
113+
Metadata = new BeatmapMetadata { AudioFile = audioFile },
114+
BeatmapSet = new BeatmapSetInfo()
115+
}
116+
};
117+
118+
if (file != null)
119+
beatmap.BeatmapInfo.BeatmapSet!.Files.Add(file);
120+
121+
return beatmap;
122+
}
123+
124+
private IBeatmap[] createBeatmapSetWithAudio(params string?[] audioFiles)
125+
{
126+
var beatmapSet = new BeatmapSetInfo();
127+
var beatmaps = new IBeatmap[audioFiles.Length];
128+
129+
for (int i = 0; i < audioFiles.Length; i++)
130+
{
131+
string? audioFile = audioFiles[i];
132+
var file = !string.IsNullOrEmpty(audioFile) ? CheckTestHelpers.CreateMockFile("mp3") : null;
133+
134+
beatmaps[i] = createBeatmapWithAudio(audioFile ?? "", file);
135+
beatmaps[i].BeatmapInfo.BeatmapSet = beatmapSet;
136+
beatmaps[i].BeatmapInfo.DifficultyName = $"Difficulty {i + 1}";
137+
beatmapSet.Beatmaps.Add(beatmaps[i].BeatmapInfo);
138+
}
139+
140+
return beatmaps;
141+
}
142+
143+
private BeatmapVerifierContext createContextWithMultipleDifficulties(IBeatmap currentBeatmap, IBeatmap[] allDifficulties)
144+
{
145+
var verifiedCurrentBeatmap = new BeatmapVerifierContext.VerifiedBeatmap(new TestWorkingBeatmap(currentBeatmap), currentBeatmap);
146+
var verifiedOtherBeatmaps = allDifficulties.Select(b => new BeatmapVerifierContext.VerifiedBeatmap(new TestWorkingBeatmap(b), b)).ToList();
147+
148+
return new BeatmapVerifierContext(verifiedCurrentBeatmap, verifiedOtherBeatmaps, DifficultyRating.ExpertPlus);
149+
}
150+
}
151+
}

osu.Game/Rulesets/Edit/BeatmapVerifier.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class BeatmapVerifier : IBeatmapVerifier
3131
new CheckDelayedHitsounds(),
3232
new CheckSongFormat(),
3333
new CheckHitsoundsFormat(),
34+
new CheckInconsistentAudio(),
3435

3536
// Files
3637
new CheckZeroByteFiles(),
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using osu.Game.Rulesets.Edit.Checks.Components;
7+
8+
namespace osu.Game.Rulesets.Edit.Checks
9+
{
10+
public class CheckInconsistentAudio : ICheck
11+
{
12+
public CheckMetadata Metadata => new CheckMetadata(CheckCategory.Audio, "Inconsistent audio files", CheckScope.BeatmapSet);
13+
14+
public IEnumerable<IssueTemplate> PossibleTemplates => new IssueTemplate[]
15+
{
16+
new IssueTemplateInconsistentAudio(this)
17+
};
18+
19+
public IEnumerable<Issue> Run(BeatmapVerifierContext context)
20+
{
21+
if (context.AllDifficulties.Count() <= 1)
22+
yield break;
23+
24+
var referenceBeatmap = context.CurrentDifficulty.Playable;
25+
string referenceAudioFile = referenceBeatmap.Metadata.AudioFile;
26+
27+
foreach (var beatmap in context.OtherDifficulties)
28+
{
29+
string currentAudioFile = beatmap.Playable.Metadata.AudioFile;
30+
31+
if (referenceAudioFile != currentAudioFile)
32+
{
33+
yield return new IssueTemplateInconsistentAudio(this).Create(
34+
string.IsNullOrEmpty(referenceAudioFile) ? "not set" : referenceAudioFile,
35+
beatmap.Playable.BeatmapInfo.DifficultyName,
36+
string.IsNullOrEmpty(currentAudioFile) ? "not set" : currentAudioFile
37+
);
38+
}
39+
}
40+
}
41+
42+
public class IssueTemplateInconsistentAudio : IssueTemplate
43+
{
44+
public IssueTemplateInconsistentAudio(ICheck check)
45+
: base(check, IssueType.Problem, "Inconsistent audio file between this difficulty ({0}) and \"{1}\" ({2}).")
46+
{
47+
}
48+
49+
public Issue Create(string referenceAudio, string otherDifficulty, string otherAudio)
50+
=> new Issue(this, referenceAudio, otherDifficulty, otherAudio);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)