Skip to content

Commit

Permalink
Fix crash on startup when ip address is null
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmayer-dev committed Sep 18, 2023
1 parent b3fe88f commit fd9cb3c
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 48 deletions.
4 changes: 2 additions & 2 deletions MacroDeck/GUI/MainWindow.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 45 additions & 36 deletions MacroDeck/GUI/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ namespace SuchByte.MacroDeck.GUI;

public partial class MainWindow : Form
{
private DeckView _deckView { get; set; }
private NotificationsList? _notificationsList;

private DeckView? _deckView;
public DeckView DeckView
{
get
{
if (_deckView == null || _deckView.IsDisposed)
if (_deckView is null || _deckView.IsDisposed || !_deckView.IsHandleCreated)
{
_deckView = new DeckView();
}
return _deckView;
}
}

private NotificationsList notificationsList;

public MainWindow()
{
Expand All @@ -59,7 +59,7 @@ private void UpdateTranslation()
lblIpAddressHostname.Text = LanguageManager.Strings.IpAddressHostNamePort;
}

private void LanguageChanged(object sender, EventArgs e)
private void LanguageChanged(object? sender, EventArgs e)
{
UpdateTranslation();
DeckView?.UpdateTranslation();
Expand Down Expand Up @@ -91,27 +91,27 @@ public void SetView(Control view)

switch (view)
{
case DeckView deck:
case DeckView _:
SelectContentButton(btnDeck);
break;
case DeviceManagerView deviceManager:
case DeviceManagerView:
SelectContentButton(btnDeviceManager);
break;
case ExtensionsView extensions:
case ExtensionsView:
SelectContentButton(btnExtensions);
break;
case SettingsView settings:
case SettingsView:
SelectContentButton(btnSettings);
break;
case VariablesView variables:
case VariablesView:
SelectContentButton(btnVariables);
break;
}
}

private void MainWindow_Load(object sender, EventArgs e)
private void MainWindow_Load(object? sender, EventArgs e)
{
lblVersion.Text = $"Macro Deck {MacroDeck.Version}";
lblVersion.Text = $@"Macro Deck {MacroDeck.Version}";

PluginManager.OnPluginsChange += OnPluginsChanged;
IconManager.OnIconPacksChanged += OnPluginsChanged;
Expand Down Expand Up @@ -159,28 +159,37 @@ private void LoadHosts()
hosts.SelectedIndexChanged -= Hosts_SelectedIndexChanged;
foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces())
{
hosts.Items.Add(networkInterface.GetIPProperties().UnicastAddresses.Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork).FirstOrDefault().Address.ToString());
var ipAddress = networkInterface
.GetIPProperties()
.UnicastAddresses
.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
?.Address
.ToString();
if (!string.IsNullOrWhiteSpace(ipAddress))
{
hosts.Items.Add(ipAddress);
}
}
hosts.Text = MacroDeck.Configuration.HostAddress;
hosts.SelectedIndexChanged += Hosts_SelectedIndexChanged;
}

private void NotificationsChanged(object sender, EventArgs e)
private void NotificationsChanged(object? sender, EventArgs e)
{
btnNotifications.NotificationCount = NotificationManager.Notifications.Count;
}

private void ExtensionStoreHelper_OnInstallationFinished(object sender, EventArgs e)
private void ExtensionStoreHelper_OnInstallationFinished(object? sender, EventArgs e)
{
RefreshPluginsLabels();
}

private void OnPackageManagerUpdateCheckFinished(object sender, EventArgs e)
private void OnPackageManagerUpdateCheckFinished(object? sender, EventArgs e)
{
RefreshPluginsLabels();
}

private void OnPluginsChanged(object sender, EventArgs e)
private void OnPluginsChanged(object? sender, EventArgs e)
{
RefreshPluginsLabels();

Expand All @@ -195,11 +204,11 @@ private void RefreshPluginsLabels()
});
}

private void OnServerStateChanged(object sender, EventArgs e)
private void OnServerStateChanged(object? sender, EventArgs e)
{
Invoke(() =>
{
if (MacroDeckServer.WebSocketServer.ListenerSocket == null)
if (MacroDeckServer.WebSocketServer?.ListenerSocket == null)
{
lblServerStatus.Text = LanguageManager.Strings.ServerOffline;
}
Expand All @@ -212,73 +221,73 @@ private void OnServerStateChanged(object sender, EventArgs e)
});
}

