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

Allow integration with launchers #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions UEVR/GameAutoStartExplanationDialog.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Window x:Class="UEVR.GameAutoStartExplanationDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UEVR"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="13"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
FontFamily="{DynamicResource MaterialDesignFont}"
Background="#FF1A1B1C"
SizeToContent="WidthAndHeight" ResizeMode="NoResize" Icon="/UEVR2.png"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
Title="Game autostart" Height="200" Width="400">
<StackPanel>
<TextBlock Text="Game auto start attempt detected.&#x0a;You can configure autostart by toggling 'Autostart game' in settings" Margin="10" />
<CheckBox x:Name="chkRememberChoice" Content="Remember my choice" Margin="10" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="10">
<Button x:Name="btnStartGame" Content="Start game" Width="125" Margin="5" Click="btnStartGame_Click"/>
<Button x:Name="btnWaitForGameStart" Content="I'll start it myself" Width="140" Margin="5" Click="btnWaitForGameStart_Click"/>
</StackPanel>
</StackPanel>
</Window>
29 changes: 29 additions & 0 deletions UEVR/GameAutoStartExplanationDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace UEVR {
public partial class GameAutoStartExplanationDialog : Window {
public bool HideAutostartWarning { get; private set; }
public bool DialogResultStartGame { get; private set; }

public GameAutoStartExplanationDialog() {
InitializeComponent();
}

private void btnStartGame_Click(object sender, RoutedEventArgs e) {
HideAutostartWarning = chkRememberChoice.IsChecked == true;
DialogResultStartGame = true;
this.Close();
}

private void btnWaitForGameStart_Click(object sender, RoutedEventArgs e) {
HideAutostartWarning = chkRememberChoice.IsChecked == true;
DialogResultStartGame = false;
this.Close();
}
}
}
4 changes: 4 additions & 0 deletions UEVR/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@
<ToggleButton x:Name="m_focusGameOnInjectionCheckbox" HorizontalAlignment="Left" IsChecked="True"/>
<TextBlock TextWrapping="Wrap" Text="Focus game on injection"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<ToggleButton x:Name="m_gameAutoStartCheckbox" HorizontalAlignment="Left" IsChecked="True"/>
<TextBlock TextWrapping="Wrap" Text="Autostart game"/>
</StackPanel>
</StackPanel>
<TextBlock x:Name="m_connectionStatus" TextWrapping="Wrap" Text="Unknown status"/>
</StackPanel>
Expand Down
138 changes: 135 additions & 3 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,7 +173,12 @@ 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;
private bool m_gameAutostartExplanationShown = false;

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
Expand All @@ -187,6 +193,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 All @@ -209,6 +239,8 @@ private void MainWindow_Loaded(object sender, RoutedEventArgs e) {
m_nullifyVRPluginsCheckbox.IsChecked = m_mainWindowSettings.NullifyVRPluginsCheckbox;
m_ignoreFutureVDWarnings = m_mainWindowSettings.IgnoreFutureVDWarnings;
m_focusGameOnInjectionCheckbox.IsChecked = m_mainWindowSettings.FocusGameOnInjection;
m_gameAutoStartCheckbox.IsChecked = m_mainWindowSettings.GameAutoStart;
m_gameAutostartExplanationShown = m_mainWindowSettings.GameAutoStartExplanationShown;

m_updateTimer.Tick += (sender, e) => Dispatcher.Invoke(MainWindow_Update);
m_updateTimer.Start();
Expand Down Expand Up @@ -251,6 +283,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 +295,74 @@ private void Update_InjectStatus() {
DateTime now = DateTime.Now;
TimeSpan oneSecond = TimeSpan.FromSeconds(1);

if (m_commandLineLaunchExe != null && !m_launchExeDone) {
if(m_gameAutostartExplanationShown == false) {
var dialog = new GameAutoStartExplanationDialog();
dialog.ShowDialog();

m_gameAutostartExplanationShown = dialog.HideAutostartWarning;

if(m_gameAutostartExplanationShown) {
m_gameAutoStartCheckbox.IsChecked = dialog.DialogResultStartGame;
}

if(!dialog.DialogResultStartGame) {
m_launchExeDone = true;
return;
}
} else if(m_gameAutoStartCheckbox.IsChecked == false) {
m_launchExeDone = true;
return;
}

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 disable them?\n" +
"NOTE: for a handful of games disabling VR plugins will lead to game crashes",
"VR Plugins detected",
MessageBoxButton.YesNo, MessageBoxImage.Question);

switch (result) {
case MessageBoxResult.Yes:
RenameVRPlugins(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 +415,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 +467,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 @@ -571,13 +685,13 @@ private void Check_VirtualDesktop() {
}

private void MainWindow_Update() {
Update_InjectorConnectionStatus();
Update_InjectStatus();

if (m_virtualDesktopChecked == false) {
m_virtualDesktopChecked = true;
Check_VirtualDesktop();
}

Update_InjectorConnectionStatus();
Update_InjectStatus();
}

private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
Expand All @@ -586,6 +700,8 @@ private void MainWindow_Closing(object sender, System.ComponentModel.CancelEvent
m_mainWindowSettings.NullifyVRPluginsCheckbox = m_nullifyVRPluginsCheckbox.IsChecked == true;
m_mainWindowSettings.IgnoreFutureVDWarnings = m_ignoreFutureVDWarnings;
m_mainWindowSettings.FocusGameOnInjection = m_focusGameOnInjectionCheckbox.IsChecked == true;
m_mainWindowSettings.GameAutoStart = m_gameAutoStartCheckbox.IsChecked == true;
m_mainWindowSettings.GameAutoStartExplanationShown = m_gameAutostartExplanationShown;

m_mainWindowSettings.Save();
}
Expand Down Expand Up @@ -637,6 +753,22 @@ private void MainWindow_Closing(object sender, System.ComponentModel.CancelEvent
return null;
}

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

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

if (Directory.Exists(pluginPath)) {
try {
Directory.Move(pluginPath, pluginPath + "_bak");
} catch (Exception) {
MessageBox.Show("Failed to rename:" + pluginPath);
}
}
}
}

private bool IsUnrealEngineGame(string gameDirectory, string targetName) {
try {
if (targetName.ToLower().EndsWith("-win64-shipping")) {
Expand Down
14 changes: 14 additions & 0 deletions UEVR/MainWindowSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,19 @@ public bool FocusGameOnInjection
get { return (bool)this["FocusGameOnInjection"]; }
set { this["FocusGameOnInjection"] = value; }
}

[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("true")]
public bool GameAutoStart {
get { return (bool)this["GameAutoStart"]; }
set { this["GameAutoStart"] = value; }
}

[UserScopedSettingAttribute()]
[DefaultSettingValueAttribute("false")]
public bool GameAutoStartExplanationShown {
get { return (bool)this["GameAutoStartExplanationShown"]; }
set { this["GameAutoStartExplanationShown"] = value; }
}
}
}
Loading