// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.IO; using System.Text.RegularExpressions; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.IO; using osu.Game.Online.API.Requests.Responses; using osu.Game.Rulesets; using osu.Game.Scoring; using osu.Game.Users; namespace osu.Game.Extensions { public static class ModelExtensions { private static readonly Regex invalid_filename_chars = new Regex(@"(?!$)[^A-Za-z0-9_()[\]. \-]", RegexOptions.Compiled); /// /// Get the relative path in osu! storage for this file. /// /// The file info. /// A relative file path. public static string GetStoragePath(this IFileInfo fileInfo) => Path.Combine(fileInfo.Hash.Remove(1), fileInfo.Hash.Remove(2), fileInfo.Hash); /// /// Returns a user-facing string representing the . /// /// /// /// Non-interface types without special handling will fall back to . /// /// /// Warning: This method is _purposefully_ not called GetDisplayTitle() like the others, because otherwise /// extension method type inference rules cause this method to call itself and cause a stack overflow. /// /// 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() ?? @"null"; return result; } /// /// Check whether this 's online ID is within the range that defines it as a legacy ruleset (ie. either osu!, osu!taiko, osu!catch or osu!mania). /// public static bool IsLegacyRuleset(this IRulesetInfo ruleset) => ruleset.OnlineID >= 0 && ruleset.OnlineID <= ILegacyRuleset.MAX_LEGACY_RULESET_ID; /// /// Check whether the online ID of two s match. /// /// The instance to compare. /// The other instance to compare against. /// Whether online IDs match. If either instance is missing an online ID, this will return false. public static bool MatchesOnlineID(this IBeatmapSetInfo? instance, IBeatmapSetInfo? other) => matchesOnlineID(instance, other); /// /// Check whether the online ID of two s match. /// /// The instance to compare. /// The other instance to compare against. /// Whether online IDs match. If either instance is missing an online ID, this will return false. public static bool MatchesOnlineID(this IBeatmapInfo? instance, IBeatmapInfo? other) => matchesOnlineID(instance, other); /// /// Check whether the online ID of two s match. /// /// The instance to compare. /// The other instance to compare against. /// Whether online IDs match. If either instance is missing an online ID, this will return false. public static bool MatchesOnlineID(this IRulesetInfo? instance, IRulesetInfo? other) => matchesOnlineID(instance, other); /// /// Check whether the online ID of two s match. /// /// The instance to compare. /// The other instance to compare against. /// Whether online IDs match. If either instance is missing an online ID, this will return false. public static bool MatchesOnlineID(this APIUser? instance, APIUser? other) => matchesOnlineID(instance, other); /// /// Check whether the online ID of two s match. /// /// The instance to compare. /// The other instance to compare against. /// Whether online IDs match. If either instance is missing an online ID, this will return false. public static bool MatchesOnlineID(this IScoreInfo? instance, IScoreInfo? other) => matchesOnlineID(instance, other); private static bool matchesOnlineID(this IHasOnlineID? instance, IHasOnlineID? other) { if (instance == null || other == null) return false; if (instance.OnlineID < 0 || other.OnlineID < 0) return false; return instance.OnlineID.Equals(other.OnlineID); } private static bool matchesOnlineID(this IHasOnlineID? instance, IHasOnlineID? other) { if (instance == null || other == null) return false; if (instance.OnlineID < 0 || other.OnlineID < 0) return false; return instance.OnlineID.Equals(other.OnlineID); } /// /// Create a valid filename which should work across all platforms. /// /// /// This function replaces all characters not included in a very pessimistic list which should be compatible /// across all operating systems. We are using this in place of as /// that function does not have per-platform considerations (and is only made to work on windows). /// public static string GetValidFilename(this string filename) => invalid_filename_chars.Replace(filename, "_"); } }