From 0dbb75051e05703471fae5baa825905bc48d735e Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Wed, 25 Nov 2020 18:00:18 +0100 Subject: [PATCH] Content changes for file dialog stuff. --- .../Instruments/InstrumentComponent.cs | 5 ++-- Content.Client/Instruments/InstrumentMenu.cs | 30 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs b/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs index 82c46e5a94..dddf6441ff 100644 --- a/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs +++ b/Content.Client/GameObjects/Components/Instruments/InstrumentComponent.cs @@ -349,12 +349,11 @@ namespace Content.Client.GameObjects.Components.Instruments return true; } - /// - public bool OpenMidi(string filename) + public bool OpenMidi(ReadOnlySpan data) { SetupRenderer(); - if (_renderer == null || !_renderer.OpenMidi(filename)) + if (_renderer == null || !_renderer.OpenMidi(data)) { return false; } diff --git a/Content.Client/Instruments/InstrumentMenu.cs b/Content.Client/Instruments/InstrumentMenu.cs index 9c9769f7c6..9bcb8d1411 100644 --- a/Content.Client/Instruments/InstrumentMenu.cs +++ b/Content.Client/Instruments/InstrumentMenu.cs @@ -1,3 +1,6 @@ +using System; +using System.IO; +using System.Threading.Tasks; using Content.Client.GameObjects.Components.Instruments; using Content.Client.UserInterface.Stylesheets; using Content.Client.Utility; @@ -174,25 +177,31 @@ namespace Content.Client.Instruments private async void MidiFileButtonOnOnPressed(BaseButton.ButtonEventArgs obj) { var filters = new FileDialogFilters(new FileDialogFilters.Group("mid", "midi")); - var filename = await _fileDialogManager.OpenFile(filters); + var file = await _fileDialogManager.OpenFile(filters); // The following checks are only in place to prevent players from playing MIDI songs locally. // There are equivalents for these checks on the server. - if (string.IsNullOrEmpty(filename)) return; + if (file == null) return; - if (!_midiManager.IsMidiFile(filename)) + /*if (!_midiManager.IsMidiFile(filename)) { Logger.Warning($"Not a midi file! Chosen file: {filename}"); return; - } + }*/ if (!PlayCheck()) return; MidiStopButtonOnPressed(null); - await Timer.Delay(100); - if (!_owner.Instrument.OpenMidi(filename)) return; + 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)); + + if (!_owner.Instrument.OpenMidi(memStream.GetBuffer().AsSpan(0, (int) memStream.Length))) + return; + MidiPlaybackSetButtonsDisabled(false); if (_midiInputButton.Pressed) _midiInputButton.Pressed = false; @@ -222,14 +231,15 @@ namespace Content.Client.Instruments var localPlayer = IoCManager.Resolve().LocalPlayer; // If we don't have a player or controlled entity, we return. - if(localPlayer?.ControlledEntity == null) return false; + if (localPlayer?.ControlledEntity == null) return false; // If the instrument is handheld and we're not holding it, we return. - if((instrument.Handheld && (conMan == null - || conMan.Owner != localPlayer.ControlledEntity))) return false; + if ((instrument.Handheld && (conMan == null + || conMan.Owner != localPlayer.ControlledEntity))) return false; // We check that we're in range unobstructed just in case. - return localPlayer.InRangeUnobstructed(instrumentEnt, predicate:(e) => e == instrumentEnt || e == localPlayer.ControlledEntity); + return localPlayer.InRangeUnobstructed(instrumentEnt, + predicate: (e) => e == instrumentEnt || e == localPlayer.ControlledEntity); } private void MidiStopButtonOnPressed(BaseButton.ButtonEventArgs obj)