Fix MIDI Loading Failing Whilst a MIDI is playing (#23339)

SS14-1148 Fix MIDI Loading Failing Whilst a MIDI is playing

The behaviour of the button event handling did some wonky
async handling that got PJB swearing repeatedly in the contributor
VC.

Improve switching MIDI songs by:

0. Add a bool that tracks if we're currently waiting for the MIDI file
browser to terminate. Use this bool to short-circuit the
MidiFileButtonOnPressed function, ensuring you don't have to close
a morbillion file windows if you spam-clicked the  button or forgot
you'd opened the window.
1. Remove a four-year-old hack involving waiting 100ms to load a MIDI
after trying to stop the last MIDI, because _the rot consumes_ or some shit
This commit is contained in:
Hannah Giovanna Dawson
2024-01-04 02:19:22 +00:00
committed by GitHub
parent 128b5da3b4
commit 1defdebd7b

View File

@@ -18,6 +18,8 @@ namespace Content.Client.Instruments.UI
{
private readonly InstrumentBoundUserInterface _owner;
private bool _isMidiFileDialogueWindowOpen;
public InstrumentMenu(InstrumentBoundUserInterface owner)
{
RobustXamlLoader.Load(this);
@@ -95,11 +97,21 @@ namespace Content.Client.Instruments.UI
private async void MidiFileButtonOnOnPressed(ButtonEventArgs obj)
{
if (_isMidiFileDialogueWindowOpen)
return;
_owner.CloseBandMenu();
var filters = new FileDialogFilters(new FileDialogFilters.Group("mid", "midi"));
// TODO: Once the file dialogue manager can handle focusing or closing windows, improve this logic to close
// or focus the previously-opened window.
_isMidiFileDialogueWindowOpen = true;
await using var file = await _owner.FileDialogManager.OpenFile(filters);
_isMidiFileDialogueWindowOpen = false;
// did the instrument menu get closed while waiting for the user to select a file?
if (Disposed)
return;
@@ -110,20 +122,12 @@ namespace Content.Client.Instruments.UI
if (file == null)
return;
/*if (!_midiManager.IsMidiFile(filename))
{
Logger.Warning($"Not a midi file! Chosen file: {filename}");
return;
}*/
if (!PlayCheck())
return;
MidiStopButtonOnPressed(null);
await using var memStream = new MemoryStream((int) file.Length);
// 100ms delay is due to a race condition or something idk.
// While we're waiting, load it into memory.
await Task.WhenAll(Timer.Delay(100), file.CopyToAsync(memStream));
await file.CopyToAsync(memStream);
if (_owner.Instrument is not {} instrument
|| !_owner.Instruments.OpenMidi(_owner.Owner, memStream.GetBuffer().AsSpan(0, (int) memStream.Length), instrument))