2020-09-27 10:44:29 +00: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-17 07:37:17 +00:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2020-09-28 17:04:39 +00:00
|
|
|
|
using System;
|
2020-09-27 10:44:29 +00:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Game.Beatmaps;
|
2020-11-06 04:26:18 +00:00
|
|
|
|
using osu.Game.Database;
|
2022-01-17 10:28:17 +00:00
|
|
|
|
using osu.Game.Rulesets.Difficulty;
|
2020-09-27 10:44:29 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
|
{
|
2020-11-02 05:50:44 +00:00
|
|
|
|
/// <summary>
|
2020-11-06 04:15:33 +00:00
|
|
|
|
/// A component which performs and acts as a central cache for performance calculations of locally databased scores.
|
|
|
|
|
/// Currently not persisted between game sessions.
|
2020-11-02 05:50:44 +00:00
|
|
|
|
/// </summary>
|
2022-01-17 10:28:17 +00:00
|
|
|
|
public class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, PerformanceAttributes>
|
2020-09-27 10:44:29 +00:00
|
|
|
|
{
|
2020-09-29 16:32:02 +00:00
|
|
|
|
[Resolved]
|
2020-11-06 04:14:23 +00:00
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
2020-09-29 16:32:02 +00:00
|
|
|
|
|
2020-11-06 04:50:51 +00:00
|
|
|
|
protected override bool CacheNullValues => false;
|
|
|
|
|
|
2020-09-27 10:44:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Calculates performance for the given <see cref="ScoreInfo"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="score">The score to do the calculation on. </param>
|
|
|
|
|
/// <param name="token">An optional <see cref="CancellationToken"/> to cancel the operation.</param>
|
2022-01-17 10:28:17 +00:00
|
|
|
|
public Task<PerformanceAttributes> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default) =>
|
2020-11-06 04:50:51 +00:00
|
|
|
|
GetAsync(new PerformanceCacheLookup(score), token);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2022-01-17 10:28:17 +00:00
|
|
|
|
protected override async Task<PerformanceAttributes> ComputeValueAsync(PerformanceCacheLookup lookup, CancellationToken token = default)
|
2020-09-28 17:04:39 +00:00
|
|
|
|
{
|
2020-11-06 04:50:51 +00:00
|
|
|
|
var score = lookup.ScoreInfo;
|
|
|
|
|
|
2021-10-04 08:35:53 +00:00
|
|
|
|
var attributes = await difficultyCache.GetDifficultyAsync(score.BeatmapInfo, score.Ruleset, score.Mods, token).ConfigureAwait(false);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2020-10-09 16:32:03 +00:00
|
|
|
|
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
|
2021-11-20 15:54:58 +00:00
|
|
|
|
if (attributes?.Attributes == null)
|
2020-10-10 17:16:21 +00:00
|
|
|
|
return null;
|
2020-10-09 16:32:03 +00:00
|
|
|
|
|
2020-10-10 17:16:21 +00:00
|
|
|
|
token.ThrowIfCancellationRequested();
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
2022-03-14 05:25:26 +00:00
|
|
|
|
return score.Ruleset.CreateInstance().CreatePerformanceCalculator()?.Calculate(score, attributes.Value.Attributes);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct PerformanceCacheLookup
|
|
|
|
|
{
|
2020-11-06 04:50:51 +00:00
|
|
|
|
public readonly ScoreInfo ScoreInfo;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
public PerformanceCacheLookup(ScoreInfo info)
|
|
|
|
|
{
|
2020-11-06 04:50:51 +00:00
|
|
|
|
ScoreInfo = info;
|
2020-09-28 17:04:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new HashCode();
|
|
|
|
|
|
2020-11-06 04:50:51 +00:00
|
|
|
|
hash.Add(ScoreInfo.Hash);
|
|
|
|
|
hash.Add(ScoreInfo.ID);
|
2020-09-28 17:04:39 +00:00
|
|
|
|
|
|
|
|
|
return hash.ToHashCode();
|
|
|
|
|
}
|
2020-09-27 10:44:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|