Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update notification text when import is paused due to gameplay #30402

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions osu.Game.Tests/Visual/Online/TestSceneReplayMissingBeatmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ public void TestSceneMissingBeatmapWithOnlineAvailable()

AddStep("import score", () =>
{
using (var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr"))
{
var importTask = new ImportTask(resourceStream, "replay.osr");
var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr");
var importTask = new ImportTask(resourceStream, "replay.osr");

Game.ScoreManager.Import(new[] { importTask });
}
Game.ScoreManager.Import(new[] { importTask });
});

AddUntilStep("Replay missing notification show", () => Game.Notifications.ChildrenOfType<MissingBeatmapNotification>().Any());
AddUntilStep("Replay missing notification shown", () => Game.Notifications.ChildrenOfType<MissingBeatmapNotification>().Any());
}

[Test]
Expand All @@ -58,15 +56,13 @@ public void TestSceneMissingBeatmapWithOnlineUnavailable()

AddStep("import score", () =>
{
using (var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr"))
{
var importTask = new ImportTask(resourceStream, "replay.osr");
var resourceStream = TestResources.OpenResource("Replays/mania-replay.osr");
var importTask = new ImportTask(resourceStream, "replay.osr");

Game.ScoreManager.Import(new[] { importTask });
}
Game.ScoreManager.Import(new[] { importTask });
});

AddUntilStep("Replay missing notification not show", () => !Game.Notifications.ChildrenOfType<MissingBeatmapNotification>().Any());
AddUntilStep("Replay missing notification not shown", () => !Game.Notifications.ChildrenOfType<MissingBeatmapNotification>().Any());
}

private void setupBeatmapResponse(APIBeatmap b)
Expand Down
25 changes: 15 additions & 10 deletions osu.Game/Database/ModelDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,23 @@ protected bool Download(T model, bool minimiseDownloadSize, TModel? originalMode
{
Task.Factory.StartNew(async () =>
{
bool importSuccessful;
bool importSuccessful = false;

if (originalModel != null)
importSuccessful = (await importer.ImportAsUpdate(notification, new ImportTask(filename), originalModel).ConfigureAwait(false)) != null;
else
importSuccessful = (await importer.Import(notification, new[] { new ImportTask(filename) }).ConfigureAwait(false)).Any();

// for now a failed import will be marked as a failed download for simplicity.
if (!importSuccessful)
DownloadFailed?.Invoke(request);
try
{
if (originalModel != null)
importSuccessful = (await importer.ImportAsUpdate(notification, new ImportTask(filename), originalModel).ConfigureAwait(false)) != null;
else
importSuccessful = (await importer.Import(notification, new[] { new ImportTask(filename) }).ConfigureAwait(false)).Any();
}
finally
{
// for now a failed import will be marked as a failed download for simplicity.
if (!importSuccessful)
DownloadFailed?.Invoke(request);

CurrentDownloads.Remove(request);
CurrentDownloads.Remove(request);
}
}, TaskCreationOptions.LongRunning);
};

Expand Down
123 changes: 71 additions & 52 deletions osu.Game/Database/RealmArchiveModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,73 +105,85 @@ public async Task<IEnumerable<Live<TModel>>> Import(ProgressNotification notific
}

notification.Progress = 0;
notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is initialising...";

int current = 0;

var imported = new List<Live<TModel>>();

parameters.Batch |= tasks.Length >= minimum_items_considered_batch_import;

await Task.WhenAll(tasks.Select(async task =>
{
if (notification.CancellationToken.IsCancellationRequested)
return;
notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is initialising...";
notification.State = ProgressNotificationState.Active;

try
await pauseIfNecessaryAsync(parameters, notification, notification.CancellationToken).ConfigureAwait(false);

try
{
await Parallel.ForEachAsync(tasks, notification.CancellationToken, async (task, cancellation) =>
{
var model = await Import(task, parameters, notification.CancellationToken).ConfigureAwait(false);
cancellation.ThrowIfCancellationRequested();

lock (imported)
try
{
if (model != null)
imported.Add(model);
current++;
await pauseIfNecessaryAsync(parameters, notification, cancellation).ConfigureAwait(false);

notification.Text = $"Imported {current} of {tasks.Length} {HumanisedModelName}s";
notification.Progress = (float)current / tasks.Length;
}
}
catch (OperationCanceledException)
{
}
catch (Exception e)
{
Logger.Error(e, $@"Could not import ({task})", LoggingTarget.Database);
}
})).ConfigureAwait(false);
var model = await Import(task, parameters, cancellation).ConfigureAwait(false);

if (imported.Count == 0)
{
if (notification.CancellationToken.IsCancellationRequested)
{
notification.State = ProgressNotificationState.Cancelled;
return imported;
}
lock (imported)
{
if (model != null)
imported.Add(model);
current++;

notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import failed! Check logs for more information.";
notification.State = ProgressNotificationState.Cancelled;
notification.Text = $"Imported {current} of {tasks.Length} {HumanisedModelName}s";
notification.Progress = (float)current / tasks.Length;
}
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception e)
{
Logger.Error(e, $@"Could not import ({task})", LoggingTarget.Database);
}
}).ConfigureAwait(false);
}
else
finally
{
if (tasks.Length > imported.Count)
notification.CompletionText = $"Imported {imported.Count} of {tasks.Length} {HumanisedModelName}s.";
else if (imported.Count > 1)
notification.CompletionText = $"Imported {imported.Count} {HumanisedModelName}s!";
else
notification.CompletionText = $"Imported {imported.First().GetDisplayString()}!";

if (imported.Count > 0 && PresentImport != null)
if (imported.Count == 0)
{
notification.CompletionText += " Click to view.";
notification.CompletionClickAction = () =>
if (notification.CancellationToken.IsCancellationRequested)
{
notification.State = ProgressNotificationState.Cancelled;
}
else
{
PresentImport?.Invoke(imported);
return true;
};
notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import failed! Check logs for more information.";
notification.State = ProgressNotificationState.Cancelled;
}
}
else
{
if (tasks.Length > imported.Count)
notification.CompletionText = $"Imported {imported.Count} of {tasks.Length} {HumanisedModelName}s.";
else if (imported.Count > 1)
notification.CompletionText = $"Imported {imported.Count} {HumanisedModelName}s!";
else
notification.CompletionText = $"Imported {imported.First().GetDisplayString()}!";

if (imported.Count > 0 && PresentImport != null)
{
notification.CompletionText += " Click to view.";
notification.CompletionClickAction = () =>
{
PresentImport?.Invoke(imported);
return true;
};
}

notification.State = ProgressNotificationState.Completed;
notification.State = ProgressNotificationState.Completed;
}
}

