1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:37:28 +08:00

Enable NRT on BeatmapDiffiultyCache

This commit is contained in:
Dean Herbert 2022-06-23 19:44:38 +09:00
parent 777ac98599
commit b068df2149

View File

@ -1,15 +1,12 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions; using osu.Framework.Extensions;
@ -50,19 +47,19 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
private readonly object bindableUpdateLock = new object(); private readonly object bindableUpdateLock = new object();
private CancellationTokenSource trackedUpdateCancellationSource; private CancellationTokenSource trackedUpdateCancellationSource = new CancellationTokenSource();
[Resolved] [Resolved]
private BeatmapManager beatmapManager { get; set; } private BeatmapManager beatmapManager { get; set; } = null!;
[Resolved] [Resolved]
private Bindable<RulesetInfo> currentRuleset { get; set; } private Bindable<RulesetInfo> currentRuleset { get; set; } = null!;
[Resolved] [Resolved]
private Bindable<IReadOnlyList<Mod>> currentMods { get; set; } private Bindable<IReadOnlyList<Mod>> currentMods { get; set; } = null!;
private ModSettingChangeTracker modSettingChangeTracker; private ModSettingChangeTracker? modSettingChangeTracker;
private ScheduledDelegate debouncedModSettingsChange; private ScheduledDelegate? debouncedModSettingsChange;
protected override void LoadComplete() protected override void LoadComplete()
{ {
@ -91,7 +88,7 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapInfo">The <see cref="BeatmapInfo"/> to get the difficulty of.</param> /// <param name="beatmapInfo">The <see cref="BeatmapInfo"/> to get the difficulty of.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="BeatmapInfo"/>.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="BeatmapInfo"/>.</param>
/// <returns>A bindable that is updated to contain the star difficulty when it becomes available. Will be null while in an initial calculating state (but not during updates to ruleset and mods if a stale value is already propagated).</returns> /// <returns>A bindable that is updated to contain the star difficulty when it becomes available. Will be null while in an initial calculating state (but not during updates to ruleset and mods if a stale value is already propagated).</returns>
public IBindable<StarDifficulty?> GetBindableDifficulty([NotNull] IBeatmapInfo beatmapInfo, CancellationToken cancellationToken = default) public IBindable<StarDifficulty?> GetBindableDifficulty(IBeatmapInfo beatmapInfo, CancellationToken cancellationToken = default)
{ {
var bindable = createBindable(beatmapInfo, currentRuleset.Value, currentMods.Value, cancellationToken); var bindable = createBindable(beatmapInfo, currentRuleset.Value, currentMods.Value, cancellationToken);
@ -112,7 +109,7 @@ namespace osu.Game.Beatmaps
/// <param name="mods">The <see cref="Mod"/>s to get the difficulty with. If <c>null</c>, no mods will be assumed.</param> /// <param name="mods">The <see cref="Mod"/>s to get the difficulty with. If <c>null</c>, no mods will be assumed.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="IBeatmapInfo"/>.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="IBeatmapInfo"/>.</param>
/// <returns>A bindable that is updated to contain the star difficulty when it becomes available. Will be null while in an initial calculating state.</returns> /// <returns>A bindable that is updated to contain the star difficulty when it becomes available. Will be null while in an initial calculating state.</returns>
public IBindable<StarDifficulty?> GetBindableDifficulty([NotNull] IBeatmapInfo beatmapInfo, [CanBeNull] IRulesetInfo rulesetInfo, [CanBeNull] IEnumerable<Mod> mods, public IBindable<StarDifficulty?> GetBindableDifficulty(IBeatmapInfo beatmapInfo, IRulesetInfo? rulesetInfo, IEnumerable<Mod>? mods,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
=> createBindable(beatmapInfo, rulesetInfo, mods, cancellationToken); => createBindable(beatmapInfo, rulesetInfo, mods, cancellationToken);
@ -128,8 +125,8 @@ namespace osu.Game.Beatmaps
/// A <see langword="null"/> return value indicates that the difficulty process failed or was interrupted early, /// A <see langword="null"/> return value indicates that the difficulty process failed or was interrupted early,
/// and as such there is no usable star difficulty value to be returned. /// and as such there is no usable star difficulty value to be returned.
/// </returns> /// </returns>
public virtual Task<StarDifficulty?> GetDifficultyAsync([NotNull] IBeatmapInfo beatmapInfo, [CanBeNull] IRulesetInfo rulesetInfo = null, public virtual Task<StarDifficulty?> GetDifficultyAsync(IBeatmapInfo beatmapInfo, IRulesetInfo? rulesetInfo = null,
[CanBeNull] IEnumerable<Mod> mods = null, CancellationToken cancellationToken = default) IEnumerable<Mod>? mods = null, CancellationToken cancellationToken = default)
{ {
// In the case that the user hasn't given us a ruleset, use the beatmap's default ruleset. // In the case that the user hasn't given us a ruleset, use the beatmap's default ruleset.
rulesetInfo ??= beatmapInfo.Ruleset; rulesetInfo ??= beatmapInfo.Ruleset;
@ -204,7 +201,6 @@ namespace osu.Game.Beatmaps
lock (bindableUpdateLock) lock (bindableUpdateLock)
{ {
cancelTrackedBindableUpdate(); cancelTrackedBindableUpdate();
trackedUpdateCancellationSource = new CancellationTokenSource();
foreach (var b in trackedBindables) foreach (var b in trackedBindables)
{ {
@ -223,16 +219,13 @@ namespace osu.Game.Beatmaps
{ {
lock (bindableUpdateLock) lock (bindableUpdateLock)
{ {
trackedUpdateCancellationSource?.Cancel(); trackedUpdateCancellationSource.Cancel();
trackedUpdateCancellationSource = null; trackedUpdateCancellationSource = new CancellationTokenSource();
if (linkedCancellationSources != null) foreach (var c in linkedCancellationSources)
{ c.Dispose();
foreach (var c in linkedCancellationSources)
c.Dispose();
linkedCancellationSources.Clear(); linkedCancellationSources.Clear();
}
} }
} }
@ -244,7 +237,7 @@ namespace osu.Game.Beatmaps
/// <param name="initialMods">The initial <see cref="Mod"/>s to get the difficulty with.</param> /// <param name="initialMods">The initial <see cref="Mod"/>s to get the difficulty with.</param>
/// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="IBeatmapInfo"/>.</param> /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> which stops updating the star difficulty for the given <see cref="IBeatmapInfo"/>.</param>
/// <returns>The <see cref="BindableStarDifficulty"/>.</returns> /// <returns>The <see cref="BindableStarDifficulty"/>.</returns>
private BindableStarDifficulty createBindable([NotNull] IBeatmapInfo beatmapInfo, [CanBeNull] IRulesetInfo initialRulesetInfo, [CanBeNull] IEnumerable<Mod> initialMods, private BindableStarDifficulty createBindable(IBeatmapInfo beatmapInfo, IRulesetInfo? initialRulesetInfo, IEnumerable<Mod>? initialMods,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
var bindable = new BindableStarDifficulty(beatmapInfo, cancellationToken); var bindable = new BindableStarDifficulty(beatmapInfo, cancellationToken);
@ -259,7 +252,7 @@ namespace osu.Game.Beatmaps
/// <param name="rulesetInfo">The <see cref="IRulesetInfo"/> to update with.</param> /// <param name="rulesetInfo">The <see cref="IRulesetInfo"/> to update with.</param>
/// <param name="mods">The <see cref="Mod"/>s to update with.</param> /// <param name="mods">The <see cref="Mod"/>s to update with.</param>
/// <param name="cancellationToken">A token that may be used to cancel this update.</param> /// <param name="cancellationToken">A token that may be used to cancel this update.</param>
private void updateBindable([NotNull] BindableStarDifficulty bindable, [CanBeNull] IRulesetInfo rulesetInfo, [CanBeNull] IEnumerable<Mod> mods, CancellationToken cancellationToken = default) private void updateBindable(BindableStarDifficulty bindable, IRulesetInfo? rulesetInfo, IEnumerable<Mod>? mods, CancellationToken cancellationToken = default)
{ {
// GetDifficultyAsync will fall back to existing data from IBeatmapInfo if not locally available // GetDifficultyAsync will fall back to existing data from IBeatmapInfo if not locally available
// (contrary to GetAsync) // (contrary to GetAsync)
@ -329,7 +322,7 @@ namespace osu.Game.Beatmaps
modSettingChangeTracker?.Dispose(); modSettingChangeTracker?.Dispose();
cancelTrackedBindableUpdate(); cancelTrackedBindableUpdate();
updateScheduler?.Dispose(); updateScheduler.Dispose();
} }
public readonly struct DifficultyCacheLookup : IEquatable<DifficultyCacheLookup> public readonly struct DifficultyCacheLookup : IEquatable<DifficultyCacheLookup>
@ -339,7 +332,7 @@ namespace osu.Game.Beatmaps
public readonly Mod[] OrderedMods; public readonly Mod[] OrderedMods;
public DifficultyCacheLookup([NotNull] BeatmapInfo beatmapInfo, [CanBeNull] RulesetInfo ruleset, IEnumerable<Mod> mods) public DifficultyCacheLookup(BeatmapInfo beatmapInfo, RulesetInfo? ruleset, IEnumerable<Mod>? mods)
{ {
BeatmapInfo = beatmapInfo; BeatmapInfo = beatmapInfo;
// In the case that the user hasn't given us a ruleset, use the beatmap's default ruleset. // In the case that the user hasn't given us a ruleset, use the beatmap's default ruleset.