From 0546065573e7833c15135d99d9dc17959d03f1b6 Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 23 Jun 2025 02:05:58 +0300 Subject: [PATCH 1/6] Update SimulateScreen.cs --- .../Screens/SimulateScreen.cs | 75 ++++++++++--------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index 735049deb5..08aa514325 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using osu.Framework; using osu.Framework.Allocation; @@ -119,6 +120,8 @@ public partial class SimulateScreen : PerformanceCalculatorScreen [Cached] private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Blue); + private CancellationTokenSource cancellationTokenSource; + public override bool ShouldShowConfirmationDialogOnSwitch => working != null; private const int file_selection_container_height = 40; @@ -531,14 +534,14 @@ private void load(OsuColour osuColour) { HotReloadCallbackReceiver.CompilationFinished += _ => Schedule(() => { - calculateDifficulty(); - calculatePerformance(); + calculateDifficultyAsync().ContinueWith((t) => calculatePerformance()); }); } } protected override void Dispose(bool isDisposing) { + cancellationTokenSource?.Cancel(); modSettingChangeTracker?.Dispose(); appliedMods.UnbindAll(); @@ -577,20 +580,18 @@ private void modsChanged(ValueChangedEvent> mods) { createCalculators(); updateMissesTextboxes(); - calculateDifficulty(); - calculatePerformance(); + calculateDifficultyAsync().ContinueWith((t) => calculatePerformance()); }, 300); }; - calculateDifficulty(); - updateCombo(false); - calculatePerformance(); + calculateDifficultyAsync().ContinueWith((t) => { updateCombo(false); calculatePerformance(); }); } private void resetBeatmap() { working = null; beatmapTitle.Clear(); + cancellationTokenSource?.Cancel(); resetMods(); beatmapDataContainer.Hide(); @@ -652,33 +653,40 @@ private void createCalculators() performanceCalculator = rulesetInstance.CreatePerformanceCalculator(); } - private void calculateDifficulty() + private Task calculateDifficultyAsync() { if (working == null || difficultyCalculator.Value == null) - return; + return Task.CompletedTask; - try - { - difficultyAttributes = difficultyCalculator.Value.Calculate(appliedMods.Value); - difficultyAttributesContainer.Attributes.Value = AttributeConversion.ToDictionary(difficultyAttributes); - } - catch (Exception e) - { - showError(e); - resetBeatmap(); - return; - } + cancellationTokenSource?.Cancel(); + cancellationTokenSource = new CancellationTokenSource(); - if (difficultyCalculator.Value is IExtendedDifficultyCalculator extendedDifficultyCalculator) + return Task.Run(() => { - // StrainSkill always skips the first object - if (working.Beatmap?.HitObjects.Count > 1) - strainVisualizer.TimeUntilFirstStrain.Value = (int)working.Beatmap.HitObjects[1].StartTime; + difficultyAttributes = difficultyCalculator.Value.Calculate(appliedMods.Value, cancellationTokenSource.Token); - strainVisualizer.Skills.Value = extendedDifficultyCalculator.GetSkills(); - } - else - strainVisualizer.Skills.Value = Array.Empty(); + if (cancellationTokenSource.IsCancellationRequested) + return; + + Schedule(() => + { + difficultyAttributesContainer.Attributes.Value = AttributeConversion.ToDictionary(difficultyAttributes); + + if (difficultyCalculator.Value is IExtendedDifficultyCalculator extendedDifficultyCalculator) + { + // StrainSkill always skips the first object + if (working.Beatmap?.HitObjects.Count > 1) + strainVisualizer.TimeUntilFirstStrain.Value = (int)working.Beatmap.HitObjects[1].StartTime; + + strainVisualizer.Skills.Value = extendedDifficultyCalculator.GetSkills(); + } + else + strainVisualizer.Skills.Value = Array.Empty(); + }); + }).ContinueWith(t => + { + Schedule(() => showError(t.Exception)); + }, TaskContinuationOptions.OnlyOnFaulted); } private void debouncedCalculatePerformance() @@ -689,7 +697,7 @@ private void debouncedCalculatePerformance() private void calculatePerformance() { - if (working == null || difficultyAttributes == null) + if (working == null || difficultyAttributes == null || cancellationTokenSource.IsCancellationRequested) return; int? countGood = null, countMeh = null; @@ -737,12 +745,11 @@ private void calculatePerformance() LegacyTotalScore = legacyTotalScore, }, difficultyAttributes); - performanceAttributesContainer.Attributes.Value = AttributeConversion.ToDictionary(ppAttributes); + Schedule(() => performanceAttributesContainer.Attributes.Value = AttributeConversion.ToDictionary(ppAttributes)); } catch (Exception e) { - showError(e); - resetBeatmap(); + Schedule(() => showError(e)); } } @@ -899,7 +906,7 @@ private void resetCalculations() resetMods(); legacyTotalScore = null; - calculateDifficulty(); + calculateDifficultyAsync(); calculatePerformance(); populateScoreParams(); } @@ -1072,7 +1079,7 @@ private void populateSettingsFromScore(long scoreId) sliderTailMissesTextBox.Text = sliderTailMisses.ToString(); } - calculateDifficulty(); + calculateDifficultyAsync(); calculatePerformance(); scoreIdPopulateButton.State.Value = ButtonState.Done; From 7c70316c57590ec548a000e3f6549bfdd006fb0c Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 23 Jun 2025 02:19:23 +0300 Subject: [PATCH 2/6] use async everywhere --- .../Screens/SimulateScreen.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index 08aa514325..5e35cf26f4 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -584,7 +584,11 @@ private void modsChanged(ValueChangedEvent> mods) }, 300); }; - calculateDifficultyAsync().ContinueWith((t) => { updateCombo(false); calculatePerformance(); }); + calculateDifficultyAsync().ContinueWith((t) => + { + updateCombo(false); + calculatePerformance(); + }); } private void resetBeatmap() @@ -906,9 +910,11 @@ private void resetCalculations() resetMods(); legacyTotalScore = null; - calculateDifficultyAsync(); - calculatePerformance(); - populateScoreParams(); + calculateDifficultyAsync().ContinueWith((t) => + { + calculatePerformance(); + Schedule(() => populateScoreParams()); + }); } // This is to make sure combo resets when classic mod is applied @@ -1079,10 +1085,12 @@ private void populateSettingsFromScore(long scoreId) sliderTailMissesTextBox.Text = sliderTailMisses.ToString(); } - calculateDifficultyAsync(); - calculatePerformance(); + calculateDifficultyAsync().ContinueWith((t) => + { + calculatePerformance(); + scoreIdPopulateButton.State.Value = ButtonState.Done; - scoreIdPopulateButton.State.Value = ButtonState.Done; + }); }); } catch (Exception e) From 7d551cb3c70af757f352f36652512955a6b51960 Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 23 Jun 2025 02:20:47 +0300 Subject: [PATCH 3/6] Update SimulateScreen.cs --- PerformanceCalculatorGUI/Screens/SimulateScreen.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index 5e35cf26f4..705f0c611c 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -1088,8 +1088,7 @@ private void populateSettingsFromScore(long scoreId) calculateDifficultyAsync().ContinueWith((t) => { calculatePerformance(); - scoreIdPopulateButton.State.Value = ButtonState.Done; - + Schedule(() => scoreIdPopulateButton.State.Value = ButtonState.Done); }); }); } From ecfbf75f334e14485a788d7c2eb7f585c84ccb28 Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 23 Jun 2025 02:22:00 +0300 Subject: [PATCH 4/6] remove lambda parameters --- PerformanceCalculatorGUI/Screens/SimulateScreen.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index 705f0c611c..5fd35f35b4 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -534,7 +534,7 @@ private void load(OsuColour osuColour) { HotReloadCallbackReceiver.CompilationFinished += _ => Schedule(() => { - calculateDifficultyAsync().ContinueWith((t) => calculatePerformance()); + calculateDifficultyAsync().ContinueWith(_ => calculatePerformance()); }); } } @@ -580,11 +580,11 @@ private void modsChanged(ValueChangedEvent> mods) { createCalculators(); updateMissesTextboxes(); - calculateDifficultyAsync().ContinueWith((t) => calculatePerformance()); + calculateDifficultyAsync().ContinueWith(_ => calculatePerformance()); }, 300); }; - calculateDifficultyAsync().ContinueWith((t) => + calculateDifficultyAsync().ContinueWith(_ => { updateCombo(false); calculatePerformance(); @@ -910,7 +910,7 @@ private void resetCalculations() resetMods(); legacyTotalScore = null; - calculateDifficultyAsync().ContinueWith((t) => + calculateDifficultyAsync().ContinueWith(_ => { calculatePerformance(); Schedule(() => populateScoreParams()); @@ -1085,7 +1085,7 @@ private void populateSettingsFromScore(long scoreId) sliderTailMissesTextBox.Text = sliderTailMisses.ToString(); } - calculateDifficultyAsync().ContinueWith((t) => + calculateDifficultyAsync().ContinueWith(_ => { calculatePerformance(); Schedule(() => scoreIdPopulateButton.State.Value = ButtonState.Done); From 5b6800a9084813d7bf2239b5c88b7453ee8c522e Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 23 Jun 2025 02:24:31 +0300 Subject: [PATCH 5/6] fix cli --- PerformanceCalculatorGUI/Screens/SimulateScreen.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index 5fd35f35b4..a22cea3c77 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -913,7 +913,7 @@ private void resetCalculations() calculateDifficultyAsync().ContinueWith(_ => { calculatePerformance(); - Schedule(() => populateScoreParams()); + Schedule(populateScoreParams); }); } From 9acb017b4d8fc9c516bb602a35ea3b3a60366f79 Mon Sep 17 00:00:00 2001 From: Givikap120 Date: Mon, 30 Jun 2025 03:31:45 +0300 Subject: [PATCH 6/6] Update SimulateScreen.cs --- PerformanceCalculatorGUI/Screens/SimulateScreen.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs index a22cea3c77..568a10067c 100644 --- a/PerformanceCalculatorGUI/Screens/SimulateScreen.cs +++ b/PerformanceCalculatorGUI/Screens/SimulateScreen.cs @@ -908,13 +908,10 @@ private void resetCalculations() createCalculators(); resetMods(); + populateScoreParams(); legacyTotalScore = null; - calculateDifficultyAsync().ContinueWith(_ => - { - calculatePerformance(); - Schedule(populateScoreParams); - }); + calculateDifficultyAsync().ContinueWith(_ => calculatePerformance()); } // This is to make sure combo resets when classic mod is applied