-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathOnClientDisconnected.ps1
132 lines (117 loc) · 3.93 KB
/
OnClientDisconnected.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
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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.
# This is optionally used by the Signaling Server to reset the UE4 exe when a user disconnects
[CmdletBinding()]
Param (
[Parameter(Position=0, Mandatory=$true, HelpMessage = "3D Application name")]
[string] $unrealAppName,
[Parameter(Position=1, Mandatory=$true, HelpMessage = "The streaming port of the 3D App name we're trying to restart")]
[int] $streamingPort
)
try {
#Two UE4 processes are spun up so we close both of them here, and only restart the one that is the original parent .exe
$processes = Get-Process ($unrealAppName + "*")
write-host "Start - Unreal processes: " $processes.Count
$finalPath = ""
$finalArgs = ""
if($processes.Count -gt 0)
{
foreach($process in $processes)
{
$path = $process.Path
$procID = $process.Id
$cmdline = (Get-WMIObject Win32_Process -Filter "Handle=$procID").CommandLine
if($cmdline.Contains("-PixelStreamingPort="+$streamingPort))
{
if($cmdline -Match (" " + $unrealAppName + " "))
{
$processToKill = $process
}
#Only grab the original parent pixel streaming unreal app, not the child one, so we can restart it
if($cmdline -notmatch (" " + $unrealAppName + " "))
{
$finalPath = $path
$finalArgs = $cmdline.substring($cmdline.IndexOf("-AudioMixer"))
}
}
}
if($processToKill -ne $null)
{
write-host "Shutting down UE4 app: $processToKill.Path"
try
{
$processToKill | Stop-Process -Force
}
catch
{
Write-Host "ERROR:::An error occurred when stopping process: "
Write-Host $_
try
{
Start-Sleep -s 1
$processToKill.Kill()
$processToKill.WaitForExit(3000)
}
catch
{
Write-Host "ERROR:::An error occurred when killing process: "
Write-Host $_
}
}
finally
{
Write-Host "Process killed"
}
Start-Sleep -s 1
}
}
else
{
Write-Host $unrealAppName " not running when trying to restart"
}
try
{
Start-Sleep -s 5
$startProcess = $false
$newProcesses = Get-Process ($unrealAppName + "*")
Write-Host "After kill - Unreal processes: " $newProcesses.Count
if($newProcesses.Count -le 0)
{
$startProcess = $true
}
else
{
$startProcess = $true
foreach($process in $newProcesses)
{
$procID = $process.Id
$cmdline = (Get-WMIObject Win32_Process -Filter "Handle=$procID").CommandLine
if($cmdline.Contains("-PixelStreamingPort="+$streamingPort))
{
$startProcess = $false
break
}
}
}
if($startProcess -eq $true)
{
write-host "Starting Process - " $finalPath $finalArgs
#Start the final application if not already restarted
Start-Process -FilePath $finalPath -ArgumentList $finalArgs
}
Start-Sleep -s 5
$newProcesses = Get-Process ($unrealAppName + "*")
Write-Host "After restart - Unreal processes: " $newProcesses.Count
}
catch
{
Write-Host "ERROR:::An error occurred when starting the process: " $finalPath $finalArgs
Write-Host $_
}
}
catch
{
Write-Host "ERROR:::An error occurred:"
Write-Host $_
Write-Host $_.ScriptStackTrace
}