Browse Source

possible null refs warnings fixes

Veloe 1 year ago
parent
commit
1829589ee0

+ 6 - 1
VeloeMinecraftLauncher/Utils/Downloader/Downloader.cs

@@ -610,9 +610,10 @@ internal class Downloader
 
     private async Task<Entity.Version.Version?> GetVersionJson()
     {
+        ArgumentNullException.ThrowIfNull(Parameters.Data.SelectedVersion);
+        
         if (Parameters.Data.SelectedModpack is not null)
         {
-            ArgumentNullException.ThrowIfNull(Parameters.Data.SelectedVersion);
             ArgumentNullException.ThrowIfNull(Parameters.Data.SelectedModpack);
 
             if (Parameters.Data.SelectedVersion.Id != Parameters.Data.SelectedModpack.Version)
@@ -704,6 +705,8 @@ internal class Downloader
 
         using var httpClient = new HttpClient(ph);
 
+        ArgumentNullException.ThrowIfNull(Parameters.Data.VersionJson);
+
         foreach (var library in Parameters.Data.VersionJson.Libraries)
         {
             if (string.IsNullOrEmpty(library.Name) || string.IsNullOrEmpty(library.Url))
@@ -713,6 +716,8 @@ internal class Downloader
             var libPath = Settings.minecraftForlderPath + "libraries/" + relativePath;
             var dirPath = Path.GetDirectoryName(libPath);
 
+            ArgumentNullException.ThrowIfNullOrEmpty(dirPath);
+
             if (!Directory.Exists(dirPath))
             {
                 Directory.CreateDirectory(dirPath);

+ 4 - 4
VeloeMinecraftLauncher/Utils/Settings.cs

@@ -20,9 +20,9 @@ internal static class Settings
     public static bool gameLogToLauncher = false;
     public static string username = string.Empty;
     public static string lastChosenVersion = string.Empty;
-    public static Serilog.ILogger logger;
-    public static CaptureFilePathHook logFilePath;
-    public static Serilog.ILogger avaloniaLogger;
+    public static Serilog.ILogger? logger;
+    public static CaptureFilePathHook? logFilePath;
+    public static Serilog.ILogger? avaloniaLogger;
     public static bool setMaxLog = false;
     public static UInt32 maxLog = 1024;
     public static LogEventLevel consoleLogEventLevel = LogEventLevel.Debug;
@@ -70,7 +70,7 @@ internal static class Settings
             }
             catch (JsonException ex)
             {
-                //TODO log
+                logger?.Error(ex.Message);
             }
         }
     }

+ 4 - 4
VeloeMinecraftLauncher/ViewModels/MainWindowViewModel.cs

@@ -103,7 +103,7 @@ public class MainWindowViewModel : ViewModelBase
             
             try
             {
-                Changelogs = new ObservableCollection<Changelog>(await Downloader.DownloadAndDeserializeJsonData<List<Changelog>>("https://files.veloe.link/launcher/changelog.json"));
+                Changelogs = new ObservableCollection<Changelog>(await Downloader.DownloadAndDeserializeJsonData<List<Changelog>>("https://files.veloe.link/launcher/changelog.json") ?? new List<Changelog>());
                 this.RaisePropertyChanged(nameof(Changelogs));
             }
             catch (Exception ex)
@@ -622,7 +622,7 @@ public class MainWindowViewModel : ViewModelBase
             StartButtonOutput = "";
             IsNoGameRunning = true;
             if (minecraftProcess.ExitCode is not (0 or 2))
-                OpenErrorWindow(new JavaProcessException($"Minecraft process exited with an error code {minecraftProcess.ExitCode}.\nCheck log in game folder or launcher console.", $"{Settings.minecraftForlderPath}versions/{_startedVersion.Version}/crash-reports"));
+                OpenErrorWindow(new JavaProcessException($"Minecraft process exited with an error code {minecraftProcess.ExitCode}.\nCheck log in game folder or launcher console.", $"{Settings.minecraftForlderPath}versions/{_startedVersion?.Version ?? "NULL"}/crash-reports"));
             else if (minecraftProcess.ExitCode is 2)
             {
                 OpenErrorWindow(new Exception("JVM exited on the startup (Exit code 2). Check your Java installation. Get more info in the laucher console."));
@@ -779,9 +779,9 @@ public class MainWindowViewModel : ViewModelBase
                 if (OperatingSystem.IsLinux())
                 {
                     using var updaterProcess = Process.Start(new ProcessStartInfo() { FileName = fileName, Arguments = " --version", RedirectStandardOutput = true });
-                    updaterProcess.WaitForExit();
+                    updaterProcess?.WaitForExit();
 
-                    var versionStr = System.Text.RegularExpressions.Regex.Match(updaterProcess.StandardOutput.ReadToEnd(), "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").Value;
+                    var versionStr = System.Text.RegularExpressions.Regex.Match(updaterProcess?.StandardOutput.ReadToEnd() ?? string.Empty, "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+").Value;
                     if (!string.IsNullOrWhiteSpace(versionStr) && 
                         latestLauncherVersion?.Updater is not null && 
                         !(new Version(latestLauncherVersion.Updater) > new Version(versionStr)))

+ 1 - 1
VeloeMinecraftLauncher/ViewModels/SettingsWindowViewModel.cs

@@ -229,7 +229,7 @@ public class SettingsWindowViewModel : ViewModelBase
 
     public void OpenLogFile()
     {
-        if (Path.Exists(Settings.logFilePath.Path))
+        if (Path.Exists(Settings.logFilePath?.Path))
         {
             System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo() { FileName = Settings.logFilePath.Path, UseShellExecute = true });
         }

+ 4 - 4
VeloeMinecraftLauncher/ViewModels/VersionsDownloaderViewModel.cs

@@ -511,19 +511,19 @@ public class VersionsDownloaderViewModel : ViewModelBase, IDisposable
                         DirectoryToTreeNode(
                             dirInfo,
                             out dirSize,
-                            (dir) => dir.Name.Equals(".mixin.out",StringComparison.Ordinal) ||
+                            (dir) => dir != null && (dir.Name.Equals(".mixin.out",StringComparison.Ordinal) ||
                                 dir.Name.Equals("assets",StringComparison.Ordinal) ||
                                 dir.Name.Equals("javaruntime", StringComparison.Ordinal) ||
                                 dir.Name.Equals("libraries", StringComparison.Ordinal) ||
                                 dir.Name.Equals("logs", StringComparison.Ordinal) ||
-                                dir.Name.Equals("versions", StringComparison.Ordinal),
-                            (file) => file.Name.Contains("Veloe",StringComparison.Ordinal) ||
+                                dir.Name.Equals("versions", StringComparison.Ordinal)),
+                            (file) => file != null && (file.Name.Contains("Veloe",StringComparison.Ordinal) ||
                                 file.Name.Contains("log", StringComparison.Ordinal) ||
                                 file.Name.Contains("exe", StringComparison.Ordinal) ||
                                 file.Name.Equals("launcher_profiles.json", StringComparison.Ordinal) ||
                                 file.Name.Equals("libHarfBuzzSharp.dll", StringComparison.Ordinal) ||
                                 file.Name.Equals("libSkiaSharp.dll", StringComparison.Ordinal) ||
-                                file.Name.Equals("settings.json", StringComparison.Ordinal))
+                                file.Name.Equals("settings.json", StringComparison.Ordinal)))
                         );
                     allSize += dirSize;
                 }