2022-06-20 17:39:53 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2022-06-30 16:03:19 +08:00
|
|
|
using System;
|
2022-06-24 18:04:20 +08:00
|
|
|
using System.Diagnostics;
|
2022-06-20 17:39:53 +08:00
|
|
|
using System.Threading.Tasks;
|
2022-06-30 16:03:19 +08:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2022-06-20 17:40:05 +08:00
|
|
|
using osu.Framework.Logging;
|
2022-06-30 16:03:19 +08:00
|
|
|
using osu.Framework.Platform;
|
2022-07-28 15:08:27 +08:00
|
|
|
using osu.Framework.Threading;
|
2022-06-20 17:39:53 +08:00
|
|
|
using osu.Game.Database;
|
2022-06-30 16:03:19 +08:00
|
|
|
using osu.Game.Online.API;
|
2022-06-20 17:39:53 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Handles all processing required to ensure a local beatmap is in a consistent state with any changes.
|
|
|
|
/// </summary>
|
2022-06-30 16:03:19 +08:00
|
|
|
public class BeatmapUpdater : IDisposable
|
2022-06-20 17:39:53 +08:00
|
|
|
{
|
2022-06-20 18:25:18 +08:00
|
|
|
private readonly IWorkingBeatmapCache workingBeatmapCache;
|
2022-07-28 15:08:27 +08:00
|
|
|
|
2022-06-20 17:39:53 +08:00
|
|
|
private readonly BeatmapDifficultyCache difficultyCache;
|
|
|
|
|
2022-07-28 15:08:27 +08:00
|
|
|
private readonly BeatmapUpdaterMetadataLookup metadataLookup;
|
|
|
|
|
|
|
|
private const int update_queue_request_concurrency = 4;
|
|
|
|
|
|
|
|
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(update_queue_request_concurrency, nameof(BeatmapUpdaterMetadataLookup));
|
|
|
|
|
2022-06-30 16:03:19 +08:00
|
|
|
public BeatmapUpdater(IWorkingBeatmapCache workingBeatmapCache, BeatmapDifficultyCache difficultyCache, IAPIProvider api, Storage storage)
|
2022-06-20 17:39:53 +08:00
|
|
|
{
|
2022-06-20 18:25:18 +08:00
|
|
|
this.workingBeatmapCache = workingBeatmapCache;
|
2022-06-20 17:39:53 +08:00
|
|
|
this.difficultyCache = difficultyCache;
|
2022-06-30 16:03:19 +08:00
|
|
|
|
2022-07-28 15:08:27 +08:00
|
|
|
metadataLookup = new BeatmapUpdaterMetadataLookup(api, storage);
|
2022-06-20 17:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Queue a beatmap for background processing.
|
|
|
|
/// </summary>
|
2022-07-28 15:55:46 +08:00
|
|
|
/// <param name="beatmapSet">The managed beatmap set to update. A transaction will be opened to apply changes.</param>
|
2023-04-12 03:42:55 +08:00
|
|
|
/// <param name="lookupScope">The preferred scope to use for metadata lookup.</param>
|
|
|
|
public void Queue(Live<BeatmapSetInfo> beatmapSet, MetadataLookupScope lookupScope = MetadataLookupScope.LocalCacheFirst)
|
2022-06-20 17:39:53 +08:00
|
|
|
{
|
2022-07-28 15:55:46 +08:00
|
|
|
Logger.Log($"Queueing change for local beatmap {beatmapSet}");
|
2023-04-12 03:42:55 +08:00
|
|
|
Task.Factory.StartNew(() => beatmapSet.PerformRead(b => Process(b, lookupScope)), default, TaskCreationOptions.HideScheduler | TaskCreationOptions.RunContinuationsAsynchronously, updateScheduler);
|
2022-06-20 17:39:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Run all processing on a beatmap immediately.
|
|
|
|
/// </summary>
|
2022-07-28 15:55:46 +08:00
|
|
|
/// <param name="beatmapSet">The managed beatmap set to update. A transaction will be opened to apply changes.</param>
|
2023-04-12 03:42:55 +08:00
|
|
|
/// <param name="lookupScope">The preferred scope to use for metadata lookup.</param>
|
2023-07-06 12:37:42 +08:00
|
|
|
public void Process(BeatmapSetInfo beatmapSet, MetadataLookupScope lookupScope = MetadataLookupScope.LocalCacheFirst) => beatmapSet.Realm!.Write(_ =>
|
2022-06-20 17:39:53 +08:00
|
|
|
{
|
2022-06-24 17:27:47 +08:00
|
|
|
// Before we use below, we want to invalidate.
|
|
|
|
workingBeatmapCache.Invalidate(beatmapSet);
|
2022-06-20 17:39:53 +08:00
|
|
|
|
2023-04-12 03:42:55 +08:00
|
|
|
if (lookupScope != MetadataLookupScope.None)
|
|
|
|
metadataLookup.Update(beatmapSet, lookupScope == MetadataLookupScope.OnlineFirst);
|
2022-06-20 17:39:53 +08:00
|
|
|
|
2022-06-24 17:27:47 +08:00
|
|
|
foreach (var beatmap in beatmapSet.Beatmaps)
|
|
|
|
{
|
2022-06-24 18:04:20 +08:00
|
|
|
difficultyCache.Invalidate(beatmap);
|
|
|
|
|
2022-06-24 20:02:14 +08:00
|
|
|
var working = workingBeatmapCache.GetWorkingBeatmap(beatmap);
|
2022-06-24 18:04:20 +08:00
|
|
|
var ruleset = working.BeatmapInfo.Ruleset.CreateInstance();
|
|
|
|
|
|
|
|
Debug.Assert(ruleset != null);
|
2022-06-20 18:25:18 +08:00
|
|
|
|
2022-06-24 18:04:20 +08:00
|
|
|
var calculator = ruleset.CreateDifficultyCalculator(working);
|
2022-06-20 18:25:18 +08:00
|
|
|
|
2022-06-24 18:04:20 +08:00
|
|
|
beatmap.StarRating = calculator.Calculate().StarRating;
|
2023-05-25 16:15:31 +08:00
|
|
|
beatmap.Length = working.Beatmap.CalculatePlayableLength();
|
2022-06-24 17:27:47 +08:00
|
|
|
beatmap.BPM = 60000 / working.Beatmap.GetMostCommonBeatLength();
|
|
|
|
}
|
2022-06-24 20:02:14 +08:00
|
|
|
|
|
|
|
// And invalidate again afterwards as re-fetching the most up-to-date database metadata will be required.
|
|
|
|
workingBeatmapCache.Invalidate(beatmapSet);
|
2022-07-07 16:37:46 +08:00
|
|
|
});
|
2022-06-20 17:39:53 +08:00
|
|
|
|
2022-06-30 16:03:19 +08:00
|
|
|
#region Implementation of IDisposable
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2022-07-28 15:08:27 +08:00
|
|
|
if (metadataLookup.IsNotNull())
|
|
|
|
metadataLookup.Dispose();
|
|
|
|
|
|
|
|
if (updateScheduler.IsNotNull())
|
|
|
|
updateScheduler.Dispose();
|
2022-06-30 16:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-06-20 17:39:53 +08:00
|
|
|
}
|
|
|
|
}
|