From: Hannah Giovanna Dawson Date: Thu, 4 Jan 2024 02:19:22 +0000 (+0000) Subject: Fix MIDI Loading Failing Whilst a MIDI is playing (#23339) X-Git-Url: https://git.smokeofanarchy.ru/gitweb.cgi?a=commitdiff_plain;h=1defdebd7bb29cafa1f4cb4ef496ef6928634168;p=space-station-14.git 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 --- diff --git a/Content.Client/Instruments/UI/InstrumentMenu.xaml.cs b/Content.Client/Instruments/UI/InstrumentMenu.xaml.cs index 201b7b6304..85c9cc1447 100644 --- a/Content.Client/Instruments/UI/InstrumentMenu.xaml.cs +++ b/Content.Client/Instruments/UI/InstrumentMenu.xaml.cs @@ -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))