Browse Source

fix passing args with spaces

Veloe 2 years ago
parent
commit
c95a5424b2

+ 27 - 21
VeloeMinecraftLauncher/MinecraftLauncher/StartCommandBuilder.cs

@@ -4,7 +4,6 @@ using System.IO;
 using System.Linq;
 using System.Text;
 using System.Text.Json;
-using System.Threading.Tasks;
 
 namespace VeloeMinecraftLauncher.MinecraftLauncher;
 
@@ -24,11 +23,11 @@ internal static class StartCommandBuilder
 
         // setting natives folder
         if (version.InheritsFrom is null)
-            returnString.Append($"-Djava.library.path={Settings.minecraftForlderPath + "versions/" + version.Id + "/natives/"}");
+            returnString.Append($"-Djava.library.path=\"{Path.GetDirectoryName(Settings.minecraftForlderPath + "versions/" + version.Id + "/natives/")}\"");
         else
-            returnString.Append($"-Djava.library.path={Settings.minecraftForlderPath + "versions/" + version.InheritsFrom + "/natives/"}"); //for forge, vanilla optifine, fabric
+            returnString.Append($"-Djava.library.path=\"{Path.GetDirectoryName(Settings.minecraftForlderPath + "versions/" + version.InheritsFrom + "/natives/")}\""); //for forge, vanilla optifine, fabric
 
-        returnString.Append(" -cp ");
+        returnString.Append(" -cp \"");
 
         // add libraries
         foreach(var library in version.Libraries)
@@ -78,19 +77,19 @@ internal static class StartCommandBuilder
                     libPath += dir+"/";
                 }
                 if (rulesVaild)
-                    returnString.Append(Settings.minecraftForlderPath + "libraries/" + libPath + dirs[dirs.Length-2]+"-"+dirs[dirs.Length-1]+".jar;");
+                    returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "libraries/" + libPath + dirs[dirs.Length-2]+"-"+dirs[dirs.Length-1]+".jar") + separator);
 
                 continue;
             }
 
             if (rulesVaild)
-                returnString.Append(Settings.minecraftForlderPath + "libraries/" + library.Downloads.Artifact.Path + separator);
+                returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "libraries/" + library.Downloads.Artifact.Path) + separator);
 
         }
         
         Entity.Version.Version inheritsFrom = new();
         if (version.InheritsFrom is null)
-            returnString.Append(Settings.minecraftForlderPath + "versions/" + version.Id + "/" + version.Id + ".jar"); //main jar file
+            returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "versions/" + version.Id + "/" + version.Id + ".jar")); //main jar file
         else
         {
             //for forge, vanilla optifine, fabric
@@ -132,21 +131,24 @@ internal static class StartCommandBuilder
                 }
 
                 if(rulesVaild)
-                    returnString.Append(Settings.minecraftForlderPath + "libraries/" + library.Downloads.Artifact.Path + separator);
+                    returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "libraries/" + library.Downloads.Artifact.Path) + separator);
 
             }
+
             //main jar file
             //check here if there is jar file not inherited
             //check if it is not 0kb size (fabric)
             var fileInfo = new FileInfo($"{Settings.minecraftForlderPath}versions/{version.Id}/{version.Id}.jar");
 
             if (fileInfo.Exists && fileInfo.Length > 0)
-                returnString.Append(Settings.minecraftForlderPath + "versions/" + version.Id + "/" + version.Id + ".jar");//for optifine
+                returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "versions/" + version.Id + "/" + version.Id + ".jar"));//for optifine
             else
-                returnString.Append(Settings.minecraftForlderPath + "versions/" + version.InheritsFrom + "/" + version.InheritsFrom + ".jar");//for forge
+                returnString.Append(Path.GetFullPath(Settings.minecraftForlderPath + "versions/" + version.InheritsFrom + "/" + version.InheritsFrom + ".jar"));//for forge
         }
 
-       //check for jvm fabric options
+        returnString.Append("\"");
+
+        //check for jvm fabric options
 
         if (version.Arguments is not null && version.InheritsFrom is not null)
         {
@@ -165,7 +167,7 @@ internal static class StartCommandBuilder
                         continue;
 
                     var value = ((JsonElement)argument).Deserialize(typeof(string)) as string;
-
+  
                     //for new forge versions (1.18, 1.19)
                     if (value.Contains("${version_name}"))
                     {
@@ -173,15 +175,21 @@ internal static class StartCommandBuilder
                     }
                     if (value.Contains("${library_directory}"))
                     {
-                        value = value.Replace("${library_directory}", Settings.minecraftForlderPath + "libraries/");
+                        value = value.Replace("${library_directory}", Path.GetDirectoryName(Settings.minecraftForlderPath + "libraries/"));
                     }
                     if (value.Contains("${classpath_separator}"))
                     {
                         value = value.Replace("${classpath_separator}", separator.ToString());
                     }
 
+                    bool containsArgWithValue = value.Contains('=');
+                    //value = value.Replace(" ", "");
+                    if (containsArgWithValue)
+                        value = value.Replace("=", "=\"");
                     returnString.Append(" ");
-                    returnString.Append(value.Replace(" ", ""));
+                    returnString.Append(value);
+                    if (containsArgWithValue)
+                        returnString.Append("\"");
                 }
         }
 
