Fix crash on attempting to edit particular beatmaps

Closes https://github.com/ppy/osu/issues/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.
This commit is contained in:
Bartłomiej Dach 2024-08-20 13:36:52 +02:00
parent f0ddb6daa4
commit bb964e32fa
No known key found for this signature in database
2 changed files with 12 additions and 4 deletions

View File

@ -16,6 +16,9 @@ namespace osu.Game.Screens.Edit
{ {
public static readonly int[] PREDEFINED_DIVISORS = { 1, 2, 3, 4, 6, 8, 12, 16 }; 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 Bindable<BeatDivisorPresetCollection> ValidDivisors { get; } = new Bindable<BeatDivisorPresetCollection>(BeatDivisorPresetCollection.COMMON);
public BindableBeatDivisor(int value = 1) public BindableBeatDivisor(int value = 1)
@ -30,8 +33,12 @@ namespace osu.Game.Screens.Edit
/// </summary> /// </summary>
/// <param name="divisor">The intended divisor.</param> /// <param name="divisor">The intended divisor.</param>
/// <param name="preferKnownPresets">Forces changing the valid divisors to a known preset.</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 the current valid divisor range doesn't contain the proposed value, attempt to find one which does.
if (preferKnownPresets || !ValidDivisors.Value.Presets.Contains(divisor)) if (preferKnownPresets || !ValidDivisors.Value.Presets.Contains(divisor))
{ {
@ -44,6 +51,7 @@ namespace osu.Game.Screens.Edit
} }
Value = divisor; Value = divisor;
return true;
} }
private void updateBindableProperties() private void updateBindableProperties()

View File

@ -330,14 +330,14 @@ namespace osu.Game.Screens.Edit.Compose.Components
private void setPresetsFromTextBoxEntry() 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(); updateState();
return; return;
} }
BeatDivisor.SetArbitraryDivisor(divisor);
this.HidePopover(); this.HidePopover();
} }