private void OnClientsConnectedChanged(object sender, EventArgs e)
private void OnClientsConnectedChanged(object? sender, EventArgs e)
{
Invoke(new Action(() =>
lblNumClientsConnected.Text = string.Format(LanguageManager.Strings.XClientsConnected, MacroDeckServer.Clients.Count)
));
}

private void BtnDeck_Click(object sender, EventArgs e)
private void BtnDeck_Click(object? sender, EventArgs e)
{
SetView(DeckView);
DeckView.UpdateButtons();
}

private void BtnExtensions_Click(object sender, EventArgs e)
private void BtnExtensions_Click(object? sender, EventArgs e)
{
SetView(new ExtensionsView());
}

private void BtnSettings_Click(object sender, EventArgs e)
private void BtnSettings_Click(object? sender, EventArgs e)
{
SetView(new SettingsView());
}

private void BtnDeviceManager_Click(object sender, EventArgs e)
private void BtnDeviceManager_Click(object? sender, EventArgs e)
{
SetView(new DeviceManagerView());
}

public void OnFormClosing(object sender, EventArgs e)
public void OnFormClosing(object? sender, EventArgs e)
{
foreach (Control control in contentPanel.Controls)
{
control.Dispose();
}
}

private void BtnVariables_Click(object sender, EventArgs e)
private void BtnVariables_Click(object? sender, EventArgs e)
{
SetView(new VariablesView());
}

private void BtnNotifications_Click(object sender, EventArgs e)
private void BtnNotifications_Click(object? sender, EventArgs e)
{
if (notificationsList == null || notificationsList.IsDisposed)
if (_notificationsList == null || _notificationsList.IsDisposed)
{
notificationsList = new NotificationsList
_notificationsList = new NotificationsList
{
Location = new Point(btnNotifications.Location.X, btnNotifications.Location.Y + btnNotifications.Height)
Location = btnNotifications.Location with { Y = btnNotifications.Location.Y + btnNotifications.Height }
};
notificationsList.OnCloseRequested += (sender, e) =>
_notificationsList.OnCloseRequested += (_, _) =>
{
Controls.Remove(notificationsList);
Controls.Remove(_notificationsList);
};
}

if (Controls.Contains(notificationsList))
if (Controls.Contains(_notificationsList))
{
Controls.Remove(notificationsList);
Controls.Remove(_notificationsList);
}
else
{
Controls.Add(notificationsList);
notificationsList.BringToFront();
Controls.Add(_notificationsList);
_notificationsList.BringToFront();
}
}

private void Hosts_SelectedIndexChanged(object sender, EventArgs e)
private void Hosts_SelectedIndexChanged(object? sender, EventArgs e)
{
MacroDeck.Configuration.HostAddress = hosts.Text;
MacroDeck.Configuration.Save(ApplicationPaths.MainConfigFilePath);
Expand Down
34 changes: 24 additions & 10 deletions MacroDeck/MacroDeck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal static void Start(StartParameters startParameters)
IconManager.Initialize();
ProfileManager.Load();

PrintNetworkInterfaces();
SearchNetworkInterfaces();
MacroDeckServer.Start(StartParameters.Port <= 0 ? Configuration.HostPort : StartParameters.Port);
BroadcastServer.Start();
ADBServerHelper.Initialize();
Expand Down Expand Up @@ -162,25 +162,39 @@ private static void OnUpdateAvailable(object? sender, UpdateApiVersionInfo e)

}

private static void PrintNetworkInterfaces()
private static void SearchNetworkInterfaces()
{
StringBuilder sb = new();
var foundNetworkInterfaces = 0;
try
{
foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces())
{
var address = adapter.GetIPProperties().UnicastAddresses
.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)?.Address;
sb.AppendLine();
sb.Append($"{adapter.Name} - {address}");
var address = adapter
.GetIPProperties()
.UnicastAddresses
.FirstOrDefault(x => x.Address.AddressFamily == AddressFamily.InterNetwork)?
.Address?
.ToString();
if (!string.IsNullOrWhiteSpace(address))
{
sb.AppendLine($"{adapter.Name} - {address}");
foundNetworkInterfaces++;
}
}
}
catch
catch (Exception ex)
{
// ignored
MacroDeckLogger.Warning($"Error while searching for network interfaces\n{ex.Message}");
}

if (foundNetworkInterfaces == 0)
{
MacroDeckLogger.Error("No network interfaces were found");
} else
{
MacroDeckLogger.Info($"Found network interfaces:\n{sb}");
}
MacroDeckLogger.Info($"Network interfaces: {sb}");
sb.Clear();
}

private static void StartInitialSetup()
Expand Down

0 comments on commit fd9cb3c

Please sign in to comment.