Pause imports during active gameplay

This commit is contained in:
Dean Herbert
2023-01-09 18:54:11 +09:00
parent 07eadba6d3
commit 62ffb4fe78
6 changed files with 40 additions and 2 deletions

View File

@ -8,6 +8,7 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Humanizer;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Logging;
@ -56,6 +57,11 @@ namespace osu.Game.Database
/// </summary>
private static readonly ThreadedTaskScheduler import_scheduler_batch = new ThreadedTaskScheduler(import_queue_request_concurrency, nameof(RealmArchiveModelImporter<TModel>));
/// <summary>
/// Temporarily pause imports to avoid performance overheads affecting gameplay scenarios.
/// </summary>
public readonly BindableBool PauseImports = new BindableBool();
public abstract IEnumerable<string> HandledExtensions { get; }
protected readonly RealmFileStore Files;
@ -253,7 +259,7 @@ namespace osu.Game.Database
/// <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 =>
{
cancellationToken.ThrowIfCancellationRequested();
pauseIfNecessary(cancellationToken);
TModel? existing;
@ -551,6 +557,22 @@ namespace osu.Game.Database
/// <returns>Whether to perform deletion.</returns>
protected virtual bool ShouldDeleteArchive(string path) => false;
private void pauseIfNecessary(CancellationToken cancellationToken)
{
if (!PauseImports.Value)
return;
Logger.Log(@"Import is being paused.");
while (PauseImports.Value)
{
cancellationToken.ThrowIfCancellationRequested();
Thread.Sleep(500);
}
Logger.Log(@"Import is being resumed.");
}
private IEnumerable<string> getIDs(IEnumerable<INamedFile> files)
{
foreach (var f in files.OrderBy(f => f.Filename))