Skip to content

Commit

Permalink
Merge pull request #542 from PixiEditor/args-parsing-fix
Browse files Browse the repository at this point in the history
Fixed Command Line parsing wrongly paths with space
  • Loading branch information
flabbet committed Jun 4, 2023
2 parents 0726b6b + 228eab5 commit db611ef
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
27 changes: 25 additions & 2 deletions src/PixiEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Media;
using PixiEditor.Helpers.UI;
using PixiEditor.Localization;
using PixiEditor.Models.Controllers;
using PixiEditor.Models.DataHolders;
Expand Down Expand Up @@ -69,6 +70,9 @@ private bool HandleNewInstance()

GC.KeepAlive(_mutex);

if (Current?.Dispatcher == null)
return true;

if (isOwned)
{
var thread = new Thread(
Expand All @@ -86,7 +90,7 @@ private bool HandleNewInstance()
List<string> args = new List<string>();
if (File.Exists(passedArgsFile))
{
args = File.ReadAllText(passedArgsFile).Split(' ').ToList();
args = CommandLineHelpers.SplitCommandLine(File.ReadAllText(passedArgsFile)).ToList();
File.Delete(passedArgsFile);
}
Expand All @@ -107,14 +111,33 @@ private bool HandleNewInstance()
}

// Notify other instance so it could bring itself to foreground.
File.WriteAllText(passedArgsFile, string.Join(' ', Environment.GetCommandLineArgs()));
File.WriteAllText(passedArgsFile, string.Join(' ', WrapSpaces(Environment.GetCommandLineArgs())));
_eventWaitHandle.Set();

// Terminate this instance.
Shutdown();
return false;
}

private string?[] WrapSpaces(string[] args)
{
string?[] wrappedArgs = new string?[args.Length];
for (int i = 0; i < args.Length; i++)
{
string arg = args[i];
if (arg.Contains(' '))
{
wrappedArgs[i] = $"\"{arg}\"";
}
else
{
wrappedArgs[i] = arg;
}
}

return wrappedArgs;
}

protected override void OnSessionEnding(SessionEndingCancelEventArgs e)
{
base.OnSessionEnding(e);
Expand Down
47 changes: 47 additions & 0 deletions src/PixiEditor/Helpers/UI/CommandLineHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace PixiEditor.Helpers.UI;

public static class CommandLineHelpers
{
public static IEnumerable<string> SplitCommandLine(string commandLine)
{
bool inQuotes = false;

return commandLine.Split(
c =>
{
if (c == '\"')
inQuotes = !inQuotes;
return !inQuotes && c == ' ';
})
.Select(arg => arg.Trim().TrimMatchingQuotes('\"'))
.Where(arg => !string.IsNullOrEmpty(arg));
}

public static IEnumerable<string> Split(
this string str,
Func<char, bool> controller)
{
int nextPiece = 0;

for (int c = 0; c < str.Length; c++)
{
if (controller(str[c]))
{
yield return str.Substring(nextPiece, c - nextPiece);
nextPiece = c + 1;
}
}

yield return str.Substring(nextPiece);
}

public static string TrimMatchingQuotes(this string input, char quote)
{
if ((input.Length >= 2) &&
(input[0] == quote) && (input[^1] == quote))
return input.Substring(1, input.Length - 2);

return input;
}
}
4 changes: 2 additions & 2 deletions src/PixiEditor/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyVersion("1.1.1.0")]
[assembly: AssemblyFileVersion("1.1.1.0")]

0 comments on commit db611ef

Please sign in to comment.