Merge pull request #11820 from smoogipoo/fix-offthread-collection-import

Fix collections being imported from BDL thread
This commit is contained in:
Dan Balasescu 2021-02-18 19:52:37 +09:00 committed by GitHub
commit cea9ecc2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,35 +139,43 @@ namespace osu.Game.Collections
PostNotification?.Invoke(notification); PostNotification?.Invoke(notification);
var collection = readCollections(stream, notification); var collection = readCollections(stream, notification);
bool importCompleted = false; await importCollections(collection);
Schedule(() =>
{
importCollections(collection);
importCompleted = true;
});
while (!IsDisposed && !importCompleted)
await Task.Delay(10);
notification.CompletionText = $"Imported {collection.Count} collections"; notification.CompletionText = $"Imported {collection.Count} collections";
notification.State = ProgressNotificationState.Completed; notification.State = ProgressNotificationState.Completed;
} }
private void importCollections(List<BeatmapCollection> newCollections) private Task importCollections(List<BeatmapCollection> newCollections)
{ {
foreach (var newCol in newCollections) var tcs = new TaskCompletionSource<bool>();
{
var existing = Collections.FirstOrDefault(c => c.Name == newCol.Name);
if (existing == null)
Collections.Add(existing = new BeatmapCollection { Name = { Value = newCol.Name.Value } });
foreach (var newBeatmap in newCol.Beatmaps) Schedule(() =>
{
try
{ {
if (!existing.Beatmaps.Contains(newBeatmap)) foreach (var newCol in newCollections)
existing.Beatmaps.Add(newBeatmap); {
var existing = Collections.FirstOrDefault(c => c.Name == newCol.Name);
if (existing == null)
Collections.Add(existing = new BeatmapCollection { Name = { Value = newCol.Name.Value } });
foreach (var newBeatmap in newCol.Beatmaps)
{
if (!existing.Beatmaps.Contains(newBeatmap))
existing.Beatmaps.Add(newBeatmap);
}
}
tcs.SetResult(true);
} }
} catch (Exception e)
{
Logger.Error(e, "Failed to import collection.");
tcs.SetException(e);
}
});
return tcs.Task;
} }
private List<BeatmapCollection> readCollections(Stream stream, ProgressNotification notification = null) private List<BeatmapCollection> readCollections(Stream stream, ProgressNotification notification = null)