2020-09-27 18:44:29 +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-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2020-09-29 01:04:39 +08:00
|
|
|
|
using System;
|
2020-09-27 18:44:29 +08:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Game.Beatmaps;
|
2020-11-06 12:26:18 +08:00
|
|
|
|
using osu.Game.Database;
|
2022-01-17 18:28:17 +08:00
|
|
|
|
using osu.Game.Rulesets.Difficulty;
|
2020-09-27 18:44:29 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Scoring
|
|
|
|
|
{
|
2020-11-02 13:50:44 +08:00
|
|
|
|
/// <summary>
|
2020-11-06 12:15:33 +08: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 13:50:44 +08:00
|
|
|
|
/// </summary>
|
2022-01-17 18:28:17 +08:00
|
|
|
|
public partial class ScorePerformanceCache : MemoryCachingComponent<ScorePerformanceCache.PerformanceCacheLookup, PerformanceAttributes>
|
2020-09-27 18:44:29 +08:00
|
|
|
|
{
|
2020-09-30 00:32:02 +08:00
|
|
|
|
[Resolved]
|
2020-11-06 12:14:23 +08:00
|
|
|
|
private BeatmapDifficultyCache difficultyCache { get; set; }
|
2020-09-30 00:32:02 +08:00
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
protected override bool CacheNullValues => false;
|
|
|
|
|
|
2020-09-27 18:44:29 +08: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 18:28:17 +08:00
|
|
|
|
public Task<PerformanceAttributes> CalculatePerformanceAsync([NotNull] ScoreInfo score, CancellationToken token = default) =>
|
2020-11-06 12:50:51 +08:00
|
|
|
|
GetAsync(new PerformanceCacheLookup(score), token);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2022-01-17 18:28:17 +08:00
|
|
|
|
protected override async Task<PerformanceAttributes> ComputeValueAsync(PerformanceCacheLookup lookup, CancellationToken token = default)
|
2020-09-29 01:04:39 +08:00
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
var score = lookup.ScoreInfo;
|
|
|
|
|
|
2021-10-04 16:35:53 +08:00
|
|
|
|
var attributes = await difficultyCache.GetDifficultyAsync(score.BeatmapInfo, score.Ruleset, score.Mods, token).ConfigureAwait(false);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2020-10-10 00:32:03 +08:00
|
|
|
|
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
|
2021-11-20 23:54:58 +08:00
|
|
|
|
if (attributes?.Attributes == null)
|
2020-10-11 01:16:21 +08:00
|
|
|
|
return null;
|
2020-10-10 00:32:03 +08:00
|
|
|
|
|
2020-10-11 01:16:21 +08:00
|
|
|
|
token.ThrowIfCancellationRequested();
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
2022-03-14 13:25:26 +08:00
|
|
|
|
return score.Ruleset.CreateInstance().CreatePerformanceCalculator()?.Calculate(score, attributes.Value.Attributes);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public readonly struct PerformanceCacheLookup
|
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
public readonly ScoreInfo ScoreInfo;
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
|
|
|
|
public PerformanceCacheLookup(ScoreInfo info)
|
|
|
|
|
{
|
2020-11-06 12:50:51 +08:00
|
|
|
|
ScoreInfo = info;
|
2020-09-29 01:04:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
var hash = new HashCode();
|
|
|
|
|
|
2020-11-06 12:50:51 +08:00
|
|
|
|
hash.Add(ScoreInfo.Hash);
|
|
|
|
|
hash.Add(ScoreInfo.ID);
|
2020-09-29 01:04:39 +08:00
|
|
|
|
|
|
|
|
|
return hash.ToHashCode();
|
|
|
|
|
}
|
2020-09-27 18:44:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|