Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Centralise supported file extensions to one helper class #30915

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion osu.Game/Beatmaps/BeatmapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ public void DeleteVideos(List<BeatmapSetInfo> items, bool silent = false)
// user requested abort
return;

var video = b.Files.FirstOrDefault(f => OsuGameBase.VIDEO_EXTENSIONS.Any(ex => f.Filename.EndsWith(ex, StringComparison.OrdinalIgnoreCase)));
var video = b.Files.FirstOrDefault(f => SupportedExtensions.VIDEO_EXTENSIONS.Any(ex => f.Filename.EndsWith(ex, StringComparison.OrdinalIgnoreCase)));

if (video != null)
{
Expand Down
3 changes: 2 additions & 1 deletion osu.Game/Beatmaps/Formats/LegacyBeatmapDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using osu.Game.Rulesets.Objects.Legacy;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Screens.Edit;
using osu.Game.Utils;

namespace osu.Game.Beatmaps.Formats
{
Expand Down Expand Up @@ -446,7 +447,7 @@ private void handleEvent(string line)
// Some very old beatmaps had incorrect type specifications for their backgrounds (ie. using 1 for VIDEO
// instead of 0 for BACKGROUND). To handle this gracefully, check the file extension against known supported
// video extensions and handle similar to a background if it doesn't match.
if (!OsuGameBase.VIDEO_EXTENSIONS.Contains(Path.GetExtension(filename).ToLowerInvariant()))
if (!SupportedExtensions.VIDEO_EXTENSIONS.Contains(Path.GetExtension(filename).ToLowerInvariant()))
{
beatmap.BeatmapInfo.Metadata.BackgroundFile = filename;
lineSupportedByEncoder = true;
Expand Down
3 changes: 2 additions & 1 deletion osu.Game/Beatmaps/Formats/LegacyStoryboardDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using osu.Game.IO;
using osu.Game.Storyboards;
using osu.Game.Storyboards.Commands;
using osu.Game.Utils;
using osuTK;
using osuTK.Graphics;

Expand Down Expand Up @@ -112,7 +113,7 @@ private void handleEvents(string line)
//
// This avoids potential weird crashes when ffmpeg attempts to parse an image file as a video
// (see https://github.com/ppy/osu/issues/22829#issuecomment-1465552451).
if (!OsuGameBase.VIDEO_EXTENSIONS.Contains(Path.GetExtension(path).ToLowerInvariant()))
if (!SupportedExtensions.VIDEO_EXTENSIONS.Contains(Path.GetExtension(path).ToLowerInvariant()))
break;

storyboard.GetLayer("Video").Add(storyboardSprite = new StoryboardVideo(path, offset));
Expand Down
27 changes: 11 additions & 16 deletions osu.Game/Graphics/UserInterfaceV2/OsuFileSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2.FileSelection;
using osu.Game.Overlays;
using osu.Game.Utils;

namespace osu.Game.Graphics.UserInterfaceV2
{
Expand Down Expand Up @@ -96,24 +97,18 @@ protected override IconUsage? Icon
{
get
{
if (OsuGameBase.VIDEO_EXTENSIONS.Contains(File.Extension.ToLowerInvariant()))
string extension = File.Extension.ToLowerInvariant();

if (SupportedExtensions.VIDEO_EXTENSIONS.Contains(extension))
return FontAwesome.Regular.FileVideo;

switch (File.Extension)
{
case @".ogg":
case @".mp3":
case @".wav":
return FontAwesome.Regular.FileAudio;

case @".jpg":
case @".jpeg":
case @".png":
return FontAwesome.Regular.FileImage;

default:
return FontAwesome.Regular.File;
}
if (SupportedExtensions.AUDIO_EXTENSIONS.Contains(extension))
return FontAwesome.Regular.FileAudio;

if (SupportedExtensions.IMAGE_EXTENSIONS.Contains(extension))
return FontAwesome.Regular.FileImage;

return FontAwesome.Regular.File;
}
}

Expand Down
2 changes: 0 additions & 2 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ namespace osu.Game
[Cached(typeof(OsuGameBase))]
public partial class OsuGameBase : Framework.Game, ICanAcceptFiles, IBeatSyncProvider
{
public static readonly string[] VIDEO_EXTENSIONS = { ".mp4", ".mov", ".avi", ".flv", ".mpg", ".wmv", ".m4v" };

#if DEBUG
public const string GAME_NAME = "osu! (development)";
#else
Expand Down
3 changes: 2 additions & 1 deletion osu.Game/Overlays/SkinEditor/SkinEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using osu.Game.Skinning;
using osu.Framework.Graphics.Cursor;
using osu.Game.Input.Bindings;
using osu.Game.Utils;

namespace osu.Game.Overlays.SkinEditor
{
Expand Down Expand Up @@ -709,7 +710,7 @@ public Task Import(params string[] paths)

Task ICanAcceptFiles.Import(ImportTask[] tasks, ImportParameters parameters) => throw new NotImplementedException();

public IEnumerable<string> HandledExtensions => new[] { ".jpg", ".jpeg", ".png" };
public IEnumerable<string> HandledExtensions => SupportedExtensions.IMAGE_EXTENSIONS;

#endregion

Expand Down
5 changes: 2 additions & 3 deletions osu.Game/Rulesets/Edit/Checks/Components/AudioCheckUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

using System.IO;
using System.Linq;
using osu.Game.Utils;

namespace osu.Game.Rulesets.Edit.Checks.Components
{
public static class AudioCheckUtils
{
public static readonly string[] AUDIO_EXTENSIONS = { "mp3", "ogg", "wav" };

public static bool HasAudioExtension(string filename) => AUDIO_EXTENSIONS.Any(Path.GetExtension(filename).ToLowerInvariant().EndsWith);
public static bool HasAudioExtension(string filename) => SupportedExtensions.AUDIO_EXTENSIONS.Contains(Path.GetExtension(filename).ToLowerInvariant());
}
}
5 changes: 3 additions & 2 deletions osu.Game/Screens/Edit/Setup/ResourcesSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Overlays;
using osu.Game.Localisation;
using osu.Game.Utils;

namespace osu.Game.Screens.Edit.Setup
{
Expand Down Expand Up @@ -48,12 +49,12 @@ private void load()

Children = new Drawable[]
{
backgroundChooser = new FormFileSelector(".jpg", ".jpeg", ".png")
backgroundChooser = new FormFileSelector(SupportedExtensions.IMAGE_EXTENSIONS)
{
Caption = GameplaySettingsStrings.BackgroundHeader,
PlaceholderText = EditorSetupStrings.ClickToSelectBackground,
},
audioTrackChooser = new FormFileSelector(".mp3", ".ogg")
audioTrackChooser = new FormFileSelector(SupportedExtensions.AUDIO_EXTENSIONS)
{
Caption = EditorSetupStrings.AudioTrack,
PlaceholderText = EditorSetupStrings.ClickToSelectTrack,
Expand Down
11 changes: 6 additions & 5 deletions osu.Game/Skinning/SkinnableSprite.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
Expand All @@ -14,6 +14,7 @@
using osu.Game.Graphics.Sprites;
using osu.Game.Localisation.SkinComponents;
using osu.Game.Overlays.Settings;
using osu.Game.Utils;
using osuTK;

namespace osu.Game.Skinning
Expand Down Expand Up @@ -93,10 +94,10 @@ protected override void LoadComplete()
// but that requires further thought.
var highestPrioritySkin = getHighestPriorityUserSkin(((SkinnableSprite)SettingSourceObject).source.AllSources) as Skin;

string[]? availableFiles = highestPrioritySkin?.SkinInfo.PerformRead(s => s.Files
.Where(f => f.Filename.EndsWith(".png", StringComparison.Ordinal)
|| f.Filename.EndsWith(".jpg", StringComparison.Ordinal))
.Select(f => f.Filename).Distinct()).ToArray();
string[]? availableFiles = highestPrioritySkin?.SkinInfo.PerformRead(
s => s.Files
.Where(f => SupportedExtensions.IMAGE_EXTENSIONS.Contains(Path.GetExtension(f.Filename).ToLowerInvariant()))
.Select(f => f.Filename).Distinct()).ToArray();

if (availableFiles?.Length > 0)
Items = availableFiles;
Expand Down
5 changes: 2 additions & 3 deletions osu.Game/Storyboards/Storyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using osu.Game.Beatmaps;
using osu.Game.Rulesets.Mods;
using osu.Game.Storyboards.Drawables;
using osu.Game.Utils;

namespace osu.Game.Storyboards
{
Expand Down Expand Up @@ -96,8 +97,6 @@ public bool ReplacesBackground
public virtual DrawableStoryboard CreateDrawable(IReadOnlyList<Mod>? mods = null) =>
new DrawableStoryboard(this, mods);

private static readonly string[] image_extensions = { @".png", @".jpg" };

public virtual string? GetStoragePathFromStoryboardPath(string path)
{
string? resolvedPath = null;
Expand All @@ -109,7 +108,7 @@ public virtual DrawableStoryboard CreateDrawable(IReadOnlyList<Mod>? mods = null
else
{
// Some old storyboards don't include a file extension, so let's best guess at one.
foreach (string ext in image_extensions)
foreach (string ext in SupportedExtensions.IMAGE_EXTENSIONS)
{
if ((resolvedPath = BeatmapInfo.BeatmapSet?.GetPathForFile($"{path}{ext}")) != null)
break;
Expand Down
19 changes: 19 additions & 0 deletions osu.Game/Utils/SupportedExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace osu.Game.Utils
{
public static class SupportedExtensions
{
public static readonly string[] VIDEO_EXTENSIONS = [@".mp4", @".mov", @".avi", @".flv", @".mpg", @".wmv", @".m4v"];
public static readonly string[] AUDIO_EXTENSIONS = [@".mp3", @".ogg", @".wav"];
public static readonly string[] IMAGE_EXTENSIONS = [@".jpg", @".jpeg", @".png"];

public static readonly string[] ALL_EXTENSIONS =
[
..VIDEO_EXTENSIONS,
..AUDIO_EXTENSIONS,
..IMAGE_EXTENSIONS
];
Comment on lines +12 to +17
Copy link
Collaborator Author

@bdach bdach Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primer for what this is: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-12#collection-expressions

I find this to be a compelling usage of the syntax because any other way to write it in C# is veeeeeeeeeery clunky, but I will go the clunky route if usage of this syntax is considered not wanted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it's fine as it shortens code while not looking extremely odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'm okay with this one.

}
}