-
Notifications
You must be signed in to change notification settings - Fork 2
/
PlayniteWatcher-EndScript.ps1
65 lines (54 loc) · 2.34 KB
/
PlayniteWatcher-EndScript.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
param($terminate)
$path = Split-Path -Path $MyInvocation.MyCommand.Path -Parent
function Send-PipeMessage($pipeName, $message) {
$pipeExists = Get-ChildItem -Path "\\.\pipe\" | Where-Object { $_.Name -eq $pipeName }
if ($pipeExists.Length -gt 0) {
$pipe = New-Object System.IO.Pipes.NamedPipeClientStream(".", $pipeName, [System.IO.Pipes.PipeDirection]::Out)
# Give up after 5 seconds, if we can't connect by then it's probably stalled.
$pipe.Connect(5)
$streamWriter = New-Object System.IO.StreamWriter($pipe)
$streamWriter.WriteLine($message)
try {
$streamWriter.Flush()
$streamWriter.Dispose()
$pipe.Dispose()
}
catch {
# We don't care if the disposal fails, this is common with async pipes.
# Also, this powershell script will terminate anyway.
}
}
}
function TerminatePipes {
Send-PipeMessage -pipeName "PlayniteWatcher-OnStreamStart" -message "Terminate"
Send-PipeMessage -pipeName "PlayniteWatcher" -message "Terminate"
}
function CloseLaunchedGame() {
$matchesFound = Get-Content -Path "$path\log.txt" | Select-String "(?<=Received GamePath:\s)(?<path>.*)"
if ($null -ne $matchesFound) {
[string]$gamePath = ($matchesFound.Matches | Select-Object -Last 1).Value
$executables = Get-ChildItem -Path $gamePath -Filter *.exe -Recurse
Write-Host "Found the following executables in game directory: $executables"
foreach ($executable in $executables) {
$process = Get-Process -Name $executable.Name.Split('.')[0] -ErrorAction SilentlyContinue
if ($null -ne $process) {
Write-Host "Stopping the following processes, since it is still open and Sunshine stream has ended: $($process.Name)"
$process | Stop-Process
}
}
}
}
function CloseDesktopGracefully() {
# Give Playnite enough time to save playtime statistics
Start-Sleep -Seconds 6
$matchesFound = Get-Content -Path "$path\log.txt" | Select-String "(?<=Launching PlayNite Fullscreen:\s)(?<path>.*)"
if ($null -ne $matchesFound) {
$desktopPath = $matchesFound.Matches[0].Value
Start-Process $desktopPath -ArgumentList "--shutdown"
}
}
if ($terminate) {
TerminatePipes
CloseLaunchedGame
CloseDesktopGracefully
}