Rename BeatmapDifficultyManager to BeatmapDifficultyCache

This commit is contained in:
Dean Herbert 2020-11-06 13:14:23 +09:00
parent 3e652fdf9c
commit 5113d4af8f
12 changed files with 38 additions and 34 deletions

View File

@ -14,8 +14,8 @@ public class BeatmapDifficultyManagerTest
[Test]
public void TestKeyEqualsWithDifferentModInstances()
{
var key1 = new BeatmapDifficultyManager.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
var key2 = new BeatmapDifficultyManager.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
var key1 = new BeatmapDifficultyCache.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
var key2 = new BeatmapDifficultyCache.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
Assert.That(key1, Is.EqualTo(key2));
}
@ -23,8 +23,8 @@ public void TestKeyEqualsWithDifferentModInstances()
[Test]
public void TestKeyEqualsWithDifferentModOrder()
{
var key1 = new BeatmapDifficultyManager.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
var key2 = new BeatmapDifficultyManager.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHidden(), new OsuModHardRock() });
var key1 = new BeatmapDifficultyCache.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHardRock(), new OsuModHidden() });
var key2 = new BeatmapDifficultyCache.DifficultyCacheLookup(1234, 0, new Mod[] { new OsuModHidden(), new OsuModHardRock() });
Assert.That(key1, Is.EqualTo(key2));
}
@ -47,7 +47,7 @@ public void TestKeyEqualsWithDifferentModOrder()
[TestCase(8.3, DifficultyRating.ExpertPlus)]
public void TestDifficultyRatingMapping(double starRating, DifficultyRating expectedBracket)
{
var actualBracket = BeatmapDifficultyManager.GetDifficultyRating(starRating);
var actualBracket = BeatmapDifficultyCache.GetDifficultyRating(starRating);
Assert.AreEqual(expectedBracket, actualBracket);
}

View File

@ -11,7 +11,7 @@
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics;
using osu.Framework.Lists;
using osu.Framework.Logging;
using osu.Framework.Threading;
@ -23,10 +23,14 @@
namespace osu.Game.Beatmaps
{
public class BeatmapDifficultyManager : CompositeDrawable
/// <summary>
/// A component which performs and acts as a central cache for difficulty calculations of beatmap/ruleset/mod combinations.
/// Currently not persisted between game sessions.
/// </summary>
public class BeatmapDifficultyCache : Component
{
// Too many simultaneous updates can lead to stutters. One thread seems to work fine for song select display purposes.
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(1, nameof(BeatmapDifficultyManager));
private readonly ThreadedTaskScheduler updateScheduler = new ThreadedTaskScheduler(1, nameof(BeatmapDifficultyCache));
// A permanent cache to prevent re-computations.
private readonly ConcurrentDictionary<DifficultyCacheLookup, StarDifficulty> difficultyCache = new ConcurrentDictionary<DifficultyCacheLookup, StarDifficulty>();
@ -387,6 +391,6 @@ public StarDifficulty(double starDifficulty, int maxCombo)
Attributes = null;
}
public DifficultyRating DifficultyRating => BeatmapDifficultyManager.GetDifficultyRating(Stars);
public DifficultyRating DifficultyRating => BeatmapDifficultyCache.GetDifficultyRating(Stars);
}
}

View File

