-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLauncher.ps1
More file actions
149 lines (142 loc) · 5.25 KB
/
Launcher.ps1
File metadata and controls
149 lines (142 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Steam Launcher changing default audio to Aux (Game) input
# temporarily during game launch and then back to normal default
# Mostly needed for Unreal Engine games (Rocket League, Fall Guys, etc.)
# Added a decent bit of error checking, but not exhaustive
# [Set,Get]-AudioDevice needs to be installed
# https://github.com/frgnca/AudioDeviceCmdlets
$script:DefaultVariables = $(Get-Variable).Name
$script:GameStarted = $false
$script:SteamDialog = $false
$script:SteamError = $false
$script:GameAlreadyRunning = $false
$script:GameReturnCode = $null
$script:GameRetries = 2
$script:GameRetryCount = $script:GameRetries
$script:ProcessName = "rocketleague"
$script:GameName = "Rocket League"
$script:SteamAppID = "252950"
function Clear-ScriptVariables
{
((Compare-Object -ReferenceObject (Get-Variable).Name -DifferenceObject $Script:DefaultVariables).InputObject) |
Where-Object {$_ -ne "_" -and $_ -ne "PSItem"} |
ForEach-Object {
Remove-Variable -Name $_ -Scope Script -ErrorAction SilentlyContinue
}
}
function Set-NewPlaybackDevice
{
$script:DefaultPlayback = Get-AudioDevice -Playback
Write-Output "Getting Audio Devices and setting default to VM Aux Input"
(Get-AudioDevice -list |Where-Object {$_.Name -match "VoiceMeeter Aux Input"} |Set-AudioDevice).Name
}
function Set-PreviousPlaybackDevice
{
Write-Output "Setting default audio device back to normal default."
($script:DefaultPlayback | Set-AudioDevice).Name
}
function Test-Game{
$try = 0
if (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue)
{
while ($try -lt 3 -and (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
$try++
Write-Warning "$script:GameName already running, waiting 10 seconds (Attempt $try of 3)"
Start-Sleep -Seconds 20
}
if ($try -ge 3)
{
$script:GameAlreadyRunning = $true
Write-Error "$script:GameName never closed, cannot start a new instance"
Throw "$script:GameName never closed, cannot start a new instance."
}
}
}
function Start-Game
{
$try = 0
# run if when game isn't started yet
if (! (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
Write-Output "Launching $script:GameName via Steam Launcher"
&"C:\Program Files (x86)\Steam\steam.exe" -applaunch $script:SteamAppID
}
# If game is current running, wait for it to close (try 3 times, ~30 seconds total)
else
{
while ($try -lt 3 -and (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
$try++
Start-Sleep -Seconds 10
if (! (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
Write-Output "Trying to Launch $script:GameName via Steam Launcher... re-attempt #$try"
&"C:\Program Files (x86)\Steam\steam.exe" -applaunch $script:SteamAppID
}
}
}
}
function Test-GameAge
{
if (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue)
{
Write-Output "$script:GameName process detected, waiting for process to reach the age of 20 seconds"
while ( ((Get-Date) - (Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue).StartTime) -lt (New-Timespan -Seconds 20) -or !(Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
Start-Sleep -Seconds 1
}
}
}
# End Functions
# Begin actual script
Try
{
Test-Game -ErrorAction Stop
# Set-NewPlaybackDevice
while ($script:GameRetryCount -gt 0 -and $script:GameStarted -eq $false -and $script:SteamDialog -eq $false -and $script:SteamError -eq $false)
{
Write-Output "GameRetryCount: $script:GameRetryCount"
Start-Game
$script:GameRetryCount--
Start-Sleep 10
$script:SteamDialog = [bool](Get-Process |Where-Object {$_.name -eq "steamwebhelper" -and $_.mainWindowTItle -eq "Steam Dialog"})
$script:SteamError = [bool](Get-Process |Where-Object {$_.name -eq "steam" -and $_.mainWindowTItle -eq "Steam - Error"})
$script:GameStarted = [bool](Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue)
if ($script:SteamDialog -eq $true -or $script:SteamError -eq $true)
{
Write-Warning "$script:GameName did not start, Check Steam Dialog window!"
pause
}
Test-GameAge
}
}
Catch
{
if ($script:GameAlreadyRunning -eq $false)
{
Write-Warning $PSItem.ToString()
}
}
Finally
{
if ($script:DefaultPlayback)
{
# Set-PreviousPlaybackDevice
}
elseif ($script:GameStarted -eq $false -and $script:GameAlreadyRunning -eq $false)
{
Write-Warning "$script:GameName did not start correctly after $($GameRetries+1) attempts"
pause
}
elseif ($script:GameAlreadyRunning -eq $true)
{
Write-Warning "$script:GameName was already running and did not exit in 30 seconds"
pause
}
if (!(Get-Process -Name $script:ProcessName -ErrorAction SilentlyContinue))
{
Write-Warning "$script:GameName started, but then exited prematurely. Check for issues."
pause
}
Clear-ScriptVariables
}