diff --git a/src/CxbxDebugger/Debugger/Debugger.cs b/src/CxbxDebugger/Debugger/Debugger.cs index 06334b7b0..6f5ea590d 100644 --- a/src/CxbxDebugger/Debugger/Debugger.cs +++ b/src/CxbxDebugger/Debugger/Debugger.cs @@ -71,30 +71,29 @@ namespace CxbxDebugger Init(); } - public Debugger(string[] x_args) + public Debugger(string[] launchArgs) { Init(); - if (x_args == null) - return; - - // Copy all arguments - args = x_args; - - // Keep quotes for any strings that may contain spaces - int scratch; - for (int i = 0; i < args.Length; ++i) + // Copy XBE path without the quotes + var loadArg = Array.FindIndex(launchArgs, x => x == "/load"); + if (loadArg != -1) { - if (int.TryParse(args[i], out scratch) == false) - { - args[i] = string.Format("\"{0}\"", args[i]); - } + Target = launchArgs[loadArg + 1].Trim(new char[] { '"' }); } - // Copy XBE path without the quotes - if(x_args.Length > 2) + args = new string[launchArgs.Length]; + + for (int i = 0; i < launchArgs.Length; ++i) { - Target = x_args[2].Trim(new char[] { '"' }); + var arg = launchArgs[i]; + + if (arg.Contains(" ")) + { + arg = string.Format($"\"{arg}\""); + } + + args[i] = arg; } } @@ -176,11 +175,13 @@ namespace CxbxDebugger var DebugCreationFlags = WinProcesses.ProcessCreationFlags.DEBUG_ONLY_THIS_PROCESS | WinProcesses.ProcessCreationFlags.CREATE_NEW_CONSOLE; - + + var launchArgs = new StringBuilder(string.Join(" ", args)); + bool bRet = WinProcesses.NativeMethods.CreateProcess ( null, - new StringBuilder(string.Join(" ", args)), + launchArgs, null, null, false, @@ -201,6 +202,10 @@ namespace CxbxDebugger State = RunState.Running; } + else + { + throw new Exception($"Failed to launch loader '{launchArgs}'"); + } return bRet; } diff --git a/src/CxbxDebugger/Form1.cs b/src/CxbxDebugger/Form1.cs index b3cfd8bc3..79dd5bdb6 100644 --- a/src/CxbxDebugger/Form1.cs +++ b/src/CxbxDebugger/Form1.cs @@ -15,7 +15,7 @@ namespace CxbxDebugger { Thread DebuggerWorkerThread; Debugger DebuggerInst; - string[] CachedArgs; + string[] StartupArgs; string CachedTitle = ""; bool SuspendedOnBp = false; @@ -45,13 +45,9 @@ namespace CxbxDebugger throw new Exception("Incorrect usage"); } - var items = new List(args.Length - 1); - for (int i = 1; i < args.Length; ++i) - { - items.Add(args[i]); - } - CachedArgs = items.ToArray(); + StartupArgs = new string[args.Length - 1]; + Array.Copy(args, 1, StartupArgs, 0, args.Length - 1); DebugEvents = new DebuggerFormEvents(this); @@ -104,7 +100,7 @@ namespace CxbxDebugger if (Create) { // Create debugger instance - DebuggerInst = new Debugger(CachedArgs); + DebuggerInst = new Debugger(StartupArgs); DebuggerInst.RegisterEventInterfaces(DebugEvents); DebuggerInst.RegisterEventInterfaces(patchMan);