// Copyright (c) ppy Pty Ltd . 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 { /// /// Given an , update length, BPM and object counts. /// 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; } /// /// A user-presentable display title representing this beatmap. /// public static string GetDisplayTitle(this IBeatmapInfo beatmapInfo) => $"{beatmapInfo.Metadata.GetDisplayTitle()} {getVersionString(beatmapInfo)}".Trim(); /// /// A user-presentable display title representing this beatmap, with localisation handling for potentially romanisable fields. /// 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}]"; /// /// Whether gameplay is allowed for this beatmap with the provided ruleset (via conversion or direct compatibility). /// 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; } /// /// Get the beatmap info page URL, or null if unavailable. /// 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}"; } } }