Skip to content

Commit

Permalink
Fix any and all migration attempts dying on MusicController
Browse files Browse the repository at this point in the history
I'm not sure why this was "fine" for as long as it apparently was,
but what `MusicController` was doing was completely incorrect and
playing with fire (accessing raw managed realm objects), which went
wrong somewhere around - admittedly -
#29917, likely because that one started
*storing* these raw managed realm objects to fields, and you know what
will happen to those after you do a migration and recycle realms.

To attempt to circumvent this, (ab)use `DetachedBeatmapStore` instead.
Which does necessitate moving it to `OsuGameBase`, but it's the simplest
way out I can see. I guess the alternative would be to faff around with
`Live<T>` but it's ugly and I'm attempting to fix this relatively quick
right now.
  • Loading branch information
bdach committed Oct 8, 2024
1 parent bfad281 commit 310eec6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
1 change: 0 additions & 1 deletion osu.Game/OsuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,6 @@ 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: 4 additions & 0 deletions osu.Game/OsuGameBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ 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
23 changes: 14 additions & 9 deletions osu.Game/Overlays/MusicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -65,6 +66,8 @@ public partial class MusicController : CompositeDrawable
[Resolved]
private RealmAccess realm { get; set; } = null!;

private IBindableList<BeatmapSetInfo> detachedBeatmaps = null!;

private BindableNumber<double> sampleVolume = null!;

private readonly BindableDouble audioDuckVolume = new BindableDouble(1);
Expand All @@ -76,13 +79,15 @@ public partial class MusicController : CompositeDrawable
private int randomHistoryDirection;

[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuConfigManager configManager)
private void load(AudioManager audio, OsuConfigManager configManager, DetachedBeatmapStore detachedBeatmapStore, CancellationToken? cancellationToken)
{
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 @@ -255,8 +260,8 @@ private PreviousTrackResult prev(bool allowProtectedTracks)
playableSet = getNextRandom(-1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Protected || allowProtectedTracks)
?? getBeatmapSets().AsEnumerable().LastOrDefault(s => !s.Protected || allowProtectedTracks);
playableSet = getBeatmapSets().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault(s => !s.Protected || allowProtectedTracks)
?? getBeatmapSets().LastOrDefault(s => !s.Protected || allowProtectedTracks);
}

if (playableSet != null)
Expand Down Expand Up @@ -351,10 +356,10 @@ private bool next(bool allowProtectedTracks)
playableSet = getNextRandom(1, allowProtectedTracks);
else
{
playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo))
playableSet = getBeatmapSets().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo))
.Where(i => !i.Protected || allowProtectedTracks)
.ElementAtOrDefault(1)
?? getBeatmapSets().AsEnumerable().FirstOrDefault(i => !i.Protected || allowProtectedTracks);
?? getBeatmapSets().FirstOrDefault(i => !i.Protected || allowProtectedTracks);
}

var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
Expand All @@ -373,7 +378,7 @@ private bool next(bool allowProtectedTracks)
{
BeatmapSetInfo result;

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

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

private TrackChangeDirection? queuedDirection;

private IQueryable<BeatmapSetInfo> getBeatmapSets() => realm.Realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending);
private IEnumerable<BeatmapSetInfo> getBeatmapSets() => detachedBeatmaps.Where(s => !s.DeletePending);

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

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

0 comments on commit 310eec6

Please sign in to comment.