Skip to content

Commit

Permalink
Fix parsing startup arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkeye116477 committed Nov 22, 2024
1 parent a2417bd commit 54e67d7
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/LegendaryGameSettingsView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void SaveBtn_Click(object sender, RoutedEventArgs e)
}
if (StartupArgumentsTxt.Text != "")
{
newGameSettings.StartupArguments = StartupArgumentsTxt.Text.Split().ToList();
newGameSettings.StartupArguments = StartupArgumentsTxt.Text.SplitOutsideQuotes(' ').ToList();
}
if (LanguageCodeTxt.Text != "")
{
Expand Down
3 changes: 3 additions & 0 deletions src/LegendaryLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Resource>
<Compile Include="..\third_party\playnite-common-plugin\src\SplitOutsideQuotesExtensions.cs">
<Link>Shared\SplitOutsideQuotesExtensions.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Converters\DownloadStatusEnumToStringConverter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Source: https://github.com/JosefNemec/Playnite.git
Commit: 2afc8359d8c45e80c66404add4221037ce600d43
Commit: fdb2a8652a34fc2ff08ec5a416f2cb50e85907e7
2 changes: 1 addition & 1 deletion third_party/PlayniteExtensions/SOURCE_INFO.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Source: https://github.com/JosefNemec/PlayniteExtensions.git
Commit: 7c471edfeea4ed1384f97c14ad76c48988453248
Commit: a78ac2cc75d696110eb51c179b91ef9cf5090fa2
2 changes: 1 addition & 1 deletion third_party/playnite-common-plugin/SOURCE_INFO.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Source: https://github.com/hawkeye116477/playnite-common-plugin.git
Commit: c8b11813f97cf97ae65b31a7289ea8001ef763b8
Commit: 1d84bcea3a040e82f24e6a4965a06fd1097a038a
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonPlugin
{
// Source: https://gist.github.com/mrpeardotnet/cba4338ffe01cb6e41d2765d8886aded
public static class SplitOutsideQuotesExtensions
{
/// <summary>
/// Splits the string by specified separator, but only when the separator is outside the quotes.
/// </summary>
/// <param name="source">The source string to separate.</param>
/// <param name="splitChar">The character used to split strings.</param>
/// <param name="trimSplits">If set to <c>true</c>, split strings are trimmed (whitespaces are removed).</param>
/// <param name="ignoreEmptyResults">If set to <c>true</c>, empty split results are ignored (not included in the result).</param>
/// <param name="preserveEscapeCharInQuotes">If set to <c>true</c>, then the escape character (\) used to escape e.g. quotes is included in the results.</param>
public static string[] SplitOutsideQuotes(this string source, char separator, bool trimSplits = true, bool ignoreEmptyResults = true, bool preserveEscapeCharInQuotes = true)
{
return source.SplitOutsideQuotes(new[] { separator }, trimSplits, ignoreEmptyResults, preserveEscapeCharInQuotes);
}

/// <summary>
/// Splits the string by specified separator, but only when the separator is outside the quotes.
/// </summary>
/// <param name="source">The source string to separate.</param>
/// <param name="splitChars">The characters used to split strings.</param>
/// <param name="trimSplits">If set to <c>true</c>, split strings are trimmed (whitespaces are removed).</param>
/// <param name="ignoreEmptyResults">If set to <c>true</c>, empty split results are ignored (not included in the result).</param>
/// <param name="preserveEscapeCharInQuotes">If set to <c>true</c>, then the escape character (\) used to escape e.g. quotes is included in the results.</param>
public static string[] SplitOutsideQuotes(this string source, char[] separators, bool trimSplits = true, bool ignoreEmptyResults = true, bool preserveEscapeCharInQuotes = true)
{
if (source == null) return null;

var result = new List<string>();
var escapeFlag = false;
var quotesOpen = false;
var currentItem = new StringBuilder();

foreach (var currentChar in source)
{
if (escapeFlag)
{
currentItem.Append(currentChar);
escapeFlag = false;
continue;
}

if (separators.Contains(currentChar) && !quotesOpen)
{
var currentItemString = trimSplits ? currentItem.ToString().Trim() : currentItem.ToString();
currentItem.Clear();
if (string.IsNullOrEmpty(currentItemString) && ignoreEmptyResults) continue;
result.Add(currentItemString);
continue;
}

switch (currentChar)
{
default:
currentItem.Append(currentChar);
break;
case '\\':
if (quotesOpen && preserveEscapeCharInQuotes) currentItem.Append(currentChar);
escapeFlag = true;
break;
case '"':
currentItem.Append(currentChar);
quotesOpen = !quotesOpen;
break;
}
}

if (escapeFlag) currentItem.Append("\\");

var lastCurrentItemString = trimSplits ? currentItem.ToString().Trim() : currentItem.ToString();
if (!(string.IsNullOrEmpty(lastCurrentItemString) && ignoreEmptyResults)) result.Add(lastCurrentItemString);

return result.ToArray();
}
}
}

0 comments on commit 54e67d7

Please sign in to comment.