From a67e156883fb6f80aa10e3af6701ae2417181c48 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:09:45 +0900 Subject: [PATCH 01/14] Add `IBeatmapInfo` equality support --- osu.Game/Beatmaps/BeatmapInfo.cs | 11 +++++++++++ osu.Game/Beatmaps/IBeatmapInfo.cs | 3 ++- osu.Game/Models/RealmBeatmap.cs | 15 +++++++++++++++ .../Online/API/Requests/Responses/APIBeatmap.cs | 12 ++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/osu.Game/Beatmaps/BeatmapInfo.cs b/osu.Game/Beatmaps/BeatmapInfo.cs index 5bbd48f26d..ea2346f060 100644 --- a/osu.Game/Beatmaps/BeatmapInfo.cs +++ b/osu.Game/Beatmaps/BeatmapInfo.cs @@ -151,6 +151,17 @@ namespace osu.Game.Beatmaps public override string ToString() => this.GetDisplayTitle(); + public bool Equals(IBeatmapInfo other) + { + if (other is BeatmapInfo b) + return Equals(b); + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + public bool Equals(BeatmapInfo other) { if (ID == 0 || other?.ID == 0) diff --git a/osu.Game/Beatmaps/IBeatmapInfo.cs b/osu.Game/Beatmaps/IBeatmapInfo.cs index 84ea6d3019..ab096b8897 100644 --- a/osu.Game/Beatmaps/IBeatmapInfo.cs +++ b/osu.Game/Beatmaps/IBeatmapInfo.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using osu.Game.Database; using osu.Game.Rulesets; @@ -11,7 +12,7 @@ namespace osu.Game.Beatmaps /// /// A single beatmap difficulty. /// - public interface IBeatmapInfo : IHasOnlineID + public interface IBeatmapInfo : IHasOnlineID, IEquatable { /// /// The user-specified name given to this beatmap. diff --git a/osu.Game/Models/RealmBeatmap.cs b/osu.Game/Models/RealmBeatmap.cs index 9311425cb7..d1907d3ffd 100644 --- a/osu.Game/Models/RealmBeatmap.cs +++ b/osu.Game/Models/RealmBeatmap.cs @@ -98,6 +98,21 @@ namespace osu.Game.Models #endregion + #region Implementation of IEquatable + + public bool Equals(IBeatmapInfo? other) + { + if (other is RealmBeatmap b) + return b.ID == ID; + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + + #endregion + public bool AudioEquals(RealmBeatmap? other) => other != null && BeatmapSet != null && other.BeatmapSet != null diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs index 2560502173..33098a2919 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs @@ -103,5 +103,17 @@ namespace osu.Game.Online.API.Requests.Responses public string Hash => throw new NotImplementedException(); #endregion + + #region Implementation of IEquatable + + public bool Equals(IBeatmapInfo? other) + { + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + + #endregion } } From 54cd1158a4b198e55d6747fd98124e50d29aa401 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:14:11 +0900 Subject: [PATCH 02/14] Add `IBeatmapSetInfo` equality support --- osu.Game/Beatmaps/BeatmapSetInfo.cs | 11 +++++++++++ osu.Game/Beatmaps/IBeatmapSetInfo.cs | 2 +- osu.Game/Models/RealmBeatmapSet.cs | 13 ++++++++++++- .../Online/API/Requests/Responses/APIBeatmapSet.cs | 12 ++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapSetInfo.cs b/osu.Game/Beatmaps/BeatmapSetInfo.cs index 0c93c4b9db..5bdaf26471 100644 --- a/osu.Game/Beatmaps/BeatmapSetInfo.cs +++ b/osu.Game/Beatmaps/BeatmapSetInfo.cs @@ -66,6 +66,17 @@ namespace osu.Game.Beatmaps public bool Protected { get; set; } + public bool Equals(IBeatmapSetInfo other) + { + if (other is BeatmapSetInfo b) + return Equals(b); + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + public bool Equals(BeatmapSetInfo other) { if (other == null) diff --git a/osu.Game/Beatmaps/IBeatmapSetInfo.cs b/osu.Game/Beatmaps/IBeatmapSetInfo.cs index 20c46d9063..67f38397d4 100644 --- a/osu.Game/Beatmaps/IBeatmapSetInfo.cs +++ b/osu.Game/Beatmaps/IBeatmapSetInfo.cs @@ -12,7 +12,7 @@ namespace osu.Game.Beatmaps /// /// A representation of a collection of beatmap difficulties, generally packaged as an ".osz" archive. /// - public interface IBeatmapSetInfo : IHasOnlineID + public interface IBeatmapSetInfo : IHasOnlineID, IEquatable { /// /// The date when this beatmap was imported. diff --git a/osu.Game/Models/RealmBeatmapSet.cs b/osu.Game/Models/RealmBeatmapSet.cs index 6735510422..ddf2cf48fa 100644 --- a/osu.Game/Models/RealmBeatmapSet.cs +++ b/osu.Game/Models/RealmBeatmapSet.cs @@ -53,7 +53,16 @@ namespace osu.Game.Models /// The name of the file to get the storage path of. public string? GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.File.StoragePath; - public override string ToString() => Metadata?.ToString() ?? base.ToString(); + public bool Equals(IBeatmapSetInfo? other) + { + if (other is RealmBeatmap b) + return b.ID == ID; + + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } public bool Equals(RealmBeatmapSet? other) { @@ -72,6 +81,8 @@ namespace osu.Game.Models return ReferenceEquals(this, other); } + public override string ToString() => Metadata?.ToString() ?? base.ToString(); + IEnumerable IBeatmapSetInfo.Beatmaps => Beatmaps; IEnumerable IBeatmapSetInfo.Files => Files; diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs index 168e9d5d51..f4515c4258 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs @@ -141,5 +141,17 @@ namespace osu.Game.Online.API.Requests.Responses double IBeatmapSetInfo.MaxBPM => BPM; #endregion + + #region Implementation of IEquatable + + public bool Equals(IBeatmapSetInfo? other) + { + if (OnlineID > 0 && other?.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; + } + + #endregion } } From 7349e5521a0dbbb73b50ad7bbbab2f8e7db19c26 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:10:06 +0900 Subject: [PATCH 03/14] Update `BeatmapCollection` to use `IBeatmapInfo` --- osu.Game/Collections/BeatmapCollection.cs | 2 +- osu.Game/Collections/CollectionFilterDropdown.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/osu.Game/Collections/BeatmapCollection.cs b/osu.Game/Collections/BeatmapCollection.cs index 7e4b15ecf9..1a739f824f 100644 --- a/osu.Game/Collections/BeatmapCollection.cs +++ b/osu.Game/Collections/BeatmapCollection.cs @@ -25,7 +25,7 @@ namespace osu.Game.Collections /// /// The beatmaps contained by the collection. /// - public readonly BindableList Beatmaps = new BindableList(); + public readonly BindableList Beatmaps = new BindableList(); /// /// The date when this collection was last modified. diff --git a/osu.Game/Collections/CollectionFilterDropdown.cs b/osu.Game/Collections/CollectionFilterDropdown.cs index 7067f82fd3..ad23874b2e 100644 --- a/osu.Game/Collections/CollectionFilterDropdown.cs +++ b/osu.Game/Collections/CollectionFilterDropdown.cs @@ -39,7 +39,7 @@ namespace osu.Game.Collections } private readonly IBindableList collections = new BindableList(); - private readonly IBindableList beatmaps = new BindableList(); + private readonly IBindableList beatmaps = new BindableList(); private readonly BindableList filters = new BindableList(); [Resolved(CanBeNull = true)] @@ -200,7 +200,7 @@ namespace osu.Game.Collections private IBindable beatmap { get; set; } [CanBeNull] - private readonly BindableList collectionBeatmaps; + private readonly BindableList collectionBeatmaps; [NotNull] private readonly Bindable collectionName; From 1c49c4a6029942c8fa8a07efe18a2a34534597a1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:32:39 +0900 Subject: [PATCH 04/14] Fix incorrect type check --- osu.Game/Models/RealmBeatmapSet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Models/RealmBeatmapSet.cs b/osu.Game/Models/RealmBeatmapSet.cs index ddf2cf48fa..3217abb419 100644 --- a/osu.Game/Models/RealmBeatmapSet.cs +++ b/osu.Game/Models/RealmBeatmapSet.cs @@ -55,7 +55,7 @@ namespace osu.Game.Models public bool Equals(IBeatmapSetInfo? other) { - if (other is RealmBeatmap b) + if (other is RealmBeatmapSet b) return b.ID == ID; if (OnlineID > 0 && other?.OnlineID > 0) From 63bc41556548be75ceab8fd9d1d0f5fb347d78b3 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:35:42 +0900 Subject: [PATCH 05/14] Warn on suspicious types --- osu.sln.DotSettings | 2 ++ 1 file changed, 2 insertions(+) diff --git a/osu.sln.DotSettings b/osu.sln.DotSettings index f35bdfce66..3fac94b243 100644 --- a/osu.sln.DotSettings +++ b/osu.sln.DotSettings @@ -209,6 +209,8 @@ WARNING SUGGESTION DO_NOT_SHOW + + True DO_NOT_SHOW WARNING WARNING From 1d962648c29bd3e06e2b7609698274f57256da80 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 18:48:34 +0900 Subject: [PATCH 06/14] Standardise implementations --- osu.Game/Beatmaps/BeatmapInfo.cs | 26 +++++++++++++---------- osu.Game/Beatmaps/BeatmapSetInfo.cs | 33 +++++++++++++---------------- osu.Game/Models/RealmBeatmap.cs | 22 +++++++++++++++---- osu.Game/Models/RealmBeatmapSet.cs | 32 ++++++++++++---------------- 4 files changed, 61 insertions(+), 52 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapInfo.cs b/osu.Game/Beatmaps/BeatmapInfo.cs index ea2346f060..211a5f4088 100644 --- a/osu.Game/Beatmaps/BeatmapInfo.cs +++ b/osu.Game/Beatmaps/BeatmapInfo.cs @@ -151,25 +151,29 @@ namespace osu.Game.Beatmaps public override string ToString() => this.GetDisplayTitle(); - public bool Equals(IBeatmapInfo other) + public bool Equals(BeatmapInfo other) { - if (other is BeatmapInfo b) - return Equals(b); + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; - if (OnlineID > 0 && other?.OnlineID > 0) - return other.OnlineID == OnlineID; + if (ID != 0 && other.ID != 0) + return ID == other.ID; return false; } - public bool Equals(BeatmapInfo other) + public bool Equals(IBeatmapInfo other) { - if (ID == 0 || other?.ID == 0) - // one of the two BeatmapInfos we are comparing isn't sourced from a database. - // fall back to reference equality. - return ReferenceEquals(this, other); + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; - return ID == other?.ID; + if (other is BeatmapInfo b && Equals(b)) + return true; + + if (OnlineID > 0 && other.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; } public bool AudioEquals(BeatmapInfo other) => other != null && BeatmapSet != null && other.BeatmapSet != null && diff --git a/osu.Game/Beatmaps/BeatmapSetInfo.cs b/osu.Game/Beatmaps/BeatmapSetInfo.cs index 5bdaf26471..51d049c8f9 100644 --- a/osu.Game/Beatmaps/BeatmapSetInfo.cs +++ b/osu.Game/Beatmaps/BeatmapSetInfo.cs @@ -66,32 +66,29 @@ namespace osu.Game.Beatmaps public bool Protected { get; set; } - public bool Equals(IBeatmapSetInfo other) - { - if (other is BeatmapSetInfo b) - return Equals(b); - - if (OnlineID > 0 && other?.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } - public bool Equals(BeatmapSetInfo other) { - if (other == null) - return false; + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; if (ID != 0 && other.ID != 0) return ID == other.ID; - if (OnlineBeatmapSetID.HasValue && other.OnlineBeatmapSetID.HasValue) - return OnlineBeatmapSetID == other.OnlineBeatmapSetID; + return false; + } - if (!string.IsNullOrEmpty(Hash) && !string.IsNullOrEmpty(other.Hash)) - return Hash == other.Hash; + public bool Equals(IBeatmapSetInfo other) + { + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; - return ReferenceEquals(this, other); + if (other is BeatmapSetInfo b && Equals(b)) + return true; + + if (OnlineID > 0 && other.OnlineID > 0) + return other.OnlineID == OnlineID; + + return false; } #region Implementation of IHasOnlineID diff --git a/osu.Game/Models/RealmBeatmap.cs b/osu.Game/Models/RealmBeatmap.cs index d1907d3ffd..f80d1f513a 100644 --- a/osu.Game/Models/RealmBeatmap.cs +++ b/osu.Game/Models/RealmBeatmap.cs @@ -20,7 +20,7 @@ namespace osu.Game.Models [ExcludeFromDynamicCompile] [Serializable] [MapTo("Beatmap")] - public class RealmBeatmap : RealmObject, IHasGuidPrimaryKey, IBeatmapInfo + public class RealmBeatmap : RealmObject, IHasGuidPrimaryKey, IBeatmapInfo, IEquatable { [PrimaryKey] public Guid ID { get; set; } = Guid.NewGuid(); @@ -98,14 +98,28 @@ namespace osu.Game.Models #endregion + public bool Equals(RealmBeatmap? other) + { + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; + + return ID == other.ID; + } + #region Implementation of IEquatable public bool Equals(IBeatmapInfo? other) { - if (other is RealmBeatmap b) - return b.ID == ID; + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; - if (OnlineID > 0 && other?.OnlineID > 0) + if (other is RealmBeatmap b && Equals(b)) + return true; + + if (!string.IsNullOrEmpty(Hash) && !string.IsNullOrEmpty(other.Hash)) + return Hash == other.Hash; + + if (OnlineID > 0 && other.OnlineID > 0) return other.OnlineID == OnlineID; return false; diff --git a/osu.Game/Models/RealmBeatmapSet.cs b/osu.Game/Models/RealmBeatmapSet.cs index 3217abb419..9cfa2de44d 100644 --- a/osu.Game/Models/RealmBeatmapSet.cs +++ b/osu.Game/Models/RealmBeatmapSet.cs @@ -53,32 +53,26 @@ namespace osu.Game.Models /// The name of the file to get the storage path of. public string? GetPathForFile(string filename) => Files.SingleOrDefault(f => string.Equals(f.Filename, filename, StringComparison.OrdinalIgnoreCase))?.File.StoragePath; - public bool Equals(IBeatmapSetInfo? other) - { - if (other is RealmBeatmapSet b) - return b.ID == ID; - - if (OnlineID > 0 && other?.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } - public bool Equals(RealmBeatmapSet? other) { - if (other == null) - return false; + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; - if (IsManaged && other.IsManaged) - return ID == other.ID; + return ID == other.ID; + } + + public bool Equals(IBeatmapSetInfo? other) + { + if (ReferenceEquals(this, other)) return true; + if (other == null) return false; + + if (other is RealmBeatmapSet b && Equals(b)) + return true; if (OnlineID > 0 && other.OnlineID > 0) return OnlineID == other.OnlineID; - if (!string.IsNullOrEmpty(Hash) && !string.IsNullOrEmpty(other.Hash)) - return Hash == other.Hash; - - return ReferenceEquals(this, other); + return false; } public override string ToString() => Metadata?.ToString() ?? base.ToString(); From a4c11e8813308b4bbd71489b711c9e556a7ca2f0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:34:50 +0900 Subject: [PATCH 07/14] Use extension method to compare online IDs --- osu.Game/Database/IHasOnlineID.cs | 3 +++ osu.Game/Extensions/ModelExtensions.cs | 29 ++++++++++++++++++++++++++ osu.Game/Overlays/Music/Playlist.cs | 3 ++- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/osu.Game/Database/IHasOnlineID.cs b/osu.Game/Database/IHasOnlineID.cs index 4e83ed8876..7a720989cd 100644 --- a/osu.Game/Database/IHasOnlineID.cs +++ b/osu.Game/Database/IHasOnlineID.cs @@ -1,11 +1,14 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; + #nullable enable namespace osu.Game.Database { public interface IHasOnlineID + where T : IEquatable { /// /// The server-side ID representing this instance, if one exists. Any value 0 or less denotes a missing ID (except in special cases where autoincrement is not used, like rulesets). diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index d8e0938d46..2545045d96 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using osu.Game.Beatmaps; +using osu.Game.Database; using osu.Game.Rulesets; using osu.Game.Scoring; using osu.Game.Users; @@ -57,5 +58,33 @@ namespace osu.Game.Extensions result ??= model?.ToString() ?? @"null"; return result; } + + /// + /// Check whether the online ID of two instances 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 IHasOnlineID instance, IHasOnlineID other) + { + if (instance.OnlineID < 0 || other.OnlineID < 0) + return false; + + return instance.OnlineID.Equals(other.OnlineID); + } + + /// + /// Check whether the online ID of two instances 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 IHasOnlineID instance, IHasOnlineID other) + { + if (instance.OnlineID < 0 || other.OnlineID < 0) + return false; + + return instance.OnlineID.Equals(other.OnlineID); + } } } diff --git a/osu.Game/Overlays/Music/Playlist.cs b/osu.Game/Overlays/Music/Playlist.cs index 4fe338926f..5de62ebfb8 100644 --- a/osu.Game/Overlays/Music/Playlist.cs +++ b/osu.Game/Overlays/Music/Playlist.cs @@ -7,6 +7,7 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; +using osu.Game.Extensions; using osu.Game.Graphics.Containers; using osuTK; @@ -29,7 +30,7 @@ namespace osu.Game.Overlays.Music var items = (SearchContainer>)ListContainer; foreach (var item in items.OfType()) - item.InSelectedCollection = criteria.Collection?.Beatmaps.Any(b => b.BeatmapSet.Equals(item.Model)) ?? true; + item.InSelectedCollection = criteria.Collection?.Beatmaps.Any(b => b.MatchesOnlineID(item.Model)) ?? true; items.SearchTerm = criteria.SearchText; } From 0b4822b55225bb995bff04411082cc36cf42a3a6 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:35:01 +0900 Subject: [PATCH 08/14] Remove newly added equality --- osu.Game/Beatmaps/BeatmapInfo.cs | 14 -------------- osu.Game/Beatmaps/BeatmapSetInfo.cs | 14 -------------- osu.Game/Beatmaps/IBeatmapInfo.cs | 3 +-- osu.Game/Beatmaps/IBeatmapSetInfo.cs | 2 +- osu.Game/Models/RealmBeatmap.cs | 21 --------------------- osu.Game/Models/RealmBeatmapSet.cs | 17 ++--------------- 6 files changed, 4 insertions(+), 67 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapInfo.cs b/osu.Game/Beatmaps/BeatmapInfo.cs index 0b234b3c2d..6f45f5ec71 100644 --- a/osu.Game/Beatmaps/BeatmapInfo.cs +++ b/osu.Game/Beatmaps/BeatmapInfo.cs @@ -163,20 +163,6 @@ namespace osu.Game.Beatmaps return false; } - public bool Equals(IBeatmapInfo other) - { - if (ReferenceEquals(this, other)) return true; - if (other == null) return false; - - if (other is BeatmapInfo b && Equals(b)) - return true; - - if (OnlineID > 0 && other.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } - public bool AudioEquals(BeatmapInfo other) => other != null && BeatmapSet != null && other.BeatmapSet != null && BeatmapSet.Hash == other.BeatmapSet.Hash && (Metadata ?? BeatmapSet.Metadata).AudioFile == (other.Metadata ?? other.BeatmapSet.Metadata).AudioFile; diff --git a/osu.Game/Beatmaps/BeatmapSetInfo.cs b/osu.Game/Beatmaps/BeatmapSetInfo.cs index fa314fe400..6dd8cc5ade 100644 --- a/osu.Game/Beatmaps/BeatmapSetInfo.cs +++ b/osu.Game/Beatmaps/BeatmapSetInfo.cs @@ -78,20 +78,6 @@ namespace osu.Game.Beatmaps return false; } - public bool Equals(IBeatmapSetInfo other) - { - if (ReferenceEquals(this, other)) return true; - if (other == null) return false; - - if (other is BeatmapSetInfo b && Equals(b)) - return true; - - if (OnlineID > 0 && other.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } - #region Implementation of IHasOnlineID int IHasOnlineID.OnlineID => OnlineID ?? -1; diff --git a/osu.Game/Beatmaps/IBeatmapInfo.cs b/osu.Game/Beatmaps/IBeatmapInfo.cs index ab096b8897..84ea6d3019 100644 --- a/osu.Game/Beatmaps/IBeatmapInfo.cs +++ b/osu.Game/Beatmaps/IBeatmapInfo.cs @@ -1,7 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System; using osu.Game.Database; using osu.Game.Rulesets; @@ -12,7 +11,7 @@ namespace osu.Game.Beatmaps /// /// A single beatmap difficulty. /// - public interface IBeatmapInfo : IHasOnlineID, IEquatable + public interface IBeatmapInfo : IHasOnlineID { /// /// The user-specified name given to this beatmap. diff --git a/osu.Game/Beatmaps/IBeatmapSetInfo.cs b/osu.Game/Beatmaps/IBeatmapSetInfo.cs index 67f38397d4..20c46d9063 100644 --- a/osu.Game/Beatmaps/IBeatmapSetInfo.cs +++ b/osu.Game/Beatmaps/IBeatmapSetInfo.cs @@ -12,7 +12,7 @@ namespace osu.Game.Beatmaps /// /// A representation of a collection of beatmap difficulties, generally packaged as an ".osz" archive. /// - public interface IBeatmapSetInfo : IHasOnlineID, IEquatable + public interface IBeatmapSetInfo : IHasOnlineID { /// /// The date when this beatmap was imported. diff --git a/osu.Game/Models/RealmBeatmap.cs b/osu.Game/Models/RealmBeatmap.cs index f80d1f513a..2a197d296a 100644 --- a/osu.Game/Models/RealmBeatmap.cs +++ b/osu.Game/Models/RealmBeatmap.cs @@ -106,27 +106,6 @@ namespace osu.Game.Models return ID == other.ID; } - #region Implementation of IEquatable - - public bool Equals(IBeatmapInfo? other) - { - if (ReferenceEquals(this, other)) return true; - if (other == null) return false; - - if (other is RealmBeatmap b && Equals(b)) - return true; - - if (!string.IsNullOrEmpty(Hash) && !string.IsNullOrEmpty(other.Hash)) - return Hash == other.Hash; - - if (OnlineID > 0 && other.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } - - #endregion - public bool AudioEquals(RealmBeatmap? other) => other != null && BeatmapSet != null && other.BeatmapSet != null diff --git a/osu.Game/Models/RealmBeatmapSet.cs b/osu.Game/Models/RealmBeatmapSet.cs index 9cfa2de44d..1747cce67a 100644 --- a/osu.Game/Models/RealmBeatmapSet.cs +++ b/osu.Game/Models/RealmBeatmapSet.cs @@ -7,6 +7,7 @@ using System.Linq; using osu.Framework.Testing; using osu.Game.Beatmaps; using osu.Game.Database; +using osu.Game.Extensions; using Realms; #nullable enable @@ -61,21 +62,7 @@ namespace osu.Game.Models return ID == other.ID; } - public bool Equals(IBeatmapSetInfo? other) - { - if (ReferenceEquals(this, other)) return true; - if (other == null) return false; - - if (other is RealmBeatmapSet b && Equals(b)) - return true; - - if (OnlineID > 0 && other.OnlineID > 0) - return OnlineID == other.OnlineID; - - return false; - } - - public override string ToString() => Metadata?.ToString() ?? base.ToString(); + public override string ToString() => Metadata?.GetDisplayString() ?? base.ToString(); IEnumerable IBeatmapSetInfo.Beatmaps => Beatmaps; From 18f73b985b82de1d0eb07a9e827ba53065330008 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:38:01 +0900 Subject: [PATCH 09/14] Make `ModelExtensions` nullable enabled --- osu.Game/Extensions/ModelExtensions.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/osu.Game/Extensions/ModelExtensions.cs b/osu.Game/Extensions/ModelExtensions.cs index 2545045d96..48a3bac112 100644 --- a/osu.Game/Extensions/ModelExtensions.cs +++ b/osu.Game/Extensions/ModelExtensions.cs @@ -7,6 +7,8 @@ using osu.Game.Rulesets; using osu.Game.Scoring; using osu.Game.Users; +#nullable enable + namespace osu.Game.Extensions { public static class ModelExtensions @@ -23,9 +25,9 @@ namespace osu.Game.Extensions /// extension method type inference rules cause this method to call itself and cause a stack overflow. /// /// - public static string GetDisplayString(this object model) + public static string GetDisplayString(this object? model) { - string result = null; + string? result = null; switch (model) { @@ -65,8 +67,11 @@ namespace osu.Game.Extensions /// 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 IHasOnlineID instance, IHasOnlineID other) + public static bool MatchesOnlineID(this IHasOnlineID? instance, IHasOnlineID? other) { + if (instance == null || other == null) + return false; + if (instance.OnlineID < 0 || other.OnlineID < 0) return false; @@ -79,8 +84,11 @@ namespace osu.Game.Extensions /// 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 IHasOnlineID instance, IHasOnlineID other) + public static bool MatchesOnlineID(this IHasOnlineID? instance, IHasOnlineID? other) { + if (instance == null || other == null) + return false; + if (instance.OnlineID < 0 || other.OnlineID < 0) return false; From 93fe57d39970ee9c9dcc7dc0a757c2537aa10e55 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:43:31 +0900 Subject: [PATCH 10/14] Update tests to match new equality not including online ID checks --- osu.Game.Tests/NonVisual/BeatmapSetInfoEqualityTest.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/osu.Game.Tests/NonVisual/BeatmapSetInfoEqualityTest.cs b/osu.Game.Tests/NonVisual/BeatmapSetInfoEqualityTest.cs index 938edf07c6..534983f869 100644 --- a/osu.Game.Tests/NonVisual/BeatmapSetInfoEqualityTest.cs +++ b/osu.Game.Tests/NonVisual/BeatmapSetInfoEqualityTest.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using osu.Game.Beatmaps; +using osu.Game.Extensions; namespace osu.Game.Tests.NonVisual { @@ -15,7 +16,8 @@ namespace osu.Game.Tests.NonVisual var ourInfo = new BeatmapSetInfo { OnlineID = 123 }; var otherInfo = new BeatmapSetInfo { OnlineID = 123 }; - Assert.AreEqual(ourInfo, otherInfo); + Assert.AreNotEqual(ourInfo, otherInfo); + Assert.IsTrue(ourInfo.MatchesOnlineID(otherInfo)); } [Test] @@ -33,7 +35,8 @@ namespace osu.Game.Tests.NonVisual var ourInfo = new BeatmapSetInfo { ID = 123, OnlineID = 12 }; var otherInfo = new BeatmapSetInfo { OnlineID = 12 }; - Assert.AreEqual(ourInfo, otherInfo); + Assert.AreNotEqual(ourInfo, otherInfo); + Assert.IsTrue(ourInfo.MatchesOnlineID(otherInfo)); } [Test] From 285b161da73612b3ed0f4f9c189730b6483cf024 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:38:14 +0900 Subject: [PATCH 11/14] Update other usages of online ID comparisons to use new extension method --- osu.Game/Online/API/Requests/Responses/APIBeatmap.cs | 9 ++------- osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs | 9 ++------- osu.Game/Online/API/Requests/Responses/APIUser.cs | 3 ++- .../BeatmapListing/Panels/BeatmapPanelDownloadButton.cs | 3 ++- osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs | 3 ++- osu.Game/Overlays/BeatmapSet/BeatmapRulesetTabItem.cs | 3 ++- osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs | 3 ++- 7 files changed, 14 insertions(+), 19 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs index 33098a2919..bbffaee87b 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs @@ -4,6 +4,7 @@ using System; using Newtonsoft.Json; using osu.Game.Beatmaps; +using osu.Game.Extensions; using osu.Game.Rulesets; #nullable enable @@ -106,13 +107,7 @@ namespace osu.Game.Online.API.Requests.Responses #region Implementation of IEquatable - public bool Equals(IBeatmapInfo? other) - { - if (OnlineID > 0 && other?.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } + public bool Equals(IBeatmapInfo? other) => this.MatchesOnlineID(other); #endregion } diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs index f4515c4258..ab8287871f 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using Newtonsoft.Json; using osu.Game.Beatmaps; using osu.Game.Database; +using osu.Game.Extensions; #nullable enable @@ -144,13 +145,7 @@ namespace osu.Game.Online.API.Requests.Responses #region Implementation of IEquatable - public bool Equals(IBeatmapSetInfo? other) - { - if (OnlineID > 0 && other?.OnlineID > 0) - return other.OnlineID == OnlineID; - - return false; - } + public bool Equals(IBeatmapInfo? other) => this.MatchesOnlineID(other); #endregion } diff --git a/osu.Game/Online/API/Requests/Responses/APIUser.cs b/osu.Game/Online/API/Requests/Responses/APIUser.cs index 49edfd036b..50f5d67796 100644 --- a/osu.Game/Online/API/Requests/Responses/APIUser.cs +++ b/osu.Game/Online/API/Requests/Responses/APIUser.cs @@ -7,6 +7,7 @@ using System.Linq; using JetBrains.Annotations; using Newtonsoft.Json; using osu.Framework.Bindables; +using osu.Game.Extensions; using osu.Game.Users; namespace osu.Game.Online.API.Requests.Responses @@ -240,6 +241,6 @@ namespace osu.Game.Online.API.Requests.Responses public int OnlineID => Id; - public bool Equals(APIUser other) => OnlineID == other?.OnlineID; + public bool Equals(APIUser other) => this.MatchesOnlineID(other); } } diff --git a/osu.Game/Overlays/BeatmapListing/Panels/BeatmapPanelDownloadButton.cs b/osu.Game/Overlays/BeatmapListing/Panels/BeatmapPanelDownloadButton.cs index 8c7846783d..1282a14c3d 100644 --- a/osu.Game/Overlays/BeatmapListing/Panels/BeatmapPanelDownloadButton.cs +++ b/osu.Game/Overlays/BeatmapListing/Panels/BeatmapPanelDownloadButton.cs @@ -8,6 +8,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Beatmaps; using osu.Game.Configuration; +using osu.Game.Extensions; using osu.Game.Graphics.Containers; using osu.Game.Graphics.UserInterface; using osu.Game.Online; @@ -80,7 +81,7 @@ namespace osu.Game.Overlays.BeatmapListing.Panels case DownloadState.LocallyAvailable: Predicate findPredicate = null; if (SelectedBeatmap.Value != null) - findPredicate = b => b.OnlineID == SelectedBeatmap.Value.OnlineID; + findPredicate = b => b.MatchesOnlineID(SelectedBeatmap.Value); game?.PresentBeatmap(beatmapSet, findPredicate); break; diff --git a/osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs b/osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs index b152375062..59e8e8db3c 100644 --- a/osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs +++ b/osu.Game/Overlays/BeatmapSet/BeatmapPicker.cs @@ -14,6 +14,7 @@ using osu.Framework.Graphics.Sprites; using osu.Framework.Input.Events; using osu.Game.Beatmaps; using osu.Game.Beatmaps.Drawables; +using osu.Game.Extensions; using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; @@ -166,7 +167,7 @@ namespace osu.Game.Overlays.BeatmapSet if (BeatmapSet != null) { Difficulties.ChildrenEnumerable = BeatmapSet.Beatmaps - .Where(b => b.Ruleset.OnlineID == ruleset.Value?.OnlineID) + .Where(b => b.Ruleset.MatchesOnlineID(ruleset.Value)) .OrderBy(b => b.StarRating) .Select(b => new DifficultySelectorButton(b) { diff --git a/osu.Game/Overlays/BeatmapSet/BeatmapRulesetTabItem.cs b/osu.Game/Overlays/BeatmapSet/BeatmapRulesetTabItem.cs index b3b3d1980b..e8cdc6913b 100644 --- a/osu.Game/Overlays/BeatmapSet/BeatmapRulesetTabItem.cs +++ b/osu.Game/Overlays/BeatmapSet/BeatmapRulesetTabItem.cs @@ -7,6 +7,7 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shapes; +using osu.Game.Extensions; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Game.Online.API.Requests.Responses; @@ -64,7 +65,7 @@ namespace osu.Game.Overlays.BeatmapSet BeatmapSet.BindValueChanged(setInfo => { - int beatmapsCount = setInfo.NewValue?.Beatmaps.Count(b => b.Ruleset.OnlineID == Value.OnlineID) ?? 0; + int beatmapsCount = setInfo.NewValue?.Beatmaps.Count(b => b.Ruleset.MatchesOnlineID(Value)) ?? 0; count.Text = beatmapsCount.ToString(); countContainer.FadeTo(beatmapsCount > 0 ? 1 : 0); diff --git a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs index 22537c3ce0..56863cafad 100644 --- a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Screens; +using osu.Game.Extensions; using osu.Game.Online.Rooms; using osu.Game.Rulesets; using osu.Game.Scoring; @@ -32,7 +33,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists private void load(IBindable ruleset) { // Sanity checks to ensure that PlaylistsPlayer matches the settings for the current PlaylistItem - if (Beatmap.Value.BeatmapInfo.OnlineID != PlaylistItem.Beatmap.Value.OnlineID) + if (!Beatmap.Value.BeatmapInfo.MatchesOnlineID(PlaylistItem.Beatmap.Value)) throw new InvalidOperationException("Current Beatmap does not match PlaylistItem's Beatmap"); if (ruleset.Value.ID != PlaylistItem.Ruleset.Value.ID) From 611b9fe9422e5e33ae7845b911dceda44a7b6b24 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 14:50:09 +0900 Subject: [PATCH 12/14] Remove now unused implementations of interface equality --- osu.Game/Online/API/Requests/Responses/APIBeatmap.cs | 7 ------- osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs | 7 ------- 2 files changed, 14 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs index bbffaee87b..2560502173 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmap.cs @@ -4,7 +4,6 @@ using System; using Newtonsoft.Json; using osu.Game.Beatmaps; -using osu.Game.Extensions; using osu.Game.Rulesets; #nullable enable @@ -104,11 +103,5 @@ namespace osu.Game.Online.API.Requests.Responses public string Hash => throw new NotImplementedException(); #endregion - - #region Implementation of IEquatable - - public bool Equals(IBeatmapInfo? other) => this.MatchesOnlineID(other); - - #endregion } } diff --git a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs index ab8287871f..168e9d5d51 100644 --- a/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs +++ b/osu.Game/Online/API/Requests/Responses/APIBeatmapSet.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using Newtonsoft.Json; using osu.Game.Beatmaps; using osu.Game.Database; -using osu.Game.Extensions; #nullable enable @@ -142,11 +141,5 @@ namespace osu.Game.Online.API.Requests.Responses double IBeatmapSetInfo.MaxBPM => BPM; #endregion - - #region Implementation of IEquatable - - public bool Equals(IBeatmapInfo? other) => this.MatchesOnlineID(other); - - #endregion } } From 73cb80d8a44a0c69717a47acdb2adb09da341b1a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 16:00:45 +0900 Subject: [PATCH 13/14] Update one more missed usage --- osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs b/osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs index 85efdcef1a..32ae7cf859 100644 --- a/osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs +++ b/osu.Game/Screens/OnlinePlay/Lounge/Components/RoomsContainer.cs @@ -82,7 +82,7 @@ namespace osu.Game.Screens.OnlinePlay.Lounge.Components { bool matchingFilter = true; - matchingFilter &= r.Room.Playlist.Count == 0 || criteria.Ruleset == null || r.Room.Playlist.Any(i => i.Ruleset.Value.Equals(criteria.Ruleset)); + matchingFilter &= r.Room.Playlist.Count == 0 || criteria.Ruleset == null || r.Room.Playlist.Any(i => i.Ruleset.Value.MatchesOnlineID(criteria.Ruleset)); if (!string.IsNullOrEmpty(criteria.SearchString)) matchingFilter &= r.FilterTerms.Any(term => term.Contains(criteria.SearchString, StringComparison.InvariantCultureIgnoreCase)); From 1988b263c60331f02fd973eee2da70f077ea78df Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 15 Nov 2021 16:02:08 +0900 Subject: [PATCH 14/14] One more missed usage.. --- osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs index 56863cafad..35d417520e 100644 --- a/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs +++ b/osu.Game/Screens/OnlinePlay/Playlists/PlaylistsPlayer.cs @@ -36,7 +36,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists if (!Beatmap.Value.BeatmapInfo.MatchesOnlineID(PlaylistItem.Beatmap.Value)) throw new InvalidOperationException("Current Beatmap does not match PlaylistItem's Beatmap"); - if (ruleset.Value.ID != PlaylistItem.Ruleset.Value.ID) + if (!ruleset.Value.MatchesOnlineID(PlaylistItem.Ruleset.Value)) throw new InvalidOperationException("Current Ruleset does not match PlaylistItem's Ruleset"); if (!PlaylistItem.RequiredMods.All(m => Mods.Value.Any(m.Equals)))