forked from SteamGridDB/steamgriddb-manager
-
Notifications
You must be signed in to change notification settings - Fork 3
/
LauncherAutoClose.ps1
77 lines (59 loc) · 2.06 KB
/
LauncherAutoClose.ps1
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
# This script is used to auto-close launchers that don't have an auto-close setting.
param (
[Parameter(Mandatory=$True)][string]$launchcmd, # Command to launch game
[Parameter(Mandatory=$True)][string]$launcher, # Launcher to kill
[Parameter(Mandatory=$True)][string[]]$game, # Game(s) to watch
[Parameter(Mandatory=$False)][bool]$bnet = $False, # Use Battle.net-specific launch method
[Parameter(Mandatory=$False)][string]$bnetpath, # Battle.net executable
[Parameter(Mandatory=$False)][string]$bnetlaunchid # Battle.net launch ID
)
$scriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
Start-Transcript -Append -Path "$scriptPath\LauncherAutoClose.log"
function Wait-ProcessChildren($id) {
Write-Host 'Wait-ProcessChildren'
$child = Get-WmiObject win32_process | where {$_.ParentProcessId -In $id}
Write-Host $child
if ($child) {
Write-Host 'Child found'
Wait-Process -Id $child.handle
Wait-ProcessChildren $child.handle
}
}
# Kill launcher
Write-Host 'Killing launcher'
Get-Process $launcher -ErrorAction SilentlyContinue | Stop-Process
# Start Game
If ($bnet) {
& "$scriptPath\BnetHelper.ps1" -bnet $bnetpath -launchid $bnetlaunchid
} Else {
Start-Process $launchcmd
}
$gameStarted = $False
Write-Host 'Waiting for game to start'
# Get current system date
$currentDate = Get-Date
Do {
$gameProcess = Get-Process $game -ErrorAction SilentlyContinue
If (!($gameProcess)) {
# Timeout after 30 minutes
If ($currentDate.AddMinutes(30) -lt (Get-Date)) {
Write-Host 'Game process could not be found'
exit
}
Start-Sleep -Seconds 1
} Else {
Write-Host 'Game started!'
$gameStarted = $true
}
} Until ($gameStarted)
# Wait until game closes
Wait-Process -InputObject $gameProcess
# Wait until child processes close
Wait-ProcessChildren $gameProcess.Id
Write-Host 'Game closed'
# Wait for cloud saves or whatever
Start-Sleep -Seconds 10
# Kill launcher
Write-Host 'Killing launcher'
Get-Process $launcher | Stop-Process
Stop-Transcript