Skip to content

Commit

Permalink
Merge pull request #31644 from peppy/fix-beatmap-offset-crash
Browse files Browse the repository at this point in the history
Fix potential crash when adjusting offset
  • Loading branch information
bdach authored Jan 24, 2025
2 parents 4ae6dfd + 721b2df commit 2f84aec
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions osu.Game/Screens/Play/PlayerSettings/BeatmapOffsetControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ protected override void LoadComplete()
// At the point we reach here, it's not guaranteed that all realm writes have taken place (there may be some in-flight).
// We are only aware of writes that originated from our own flow, so if we do see one that's active we can avoid handling the feedback value arriving.
if (realmWriteTask == null)
{
Current.Disabled = false;
Current.Value = val;
Current.Disabled = allowOffsetAdjust;
}

if (realmWriteTask?.IsCompleted == true)
{
Expand All @@ -134,15 +138,15 @@ protected override void LoadComplete()
ReferenceScore.BindValueChanged(scoreChanged, true);
}

// the last play graph is relative to the offset at the point of the last play, so we need to factor that out for some usages.
private double adjustmentSinceLastPlay => lastPlayBeatmapOffset - Current.Value;

private void currentChanged(ValueChangedEvent<double> offset)
{
Scheduler.AddOnce(updateOffset);

void updateOffset()
{
// the last play graph is relative to the offset at the point of the last play, so we need to factor that out.
double adjustmentSinceLastPlay = lastPlayBeatmapOffset - Current.Value;

// Negative is applied here because the play graph is considering a hit offset, not track (as we currently use for clocks).
lastPlayGraph?.UpdateOffset(-adjustmentSinceLastPlay);

Expand All @@ -153,11 +157,6 @@ void updateOffset()
return;
}

if (useAverageButton != null)
{
useAverageButton.Enabled.Value = !Precision.AlmostEquals(lastPlayAverage, adjustmentSinceLastPlay, Current.Precision / 2);
}

realmWriteTask = realm.WriteAsync(r =>
{
var setInfo = r.Find<BeatmapSetInfo>(beatmap.Value.BeatmapSetInfo.ID);
Expand Down Expand Up @@ -245,10 +244,12 @@ private void scoreChanged(ValueChangedEvent<ScoreInfo?> score)
Text = BeatmapOffsetControlStrings.CalibrateUsingLastPlay,
Action = () =>
{
if (Current.Disabled)
return;

Current.Value = lastPlayBeatmapOffset - lastPlayAverage;
lastAppliedScore.Value = ReferenceScore.Value;
},
Enabled = { Value = !Precision.AlmostEquals(lastPlayAverage, 0, Current.Precision / 2) }
},
globalOffsetText = new LinkFlowContainer
{
Expand Down Expand Up @@ -277,7 +278,13 @@ protected override void Dispose(bool isDisposing)
protected override void Update()
{
base.Update();
Current.Disabled = !allowOffsetAdjust;

bool allow = allowOffsetAdjust;

if (useAverageButton != null)
useAverageButton.Enabled.Value = allow && !Precision.AlmostEquals(lastPlayAverage, adjustmentSinceLastPlay, Current.Precision / 2);

Current.Disabled = !allow;
}

private bool allowOffsetAdjust
Expand Down

0 comments on commit 2f84aec

Please sign in to comment.