diff --git a/osu.Game/Beatmaps/BeatmapManager.cs b/osu.Game/Beatmaps/BeatmapManager.cs index 4de5d49b1e..8e7b38a50c 100644 --- a/osu.Game/Beatmaps/BeatmapManager.cs +++ b/osu.Game/Beatmaps/BeatmapManager.cs @@ -68,7 +68,7 @@ public class BeatmapManager private readonly APIAccess api; - private readonly Dictionary downloadsMap; + private readonly List downloadsList; // ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised) private BeatmapIPCChannel ipc; @@ -100,7 +100,7 @@ public BeatmapManager(Storage storage, FileStore files, SQLiteConnection connect if (importHost != null) ipc = new BeatmapIPCChannel(importHost, this); - downloadsMap = new Dictionary(); + downloadsList = new List(); } /// @@ -194,7 +194,8 @@ public void Import(BeatmapSetInfo beatmapSetInfo) /// The new , or null if a download already exists for the same beatmap. public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo) { - if (api == null || downloadsMap.ContainsKey(beatmapSetInfo.OnlineBeatmapSetID)) return null; + if (api == null || downloadsList.Find(d => d.BeatmapSet.OnlineBeatmapSetID == beatmapSetInfo.OnlineBeatmapSetID) != null) + return null; ProgressNotification downloadNotification = new ProgressNotification { @@ -217,29 +218,29 @@ public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo) using (var archive = new OszArchiveReader(stream)) Import(archive); - downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID); + downloadsList.Remove(request); }; request.Failure += data => { downloadNotification.State = ProgressNotificationState.Completed; Logger.Error(data, "Failed to get beatmap download information"); - downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID); + downloadsList.Remove(request); }; downloadNotification.CancelRequested += () => { - Logger.Log("Cancel requested"); - downloadsMap[beatmapSetInfo.OnlineBeatmapSetID].Cancel(); - downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID); + request.Cancel(); + downloadsList.Remove(request); downloadNotification.State = ProgressNotificationState.Cancelled; return true; }; - downloadsMap[beatmapSetInfo.OnlineBeatmapSetID] = request; + downloadsList.Add(request); PostNotification?.Invoke(downloadNotification); // don't run in the main api queue as this is a long-running task. + // TODO: ensure the Success/Failure callbacks are being scheduled to the main thread for thread safety. Task.Run(() => request.Perform(api)); return request; @@ -250,7 +251,7 @@ public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo) /// /// The whose download request is wanted. /// The object if it exists, or null. - public DownloadBeatmapSetRequest GetExistingDownload(BeatmapSetInfo beatmap) => downloadsMap.GetOrDefault(beatmap.OnlineBeatmapSetID); + public DownloadBeatmapSetRequest GetExistingDownload(BeatmapSetInfo beatmap) => downloadsList.Find(d => d.BeatmapSet.OnlineBeatmapSetID == beatmap.OnlineBeatmapSetID); /// /// Delete a beatmap from the manager. diff --git a/osu.Game/Online/API/Requests/DownloadBeatmapSetRequest.cs b/osu.Game/Online/API/Requests/DownloadBeatmapSetRequest.cs index 0ba4adf9c9..5a9f609bca 100644 --- a/osu.Game/Online/API/Requests/DownloadBeatmapSetRequest.cs +++ b/osu.Game/Online/API/Requests/DownloadBeatmapSetRequest.cs @@ -8,17 +8,17 @@ namespace osu.Game.Online.API.Requests { public class DownloadBeatmapSetRequest : APIDownloadRequest { - private readonly BeatmapSetInfo beatmapSet; + public readonly BeatmapSetInfo BeatmapSet; public Action DownloadProgressed; - public DownloadBeatmapSetRequest(BeatmapSetInfo beatmapSet) + public DownloadBeatmapSetRequest(BeatmapSetInfo set) { - this.beatmapSet = beatmapSet; + BeatmapSet = set; Progress += (current, total) => DownloadProgressed?.Invoke((float) current / total); } - protected override string Target => $@"beatmapsets/{beatmapSet.OnlineBeatmapSetID}/download"; + protected override string Target => $@"beatmapsets/{BeatmapSet.OnlineBeatmapSetID}/download"; } }