From 17e34704413ee1cf1d5520cd4a7f10a2569ac177 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Sun, 16 Feb 2020 00:22:14 +0900 Subject: [PATCH 1/2] Replace hashcode override with local equality comparer --- .../Multiplayer/TestSceneDrawableRoomPlaylist.cs | 10 ++++++++++ osu.Game/Online/Multiplayer/PlaylistItem.cs | 7 ------- osu.Game/Online/Multiplayer/Room.cs | 14 ++++++++++---- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/osu.Game.Tests/Visual/Multiplayer/TestSceneDrawableRoomPlaylist.cs b/osu.Game.Tests/Visual/Multiplayer/TestSceneDrawableRoomPlaylist.cs index d2d02412cd..4f54e451b7 100644 --- a/osu.Game.Tests/Visual/Multiplayer/TestSceneDrawableRoomPlaylist.cs +++ b/osu.Game.Tests/Visual/Multiplayer/TestSceneDrawableRoomPlaylist.cs @@ -179,6 +179,16 @@ namespace osu.Game.Tests.Visual.Multiplayer AddAssert("item 0 is selected", () => playlist.SelectedItem.Value == playlist.Items[0]); } + [Test] + public void TestChangeBeatmapAndRemove() + { + createPlaylist(true, true); + + AddStep("change beatmap of first item", () => playlist.Items[0].BeatmapID = 30); + moveToDeleteButton(0); + AddStep("click delete button", () => InputManager.Click(MouseButton.Left)); + } + private void moveToItem(int index, Vector2? offset = null) => AddStep($"move mouse to item {index}", () => InputManager.MoveMouseTo(playlist.ChildrenOfType>().ElementAt(index), offset)); diff --git a/osu.Game/Online/Multiplayer/PlaylistItem.cs b/osu.Game/Online/Multiplayer/PlaylistItem.cs index 69e1f0db13..11e4854174 100644 --- a/osu.Game/Online/Multiplayer/PlaylistItem.cs +++ b/osu.Game/Online/Multiplayer/PlaylistItem.cs @@ -93,12 +93,5 @@ namespace osu.Game.Online.Multiplayer public bool ShouldSerializeapiBeatmap() => false; public bool Equals(PlaylistItem other) => ID == other?.ID && BeatmapID == other.BeatmapID && RulesetID == other.RulesetID; - - public override int GetHashCode() - { - // ReSharper disable NonReadonlyMemberInGetHashCode - return HashCode.Combine(ID, BeatmapID, RulesetID); - // ReSharper restore NonReadonlyMemberInGetHashCode - } } } diff --git a/osu.Game/Online/Multiplayer/Room.cs b/osu.Game/Online/Multiplayer/Room.cs index 2459f65b84..b2226a6f22 100644 --- a/osu.Game/Online/Multiplayer/Room.cs +++ b/osu.Game/Online/Multiplayer/Room.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using osu.Framework.Allocation; @@ -124,15 +125,13 @@ namespace osu.Game.Online.Multiplayer var localItem = Playlist.FirstOrDefault(i => i.BeatmapID == item.BeatmapID); if (localItem != null) - { item.Beatmap.Value.Metadata = localItem.Beatmap.Value.Metadata; - } } - foreach (var removeableItem in Playlist.Except(other.Playlist).ToArray()) + foreach (var removeableItem in Playlist.Except(other.Playlist, new PlaylistEqualityComparer()).ToArray()) Playlist.Remove(removeableItem); - Playlist.AddRange(other.Playlist.Except(Playlist).ToArray()); + Playlist.AddRange(other.Playlist.Except(Playlist, new PlaylistEqualityComparer()).ToArray()); foreach (var removedItem in Participants.Except(other.Participants).ToArray()) Participants.Remove(removedItem); @@ -144,5 +143,12 @@ namespace osu.Game.Online.Multiplayer public bool ShouldSerializeRoomID() => false; public bool ShouldSerializeHost() => false; public bool ShouldSerializeEndDate() => false; + + private class PlaylistEqualityComparer : IEqualityComparer + { + public bool Equals(PlaylistItem x, PlaylistItem y) => x?.Equals(y) ?? false; + + public int GetHashCode(PlaylistItem obj) => HashCode.Combine(obj.ID, obj.BeatmapID, obj.RulesetID); + } } } From c8b81bcf3c9ebe6e08696910944e96d9c904fdeb Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Sun, 16 Feb 2020 16:23:46 +0900 Subject: [PATCH 2/2] Use equality comparison rather than hash set --- osu.Game/Online/Multiplayer/Room.cs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/osu.Game/Online/Multiplayer/Room.cs b/osu.Game/Online/Multiplayer/Room.cs index b2226a6f22..2bfcc019fa 100644 --- a/osu.Game/Online/Multiplayer/Room.cs +++ b/osu.Game/Online/Multiplayer/Room.cs @@ -2,7 +2,6 @@ // See the LICENCE file in the repository root for full licence text. using System; -using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using osu.Framework.Allocation; @@ -128,14 +127,17 @@ namespace osu.Game.Online.Multiplayer item.Beatmap.Value.Metadata = localItem.Beatmap.Value.Metadata; } - foreach (var removeableItem in Playlist.Except(other.Playlist, new PlaylistEqualityComparer()).ToArray()) - Playlist.Remove(removeableItem); + if (!Playlist.SequenceEqual(other.Playlist)) + { + Playlist.Clear(); + Playlist.AddRange(other.Playlist); + } - Playlist.AddRange(other.Playlist.Except(Playlist, new PlaylistEqualityComparer()).ToArray()); - - foreach (var removedItem in Participants.Except(other.Participants).ToArray()) - Participants.Remove(removedItem); - Participants.AddRange(other.Participants.Except(Participants).ToArray()); + if (!Participants.SequenceEqual(other.Participants)) + { + Participants.Clear(); + Participants.AddRange(other.Participants); + } Position = other.Position; } @@ -143,12 +145,5 @@ namespace osu.Game.Online.Multiplayer public bool ShouldSerializeRoomID() => false; public bool ShouldSerializeHost() => false; public bool ShouldSerializeEndDate() => false; - - private class PlaylistEqualityComparer : IEqualityComparer - { - public bool Equals(PlaylistItem x, PlaylistItem y) => x?.Equals(y) ?? false; - - public int GetHashCode(PlaylistItem obj) => HashCode.Combine(obj.ID, obj.BeatmapID, obj.RulesetID); - } } }