From 010720de74c67df74f140aa2263cb85c4054b563 Mon Sep 17 00:00:00 2001 From: Naxess <30292137+Naxesss@users.noreply.github.com> Date: Sun, 18 Apr 2021 02:07:33 +0200 Subject: [PATCH] Factor out general file presence checking This allows us to use the same method of checking for other files that should exist, for example the audio file. By using the same method, they all share test cases too. --- ...groundTest.cs => CheckFilePresenceTest.cs} | 10 +++---- osu.Game/Rulesets/Edit/BeatmapVerifier.cs | 7 +++-- .../Edit/Checks/CheckBackgroundPresence.cs | 15 ++++++++++ ...heckBackground.cs => CheckFilePresence.cs} | 28 +++++++++++-------- 4 files changed, 41 insertions(+), 19 deletions(-) rename osu.Game.Tests/Editing/Checks/{CheckBackgroundTest.cs => CheckFilePresenceTest.cs} (84%) create mode 100644 osu.Game/Rulesets/Edit/Checks/CheckBackgroundPresence.cs rename osu.Game/Rulesets/Edit/Checks/{CheckBackground.cs => CheckFilePresence.cs} (57%) diff --git a/osu.Game.Tests/Editing/Checks/CheckBackgroundTest.cs b/osu.Game.Tests/Editing/Checks/CheckFilePresenceTest.cs similarity index 84% rename from osu.Game.Tests/Editing/Checks/CheckBackgroundTest.cs rename to osu.Game.Tests/Editing/Checks/CheckFilePresenceTest.cs index d61f0989a6..1149115b55 100644 --- a/osu.Game.Tests/Editing/Checks/CheckBackgroundTest.cs +++ b/osu.Game.Tests/Editing/Checks/CheckFilePresenceTest.cs @@ -12,15 +12,15 @@ using osu.Game.Tests.Beatmaps; namespace osu.Game.Tests.Editing.Checks { [TestFixture] - public class CheckBackgroundTest + public class CheckFilePresenceTest { - private CheckBackground check; + private CheckBackgroundPresence check; private WorkingBeatmap beatmap; [SetUp] public void Setup() { - check = new CheckBackground(); + check = new CheckBackgroundPresence(); beatmap = new TestWorkingBeatmap(new Beatmap { BeatmapInfo = new BeatmapInfo @@ -51,7 +51,7 @@ namespace osu.Game.Tests.Editing.Checks var issues = check.Run(beatmap).ToList(); Assert.That(issues, Has.Count.EqualTo(1)); - Assert.That(issues.Single().Template is CheckBackground.IssueTemplateDoesNotExist); + Assert.That(issues.Single().Template is CheckFilePresence.IssueTemplateDoesNotExist); } [Test] @@ -62,7 +62,7 @@ namespace osu.Game.Tests.Editing.Checks var issues = check.Run(beatmap).ToList(); Assert.That(issues, Has.Count.EqualTo(1)); - Assert.That(issues.Single().Template is CheckBackground.IssueTemplateNoneSet); + Assert.That(issues.Single().Template is CheckFilePresence.IssueTemplateNoneSet); } } } diff --git a/osu.Game/Rulesets/Edit/BeatmapVerifier.cs b/osu.Game/Rulesets/Edit/BeatmapVerifier.cs index 24a4f473de..90447049f8 100644 --- a/osu.Game/Rulesets/Edit/BeatmapVerifier.cs +++ b/osu.Game/Rulesets/Edit/BeatmapVerifier.cs @@ -16,8 +16,11 @@ namespace osu.Game.Rulesets.Edit { private readonly List checks = new List { - new CheckBackground(), - new CheckBackgroundQuality() + // Resources + new CheckBackgroundPresence(), + new CheckBackgroundQuality(), + // Audio + new CheckAudioPresence(), }; public IEnumerable Run(WorkingBeatmap workingBeatmap) => checks.SelectMany(check => check.Run(workingBeatmap)); diff --git a/osu.Game/Rulesets/Edit/Checks/CheckBackgroundPresence.cs b/osu.Game/Rulesets/Edit/Checks/CheckBackgroundPresence.cs new file mode 100644 index 0000000000..3a229b889b --- /dev/null +++ b/osu.Game/Rulesets/Edit/Checks/CheckBackgroundPresence.cs @@ -0,0 +1,15 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Beatmaps; +using osu.Game.Rulesets.Edit.Checks.Components; + +namespace osu.Game.Rulesets.Edit.Checks +{ + public class CheckBackgroundPresence : CheckFilePresence + { + protected override CheckCategory Category => CheckCategory.Resources; + protected override string TypeOfFile => "background"; + protected override string GetFilename(IWorkingBeatmap workingBeatmap) => workingBeatmap.Beatmap.Metadata?.BackgroundFile; + } +} diff --git a/osu.Game/Rulesets/Edit/Checks/CheckBackground.cs b/osu.Game/Rulesets/Edit/Checks/CheckFilePresence.cs similarity index 57% rename from osu.Game/Rulesets/Edit/Checks/CheckBackground.cs rename to osu.Game/Rulesets/Edit/Checks/CheckFilePresence.cs index 71821b8073..37fa79568f 100644 --- a/osu.Game/Rulesets/Edit/Checks/CheckBackground.cs +++ b/osu.Game/Rulesets/Edit/Checks/CheckFilePresence.cs @@ -7,9 +7,13 @@ using osu.Game.Rulesets.Edit.Checks.Components; namespace osu.Game.Rulesets.Edit.Checks { - public class CheckBackground : ICheck + public abstract class CheckFilePresence : ICheck { - public CheckMetadata Metadata { get; } = new CheckMetadata(CheckCategory.Resources, "Missing background"); + protected abstract CheckCategory Category { get; } + protected abstract string TypeOfFile { get; } + protected abstract string GetFilename(IWorkingBeatmap workingBeatmap); + + public CheckMetadata Metadata => new CheckMetadata(Category, $"Missing {TypeOfFile}"); public IEnumerable PossibleTemplates => new IssueTemplate[] { @@ -19,41 +23,41 @@ namespace osu.Game.Rulesets.Edit.Checks public IEnumerable Run(IWorkingBeatmap workingBeatmap) { - string backgroundFile = workingBeatmap.Beatmap.Metadata?.BackgroundFile; + var filename = GetFilename(workingBeatmap); - if (backgroundFile == null) + if (filename == null) { - yield return new IssueTemplateNoneSet(this).Create(); + yield return new IssueTemplateNoneSet(this).Create(TypeOfFile); yield break; } - // If the background is set, also make sure it still exists. - var storagePath = workingBeatmap.Beatmap.BeatmapInfo.BeatmapSet.GetPathForFile(backgroundFile); + // If the file is set, also make sure it still exists. + var storagePath = workingBeatmap.Beatmap.BeatmapInfo.BeatmapSet.GetPathForFile(filename); if (storagePath != null) yield break; - yield return new IssueTemplateDoesNotExist(this).Create(backgroundFile); + yield return new IssueTemplateDoesNotExist(this).Create(TypeOfFile, filename); } public class IssueTemplateNoneSet : IssueTemplate { public IssueTemplateNoneSet(ICheck check) - : base(check, IssueType.Problem, "No background has been set.") + : base(check, IssueType.Problem, "No {0} has been set.") { } - public Issue Create() => new Issue(this); + public Issue Create(string typeOfFile) => new Issue(this, typeOfFile); } public class IssueTemplateDoesNotExist : IssueTemplate { public IssueTemplateDoesNotExist(ICheck check) - : base(check, IssueType.Problem, "The background file \"{0}\" does not exist.") + : base(check, IssueType.Problem, "The {0} file \"{1}\" does not exist.") { } - public Issue Create(string filename) => new Issue(this, filename); + public Issue Create(string typeOfFile, string filename) => new Issue(this, typeOfFile, filename); } } }