mirror of
https://github.com/ppy/osu.git
synced 2025-03-17 22:17:25 +08:00
Add GetDisplayString()
extension to handle all model interface types globally
This commit is contained in:
parent
a1b55d6490
commit
5ec8288508
@ -15,6 +15,7 @@ using osu.Framework.Extensions.IEnumerableExtensions;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Framework.Threading;
|
||||
using osu.Game.Extensions;
|
||||
using osu.Game.IO;
|
||||
using osu.Game.IO.Archives;
|
||||
using osu.Game.IPC;
|
||||
@ -192,7 +193,7 @@ namespace osu.Game.Database
|
||||
else
|
||||
{
|
||||
notification.CompletionText = imported.Count == 1
|
||||
? $"Imported {imported.First().Value}!"
|
||||
? $"Imported {imported.First().Value.GetDisplayString()}!"
|
||||
: $"Imported {imported.Count} {HumanisedModelName}s!";
|
||||
|
||||
if (imported.Count > 0 && PostImport != null)
|
||||
|
@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using Humanizer;
|
||||
using osu.Framework.Logging;
|
||||
using osu.Framework.Platform;
|
||||
using osu.Game.Extensions;
|
||||
using osu.Game.Online.API;
|
||||
using osu.Game.Overlays.Notifications;
|
||||
|
||||
@ -50,7 +51,7 @@ namespace osu.Game.Database
|
||||
|
||||
DownloadNotification notification = new DownloadNotification
|
||||
{
|
||||
Text = $"Downloading {request.Model}",
|
||||
Text = $"Downloading {request.Model.GetDisplayString()}",
|
||||
};
|
||||
|
||||
request.DownloadProgressed += progress =>
|
||||
|
61
osu.Game/Extensions/ModelExtensions.cs
Normal file
61
osu.Game/Extensions/ModelExtensions.cs
Normal file
@ -0,0 +1,61 @@
|
||||
// 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 osu.Game.Beatmaps;
|
||||
using osu.Game.Rulesets;
|
||||
using osu.Game.Scoring;
|
||||
using osu.Game.Users;
|
||||
|
||||
namespace osu.Game.Extensions
|
||||
{
|
||||
public static class ModelExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a user-facing string representing the <paramref name="model"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Non-interface types without special handling will fall back to <see cref="object.ToString()"/>.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Warning: This method is _purposefully_ not called <c>GetDisplayTitle()</c> like the others, because otherwise
|
||||
/// extension method type inference rules cause this method to call itself and cause a stack overflow.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public static string GetDisplayString(this object model)
|
||||
{
|
||||
string result = null;
|
||||
|
||||
switch (model)
|
||||
{
|
||||
case IBeatmapSetInfo beatmapSetInfo:
|
||||
result = beatmapSetInfo.Metadata?.GetDisplayTitle();
|
||||
break;
|
||||
|
||||
case IBeatmapInfo beatmapInfo:
|
||||
result = beatmapInfo.GetDisplayTitle();
|
||||
break;
|
||||
|
||||
case IBeatmapMetadataInfo metadataInfo:
|
||||
result = metadataInfo.GetDisplayTitle();
|
||||
break;
|
||||
|
||||
case IScoreInfo scoreInfo:
|
||||
result = scoreInfo.GetDisplayTitle();
|
||||
break;
|
||||
|
||||
case IRulesetInfo rulesetInfo:
|
||||
result = rulesetInfo.Name;
|
||||
break;
|
||||
|
||||
case IUser user:
|
||||
result = user.Username;
|
||||
break;
|
||||
}
|
||||
|
||||
// fallback in case none of the above happens to match.
|
||||
result ??= model.ToString();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user