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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 21 additions & 10 deletions osu.Game/Database/RealmArchiveModelImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,27 @@ 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 =>
notification.Text = $"{HumanisedModelName.Humanize(LetterCasing.Title)} import is initialising...";
notification.State = ProgressNotificationState.Active;

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

await Parallel.ForEachAsync(tasks, notification.CancellationToken, async (task, cancellation) =>
{
if (notification.CancellationToken.IsCancellationRequested)
return;
cancellation.ThrowIfCancellationRequested();

try
{
var model = await Import(task, parameters, notification.CancellationToken).ConfigureAwait(false);
await pauseIfNecessaryAsync(parameters, notification, cancellation).ConfigureAwait(false);

var model = await Import(task, parameters, cancellation).ConfigureAwait(false);

lock (imported)
{
Expand All @@ -139,7 +144,7 @@ await Task.WhenAll(tasks.Select(async task =>
{
Logger.Error(e, $@"Could not import ({task})", LoggingTarget.Database);
}
})).ConfigureAwait(false);
}).ConfigureAwait(false);

if (imported.Count == 0)
{
Expand Down Expand Up @@ -286,8 +291,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);

TModel? existing;

if (parameters.Batch && archive != null)
Expand Down Expand Up @@ -575,21 +578,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
Loading