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.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2022-06-30 16:03:19 +08:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
|
|
|
using osu.Framework.Platform;
|
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
|
|
|
using osu.Game.Rulesets.Objects;
|
|
|
|
|
|
|
|
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-06-20 17:39:53 +08:00
|
|
|
private readonly BeatmapOnlineLookupQueue onlineLookupQueue;
|
|
|
|
private readonly BeatmapDifficultyCache difficultyCache;
|
|
|
|
|
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
|
|
|
|
|
|
|
onlineLookupQueue = new BeatmapOnlineLookupQueue(api, storage);
|
2022-06-20 17:39:53 +08:00
|
|
|
}
|
|
|
|
|
2022-07-04 17:13:53 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Queue a beatmap for background processing.
|
|
|
|
/// </summary>
|
|
|
|
public void Queue(int beatmapSetId)
|
|
|
|
{
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
|
2022-06-20 17:39:53 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Queue a beatmap for background processing.
|
|
|
|
/// </summary>
|
|
|
|
public void Queue(Live<BeatmapSetInfo> beatmap)
|
|
|
|
{
|
|
|
|
// For now, just fire off a task.
|
|
|
|
// TODO: Add actual queueing probably.
|
|
|
|
Task.Factory.StartNew(() => beatmap.PerformRead(Process));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Run all processing on a beatmap immediately.
|
|
|
|
/// </summary>
|
2022-07-07 16:37:46 +08:00
|
|
|
public void Process(BeatmapSetInfo beatmapSet) => beatmapSet.Realm.Write(r =>
|
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
|
|
|
|
2022-06-20 17:59:08 +08:00
|
|
|
// TODO: this call currently uses the local `online.db` lookup.
|
|
|
|
// We probably don't want this to happen after initial import (as the data may be stale).
|
2022-06-24 17:27:47 +08:00
|
|
|
onlineLookupQueue.Update(beatmapSet);
|
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;
|
2022-06-24 17:27:47 +08:00
|
|
|
beatmap.Length = calculateLength(working.Beatmap);
|
|
|
|
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
|
|
|
|
|
|
|
private double calculateLength(IBeatmap b)
|
|
|
|
{
|
|
|
|
if (!b.HitObjects.Any())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
var lastObject = b.HitObjects.Last();
|
|
|
|
|
|
|
|
//TODO: this isn't always correct (consider mania where a non-last object may last for longer than the last in the list).
|
|
|
|
double endTime = lastObject.GetEndTime();
|
|
|
|
double startTime = b.HitObjects.First().StartTime;
|
|
|
|
|
|
|
|
return endTime - startTime;
|
|
|
|
}
|
2022-06-30 16:03:19 +08:00
|
|
|
|
|
|
|
#region Implementation of IDisposable
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
if (onlineLookupQueue.IsNotNull())
|
|
|
|
onlineLookupQueue.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-06-20 17:39:53 +08:00
|
|
|
}
|
|
|
|
}
|