@ -136,7 +136,7 @@ public string StoredBookmarks
public List<ScoreInfo> Scores { get; set; }
[JsonIgnore]
public DifficultyRating DifficultyRating => BeatmapDifficultyManager.GetDifficultyRating(StarDifficulty);
public DifficultyRating DifficultyRating => BeatmapDifficultyCache.GetDifficultyRating(StarDifficulty);
public string[] SearchableTerms => new[]
{

View File

@ -142,7 +142,7 @@ private class DifficultyRetriever : Component
private CancellationTokenSource difficultyCancellation;
[Resolved]
private BeatmapDifficultyManager difficultyManager { get; set; }
private BeatmapDifficultyCache difficultyCache { get; set; }
public DifficultyRetriever(BeatmapInfo beatmap, RulesetInfo ruleset, IReadOnlyList<Mod> mods)
{
@ -158,8 +158,8 @@ private void load()
{
difficultyCancellation = new CancellationTokenSource();
localStarDifficulty = ruleset != null
? difficultyManager.GetBindableDifficulty(beatmap, ruleset, mods, difficultyCancellation.Token)
: difficultyManager.GetBindableDifficulty(beatmap, difficultyCancellation.Token);
? difficultyCache.GetBindableDifficulty(beatmap, ruleset, mods, difficultyCancellation.Token)
: difficultyCache.GetBindableDifficulty(beatmap, difficultyCancellation.Token);
localStarDifficulty.BindValueChanged(difficulty => StarDifficulty.Value = difficulty.NewValue);
}

View File

@ -59,7 +59,7 @@ public class OsuGameBase : Framework.Game, ICanAcceptFiles
protected ScoreManager ScoreManager;
protected BeatmapDifficultyManager DifficultyManager;
protected BeatmapDifficultyCache DifficultyCache;
protected SkinManager SkinManager;
@ -202,7 +202,7 @@ private void load()
dependencies.Cache(FileStore = new FileStore(contextFactory, Storage));
// ordering is important here to ensure foreign keys rules are not broken in ModelStore.Cleanup()
dependencies.Cache(ScoreManager = new ScoreManager(RulesetStore, () => BeatmapManager, Storage, API, contextFactory, Host, () => DifficultyManager, LocalConfig));
dependencies.Cache(ScoreManager = new ScoreManager(RulesetStore, () => BeatmapManager, Storage, API, contextFactory, Host, () => DifficultyCache, LocalConfig));
dependencies.Cache(BeatmapManager = new BeatmapManager(Storage, contextFactory, RulesetStore, API, Audio, Host, defaultBeatmap, true));
// this should likely be moved to ArchiveModelManager when another case appers where it is necessary
@ -226,8 +226,8 @@ List<ScoreInfo> getBeatmapScores(BeatmapSetInfo set)
ScoreManager.Undelete(getBeatmapScores(item), true);
});
dependencies.Cache(DifficultyManager = new BeatmapDifficultyManager());
AddInternal(DifficultyManager);
dependencies.Cache(DifficultyCache = new BeatmapDifficultyCache());
AddInternal(DifficultyCache);
var scorePerformanceManager = new ScorePerformanceManager();
dependencies.Cache(scorePerformanceManager);

View File

