From 172552d55173791a5a5293bcab1d2240b8b02783 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sun, 17 Jan 2021 21:02:33 +0300 Subject: [PATCH 1/5] Use `TaskCompletionSource` for better awaiting --- .../Online/TestSceneMultiplayerBeatmapTracker.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs index 839d47afc2..05e8f8b2e4 100644 --- a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs +++ b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs @@ -47,6 +47,8 @@ namespace osu.Game.Tests.Online [SetUp] public void SetUp() => Schedule(() => { + beatmaps.AllowImport = new TaskCompletionSource(); + testBeatmapFile = getTestBeatmapOsz(); testBeatmapInfo = new TestBeatmap(Ruleset.Value).BeatmapInfo; @@ -83,7 +85,7 @@ namespace osu.Game.Tests.Online AddStep("finish download", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile)); addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing); - AddStep("allow importing", () => beatmaps.AllowImport.Set()); + AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true)); AddUntilStep("wait for import", () => beatmaps.IsAvailableLocally(testBeatmapSet)); addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable); } @@ -91,7 +93,7 @@ namespace osu.Game.Tests.Online [Test] public void TestTrackerRespectsSoftDeleting() { - AddStep("allow importing", () => beatmaps.AllowImport.Set()); + AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true)); AddStep("import beatmap", () => beatmaps.Import(testBeatmapSet).Wait()); addAvailabilityCheckStep("state locally available", BeatmapAvailability.LocallyAvailable); @@ -105,7 +107,7 @@ namespace osu.Game.Tests.Online [Test] public void TestTrackerRespectsChecksum() { - AddStep("allow importing", () => beatmaps.AllowImport.Set()); + AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true)); BeatmapInfo wrongBeatmap = null; @@ -144,7 +146,7 @@ namespace osu.Game.Tests.Online private class TestBeatmapManager : BeatmapManager { - public readonly ManualResetEventSlim AllowImport = new ManualResetEventSlim(); + public TaskCompletionSource AllowImport = new TaskCompletionSource(); protected override ArchiveDownloadRequest CreateDownloadRequest(BeatmapSetInfo set, bool minimiseDownloadSize) => new TestDownloadRequest(set); @@ -156,9 +158,7 @@ namespace osu.Game.Tests.Online public override async Task Import(BeatmapSetInfo item, ArchiveReader archive = null, CancellationToken cancellationToken = default) { - while (!AllowImport.IsSet) - await Task.Delay(10, cancellationToken); - + await AllowImport.Task; return await base.Import(item, archive, cancellationToken); } } From d93a853dfd531e9d8d83473b04e3c22b5d290023 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sun, 17 Jan 2021 21:16:45 +0300 Subject: [PATCH 2/5] Enforce `float` type in the download progress path instead --- .../Online/TestSceneMultiplayerBeatmapTracker.cs | 8 ++++---- osu.Game/Online/API/ArchiveDownloadRequest.cs | 6 +++--- osu.Game/Online/DownloadTrackingComposite.cs | 2 +- osu.Game/Online/Rooms/BeatmapAvailability.cs | 6 +++--- osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs | 2 +- .../Overlays/Notifications/ProgressNotification.cs | 10 +++++----- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs index 05e8f8b2e4..60a4508b3d 100644 --- a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs +++ b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs @@ -77,10 +77,10 @@ namespace osu.Game.Tests.Online addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded); AddStep("start downloading", () => beatmaps.Download(testBeatmapSet)); - addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0)); + addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0f)); - AddStep("set progress 40%", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).SetProgress(0.4)); - addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4)); + AddStep("set progress 40%", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).SetProgress(0.4f)); + addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f)); AddStep("finish download", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile)); addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing); @@ -165,7 +165,7 @@ namespace osu.Game.Tests.Online private class TestDownloadRequest : ArchiveDownloadRequest { - public new void SetProgress(double progress) => base.SetProgress(progress); + public new void SetProgress(float progress) => base.SetProgress(progress); public TestDownloadRequest(BeatmapSetInfo model) : base(model) diff --git a/osu.Game/Online/API/ArchiveDownloadRequest.cs b/osu.Game/Online/API/ArchiveDownloadRequest.cs index fdb2a984dc..ccb4e9c119 100644 --- a/osu.Game/Online/API/ArchiveDownloadRequest.cs +++ b/osu.Game/Online/API/ArchiveDownloadRequest.cs @@ -12,16 +12,16 @@ namespace osu.Game.Online.API public double Progress { get; private set; } - public event Action DownloadProgressed; + public event Action DownloadProgressed; protected ArchiveDownloadRequest(TModel model) { Model = model; - Progressed += (current, total) => SetProgress((double)current / total); + Progressed += (current, total) => SetProgress((float)current / total); } - protected void SetProgress(double progress) + protected void SetProgress(float progress) { Progress = progress; DownloadProgressed?.Invoke(progress); diff --git a/osu.Game/Online/DownloadTrackingComposite.cs b/osu.Game/Online/DownloadTrackingComposite.cs index b72cf38369..1631d9790b 100644 --- a/osu.Game/Online/DownloadTrackingComposite.cs +++ b/osu.Game/Online/DownloadTrackingComposite.cs @@ -144,7 +144,7 @@ namespace osu.Game.Online private void onRequestSuccess(string _) => Schedule(() => State.Value = DownloadState.Importing); - private void onRequestProgress(double progress) => Schedule(() => Progress.Value = progress); + private void onRequestProgress(float progress) => Schedule(() => Progress.Value = progress); private void onRequestFailure(Exception e) => Schedule(() => attachDownload(null)); diff --git a/osu.Game/Online/Rooms/BeatmapAvailability.cs b/osu.Game/Online/Rooms/BeatmapAvailability.cs index e7dbc5f436..170009a85b 100644 --- a/osu.Game/Online/Rooms/BeatmapAvailability.cs +++ b/osu.Game/Online/Rooms/BeatmapAvailability.cs @@ -19,17 +19,17 @@ namespace osu.Game.Online.Rooms /// /// The beatmap's downloading progress, null when not in state. /// - public readonly double? DownloadProgress; + public readonly float? DownloadProgress; [JsonConstructor] - private BeatmapAvailability(DownloadState state, double? downloadProgress = null) + private BeatmapAvailability(DownloadState state, float? downloadProgress = null) { State = state; DownloadProgress = downloadProgress; } public static BeatmapAvailability NotDownloaded() => new BeatmapAvailability(DownloadState.NotDownloaded); - public static BeatmapAvailability Downloading(double progress) => new BeatmapAvailability(DownloadState.Downloading, progress); + public static BeatmapAvailability Downloading(float progress) => new BeatmapAvailability(DownloadState.Downloading, progress); public static BeatmapAvailability Importing() => new BeatmapAvailability(DownloadState.Importing); public static BeatmapAvailability LocallyAvailable() => new BeatmapAvailability(DownloadState.LocallyAvailable); diff --git a/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs b/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs index b22d17f3ef..b15399ad62 100644 --- a/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs +++ b/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs @@ -70,7 +70,7 @@ namespace osu.Game.Online.Rooms break; case DownloadState.Downloading: - availability.Value = BeatmapAvailability.Downloading(Progress.Value); + availability.Value = BeatmapAvailability.Downloading((float)Progress.Value); break; case DownloadState.Importing: diff --git a/osu.Game/Overlays/Notifications/ProgressNotification.cs b/osu.Game/Overlays/Notifications/ProgressNotification.cs index e18bab8e83..3105ecd742 100644 --- a/osu.Game/Overlays/Notifications/ProgressNotification.cs +++ b/osu.Game/Overlays/Notifications/ProgressNotification.cs @@ -23,9 +23,9 @@ namespace osu.Game.Overlays.Notifications public string CompletionText { get; set; } = "Task has completed!"; - private double progress; + private float progress; - public double Progress + public float Progress { get => progress; set @@ -185,9 +185,9 @@ namespace osu.Game.Overlays.Notifications private Color4 colourActive; private Color4 colourInactive; - private double progress; + private float progress; - public double Progress + public float Progress { get => progress; set @@ -195,7 +195,7 @@ namespace osu.Game.Overlays.Notifications if (progress == value) return; progress = value; - box.ResizeTo(new Vector2((float)progress, 1), 100, Easing.OutQuad); + box.ResizeTo(new Vector2(progress, 1), 100, Easing.OutQuad); } } From 0425a659a8c0bae3117e77dd512e899ca6a5c3da Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sun, 17 Jan 2021 21:19:55 +0300 Subject: [PATCH 3/5] Add null-permissive operator to manager back --- osu.Game/Online/DownloadTrackingComposite.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Online/DownloadTrackingComposite.cs b/osu.Game/Online/DownloadTrackingComposite.cs index 1631d9790b..188cb9be7a 100644 --- a/osu.Game/Online/DownloadTrackingComposite.cs +++ b/osu.Game/Online/DownloadTrackingComposite.cs @@ -80,7 +80,7 @@ namespace osu.Game.Online /// By default, this calls , /// but can be overriden to add additional checks for verifying the model in database. /// - protected virtual bool IsModelAvailableLocally() => Manager.IsAvailableLocally(Model.Value); + protected virtual bool IsModelAvailableLocally() => Manager?.IsAvailableLocally(Model.Value) == true; private void downloadBegan(ValueChangedEvent>> weakRequest) { From ccef50e2a2e16767565c75bdc70c52faba7c57e4 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sun, 17 Jan 2021 22:04:12 +0300 Subject: [PATCH 4/5] Log important message if user imported bad-checksum beatmap --- osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs b/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs index b15399ad62..659f52f00f 100644 --- a/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs +++ b/osu.Game/Online/Rooms/MultiplayerBeatmapTracker.cs @@ -4,6 +4,7 @@ using System; using System.Linq; using osu.Framework.Bindables; +using osu.Framework.Logging; using osu.Game.Beatmaps; namespace osu.Game.Online.Rooms @@ -34,6 +35,15 @@ namespace osu.Game.Online.Rooms } protected override bool VerifyDatabasedModel(BeatmapSetInfo databasedSet) + { + var verified = verifyDatabasedModel(databasedSet); + if (!verified) + Logger.Log("The imported beatmapset does not match the online version.", LoggingTarget.Runtime, LogLevel.Important); + + return verified; + } + + private bool verifyDatabasedModel(BeatmapSetInfo databasedSet) { int? beatmapId = SelectedItem.Value.Beatmap.Value.OnlineBeatmapID; string checksum = SelectedItem.Value.Beatmap.Value.MD5Hash; From b6a37c1c15fe2a95b7efb94b61e37de132d9864e Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sun, 17 Jan 2021 22:08:28 +0300 Subject: [PATCH 5/5] Make `TriggerSuccess(filename)` protected and expose in test instead --- osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs | 1 + osu.Game/Online/API/APIDownloadRequest.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs index 60a4508b3d..4b8992052e 100644 --- a/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs +++ b/osu.Game.Tests/Online/TestSceneMultiplayerBeatmapTracker.cs @@ -166,6 +166,7 @@ namespace osu.Game.Tests.Online private class TestDownloadRequest : ArchiveDownloadRequest { public new void SetProgress(float progress) => base.SetProgress(progress); + public new void TriggerSuccess(string filename) => base.TriggerSuccess(filename); public TestDownloadRequest(BeatmapSetInfo model) : base(model) diff --git a/osu.Game/Online/API/APIDownloadRequest.cs b/osu.Game/Online/API/APIDownloadRequest.cs index 02c589403c..62e22d8f88 100644 --- a/osu.Game/Online/API/APIDownloadRequest.cs +++ b/osu.Game/Online/API/APIDownloadRequest.cs @@ -29,7 +29,7 @@ namespace osu.Game.Online.API private void request_Progress(long current, long total) => API.Schedule(() => Progressed?.Invoke(current, total)); - internal void TriggerSuccess(string filename) + protected void TriggerSuccess(string filename) { if (this.filename != null) throw new InvalidOperationException("Attempted to trigger success more than once");