]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Fix MIDI Loading Failing Whilst a MIDI is playing (#23339)
authorHannah Giovanna Dawson <karakkaraz@gmail.com>
Thu, 4 Jan 2024 02:19:22 +0000 (02:19 +0000)
committerGitHub <noreply@github.com>
Thu, 4 Jan 2024 02:19:22 +0000 (13:19 +1100)
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

Content.Client/Instruments/UI/InstrumentMenu.xaml.cs

index 201b7b630418e77b6753b351fb6ddcdadb3116cf..85c9cc1447e7a67ba90f1f8481fc38dca98c8da9 100644 (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))