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;
|
2023-12-09 20:57:34 +08:00
|
|
|
using System.Linq;
|
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;
|
2023-12-09 20:57:34 +08:00
|
|
|
using osu.Game.Rulesets.Objects.Types;
|
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-12-09 20:57:34 +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();
|
2023-12-18 17:34:55 +08:00
|
|
|
beatmap.EndTimeObjectCount = working.Beatmap.HitObjects.Count(h => h is IHasDuration);
|
|
|
|
beatmap.TotalObjectCount = working.Beatmap.HitObjects.Count;
|
2022-06-24 17:27:47 +08:00
|
|
|
}
|
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
|
|
|
|
2023-12-09 20:57:34 +08:00
|
|
|
public void ProcessObjectCounts(BeatmapInfo beatmapInfo, MetadataLookupScope lookupScope = MetadataLookupScope.LocalCacheFirst) => beatmapInfo.Realm!.Write(_ =>
|
|
|
|
{
|
|
|
|
// Before we use below, we want to invalidate.
|
|
|
|
workingBeatmapCache.Invalidate(beatmapInfo);
|
|
|
|
|
|
|
|
var working = workingBeatmapCache.GetWorkingBeatmap(beatmapInfo);
|
|
|
|
var beatmap = working.Beatmap;
|
|
|
|
|
2023-12-13 16:33:24 +08:00
|
|
|
beatmapInfo.EndTimeObjectCount = beatmap.HitObjects.Count(h => h is IHasDuration);
|
|
|
|
beatmapInfo.TotalObjectCount = beatmap.HitObjects.Count;
|
2023-12-09 20:57:34 +08:00
|
|
|
|
|
|
|
// And invalidate again afterwards as re-fetching the most up-to-date database metadata will be required.
|
|
|
|
workingBeatmapCache.Invalidate(beatmapInfo);
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|