Skip to content

Commit

Permalink
Merge pull request ppy#30164 from bdach/am-i-fired-this-time
Browse files Browse the repository at this point in the history
Fix game deadlocking on startup when set to single thread mode
  • Loading branch information
peppy authored Oct 9, 2024
2 parents 270c4c4 + baa64d1 commit a6f5603
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
1 change: 1 addition & 0 deletions osu.Game/OsuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,7 @@ protected override void LoadComplete()
loadComponentSingleFile(new MedalOverlay(), topMostOverlayContent.Add);

loadComponentSingleFile(new BackgroundDataStoreProcessor(), Add);
loadComponentSingleFile(new DetachedBeatmapStore(), Add, true);

Add(difficultyRecommender);
Add(externalLinkOpener = new ExternalLinkOpener());
Expand Down
4 changes: 0 additions & 4 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,6 @@ private void load(ReadableKeyCombinationProvider keyCombinationProvider, Framewo
dependencies.Cache(previewTrackManager = new PreviewTrackManager(BeatmapManager.BeatmapTrackStore));
base.Content.Add(previewTrackManager);

var detachedBeatmapStore = new DetachedBeatmapStore();
base.Content.Add(detachedBeatmapStore);
dependencies.CacheAs(detachedBeatmapStore);

base.Content.Add(MusicController = new MusicController());
dependencies.CacheAs(MusicController);

Expand Down
42 changes: 21 additions & 21 deletions osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
Expand Down Expand Up @@ -63,7 +62,8 @@ public partial class MusicController : CompositeDrawable

public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000));

private IBindableList<BeatmapSetInfo> detachedBeatmaps = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;

private BindableNumber<double> sampleVolume = null!;

Expand All @@ -72,19 +72,17 @@ public partial class MusicController : CompositeDrawable
private AudioFilter audioDuckFilter = null!;

private readonly Bindable<RandomSelectAlgorithm> randomSelectAlgorithm = new Bindable<RandomSelectAlgorithm>();
private readonly List<BeatmapSetInfo> previousRandomSets = new List<BeatmapSetInfo>();
private readonly List<Live<BeatmapSetInfo>> previousRandomSets = new List<Live<BeatmapSetInfo>>();
private int randomHistoryDirection;

[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuConfigManager configManager, DetachedBeatmapStore detachedBeatmapStore, CancellationToken? cancellationToken)
private void load(AudioManager audio, OsuConfigManager configManager)
{
AddInternal(audioDuckFilter = new AudioFilter(audio.TrackMixer));
audio.Tracks.AddAdjustment(AdjustableProperty.Volume, audioDuckVolume);
sampleVolume = audio.VolumeSample.GetBoundCopy();

configManager.BindWith(OsuSetting.RandomSelectAlgorithm, randomSelectAlgorithm);

detachedBeatmaps = detachedBeatmapStore.GetDetachedBeatmaps(cancellationToken);
}

protected override void LoadComplete()
Expand Down Expand Up @@ -251,19 +249,19 @@ private PreviousTrackResult prev(bool allowProtectedTracks)

queuedDirection = TrackChangeDirection.Prev;

BeatmapSetInfo? playableSet;
Live<BeatmapSetInfo>? playableSet;

if (Shuffle.Value)
playableSet = getNextRandom(-1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Protected || allowProtectedTracks)
?? getBeatmapSets().LastOrDefault(s => !s.Protected || allowProtectedTracks);
playableSet = getBeatmapSets().TakeWhile(i => !i.Value.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Value.Protected || allowProtectedTracks)
?? getBeatmapSets().LastOrDefault(s => !s.Value.Protected || allowProtectedTracks);
}

if (playableSet != null)
{
changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Beatmaps.First()));
changeBeatmap(beatmaps.GetWorkingBeatmap(playableSet.Value.Beatmaps.First()));
restartTrack();
return PreviousTrackResult.Previous;
}
Expand Down Expand Up @@ -347,19 +345,19 @@ private bool next(bool allowProtectedTracks)

queuedDirection = TrackChangeDirection.Next;

BeatmapSetInfo? playableSet;
Live<BeatmapSetInfo>? playableSet;

if (Shuffle.Value)
playableSet = getNextRandom(1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo))
.Where(i => !i.Protected || allowProtectedTracks)
playableSet = getBeatmapSets().SkipWhile(i => !i.Value.Equals(current?.BeatmapSetInfo))
.Where(i => !i.Value.Protected || allowProtectedTracks)
.ElementAtOrDefault(1)
?? getBeatmapSets().FirstOrDefault(i => !i.Protected || allowProtectedTracks);
?? getBeatmapSets().FirstOrDefault(i => !i.Value.Protected || allowProtectedTracks);
}

var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
var playableBeatmap = playableSet?.Value.Beatmaps.FirstOrDefault();

if (playableBeatmap != null)
{
Expand All @@ -371,11 +369,11 @@ private bool next(bool allowProtectedTracks)
return false;
}

private BeatmapSetInfo? getNextRandom(int direction, bool allowProtectedTracks)
private Live<BeatmapSetInfo>? getNextRandom(int direction, bool allowProtectedTracks)
{
BeatmapSetInfo result;
Live<BeatmapSetInfo> result;

var possibleSets = getBeatmapSets().Where(s => !s.Protected || allowProtectedTracks).ToArray();
var possibleSets = getBeatmapSets().Where(s => !s.Value.Protected || allowProtectedTracks).ToArray();

if (possibleSets.Length == 0)
return null;
Expand Down Expand Up @@ -434,7 +432,9 @@ private void restartTrack()

private TrackChangeDirection? queuedDirection;

private IEnumerable<BeatmapSetInfo> getBeatmapSets() => detachedBeatmaps.Where(s => !s.DeletePending);
private IEnumerable<Live<BeatmapSetInfo>> getBeatmapSets() => realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending)
.AsEnumerable()
.Select(s => new RealmLive<BeatmapSetInfo>(s, realm));

private void changeBeatmap(WorkingBeatmap newWorking)
{
Expand All @@ -461,8 +461,8 @@ private void changeBeatmap(WorkingBeatmap newWorking)
else
{
// figure out the best direction based on order in playlist.
int last = getBeatmapSets().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count();
int next = getBeatmapSets().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();
int last = getBeatmapSets().TakeWhile(b => !b.Value.Equals(current.BeatmapSetInfo)).Count();
int next = getBeatmapSets().TakeWhile(b => !b.Value.Equals(newWorking.BeatmapSetInfo)).Count();

direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
}
Expand Down

0 comments on commit a6f5603

Please sign in to comment.