From 1acae27793c94f922616a613dfda0fa11a3d7693 Mon Sep 17 00:00:00 2001 From: Heavy Bob <ferrettclay@gmail.com> Date: Mon, 7 Apr 2025 10:18:52 +1000 Subject: [PATCH] Fixed all the cursed warnings when building. Fixed all the values that can be nullable on the client. --- AutoTrackR2/ConfigPage.xaml.cs | 16 +++++++--- AutoTrackR2/HomePage.xaml.cs | 15 ++++++++-- .../VehicleDestructionEvent.cs | 6 +++- AutoTrackR2/LogHandler.cs | 6 +++- AutoTrackR2/MainWindow.xaml.cs | 29 +++++++++---------- AutoTrackR2/UpdatePage.xaml.cs | 9 +++--- 6 files changed, 52 insertions(+), 29 deletions(-) diff --git a/AutoTrackR2/ConfigPage.xaml.cs b/AutoTrackR2/ConfigPage.xaml.cs index bf83314..9191f75 100644 --- a/AutoTrackR2/ConfigPage.xaml.cs +++ b/AutoTrackR2/ConfigPage.xaml.cs @@ -399,11 +399,14 @@ namespace AutoTrackR2 dialog.ValidateNames = false; dialog.Filter = "All files|*.*"; - if (dialog.ShowDialog() == true) + if (dialog.ShowDialog() == true && dialog.FileName != null) { // Extract only the directory path from the file - string selectedFolder = Path.GetDirectoryName(dialog.FileName); - VideoPath.Text = selectedFolder; // Set the folder path + string? selectedFolder = Path.GetDirectoryName(dialog.FileName); + if (selectedFolder != null) + { + VideoPath.Text = selectedFolder; // Set the folder path + } } } @@ -412,6 +415,11 @@ namespace AutoTrackR2 Slider slider = (Slider)sender; // Build the dynamic file path for the current user + if (string.IsNullOrEmpty(ConfigManager.AHKScriptFolder)) + { + MessageBox.Show("AHK script folder path is not configured.", "Configuration Error", MessageBoxButton.OK, MessageBoxImage.Warning); + return; + } string filePath = Path.Combine( ConfigManager.AHKScriptFolder, "visorwipe.ahk" @@ -524,7 +532,7 @@ namespace AutoTrackR2 private void FlashSaveButton() { - string originalText = SaveButton.Content.ToString(); + string? originalText = SaveButton.Content?.ToString() ?? string.Empty; SaveButton.Content = "Saved"; // Save button color change effect diff --git a/AutoTrackR2/HomePage.xaml.cs b/AutoTrackR2/HomePage.xaml.cs index f8755df..3d4b897 100644 --- a/AutoTrackR2/HomePage.xaml.cs +++ b/AutoTrackR2/HomePage.xaml.cs @@ -26,6 +26,10 @@ public partial class HomePage : UserControl { InitializeComponent(); + if (string.IsNullOrEmpty(ConfigManager.KillHistoryFile)) + { + throw new InvalidOperationException("KillHistoryFile path is not configured."); + } _killHistoryManager = new KillHistoryManager(ConfigManager.KillHistoryFile); // Set the TextBlock text @@ -51,7 +55,7 @@ public partial class HomePage : UserControl } } - private void CheckStarCitizenStatus(object sender, ElapsedEventArgs e) + private void CheckStarCitizenStatus(object? sender, ElapsedEventArgs e) { bool isRunning = IsStarCitizenRunning(); Dispatcher.Invoke(() => @@ -311,7 +315,7 @@ public partial class HomePage : UserControl // Create the Image for the profile var profileImage = new Image { - Source = new BitmapImage(new Uri(killData.PFP)), // Assuming the 8th part contains the profile image URL + Source = new BitmapImage(new Uri(killData.PFP ?? "https://cdn.robertsspaceindustries.com/static/images/account/avatar_default_big.jpg")), Width = 90, Height = 90, Stretch = Stretch.Fill, // Adjust how the image fits @@ -392,8 +396,13 @@ public partial class HomePage : UserControl textBlock.FontSize = fontSize; } - public static void RunAHKScript(string path) + public static void RunAHKScript(string? path) { + if (string.IsNullOrEmpty(path) || string.IsNullOrEmpty(ConfigManager.AHKScriptFolder)) + { + return; + } + string scriptPath = Path.Combine(ConfigManager.AHKScriptFolder, path); if (!File.Exists(scriptPath)) diff --git a/AutoTrackR2/LogEventHandlers/VehicleDestructionEvent.cs b/AutoTrackR2/LogEventHandlers/VehicleDestructionEvent.cs index 041859a..786f46f 100644 --- a/AutoTrackR2/LogEventHandlers/VehicleDestructionEvent.cs +++ b/AutoTrackR2/LogEventHandlers/VehicleDestructionEvent.cs @@ -19,7 +19,7 @@ public struct VehicleDestructionData public class VehicleDestructionEvent : ILogEventHandler { public Regex Pattern { get; } - + public VehicleDestructionEvent() { Pattern = new Regex(""" @@ -34,6 +34,10 @@ public class VehicleDestructionEvent : ILogEventHandler public void Handle(LogEntry entry) { + if (entry.Message == null) + { + return; + } var match = Pattern.Match(entry.Message); if (!match.Success) { diff --git a/AutoTrackR2/LogHandler.cs b/AutoTrackR2/LogHandler.cs index fed7b33..ae62e25 100644 --- a/AutoTrackR2/LogHandler.cs +++ b/AutoTrackR2/LogHandler.cs @@ -46,8 +46,12 @@ public class LogHandler new RequestJumpFailedEvent() ]; - public LogHandler(string logPath) + public LogHandler(string? logPath) { + if (string.IsNullOrEmpty(logPath)) + { + throw new ArgumentNullException(nameof(logPath), "Log path cannot be null or empty"); + } _logPath = logPath; } diff --git a/AutoTrackR2/MainWindow.xaml.cs b/AutoTrackR2/MainWindow.xaml.cs index 4efcee9..8c0a4e9 100644 --- a/AutoTrackR2/MainWindow.xaml.cs +++ b/AutoTrackR2/MainWindow.xaml.cs @@ -58,7 +58,7 @@ namespace AutoTrackR2 } } - private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) + private void MainWindow_Closing(object? sender, System.ComponentModel.CancelEventArgs e) { // Clean up resources homePage?.Cleanup(); @@ -163,10 +163,10 @@ namespace AutoTrackR2 // Set the fields in ConfigPage.xaml.cs based on the loaded config configPage.SetConfigValues( - ConfigManager.LogFile, - ConfigManager.ApiUrl, - ConfigManager.ApiKey, - ConfigManager.VideoPath, + ConfigManager.LogFile ?? string.Empty, + ConfigManager.ApiUrl ?? string.Empty, + ConfigManager.ApiKey ?? string.Empty, + ConfigManager.VideoPath ?? string.Empty, ConfigManager.VisorWipe, ConfigManager.VideoRecord, ConfigManager.OfflineMode, @@ -177,17 +177,14 @@ namespace AutoTrackR2 public static class ConfigManager { - public static string LogFile { get; set; } - public static string KillHistoryFile { get; set; } - - public static string AHKScriptFolder { get; set; } - - public static string VisorWipeScript { get; set; } - public static string VideoRecordScript { get; set; } - - public static string ApiUrl { get; set; } - public static string ApiKey { get; set; } - public static string VideoPath { get; set; } + public static string? LogFile { get; set; } = string.Empty; + public static string? KillHistoryFile { get; set; } = string.Empty; + public static string? AHKScriptFolder { get; set; } = string.Empty; + public static string? VisorWipeScript { get; set; } = string.Empty; + public static string? VideoRecordScript { get; set; } = string.Empty; + public static string? ApiUrl { get; set; } = string.Empty; + public static string? ApiKey { get; set; } = string.Empty; + public static string? VideoPath { get; set; } = string.Empty; public static int VisorWipe { get; set; } public static int VideoRecord { get; set; } public static int OfflineMode { get; set; } diff --git a/AutoTrackR2/UpdatePage.xaml.cs b/AutoTrackR2/UpdatePage.xaml.cs index 87b260e..69bed10 100644 --- a/AutoTrackR2/UpdatePage.xaml.cs +++ b/AutoTrackR2/UpdatePage.xaml.cs @@ -10,7 +10,7 @@ namespace AutoTrackR2 public partial class UpdatePage : UserControl { public static string currentVersion = "v2.09"; - private string latestVersion; + private string? latestVersion = string.Empty; public UpdatePage() { @@ -60,7 +60,7 @@ namespace AutoTrackR2 // Parse the JSON using System.Text.Json using var document = System.Text.Json.JsonDocument.Parse(response); var root = document.RootElement; - var tagName = root.GetProperty("tag_name").GetString(); + var tagName = root.GetProperty("tag_name").GetString() ?? "unknown"; return tagName; } @@ -77,7 +77,8 @@ namespace AutoTrackR2 if (root.GetArrayLength() > 0) { var firstRelease = root[0]; - return firstRelease.GetProperty("tag_name").GetString(); + var tagName = firstRelease.GetProperty("tag_name").GetString() ?? "unknown"; + return tagName; } throw new Exception("No releases found."); @@ -90,7 +91,7 @@ namespace AutoTrackR2 return !currentVersion.Equals(latestVersion, StringComparison.Ordinal); } - private async void InstallButton_Click(object sender, RoutedEventArgs e) + private void InstallButton_Click(object sender, RoutedEventArgs e) { try {