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

Add option to automatically delete VR plugins #10

Closed
Closed
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
108 changes: 108 additions & 0 deletions UEVR/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
using static UEVR.SharedMemory;
using System.Threading.Channels;
using System.Security.Principal;
using Path = System.IO.Path;

namespace UEVR {
class GameSettingEntry : INotifyPropertyChanged {
Expand Down Expand Up @@ -172,6 +173,10 @@ public partial class MainWindow : Window {

private ExecutableFilter m_executableFilter = new ExecutableFilter();
private string? m_commandLineAttachExe = null;
private string? m_commandLineAttachExePath = null;
private string? m_commandLineLaunchExe = null;
private string? m_commandLineLaunchArgs = null;
private int m_commandLineDelayInjection = 0;
private bool m_ignoreFutureVDWarnings = false;

[System.Runtime.InteropServices.DllImport("user32.dll")]
Expand All @@ -187,6 +192,30 @@ public MainWindow() {
foreach (string arg in args) {
if (arg.StartsWith("--attach=")) {
m_commandLineAttachExe = arg.Split('=')[1];

if(m_commandLineAttachExe.EndsWith(".exe")) {
m_commandLineAttachExePath = Path.GetDirectoryName(m_commandLineAttachExe);
m_commandLineAttachExe = Path.GetFileNameWithoutExtension(m_commandLineAttachExe);
}
continue;
}
if (arg.StartsWith("--launch=")) {
m_commandLineLaunchExe = arg.Substring(arg.IndexOf('=') + 1); // game exe URI may contain any characters including '='
continue;
}
if (arg.StartsWith("--launch_args=")) {
m_commandLineLaunchArgs = arg.Substring(arg.IndexOf('=') + 1); // space separated list of game exe arguments
continue;
}
if (arg.StartsWith("--delay=")) {
try {
m_commandLineDelayInjection = int.Parse(arg.Split('=')[1]);
}
catch {
m_commandLineDelayInjection = 0;
}

continue;
}
}
}
Expand Down Expand Up @@ -251,6 +280,8 @@ private void RestartAsAdminButton_Click(object sender, RoutedEventArgs e) {
}

private DateTime m_lastAutoInjectTime = DateTime.MinValue;
private bool m_launchExeDone = false;
private int? m_delayInjectionTimer = null;

private void Update_InjectStatus() {
if (m_connected) {
Expand All @@ -261,6 +292,55 @@ private void Update_InjectStatus() {
DateTime now = DateTime.Now;
TimeSpan oneSecond = TimeSpan.FromSeconds(1);

if (m_commandLineLaunchExe != null && !m_launchExeDone) {
if(m_commandLineAttachExePath != null && m_commandLineAttachExe != null) {
if(!IsUnrealEngineGame(m_commandLineAttachExePath, m_commandLineAttachExe)) {
var result = MessageBox.Show(m_lastSelectedProcessName + " does not appear to be an Unreal Engine title.\n" +
"Do you want to proceed?",
"Non UE game",
MessageBoxButton.YesNo, MessageBoxImage.Warning);
switch (result) {
case MessageBoxResult.Yes:
break;
case MessageBoxResult.No:
Application.Current.Dispatcher.Invoke(new Action(() => { Application.Current.Shutdown(1); }));
break;
};
}

var pluginsDir = AreVRPluginsPresent(m_commandLineAttachExePath);
if(pluginsDir != null) {
var result = MessageBox.Show("VR plugins have been detected in game directory.\n" +
"Do you want to automatically remove them?\n" +
"NOTE: for a handful of games deleting VR plugins will lead to game crashes",
"VR Plugins detected",
MessageBoxButton.YesNo, MessageBoxImage.Question);

switch (result) {
case MessageBoxResult.Yes:
RemoveVRPlugins(pluginsDir);
break;
case MessageBoxResult.No:
break;
};
}
}

try {
Process.Start(new ProcessStartInfo {
FileName = m_commandLineLaunchExe,
UseShellExecute = true, // for launcher compatiblity, executable might be an URI
Arguments = m_commandLineLaunchArgs
});

m_launchExeDone = true;
}
catch (Exception) {
MessageBox.Show("Failed to launch: " + m_commandLineLaunchExe, "Launch error", MessageBoxButton.OK, MessageBoxImage.Error);
Application.Current.Dispatcher.Invoke(new Action(() => { Application.Current.Shutdown(1); }));
}
}

if (m_commandLineAttachExe == null) {
if (m_lastSelectedProcessId == 0) {
m_injectButton.Content = "Inject";
Expand Down Expand Up @@ -313,6 +393,16 @@ private void Update_InjectStatus() {
return;
}

if (m_commandLineDelayInjection > 0) {
if (m_delayInjectionTimer == null) {
m_delayInjectionTimer = m_commandLineDelayInjection;
}

m_injectButton.Content = m_commandLineAttachExe.ToLower() + " found. Delaying " + m_delayInjectionTimer + "s";
m_delayInjectionTimer--;
if (m_delayInjectionTimer > 0) return;
}

if (now - m_lastAutoInjectTime > oneSecond) {
if (m_nullifyVRPluginsCheckbox.IsChecked == true) {
IntPtr nullifierBase;
Expand Down Expand Up @@ -355,6 +445,8 @@ private void Update_InjectStatus() {

m_lastAutoInjectTime = now;
m_commandLineAttachExe = null; // no need anymore.
m_delayInjectionTimer = null;
m_commandLineDelayInjection = 0;
FillProcessList();
if (m_focusGameOnInjectionCheckbox.IsChecked == true)
{
Expand Down Expand Up @@ -637,6 +729,22 @@ private void MainWindow_Closing(object sender, System.ComponentModel.CancelEvent
return null;
}

private void RemoveVRPlugins(string? pluginsPath) {
if(pluginsPath == null) return;

foreach (string discouragedPlugin in m_discouragedPlugins) {
string pluginPath = pluginsPath + "\\" + discouragedPlugin;

if (Directory.Exists(pluginPath)) {
try {
Directory.Delete(pluginPath, true);
} catch (Exception) {
MessageBox.Show("Failed to delete:" + pluginPath);
}
}
}
}

private bool IsUnrealEngineGame(string gameDirectory, string targetName) {
try {
if (targetName.ToLower().EndsWith("-win64-shipping")) {
Expand Down
Loading