@@ -279,8 +287,6 @@ internal static class StartCommandBuilder
 
                     var value = ((JsonElement)argument).Deserialize(typeof(string));
 
-                    //if ()
-
                     if (!(value as string).Contains("--"))
                     {
                         argsValues.Add(value as string);
@@ -318,21 +324,21 @@ internal static class StartCommandBuilder
                     returnString.Append(" --username " + username);
                     break;
                 case "--version":
-                    returnString.Append(" --version " + version.Id /*"\"Copy of VeloeLauncher\""*/);
+                    returnString.Append(" --version " + version.Id /*"'Copy of VeloeLauncher'"*/);
                     break;
                 case "--gameDir":
                     //for forge
                     if (!(argsValues.Where(x => x.Contains("forge")).Count() > 0 || version.Id.ToLower().Contains("fabric")))
-                        returnString.Append(" --gameDir " + Settings.minecraftForlderPath);
+                        returnString.Append(" --gameDir " + "\"" + Path.GetDirectoryName(Settings.minecraftForlderPath) + "\"");
                     else
-                        returnString.Append(" --gameDir " + Settings.minecraftForlderPath + "versions/" + version.Id);
+                        returnString.Append(" --gameDir " + "\"" + Path.GetDirectoryName(Settings.minecraftForlderPath + "versions/" + version.Id + "/") + "\"");
                     break;
                 case "--assetsDir":
                     //for forge
                     if (version.InheritsFrom is null)
-                        returnString.Append(" --assetsDir " + Settings.minecraftForlderPath + "assets/" + version.Assets + "/");
+                        returnString.Append(" --assetsDir " + "\"" + Path.GetDirectoryName(Settings.minecraftForlderPath + "assets/" + version.Assets + "/") + "\"");
                     else
-                        returnString.Append(" --assetsDir " + Settings.minecraftForlderPath + "assets/" + inheritsFrom.Assets + "/");
+                        returnString.Append(" --assetsDir " + "\"" + Path.GetDirectoryName(Settings.minecraftForlderPath + "assets/" + inheritsFrom.Assets + "/") + "\"");
                     break;
                 case "--assetIndex":
                     //for forge

+ 8 - 7
VeloeMinecraftLauncher/ViewModels/MainWindowViewModel.cs

@@ -1,4 +1,4 @@
-using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Controls.ApplicationLifetimes;
 using ReactiveUI;
 using System;
 using System.Collections.ObjectModel;
@@ -25,6 +25,7 @@ using Avalonia.Data;
 using System.Collections.Generic;
 using VeloeMinecraftLauncher.Models.Entity;
 using VeloeMinecraftLauncher.Models;
+using System.Collections.Specialized;
 
 namespace VeloeMinecraftLauncher.ViewModels;
 
@@ -413,7 +414,7 @@ public class MainWindowViewModel : ViewModelBase
                         }
                     }
 
-                    ConsoleText += arguments;
+                    //ConsoleText += arguments;
                     ArgumentsBox = arguments;
                 }
 
@@ -429,7 +430,7 @@ public class MainWindowViewModel : ViewModelBase
                         javaPath += ".exe";
                 }
 
-                _logger.Debug("Java version path: {0}", javaPath);
+                _logger.Debug("Java version path: {0}", Path.Combine(Settings.minecraftForlderPath, javaPath));
                 _logger.Debug("Minecraft arguments: {0}", ArgumentsBox);
 
                 ProcessStartInfo proc;
@@ -440,13 +441,12 @@ public class MainWindowViewModel : ViewModelBase
                     RedirectStandardError = true,
                     WindowStyle = ProcessWindowStyle.Hidden,
                     CreateNoWindow = true,
-                    FileName = System.IO.Path.Combine(Settings.minecraftForlderPath, javaPath),
+                    FileName = Path.GetFullPath(Path.Combine(Settings.minecraftForlderPath, javaPath)),
                     StandardErrorEncoding = Encoding.UTF8,
-                    WorkingDirectory = Settings.minecraftForlderPath,
+                    WorkingDirectory = Path.GetDirectoryName(Path.Combine(Settings.minecraftForlderPath)),
                     Arguments = ArgumentsBox
                 };
 
-
                 Process minecraft = new Process();
 
                 minecraft.StartInfo = proc;
@@ -483,6 +483,7 @@ public class MainWindowViewModel : ViewModelBase
                     }
                     catch (Exception ex)
                     {
+                        IsNoGameRunning = true;
                         OpenErrorWindow(ex);
                         return Task.CompletedTask;
                     }
@@ -494,7 +495,7 @@ public class MainWindowViewModel : ViewModelBase
 
             }
             catch (Exception ex)
-            {
+            {              
                 OpenErrorWindow(ex);
             }
         });