-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp.ipc.ps1
More file actions
executable file
·62 lines (52 loc) · 1.97 KB
/
Copy pathmp.ipc.ps1
File metadata and controls
executable file
·62 lines (52 loc) · 1.97 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
#Requires -Version 7.2
param (
[String] $CommandMessage = '{ "command": ["get_property", "playback-time"] }',
[String] $MpvPipeName = 'ipc_mpv'
)
if ([String]::IsNullOrEmpty($CommandMessage)) {
Write-Host '[mp.ipc] Usage: mp.ipc.ps1 [-CommandMessage] <mpv IPC Command Message> [[-MpvPipeName] <mpv IPC Pipe Name>]'
exit 0
}
if ($null -eq ([System.IO.Directory]::GetFiles('\\.\\pipe\\') | Where-Object {$_ -match $MpvPipeName})) {
Write-Host '[mp.ipc] Error: Named pipe not found, make sure that an mpv instance with IPC enabled is running!'
exit 1
}
#### Processing of the Command Message
# Remove all line breaks in the command message
$RelayMessage = $CommandMessage -replace "`n|`r"
# According to https://github.com/mpv-player/mpv/blob/master/DOCS/man/ipc.rst, every message must be terminated with \n
$RelayMessage = [String]::Concat($RelayMessage.Trim(), "`n")
if ($RelayMessage.StartsWith('{')) {
$ExpectReply = $true
} else {
$ExpectReply = $false
}
#### Init()
$PipeDirectiOpt = [System.IO.Pipes.PipeDirection]::InOut
$PipeOptionsOpt = [System.IO.Pipes.PipeOptions]::Asynchronous
$PipeImpersonlOpt = [System.Security.Principal.TokenImpersonationLevel]::Impersonation
$PipeClient = [System.IO.Pipes.NamedPipeClientStream]::new('.', $MpvPipeName, $PipeDirectiOpt, $PipeOptionsOpt, $PipeImpersonlOpt)
$PipeReader = [System.IO.StreamReader]::new($PipeClient)
$PipeWriter = [System.IO.StreamWriter]::new($PipeClient)
#### Main()
try {
$PipeClient.Connect()
$PipeWriter.AutoFlush = $true
$PipeWriter.WriteLine($RelayMessage)
if ($ExpectReply) {
[cultureinfo]::CurrentCulture = [cultureinfo]::InvariantCulture
$IncomingData = $PipeReader.ReadLine()
$myJson = $IncomingData | ConvertFrom-Json
Write-Output $myJson.data
}
}
catch {
Write-Host '[mp.ipc] Unhandled exception occured!' -ForegroundColor Red
Write-Host $_ -ForegroundColor DarkRed
Write-Host $_.ScriptStackTrace -ForegroundColor DarkRed
}
finally {
$PipeWriter.Dispose()
$PipeReader.Dispose()
$PipeClient.Dispose()
}