This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
LauncherAutoClose.ps1
71 lines (55 loc) · 1.92 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
# 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
function Wait-ProcessChildren($id) {
$child = Get-WmiObject win32_process | where {$_.ParentProcessId -In $id}
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 5
# Kill launcher
Write-Host 'Killing launcher'
Get-Process $launcher | Stop-Process