1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-30 02:22:55 +08:00

Use a List instead of a Dictionary.

This commit is contained in:
naoey 2017-09-09 09:51:37 +05:30
parent 20becbe576
commit 5f5dd54f9d
2 changed files with 15 additions and 14 deletions

View File

@ -68,7 +68,7 @@ namespace osu.Game.Beatmaps
private readonly APIAccess api; private readonly APIAccess api;
private readonly Dictionary<int?, DownloadBeatmapSetRequest> downloadsMap; private readonly List<DownloadBeatmapSetRequest> downloadsList;
// ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised) // ReSharper disable once NotAccessedField.Local (we should keep a reference to this so it is not finalised)
private BeatmapIPCChannel ipc; private BeatmapIPCChannel ipc;
@ -100,7 +100,7 @@ namespace osu.Game.Beatmaps
if (importHost != null) if (importHost != null)
ipc = new BeatmapIPCChannel(importHost, this); ipc = new BeatmapIPCChannel(importHost, this);
downloadsMap = new Dictionary<int?, DownloadBeatmapSetRequest>(); downloadsList = new List<DownloadBeatmapSetRequest>();
} }
/// <summary> /// <summary>
@ -194,7 +194,8 @@ namespace osu.Game.Beatmaps
/// <returns>The new <see cref="DownloadBeatmapSetRequest"/>, or null if a download already exists for the same beatmap.</returns> /// <returns>The new <see cref="DownloadBeatmapSetRequest"/>, or null if a download already exists for the same beatmap.</returns>
public DownloadBeatmapSetRequest Download(BeatmapSetInfo beatmapSetInfo) 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 ProgressNotification downloadNotification = new ProgressNotification
{ {
@ -217,29 +218,29 @@ namespace osu.Game.Beatmaps
using (var archive = new OszArchiveReader(stream)) using (var archive = new OszArchiveReader(stream))
Import(archive); Import(archive);
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID); downloadsList.Remove(request);
}; };
request.Failure += data => request.Failure += data =>
{ {
downloadNotification.State = ProgressNotificationState.Completed; downloadNotification.State = ProgressNotificationState.Completed;
Logger.Error(data, "Failed to get beatmap download information"); Logger.Error(data, "Failed to get beatmap download information");
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID); downloadsList.Remove(request);
}; };
downloadNotification.CancelRequested += () => downloadNotification.CancelRequested += () =>
{ {
Logger.Log("Cancel requested"); request.Cancel();
downloadsMap[beatmapSetInfo.OnlineBeatmapSetID].Cancel(); downloadsList.Remove(request);
downloadsMap.Remove(beatmapSetInfo.OnlineBeatmapSetID);
downloadNotification.State = ProgressNotificationState.Cancelled; downloadNotification.State = ProgressNotificationState.Cancelled;
return true; return true;
}; };
downloadsMap[beatmapSetInfo.OnlineBeatmapSetID] = request; downloadsList.Add(request);
PostNotification?.Invoke(downloadNotification); PostNotification?.Invoke(downloadNotification);
// don't run in the main api queue as this is a long-running task. // 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)); Task.Run(() => request.Perform(api));
return request; return request;
@ -250,7 +251,7 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
/// <param name="beatmap">The <see cref="BeatmapSetInfo"/> whose download request is wanted.</param> /// <param name="beatmap">The <see cref="BeatmapSetInfo"/> whose download request is wanted.</param>
/// <returns>The <see cref="DownloadBeatmapSetRequest"/> object if it exists, or null.</returns> /// <returns>The <see cref="DownloadBeatmapSetRequest"/> object if it exists, or null.</returns>
public DownloadBeatmapSetRequest GetExistingDownload(BeatmapSetInfo beatmap) => downloadsMap.GetOrDefault(beatmap.OnlineBeatmapSetID); public DownloadBeatmapSetRequest GetExistingDownload(BeatmapSetInfo beatmap) => downloadsList.Find(d => d.BeatmapSet.OnlineBeatmapSetID == beatmap.OnlineBeatmapSetID);
/// <summary> /// <summary>
/// Delete a beatmap from the manager. /// Delete a beatmap from the manager.

View File

@ -8,17 +8,17 @@ namespace osu.Game.Online.API.Requests
{ {
public class DownloadBeatmapSetRequest : APIDownloadRequest public class DownloadBeatmapSetRequest : APIDownloadRequest
{ {
private readonly BeatmapSetInfo beatmapSet; public readonly BeatmapSetInfo BeatmapSet;
public Action<float> DownloadProgressed; public Action<float> DownloadProgressed;
public DownloadBeatmapSetRequest(BeatmapSetInfo beatmapSet) public DownloadBeatmapSetRequest(BeatmapSetInfo set)
{ {
this.beatmapSet = beatmapSet; BeatmapSet = set;
Progress += (current, total) => DownloadProgressed?.Invoke((float) current / total); Progress += (current, total) => DownloadProgressed?.Invoke((float) current / total);
} }
protected override string Target => $@"beatmapsets/{beatmapSet.OnlineBeatmapSetID}/download"; protected override string Target => $@"beatmapsets/{BeatmapSet.OnlineBeatmapSetID}/download";
} }
} }