1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-03 13:33:07 +08:00

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

@ -62,6 +62,7 @@ namespace osu.Game.Beatmaps
BeatmapTrackStore = audioManager.GetTrackStore(userResources);
beatmapImporter = CreateBeatmapImporter(storage, realm);
beatmapImporter.PauseImports.BindTo(PauseImports);
beatmapImporter.ProcessBeatmap = args => ProcessBeatmap?.Invoke(args);
beatmapImporter.PostNotification = obj => PostNotification?.Invoke(obj);
@ -458,7 +459,8 @@ namespace osu.Game.Beatmaps
public Task Import(ImportTask[] tasks, ImportParameters parameters = default) => beatmapImporter.Import(tasks, parameters);
public Task<IEnumerable<Live<BeatmapSetInfo>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) => beatmapImporter.Import(notification, tasks, parameters);
public Task<IEnumerable<Live<BeatmapSetInfo>>> Import(ProgressNotification notification, ImportTask[] tasks, ImportParameters parameters = default) =>
beatmapImporter.Import(notification, tasks, parameters);
public Task<Live<BeatmapSetInfo>?> Import(ImportTask task, ImportParameters parameters = default, CancellationToken cancellationToken = default) =>
beatmapImporter.Import(task, parameters, cancellationToken);

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Extensions;
@ -18,6 +19,11 @@ namespace osu.Game.Database
public class ModelManager<TModel> : IModelManager<TModel>, IModelFileManager<TModel, RealmNamedFileUsage>
where TModel : RealmObject, IHasRealmFiles, IHasGuidPrimaryKey, ISoftDelete
{
/// <summary>
/// Temporarily pause imports to avoid performance overheads affecting gameplay scenarios.
/// </summary>
public readonly BindableBool PauseImports = new BindableBool();
protected RealmAccess Realm { get; }
private readonly RealmFileStore realmFileStore;

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))

View File

@ -307,6 +307,10 @@ namespace osu.Game
// Transfer any runtime changes back to configuration file.
SkinManager.CurrentSkinInfo.ValueChanged += skin => configSkin.Value = skin.NewValue.ID.ToString();
BeatmapManager.PauseImports.BindTo(LocalUserPlaying);
SkinManager.PauseImports.BindTo(LocalUserPlaying);
ScoreManager.PauseImports.BindTo(LocalUserPlaying);
IsActive.BindValueChanged(active => updateActiveState(active.NewValue), true);
Audio.AddAdjustment(AdjustableProperty.Volume, inactiveVolumeFade);

View File

@ -38,6 +38,8 @@ namespace osu.Game.Scoring
{
PostNotification = obj => PostNotification?.Invoke(obj)
};
scoreImporter.PauseImports.BindTo(PauseImports);
}
public Score GetScore(ScoreInfo score) => scoreImporter.GetScore(score);

View File

@ -79,6 +79,8 @@ namespace osu.Game.Skinning
PostNotification = obj => PostNotification?.Invoke(obj),
};
skinImporter.PauseImports.BindTo(PauseImports);
var defaultSkins = new[]
{
DefaultClassicSkin = new DefaultLegacySkin(this),