From bb964e32fa5a1e5f2aeb1b3f14308f9c85be02ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 20 Aug 2024 13:36:52 +0200 Subject: [PATCH] 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. --- osu.Game/Screens/Edit/BindableBeatDivisor.cs | 10 +++++++++- .../Edit/Compose/Components/BeatDivisorControl.cs | 6 +++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/osu.Game/Screens/Edit/BindableBeatDivisor.cs b/osu.Game/Screens/Edit/BindableBeatDivisor.cs index 4b0726658fe4..3bb1b4e0797d 100644 --- a/osu.Game/Screens/Edit/BindableBeatDivisor.cs +++ b/osu.Game/Screens/Edit/BindableBeatDivisor.cs @@ -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 ValidDivisors { get; } = new Bindable(BeatDivisorPresetCollection.COMMON); public BindableBeatDivisor(int value = 1) @@ -30,8 +33,12 @@ public BindableBeatDivisor(int value = 1) /// /// The intended divisor. /// Forces changing the valid divisors to a known preset. - public void SetArbitraryDivisor(int divisor, bool preferKnownPresets = false) + /// Whether the divisor was successfully set. + 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)) { @@ -44,6 +51,7 @@ public void SetArbitraryDivisor(int divisor, bool preferKnownPresets = false) } Value = divisor; + return true; } private void updateBindableProperties() diff --git a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs index 1d8266d610e8..3c2a66b8bbc9 100644 --- a/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs +++ b/osu.Game/Screens/Edit/Compose/Components/BeatDivisorControl.cs @@ -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(); }