forked from ppy/osu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBeatmapInfoExtensions.cs
More file actions
109 lines (91 loc) · 5.11 KB
/
Copy pathBeatmapInfoExtensions.cs
File metadata and controls
109 lines (91 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 System.Linq;
using osu.Framework.Localisation;
using osu.Game.Online.API;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Objects.Types;
using osu.Game.Screens.Select;
namespace osu.Game.Beatmaps
{
public static class BeatmapInfoExtensions
{
/// <summary>
/// Given an <see cref="IBeatmap"/>, update length, BPM and object counts.
/// </summary>
public static void UpdateStatisticsFromBeatmap(this BeatmapInfo beatmapInfo, IBeatmap beatmap)
{
beatmapInfo.Length = beatmap.CalculatePlayableLength();
beatmapInfo.BPM = 60000 / beatmap.GetMostCommonBeatLength();
beatmapInfo.EndTimeObjectCount = beatmap.HitObjects.Count(h => h is IHasDuration);
beatmapInfo.TotalObjectCount = beatmap.HitObjects.Count;
// Compute the maximum repeat count for pool sizing purposes.
// RepeatCount is set during decoding and is available on the raw (pre-ApplyDefaults) beatmap,
// so this can be derived without a full conversion pass.
beatmapInfo.MaxSliderRepeats = beatmap.HitObjects
.OfType<IHasRepeats>()
.Select(h => h.RepeatCount)
.DefaultIfEmpty(0)
.Max();
// Slider tick objects are generated as nested hit objects after defaults have been applied.
// This method is called both with raw and playable beatmaps; for raw beatmaps this will
// naturally become 0 and later be backfilled by background processing.
beatmapInfo.MaxSliderTicks = beatmap.HitObjects
.Select(h => h.NestedHitObjects.Count(n => n is ISliderTick))
.DefaultIfEmpty(0)
.Max();
}
/// <summary>
/// A user-presentable display title representing this beatmap.
/// </summary>
public static string GetDisplayTitle(this IBeatmapInfo beatmapInfo) => $"{beatmapInfo.Metadata.GetDisplayTitle()} {getVersionString(beatmapInfo)}".Trim();
/// <summary>
/// A user-presentable display title representing this beatmap, with localisation handling for potentially romanisable fields.
/// </summary>
public static RomanisableString GetDisplayTitleRomanisable(this IBeatmapInfo beatmapInfo, bool includeDifficultyName = true, bool includeCreator = true)
{
var metadata = beatmapInfo.Metadata.GetDisplayTitleRomanisable(includeCreator);
if (includeDifficultyName)
{
string versionString = getVersionString(beatmapInfo);
return new RomanisableString($"{metadata.GetPreferred(true)} {versionString}".Trim(), $"{metadata.GetPreferred(false)} {versionString}".Trim());
}
return new RomanisableString($"{metadata.GetPreferred(true)}".Trim(), $"{metadata.GetPreferred(false)}".Trim());
}
public static bool Match(this IBeatmapInfo beatmapInfo, params FilterCriteria.OptionalTextFilter[] filters)
{
foreach (var filter in filters)
{
if (filter.Matches(beatmapInfo.DifficultyName))
continue;
if (BeatmapMetadataInfoExtensions.Match(beatmapInfo.Metadata, filter))
continue;
// failed to match a single filter at all - fail the whole match.
return false;
}
// got through all filters without failing any - pass the whole match.
return true;
}
private static string getVersionString(IBeatmapInfo beatmapInfo) => string.IsNullOrEmpty(beatmapInfo.DifficultyName) ? string.Empty : $"[{beatmapInfo.DifficultyName}]";
/// <summary>
/// Whether gameplay is allowed for this beatmap with the provided ruleset (via conversion or direct compatibility).
/// </summary>
public static bool AllowGameplayWithRuleset(this IBeatmapInfo beatmap, RulesetInfo ruleset, bool allowConversion)
{
if (beatmap.Ruleset.ShortName == ruleset.ShortName)
return true;
if (allowConversion && beatmap.Ruleset.OnlineID == 0 && ruleset.OnlineID != 0)
return true;
return false;
}
/// <summary>
/// Get the beatmap info page URL, or <c>null</c> if unavailable.
/// </summary>
public static string? GetOnlineURL(this IBeatmapInfo beatmapInfo, IAPIProvider api, IRulesetInfo? ruleset = null)
{
if (beatmapInfo.OnlineID <= 0 || beatmapInfo.BeatmapSet == null)
return null;
return $@"{api.Endpoints.WebsiteUrl}/beatmapsets/{beatmapInfo.BeatmapSet.OnlineID}#{ruleset?.ShortName ?? beatmapInfo.Ruleset.ShortName}/{beatmapInfo.OnlineID}";
}
}
}