From e45693fe37d6c78a9de1aee4602917068944836a Mon Sep 17 00:00:00 2001 From: Manuel Mayer Date: Sun, 16 Oct 2022 17:08:23 +0200 Subject: [PATCH] Fixed crash --- Macro-Deck-Stream-Deck-Connector.csproj | 3 +-- MacroDeckClient.cs | 14 ++++++++----- Models/ActionButtonModel.cs | 26 ++++++++++++++----------- Models/ConnectedDevice.cs | 8 ++------ USBHelper.cs | 2 ++ Utils/EmptyButtonImageGenerator.cs | 26 +++++++++++-------------- 6 files changed, 40 insertions(+), 39 deletions(-) diff --git a/Macro-Deck-Stream-Deck-Connector.csproj b/Macro-Deck-Stream-Deck-Connector.csproj index a268754..52394dd 100644 --- a/Macro-Deck-Stream-Deck-Connector.csproj +++ b/Macro-Deck-Stream-Deck-Connector.csproj @@ -12,7 +12,6 @@ true 8 enable - 1.2.0 @@ -32,7 +31,7 @@ embedded - 1.2.0 + 1.2.1 diff --git a/MacroDeckClient.cs b/MacroDeckClient.cs index 32b5b04..dbeb41e 100644 --- a/MacroDeckClient.cs +++ b/MacroDeckClient.cs @@ -51,11 +51,16 @@ internal void Close() button?.Dispose(); } _websocketClient?.Dispose(); - if (_frameUpdateTimer != null) + try { _frameUpdateTimer.Stop(); _frameUpdateTimer.Dispose(); } + catch + { + // ignored + } + _connectedDevice?.Close(); } @@ -126,23 +131,22 @@ private async Task HandleMessageAsync(string message) private bool _buttonsUpdating; private void UpdateAllButtons() { - if (_connectedDevice == null || _buttonsUpdating) return; + if (_buttonsUpdating) return; _buttonsUpdating = true; for (var row = 0; row < _connectedDevice?.Rows; row++) { for (var col = 0; col < _connectedDevice?.Columns; col++) { var keyId = row * _connectedDevice.Columns + col; - var actionButton = _buttons.ToArray().FirstOrDefault(x => x.Column == col && x.Row == row); + var actionButton = _buttons.ToArray().FirstOrDefault(x => x != null && x.Column == col && x.Row == row); UpdateButton(keyId, actionButton); } } _buttonsUpdating = false; } - private void UpdateButton(int id, ActionButtonModel actionButton) + private void UpdateButton(int id, ActionButtonModel? actionButton) { - if (_connectedDevice == null) return; if (actionButton != null) { actionButton.FrameTick(); diff --git a/Models/ActionButtonModel.cs b/Models/ActionButtonModel.cs index 32ef886..9dadd63 100644 --- a/Models/ActionButtonModel.cs +++ b/Models/ActionButtonModel.cs @@ -70,9 +70,9 @@ public void Dispose() private string _labelBase64; - private Image _iconImage; + private Image? _iconImage; - private Bitmap _labelBitmap; + private Bitmap? _labelBitmap; private int _frameIndex = 0; @@ -96,8 +96,11 @@ public string IconBase64 if (_iconImage.RawFormat.Guid == ImageFormat.Gif.Guid) { var item = _iconImage.GetPropertyItem(0x5100); - _frameDelay = (item.Value[0] + item.Value[1] * 256) * 10; - _frameCount = _iconImage.GetFrameCount(FrameDimension.Time); + if (item?.Value != null) + { + _frameDelay = (item.Value[0] + item.Value[1] * 256) * 10; + _frameCount = _iconImage.GetFrameCount(FrameDimension.Time); + } } _frameIndex = 0; UpdateCurrentFrame(); @@ -165,6 +168,7 @@ private void UpdateCurrentFrame() public KeyBitmap? GetCurrentFrame(int size) { if (IsDisposed) return null; + const int iconPosition = 0; try { var combined = new Bitmap(size, size, PixelFormat.Format24bppRgb); @@ -175,9 +179,7 @@ private void UpdateCurrentFrame() g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; - - const int iconPosition = 0; - + using (var brush = new SolidBrush(BackgroundColor)) { g.FillRectangle(brush, iconPosition, iconPosition, size, size); @@ -201,14 +203,16 @@ private void UpdateCurrentFrame() } - using var bufferStream = new MemoryStream(); - combined.Save(bufferStream, ImageFormat.Png); + //using var bufferStream = new MemoryStream(); + //combined.Save(bufferStream, ImageFormat.Png); + + return KeyBitmap.Create.FromBitmap(combined); - return KeyBitmap.Create.FromStream(bufferStream); + //return KeyBitmap.Create.FromStream(bufferStream); } catch (Exception ex) { - Console.WriteLine(ex.Message); + Console.WriteLine($"Error while rendering current frame: {ex.Message}"); return null; } } diff --git a/Models/ConnectedDevice.cs b/Models/ConnectedDevice.cs index 45ca1de..a72b8c9 100644 --- a/Models/ConnectedDevice.cs +++ b/Models/ConnectedDevice.cs @@ -6,7 +6,7 @@ using StreamDeckSharp; using Timer = System.Timers.Timer; -namespace MacroDeck.StreamDeckConnector +namespace MacroDeck.StreamDeckConnector.Models { public class ConnectedDevice { @@ -82,11 +82,6 @@ public ConnectedDevice(string path) private void StreamDeck_KeyStateChanged(object sender, KeyEventArgs e) { - if (PressedButtonId == -1) - { - OnButtonPress?.Invoke(sender, new ButtonPressEventArgs(PressedButtonId, ButtonEventKind.UP)); - } - if (e.Key > -1) { PressedButtonId = e.Key; @@ -100,6 +95,7 @@ private void StreamDeck_KeyStateChanged(object sender, KeyEventArgs e) } else { + OnButtonPress?.Invoke(sender, new ButtonPressEventArgs(PressedButtonId, ButtonEventKind.UP)); if (longPress) { buttonKind = ButtonEventKind.LONG_UP; diff --git a/USBHelper.cs b/USBHelper.cs index c66f1ee..ddc827a 100644 --- a/USBHelper.cs +++ b/USBHelper.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using MacroDeck.StreamDeckConnector.Models; using MacroDeck.StreamDeckConnector.Parsers; using StreamDeckSharp; using Usb.Events; @@ -51,6 +52,7 @@ public static void Initialize() var devices = StreamDeck.EnumerateDevices(); foreach (var device in devices) { + Console.WriteLine($"Found {device.DeviceName} @ {device.DevicePath}"); try { var connectedDevice = new ConnectedDevice(device.DevicePath); diff --git a/Utils/EmptyButtonImageGenerator.cs b/Utils/EmptyButtonImageGenerator.cs index 4301eb1..5d2a37c 100644 --- a/Utils/EmptyButtonImageGenerator.cs +++ b/Utils/EmptyButtonImageGenerator.cs @@ -11,30 +11,25 @@ namespace MacroDeck.StreamDeckConnector.Utils { public class EmptyButtonImageGenerator { - public static KeyBitmap GetEmptyButton(int size, bool cropped = false) + public static KeyBitmap GetEmptyButton(int size) { try { - Bitmap combined = new Bitmap(size, size, PixelFormat.Format24bppRgb); + var combined = new Bitmap(size, size, PixelFormat.Format24bppRgb); - using (Graphics g = Graphics.FromImage(combined)) + const int iconPosition = 0; + + using (var g = Graphics.FromImage(combined)) { g.CompositingQuality = CompositingQuality.HighQuality; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; - - int iconPosition = cropped ? 10 : 0; - int iconSize = cropped ? size - 20 : size; - - - using SolidBrush brush = new SolidBrush(Color.FromArgb(35, 35, 35)); - g.FillRectangle(brush, iconPosition, iconPosition, iconSize, iconSize); - + + using var brush = new SolidBrush(Color.FromArgb(35, 35, 35)); + g.FillRectangle(brush, iconPosition, iconPosition, size, size); } - - combined.RotateFlip(RotateFlipType.Rotate180FlipNone); - + using var bufferStream = new MemoryStream(); combined.Save(bufferStream, ImageFormat.Png); @@ -42,8 +37,9 @@ public static KeyBitmap GetEmptyButton(int size, bool cropped = false) } catch { - return null; + return KeyBitmap.Black; } + } }