Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Command Line parsing wrongly paths with space #542

Merged
merged 1 commit into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")]