return imported;
Expand Down Expand Up @@ -286,8 +298,6 @@ public async Task<ExternalEditOperation<TModel>> BeginExternalEditing(TModel mod
/// <param name="cancellationToken">An optional cancellation token.</param>
public virtual Live<TModel>? ImportModel(TModel item, ArchiveReader? archive = null, ImportParameters parameters = default, CancellationToken cancellationToken = default) => Realm.Run(realm =>
{
pauseIfNecessary(parameters, cancellationToken);
smoogipoo marked this conversation as resolved.
Show resolved Hide resolved

TModel? existing;

if (parameters.Batch && archive != null)
Expand Down Expand Up @@ -528,7 +538,8 @@ protected virtual void PostImport(TModel model, Realm realm, ImportParameters pa
/// <param name="model">The new model proposed for import.</param>
/// <param name="realm">The current realm context.</param>
/// <returns>An existing model which matches the criteria to skip importing, else null.</returns>
protected TModel? CheckForExisting(TModel model, Realm realm) => string.IsNullOrEmpty(model.Hash) ? null : realm.All<TModel>().OrderBy(b => b.DeletePending).FirstOrDefault(b => b.Hash == model.Hash);
protected TModel? CheckForExisting(TModel model, Realm realm) =>
string.IsNullOrEmpty(model.Hash) ? null : realm.All<TModel>().OrderBy(b => b.DeletePending).FirstOrDefault(b => b.Hash == model.Hash);

/// <summary>
/// Whether import can be skipped after finding an existing import early in the process.
Expand Down Expand Up @@ -575,21 +586,29 @@ protected virtual void UndeleteForReuse(TModel existing)
/// <returns>Whether to perform deletion.</returns>
protected virtual bool ShouldDeleteArchive(string path) => false;

private void pauseIfNecessary(ImportParameters importParameters, CancellationToken cancellationToken)
private async Task pauseIfNecessaryAsync(ImportParameters importParameters, ProgressNotification notification, CancellationToken cancellationToken)
{
if (!PauseImports || importParameters.ImportImmediately)
return;

Logger.Log($@"{GetType().Name} is being paused.");

// A paused state could obviously be entered mid-import (during the `Task.WhenAll` below),
// but in order to keep things simple let's focus on the most common scenario.
notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is paused due to gameplay...";
notification.State = ProgressNotificationState.Queued;

while (PauseImports)
{
cancellationToken.ThrowIfCancellationRequested();
Thread.Sleep(500);
await Task.Delay(500, cancellationToken).ConfigureAwait(false);
}

cancellationToken.ThrowIfCancellationRequested();
Logger.Log($@"{GetType().Name} is being resumed.");

notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is resuming...";
notification.State = ProgressNotificationState.Active;
}

private IEnumerable<string> getIDs(IEnumerable<INamedFile> files)
Expand Down