mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-05-07 14:56:06 +02:00
Rework Startup Args (#946)
* Rework startup args * Preserve previous startup args on re-launch * Ignore subprocess when checking for duplicate processes * Cleanup code * Remove extra process list grabbing * Use `IsUpgradePrefix` when using `RestartApplication` * Change `ProxyServerPrefix` to `ProxyUrlPrefix`
This commit is contained in:
+21
-4
@@ -278,16 +278,33 @@ namespace VRCX
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Restarts the VRCX application for an update by launching a new process with the "/Upgrade" argument and exiting the current process.
|
/// Restarts the VRCX application for an update by launching a new process with the upgrade argument and exiting the current process.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public void RestartApplication()
|
public void RestartApplication()
|
||||||
{
|
{
|
||||||
|
List<string> args = [ StartupArgs.VrcxLaunchArguements.IsUpgradePrefix ];
|
||||||
|
|
||||||
|
if (StartupArgs.LaunchArguements.IsDebug == true)
|
||||||
|
{
|
||||||
|
args.Add(StartupArgs.VrcxLaunchArguements.IsDebugPrefix);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(StartupArgs.LaunchArguements.ConfigDirectory))
|
||||||
|
{
|
||||||
|
args.Add($"{StartupArgs.VrcxLaunchArguements.ConfigDirectoryPrefix}={StartupArgs.LaunchArguements.ConfigDirectory}");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(StartupArgs.LaunchArguements.ProxyUrl))
|
||||||
|
{
|
||||||
|
args.Add($"{StartupArgs.VrcxLaunchArguements.ProxyUrlPrefix}={StartupArgs.LaunchArguements.ProxyUrl}");
|
||||||
|
}
|
||||||
|
|
||||||
var vrcxProcess = new Process
|
var vrcxProcess = new Process
|
||||||
{
|
{
|
||||||
StartInfo = new ProcessStartInfo
|
StartInfo = new ProcessStartInfo
|
||||||
{
|
{
|
||||||
FileName = Path.Combine(Program.BaseDirectory, "VRCX.exe"),
|
FileName = Path.Combine(Program.BaseDirectory, "VRCX.exe"),
|
||||||
Arguments = "/Upgrade",
|
Arguments = string.Join(' ', args),
|
||||||
UseShellExecute = true,
|
UseShellExecute = true,
|
||||||
WorkingDirectory = Program.BaseDirectory
|
WorkingDirectory = Program.BaseDirectory
|
||||||
}
|
}
|
||||||
@@ -362,8 +379,8 @@ namespace VRCX
|
|||||||
/// <returns>The launch command.</returns>
|
/// <returns>The launch command.</returns>
|
||||||
public string GetLaunchCommand()
|
public string GetLaunchCommand()
|
||||||
{
|
{
|
||||||
var command = StartupArgs.LaunchCommand;
|
var command = StartupArgs.LaunchArguements.LaunchCommand;
|
||||||
StartupArgs.LaunchCommand = string.Empty;
|
StartupArgs.LaunchArguements.LaunchCommand = string.Empty;
|
||||||
return command;
|
return command;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+96
-35
@@ -4,13 +4,13 @@
|
|||||||
// This work is licensed under the terms of the MIT license.
|
// This work is licensed under the terms of the MIT license.
|
||||||
// For a copy, see <https://opensource.org/licenses/MIT>.
|
// For a copy, see <https://opensource.org/licenses/MIT>.
|
||||||
|
|
||||||
using CefSharp;
|
|
||||||
using CefSharp.Internals;
|
using CefSharp.Internals;
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Pipes;
|
using System.IO.Pipes;
|
||||||
using System.Net;
|
using System.Linq;
|
||||||
|
using System.Management;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
|
||||||
@@ -18,59 +18,120 @@ namespace VRCX
|
|||||||
{
|
{
|
||||||
internal class StartupArgs
|
internal class StartupArgs
|
||||||
{
|
{
|
||||||
public static string LaunchCommand;
|
public static VrcxLaunchArguements LaunchArguements = new();
|
||||||
public static string ProxyUrl;
|
|
||||||
public static Process[] processList;
|
|
||||||
|
|
||||||
public static void ArgsCheck()
|
public static void ArgsCheck()
|
||||||
{
|
{
|
||||||
var args = Environment.GetCommandLineArgs();
|
var args = Environment.GetCommandLineArgs();
|
||||||
processList = Process.GetProcessesByName("VRCX");
|
|
||||||
|
|
||||||
Debug.Assert(Program.LaunchDebug = true);
|
Debug.Assert(Program.LaunchDebug = true);
|
||||||
|
|
||||||
var disableClosing = false;
|
var currentProcessArgs = ParseArgs(args);
|
||||||
foreach (var arg in args)
|
LaunchArguements = currentProcessArgs;
|
||||||
{
|
|
||||||
if (arg == "/Upgrade")
|
|
||||||
disableClosing = true;
|
|
||||||
|
|
||||||
if (arg.Length > 12 && arg.Substring(0, 12) == "/uri=vrcx://")
|
|
||||||
LaunchCommand = arg.Substring(12);
|
|
||||||
|
|
||||||
if (arg.Length > 8 && arg.Substring(0, 8) == "--config")
|
if (LaunchArguements.IsDebug)
|
||||||
|
Program.LaunchDebug = true;
|
||||||
|
|
||||||
|
if (LaunchArguements.ConfigDirectory != null)
|
||||||
|
{
|
||||||
|
if (File.Exists(LaunchArguements.ConfigDirectory))
|
||||||
{
|
{
|
||||||
var filePath = arg.Substring(9);
|
MessageBox.Show("Move your \"VRCX.sqlite3\" into a folder then specify the folder in the launch parameter e.g.\n--config=\"C:\\VRCX\\\"", "--config is now a directory", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
if (File.Exists(filePath))
|
Environment.Exit(0);
|
||||||
{
|
|
||||||
MessageBox.Show("Move your \"VRCX.sqlite3\" into a folder then specify the folder in the launch parameter e.g.\n--config=\"C:\\VRCX\\\"", "--config is now a directory", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
Environment.Exit(0);
|
|
||||||
}
|
|
||||||
Program.AppDataDirectory = filePath;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg.Length >= 7 && arg.Substring(0, 7) == "--debug")
|
Program.AppDataDirectory = LaunchArguements.ConfigDirectory;
|
||||||
Program.LaunchDebug = true;
|
|
||||||
|
|
||||||
if (arg.Length >= 16 && arg.Substring(0, 14) == "--proxy-server")
|
|
||||||
ProxyUrl = arg.Substring(15).Replace("'", string.Empty).Replace("\"", string.Empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = CommandLineArgsParser.GetArgumentValue(args, CefSharpArguments.SubProcessTypeArgument);
|
var disableClosing = false;
|
||||||
if (!string.IsNullOrEmpty(type))
|
|
||||||
disableClosing = true; // we're launching a subprocess, allow it
|
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(Program.AppDataDirectory))
|
if (LaunchArguements.IsUpgrade || // we're upgrading, allow it
|
||||||
disableClosing = true; // we're launching with a custom config path, allow it
|
!string.IsNullOrEmpty(CommandLineArgsParser.GetArgumentValue(args, CefSharpArguments.SubProcessTypeArgument))) // we're launching a subprocess, allow it
|
||||||
|
disableClosing = true;
|
||||||
|
|
||||||
// if we're launching a second instance, focus the first instance then exit
|
// if we're launching a second instance with same config directory, focus the first instance then exit
|
||||||
if (!disableClosing && processList.Length > 1)
|
if (!disableClosing && IsDuplicateProcessRunning(LaunchArguements))
|
||||||
{
|
{
|
||||||
IPCToMain();
|
IPCToMain();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static VrcxLaunchArguements ParseArgs(string[] args)
|
||||||
|
{
|
||||||
|
VrcxLaunchArguements arguements = new VrcxLaunchArguements();
|
||||||
|
foreach (var arg in args)
|
||||||
|
{
|
||||||
|
if (arg == VrcxLaunchArguements.IsUpgradePrefix)
|
||||||
|
arguements.IsUpgrade = true;
|
||||||
|
|
||||||
|
if (arg.StartsWith(VrcxLaunchArguements.IsDebugPrefix))
|
||||||
|
arguements.IsDebug = true;
|
||||||
|
|
||||||
|
if (arg.StartsWith(VrcxLaunchArguements.LaunchCommandPrefix) && arg.Length > VrcxLaunchArguements.LaunchCommandPrefix.Length)
|
||||||
|
arguements.LaunchCommand = arg.Substring(VrcxLaunchArguements.LaunchCommandPrefix.Length);
|
||||||
|
|
||||||
|
if (arg.StartsWith(VrcxLaunchArguements.ConfigDirectoryPrefix) && arg.Length > VrcxLaunchArguements.ConfigDirectoryPrefix.Length)
|
||||||
|
arguements.ConfigDirectory = arg.Substring(VrcxLaunchArguements.ConfigDirectoryPrefix.Length + 1);
|
||||||
|
|
||||||
|
if (arg.StartsWith(VrcxLaunchArguements.ProxyUrlPrefix) && arg.Length > VrcxLaunchArguements.ProxyUrlPrefix.Length)
|
||||||
|
arguements.ProxyUrl = arg.Substring(VrcxLaunchArguements.ProxyUrlPrefix.Length + 1).Replace("'", string.Empty).Replace("\"", string.Empty);
|
||||||
|
}
|
||||||
|
return arguements;
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class VrcxLaunchArguements
|
||||||
|
{
|
||||||
|
public const string IsUpgradePrefix = "/Upgrade";
|
||||||
|
public bool IsUpgrade { get; set; } = false;
|
||||||
|
|
||||||
|
public const string IsDebugPrefix = "--debug";
|
||||||
|
public bool IsDebug { get; set; } = false;
|
||||||
|
|
||||||
|
public const string LaunchCommandPrefix = "/uri=vrcx://";
|
||||||
|
public string LaunchCommand { get; set; } = null;
|
||||||
|
|
||||||
|
public const string ConfigDirectoryPrefix = "--config";
|
||||||
|
public string ConfigDirectory { get; set; } = null;
|
||||||
|
|
||||||
|
public const string ProxyUrlPrefix = "--proxy-server";
|
||||||
|
public string ProxyUrl { get; set; } = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsDuplicateProcessRunning(VrcxLaunchArguements launchArguements)
|
||||||
|
{
|
||||||
|
var processes = Process.GetProcessesByName("VRCX");
|
||||||
|
foreach (var process in processes)
|
||||||
|
{
|
||||||
|
var commandLine = string.Empty;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var searcher = new ManagementObjectSearcher("SELECT CommandLine FROM Win32_Process WHERE ProcessId = " + process.Id))
|
||||||
|
{
|
||||||
|
using (var objects = searcher.Get())
|
||||||
|
{
|
||||||
|
commandLine = objects.Cast<ManagementBaseObject>().SingleOrDefault()?["CommandLine"]?.ToString() ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
|
||||||
|
if (commandLine.Contains(CefSharpArguments.SubProcessTypeArgument)) // ignore subprocesses
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var processArguements = ParseArgs(commandLine.Split(' '));
|
||||||
|
if (processArguements.ConfigDirectory == launchArguements.ConfigDirectory)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private static void IPCToMain()
|
private static void IPCToMain()
|
||||||
{
|
{
|
||||||
new IPCServer().CreateIPCServer();
|
new IPCServer().CreateIPCServer();
|
||||||
@@ -79,7 +140,7 @@ namespace VRCX
|
|||||||
|
|
||||||
if (ipcClient.IsConnected)
|
if (ipcClient.IsConnected)
|
||||||
{
|
{
|
||||||
var buffer = Encoding.UTF8.GetBytes($"{{\"type\":\"LaunchCommand\",\"command\":\"{LaunchCommand}\"}}" + (char)0x00);
|
var buffer = Encoding.UTF8.GetBytes($"{{\"type\":\"LaunchCommand\",\"command\":\"{LaunchArguements.LaunchCommand}\"}}" + (char)0x00);
|
||||||
ipcClient.BeginWrite(buffer, 0, buffer.Length, IPCClient.Close, ipcClient);
|
ipcClient.BeginWrite(buffer, 0, buffer.Length, IPCClient.Close, ipcClient);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -59,8 +59,8 @@ namespace VRCX
|
|||||||
|
|
||||||
private void SetProxy()
|
private void SetProxy()
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(StartupArgs.ProxyUrl))
|
if (!string.IsNullOrEmpty(StartupArgs.LaunchArguements.ProxyUrl))
|
||||||
ProxyUrl = StartupArgs.ProxyUrl;
|
ProxyUrl = StartupArgs.LaunchArguements.ProxyUrl;
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(ProxyUrl))
|
if (string.IsNullOrEmpty(ProxyUrl))
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -90,6 +90,7 @@
|
|||||||
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
|
<PackageReference Include="SharpDX.Mathematics" Version="4.2.0" />
|
||||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.119" />
|
||||||
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
|
<PackageReference Include="System.Drawing.Common" Version="8.0.10" />
|
||||||
|
<PackageReference Include="System.Management" Version="8.0.0" />
|
||||||
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
<PackageReference Include="System.Net.Http" Version="4.3.4" />
|
||||||
<PackageReference Include="System.Text.Json" Version="8.0.5" />
|
<PackageReference Include="System.Text.Json" Version="8.0.5" />
|
||||||
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
|
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
|
||||||
|
|||||||
Reference in New Issue
Block a user