Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 46 additions & 39 deletions PerformanceCalculatorGUI/Screens/SimulateScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -531,14 +534,14 @@ private void load(OsuColour osuColour)
{
HotReloadCallbackReceiver.CompilationFinished += _ => Schedule(() =>
{
calculateDifficulty();
calculatePerformance();
calculateDifficultyAsync().ContinueWith(_ => calculatePerformance());
});
}
}

protected override void Dispose(bool isDisposing)
{
cancellationTokenSource?.Cancel();
modSettingChangeTracker?.Dispose();

appliedMods.UnbindAll();
Expand Down Expand Up @@ -577,20 +580,22 @@ private void modsChanged(ValueChangedEvent<IReadOnlyList<Mod>> mods)
{
createCalculators();
updateMissesTextboxes();
calculateDifficulty();
calculatePerformance();
calculateDifficultyAsync().ContinueWith(_ => calculatePerformance());
}, 300);
};

calculateDifficulty();
updateCombo(false);
calculatePerformance();
calculateDifficultyAsync().ContinueWith(_ =>
{
updateCombo(false);
calculatePerformance();
});
}

private void resetBeatmap()
{
working = null;
beatmapTitle.Clear();
cancellationTokenSource?.Cancel();
resetMods();
beatmapDataContainer.Hide();

Expand Down Expand Up @@ -652,33 +657,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<Skill>();
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<Skill>();
});
}).ContinueWith(t =>
{
Schedule(() => showError(t.Exception));
}, TaskContinuationOptions.OnlyOnFaulted);
}

private void debouncedCalculatePerformance()
Expand All @@ -689,7 +701,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;
Expand Down Expand Up @@ -737,12 +749,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));
}
}

Expand Down Expand Up @@ -897,11 +908,10 @@ private void resetCalculations()
createCalculators();

resetMods();
populateScoreParams();
legacyTotalScore = null;

calculateDifficulty();
calculatePerformance();
populateScoreParams();
calculateDifficultyAsync().ContinueWith(_ => calculatePerformance());
}

// This is to make sure combo resets when classic mod is applied
Expand Down Expand Up @@ -1070,10 +1080,7 @@ private void populateSettingsFromScore(long scoreId)
sliderTailMissesTextBox.Text = sliderTailMisses.ToString();
}

calculateDifficulty();
calculatePerformance();

scoreIdPopulateButton.State.Value = ButtonState.Done;
calculateDifficultyAsync().ContinueWith(_ => calculatePerformance());
});
}).ContinueWith(t =>
{
Expand Down
Loading