1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:20:04 +08:00

Perform ordering using IComparable instead

This commit is contained in:
Dean Herbert 2022-01-27 15:59:20 +09:00
parent f30d63107a
commit 5637fd64d6
7 changed files with 21 additions and 10 deletions

View File

@ -25,7 +25,7 @@ namespace osu.Game.Tests.NonVisual
new RulesetInfo("custom3", "Custom Ruleset 3", string.Empty, -1),
};
var orderedRulesets = rulesets.OrderBy(r => r.SortID);
var orderedRulesets = rulesets.OrderBy(r => r);
// Ensure all customs are after official.
Assert.That(orderedRulesets.Select(r => r.OnlineID), Is.EqualTo(new[] { 0, 2, -1, -1, -1, -1 }));

View File

@ -42,6 +42,8 @@ namespace osu.Game.Rulesets
public bool Equals(EFRulesetInfo other) => other != null && ID == other.ID && Available == other.Available && Name == other.Name && InstantiationInfo == other.InstantiationInfo;
public int CompareTo(RulesetInfo other) => ID?.CompareTo(other.ID) ?? -1;
public override bool Equals(object obj) => obj is EFRulesetInfo rulesetInfo && Equals(rulesetInfo);
public bool Equals(IRulesetInfo other) => other is RulesetInfo b && Equals(b);

View File

@ -11,7 +11,7 @@ namespace osu.Game.Rulesets
/// <summary>
/// A representation of a ruleset's metadata.
/// </summary>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>
public interface IRulesetInfo : IHasOnlineID<int>, IEquatable<IRulesetInfo>, IComparable<RulesetInfo>
{
/// <summary>
/// The user-exposed name of this ruleset.

View File

@ -24,11 +24,6 @@ namespace osu.Game.Rulesets
public string InstantiationInfo { get; set; } = string.Empty;
/// <summary>
/// A best effort sort ID which provides stable ordering and puts online rulesets before non-online rulesets.
/// </summary>
public int SortID => OnlineID >= 0 ? OnlineID : Math.Abs(ShortName.GetHashCode());
public RulesetInfo(string shortName, string name, string instantiationInfo, int onlineID)
{
ShortName = shortName;
@ -62,6 +57,20 @@ namespace osu.Game.Rulesets
public bool Equals(IRulesetInfo? other) => other is RulesetInfo b && Equals(b);
public int CompareTo(RulesetInfo other)
{
// Official rulesets are always given precedence for the time being.
if (OnlineID >= 0)
{
if (other.OnlineID >= 0)
return OnlineID.CompareTo(other.OnlineID);
return -1;
}
return string.Compare(ShortName, other.ShortName, StringComparison.Ordinal);
}
public override int GetHashCode()
{
// Importantly, ignore the underlying realm hash code, as it will usually not match.

View File

@ -163,7 +163,7 @@ namespace osu.Game.Rulesets
}
}
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r.SortID));
availableRulesets.AddRange(detachedRulesets.OrderBy(r => r));
});
}

View File

@ -89,7 +89,7 @@ namespace osu.Game.Screens.Select.Carousel
{
default:
case SortMode.Difficulty:
int ruleset = BeatmapInfo.Ruleset.SortID.CompareTo(otherBeatmap.BeatmapInfo.Ruleset.SortID);
int ruleset = BeatmapInfo.Ruleset.CompareTo(otherBeatmap.BeatmapInfo.Ruleset);
if (ruleset != 0) return ruleset;
return BeatmapInfo.StarRating.CompareTo(otherBeatmap.BeatmapInfo.StarRating);

View File

@ -39,7 +39,7 @@ namespace osu.Game.Screens.Select.Carousel
beatmapSet.Beatmaps
.Where(b => !b.Hidden)
.OrderBy(b => b.Ruleset.SortID)
.OrderBy(b => b.Ruleset)
.ThenBy(b => b.StarRating)
.Select(b => new CarouselBeatmap(b))
.ForEach(AddChild);