@ -37,13 +37,13 @@ public class ScoreManager : DownloadableArchiveModelManager<ScoreInfo, ScoreFile
private readonly Func<BeatmapManager> beatmaps;
[CanBeNull]
private readonly Func<BeatmapDifficultyManager> difficulties;
private readonly Func<BeatmapDifficultyCache> difficulties;
[CanBeNull]
private readonly OsuConfigManager configManager;
public ScoreManager(RulesetStore rulesets, Func<BeatmapManager> beatmaps, Storage storage, IAPIProvider api, IDatabaseContextFactory contextFactory, IIpcHost importHost = null,
Func<BeatmapDifficultyManager> difficulties = null, OsuConfigManager configManager = null)
Func<BeatmapDifficultyCache> difficulties = null, OsuConfigManager configManager = null)
: base(storage, contextFactory, api, new ScoreStore(contextFactory, storage), importHost)
{
this.rulesets = rulesets;
@ -121,14 +121,14 @@ private class TotalScoreBindable : Bindable<long>
public readonly Bindable<ScoringMode> ScoringMode = new Bindable<ScoringMode>();
private readonly ScoreInfo score;
private readonly Func<BeatmapDifficultyManager> difficulties;
private readonly Func<BeatmapDifficultyCache> difficulties;
/// <summary>
/// Creates a new <see cref="TotalScoreBindable"/>.
/// </summary>
/// <param name="score">The <see cref="ScoreInfo"/> to provide the total score of.</param>
/// <param name="difficulties">A function to retrieve the <see cref="BeatmapDifficultyManager"/>.</param>
public TotalScoreBindable(ScoreInfo score, Func<BeatmapDifficultyManager> difficulties)
/// <param name="difficulties">A function to retrieve the <see cref="BeatmapDifficultyCache"/>.</param>
public TotalScoreBindable(ScoreInfo score, Func<BeatmapDifficultyCache> difficulties)
{
this.score = score;
this.difficulties = difficulties;

View File

@ -22,7 +22,7 @@ public class ScorePerformanceManager : Component
private readonly ConcurrentDictionary<PerformanceCacheLookup, double> performanceCache = new ConcurrentDictionary<PerformanceCacheLookup, double>();
[Resolved]
private BeatmapDifficultyManager difficultyManager { get; set; }
private BeatmapDifficultyCache difficultyCache { get; set; }
/// <summary>
/// Calculates performance for the given <see cref="ScoreInfo"/>.
@ -41,7 +41,7 @@ public class ScorePerformanceManager : Component
private async Task<double?> computePerformanceAsync(ScoreInfo score, PerformanceCacheLookup lookupKey, CancellationToken token = default)
{
var attributes = await difficultyManager.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
var attributes = await difficultyCache.GetDifficultyAsync(score.Beatmap, score.Ruleset, score.Mods, token);
// Performance calculation requires the beatmap and ruleset to be locally available. If not, return a default value.
if (attributes.Attributes == null)

View File

@ -56,7 +56,7 @@ public ExpandedPanelMiddleContent(ScoreInfo score, bool withFlair = false)
}
[BackgroundDependencyLoader]
private void load(BeatmapDifficultyManager beatmapDifficultyManager)
private void load(BeatmapDifficultyCache beatmapDifficultyCache)
{
var beatmap = score.Beatmap;
var metadata = beatmap.BeatmapSet?.Metadata ?? beatmap.Metadata;
@ -143,7 +143,7 @@ private void load(BeatmapDifficultyManager beatmapDifficultyManager)
Spacing = new Vector2(5, 0),
Children = new Drawable[]
{
new StarRatingDisplay(beatmapDifficultyManager.GetDifficulty(beatmap, score.Ruleset, score.Mods))
new StarRatingDisplay(beatmapDifficultyCache.GetDifficulty(beatmap, score.Ruleset, score.Mods))
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft

View File

@ -34,7 +34,7 @@ public StarRatingDisplay(StarDifficulty starDifficulty)
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, BeatmapDifficultyManager difficultyManager)
private void load(OsuColour colours, BeatmapDifficultyCache difficultyCache)
{
AutoSizeAxes = Axes.Both;

View File

@ -41,7 +41,7 @@ public class BeatmapInfoWedge : VisibilityContainer
private readonly IBindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
[Resolved]
private BeatmapDifficultyManager difficultyManager { get; set; }
private BeatmapDifficultyCache difficultyCache { get; set; }
private IBindable<StarDifficulty> beatmapDifficulty;
@ -100,7 +100,7 @@ public WorkingBeatmap Beatmap
cancellationSource = new CancellationTokenSource();
beatmapDifficulty?.UnbindAll();
beatmapDifficulty = difficultyManager.GetBindableDifficulty(beatmap.BeatmapInfo, cancellationSource.Token);
beatmapDifficulty = difficultyCache.GetBindableDifficulty(beatmap.BeatmapInfo, cancellationSource.Token);
beatmapDifficulty.BindValueChanged(_ => updateDisplay());
updateDisplay();

View File

@ -55,7 +55,7 @@ public class DrawableCarouselBeatmap : DrawableCarouselItem, IHasContextMenu
private BeatmapSetOverlay beatmapOverlay { get; set; }
[Resolved]
private BeatmapDifficultyManager difficultyManager { get; set; }
private BeatmapDifficultyCache difficultyCache { get; set; }
[Resolved(CanBeNull = true)]
private CollectionManager collectionManager { get; set; }
@ -216,7 +216,7 @@ protected override void ApplyState()
if (Item.State.Value != CarouselItemState.Collapsed)
{
// We've potentially cancelled the computation above so a new bindable is required.
starDifficultyBindable = difficultyManager.GetBindableDifficulty(beatmap, (starDifficultyCancellationSource = new CancellationTokenSource()).Token);
starDifficultyBindable = difficultyCache.GetBindableDifficulty(beatmap, (starDifficultyCancellationSource = new CancellationTokenSource()).Token);
starDifficultyBindable.BindValueChanged(d => starCounter.Current = (float)d.NewValue.Stars, true);
}

View File

@ -32,7 +32,7 @@ public class AdvancedStats : Container
private IBindable<RulesetInfo> ruleset { get; set; }
[Resolved]
private BeatmapDifficultyManager difficultyManager { get; set; }
private BeatmapDifficultyCache difficultyCache { get; set; }
protected readonly StatisticRow FirstValue, HpDrain, Accuracy, ApproachRate;
private readonly StatisticRow starDifficulty;
@ -161,8 +161,8 @@ private void updateStarDifficulty()
starDifficultyCancellationSource = new CancellationTokenSource();
normalStarDifficulty = difficultyManager.GetBindableDifficulty(Beatmap, ruleset.Value, null, starDifficultyCancellationSource.Token);
moddedStarDifficulty = difficultyManager.GetBindableDifficulty(Beatmap, ruleset.Value, mods.Value, starDifficultyCancellationSource.Token);
normalStarDifficulty = difficultyCache.GetBindableDifficulty(Beatmap, ruleset.Value, null, starDifficultyCancellationSource.Token);
moddedStarDifficulty = difficultyCache.GetBindableDifficulty(Beatmap, ruleset.Value, mods.Value, starDifficultyCancellationSource.Token);
normalStarDifficulty.BindValueChanged(_ => updateDisplay());
moddedStarDifficulty.BindValueChanged(_ => updateDisplay(), true);