Skip to content

Commit

Permalink
Fix crash on attempting to edit particular beatmaps
Browse files Browse the repository at this point in the history
Closes #29492.

I'm not immediately sure why this happened, but some old locally
modified beatmaps in my local realm database have a `BeatDivisor` of 0
stored, which is then passed to
`BindableBeatDivisor.SetArbitraryDivisor()`, which then blows up.

To stop this from happening, just refuse to use values outside of a sane
range.
  • Loading branch information
bdach committed Aug 20, 2024
1 parent f0ddb6d commit bb964e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 9 additions & 1 deletion osu.Game/Screens/Edit/BindableBeatDivisor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public class BindableBeatDivisor : BindableInt
{
public static readonly int[] PREDEFINED_DIVISORS = { 1, 2, 3, 4, 6, 8, 12, 16 };

public const int MINIMUM_DIVISOR = 1;
public const int MAXIMUM_DIVISOR = 64;

public Bindable<BeatDivisorPresetCollection> ValidDivisors { get; } = new Bindable<BeatDivisorPresetCollection>(BeatDivisorPresetCollection.COMMON);

public BindableBeatDivisor(int value = 1)
Expand All @@ -30,8 +33,12 @@ public BindableBeatDivisor(int value = 1)
/// </summary>
/// <param name="divisor">The intended divisor.</param>
/// <param name="preferKnownPresets">Forces changing the valid divisors to a known preset.</param>
public void SetArbitraryDivisor(int divisor, bool preferKnownPresets = false)
/// <returns>Whether the divisor was successfully set.</returns>
public bool SetArbitraryDivisor(int divisor, bool preferKnownPresets = false)
{
if (divisor < MINIMUM_DIVISOR || divisor > MAXIMUM_DIVISOR)
return false;

// If the current valid divisor range doesn't contain the proposed value, attempt to find one which does.
if (preferKnownPresets || !ValidDivisors.Value.Presets.Contains(divisor))
{
Expand All @@ -44,6 +51,7 @@ public void SetArbitraryDivisor(int divisor, bool preferKnownPresets = false)
}

Value = divisor;
return true;
}

private void updateBindableProperties()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,14 @@ protected override void LoadComplete()

private void setPresetsFromTextBoxEntry()
{
if (!int.TryParse(divisorTextBox.Text, out int divisor) || divisor < 1 || divisor > 64)
if (!int.TryParse(divisorTextBox.Text, out int divisor) || !BeatDivisor.SetArbitraryDivisor(divisor))
{
// the text either didn't parse as a divisor, or the divisor was not set due to being out of range.
// force a state update to reset the text box's value to the last sane value.
updateState();
return;
}

BeatDivisor.SetArbitraryDivisor(divisor);

this.HidePopover();
}

Expand Down

0 comments on commit bb964e3

Please sign in to comment.