|
| 1 | +// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. |
| 2 | +// See the LICENCE file in the repository root for full licence text. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Linq; |
| 8 | +using osu.Framework.Allocation; |
| 9 | +using osu.Framework.Graphics; |
| 10 | +using osu.Framework.Logging; |
| 11 | +using osu.Game.Rulesets; |
| 12 | +using osu.Game.Rulesets.Mods; |
| 13 | + |
| 14 | +namespace osu.Game.Beatmaps |
| 15 | +{ |
| 16 | + /// <summary> |
| 17 | + /// A session-level cache for post-conversion playable <see cref="IBeatmap"/> instances. |
| 18 | + /// Avoids repeating the expensive conversion + <c>ApplyDefaults</c> pipeline when the same beatmap, |
| 19 | + /// ruleset and mod combination is requested again (e.g. quick retry, replay reload, repeated plays). |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// <para> |
| 23 | + /// The cache stores one <em>canonical</em> (immutable) copy keyed by |
| 24 | + /// <c>(BeatmapInfo.ID, rulesetShortName, orderedModsKey)</c>. |
| 25 | + /// Callers receive a shallow <see cref="IBeatmap.Clone"/> so that gameplay state does not |
| 26 | + /// bleed across sessions while still reusing the pre-built hit-object graph. |
| 27 | + /// </para> |
| 28 | + /// <para> |
| 29 | + /// All entries belonging to a particular <see cref="BeatmapInfo.ID"/> are evicted whenever |
| 30 | + /// <see cref="WorkingBeatmapCache.OnInvalidated"/> fires for that beatmap (e.g. on beatmap |
| 31 | + /// update or reimport). |
| 32 | + /// </para> |
| 33 | + /// </remarks> |
| 34 | + public class PlayableBeatmapCache : Component |
| 35 | + { |
| 36 | + private readonly record struct CacheKey(Guid BeatmapId, string RulesetShortName, string ModsKey); |
| 37 | + |
| 38 | + private readonly Dictionary<CacheKey, IBeatmap> cache = new Dictionary<CacheKey, IBeatmap>(); |
| 39 | + |
| 40 | + private WorkingBeatmapCache? workingBeatmapCache; |
| 41 | + |
| 42 | + [BackgroundDependencyLoader] |
| 43 | + private void load(IWorkingBeatmapCache beatmapCache) |
| 44 | + { |
| 45 | + if (beatmapCache is WorkingBeatmapCache concrete) |
| 46 | + { |
| 47 | + workingBeatmapCache = concrete; |
| 48 | + workingBeatmapCache.OnInvalidated += handleInvalidated; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Try to retrieve a pre-built playable beatmap from the cache. |
| 54 | + /// </summary> |
| 55 | + /// <param name="beatmapInfo">The beatmap whose playable representation is requested.</param> |
| 56 | + /// <param name="ruleset">The ruleset used for conversion.</param> |
| 57 | + /// <param name="mods">The mods applied during conversion.</param> |
| 58 | + /// <param name="playable"> |
| 59 | + /// On success, a shallow clone of the cached beatmap; ready for use in a new gameplay session. |
| 60 | + /// </param> |
| 61 | + /// <returns><c>true</c> if a cached entry was found; <c>false</c> otherwise.</returns> |
| 62 | + public bool TryGetPlayableBeatmap(BeatmapInfo beatmapInfo, IRulesetInfo ruleset, IReadOnlyList<Mod> mods, [NotNullWhen(true)] out IBeatmap? playable) |
| 63 | + { |
| 64 | + var key = makeKey(beatmapInfo, ruleset, mods); |
| 65 | + |
| 66 | + lock (cache) |
| 67 | + { |
| 68 | + if (cache.TryGetValue(key, out var cached)) |
| 69 | + { |
| 70 | + playable = cached.Clone(); |
| 71 | + return true; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + playable = null; |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Store a playable beatmap in the cache so subsequent requests can reuse it. |
| 81 | + /// </summary> |
| 82 | + /// <param name="beatmapInfo">The beatmap whose playable representation is being stored.</param> |
| 83 | + /// <param name="ruleset">The ruleset used for conversion.</param> |
| 84 | + /// <param name="mods">The mods applied during conversion.</param> |
| 85 | + /// <param name="playable">The fully-built playable beatmap to cache.</param> |
| 86 | + public void CachePlayableBeatmap(BeatmapInfo beatmapInfo, IRulesetInfo ruleset, IReadOnlyList<Mod> mods, IBeatmap playable) |
| 87 | + { |
| 88 | + var key = makeKey(beatmapInfo, ruleset, mods); |
| 89 | + |
| 90 | + lock (cache) |
| 91 | + cache[key] = playable; |
| 92 | + } |
| 93 | + |
| 94 | + private void handleInvalidated(WorkingBeatmap working) |
| 95 | + { |
| 96 | + Guid id = working.BeatmapInfo.ID; |
| 97 | + |
| 98 | + lock (cache) |
| 99 | + { |
| 100 | + int removed = 0; |
| 101 | + |
| 102 | + foreach (var key in cache.Keys.Where(k => k.BeatmapId == id).ToList()) |
| 103 | + { |
| 104 | + cache.Remove(key); |
| 105 | + removed++; |
| 106 | + } |
| 107 | + |
| 108 | + if (removed > 0) |
| 109 | + Logger.Log($"Evicted {removed} playable beatmap cache entr{(removed == 1 ? "y" : "ies")} for {working.BeatmapInfo}"); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private static CacheKey makeKey(BeatmapInfo beatmapInfo, IRulesetInfo ruleset, IReadOnlyList<Mod> mods) |
| 114 | + { |
| 115 | + // Build a deterministic key from ordered mod acronyms and their settings hash. |
| 116 | + // Mod.GetHashCode() accounts for both the type and any user-adjustable settings. |
| 117 | + string modsKey = string.Join(';', mods |
| 118 | + .OrderBy(m => m.Acronym) |
| 119 | + .Select(m => $"{m.Acronym}:{m.GetHashCode()}")); |
| 120 | + |
| 121 | + return new CacheKey(beatmapInfo.ID, ruleset.ShortName, modsKey); |
| 122 | + } |
| 123 | + |
| 124 | + protected override void Dispose(bool isDisposing) |
| 125 | + { |
| 126 | + base.Dispose(isDisposing); |
| 127 | + |
| 128 | + if (workingBeatmapCache != null) |
| 129 | + workingBeatmapCache.OnInvalidated -= handleInvalidated; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments