Skip to content

Commit

Permalink
Fixed a bug that prevented the self terminate from functioning proper…
Browse files Browse the repository at this point in the history
…ly (#50)
  • Loading branch information
Nonary committed Jan 15, 2024
1 parent da21d9b commit 9ef2409
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
39 changes: 20 additions & 19 deletions ResolutionMatcher-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,33 @@ function Assert-ResolutionChange($width, $height, $refresh) {

function Join-Overrides($width, $height, $refresh) {
$overrides = Get-Content ".\overrides.txt" -ErrorAction SilentlyContinue

foreach ($line in $overrides) {
$overrides = $line | Select-String "(?<width>\d{1,})x(?<height>\d*)x?(?<refresh>\d*)?" -AllMatches

$heights = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'height' }
$widths = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'width' }
$refreshes = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'refresh' }

if ($widths[0].Value -eq $width -and $heights[0].Value -eq $height -and $refreshes[0].Value -eq $refresh) {
$width = $widths[1].Value
$height = $heights[1].Value
$refresh = $refreshes[1].Value
break
if ($null -ne $line -and "" -ne $line) {
$overrides = $line | Select-String "(?<width>\d{1,})x(?<height>\d*)x?(?<refresh>\d*)?" -AllMatches

$heights = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'height' }
$widths = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'width' }
$refreshes = $overrides[0].Matches.Groups | Where-Object { $_.Name -eq 'refresh' }

if ($widths[0].Value -eq $width -and $heights[0].Value -eq $height -and $refreshes[0].Value -eq $refresh) {
$width = $widths[1].Value
$height = $heights[1].Value
$refresh = $refreshes[1].Value
break
}
}

return @{
height = $height
width = $width
refresh = $refresh
}
}

return @{
height = $height
width = $width
refresh = $refresh
}
}



function UserIsStreaming() {
function IsCurrentlyStreaming() {
return $null -ne (Get-NetUDPEndpoint -OwningProcess (Get-Process sunshine).Id -ErrorAction Ignore)
}

Expand Down
41 changes: 23 additions & 18 deletions ResolutionMatcher.ps1
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
param($async)

$path = (Split-Path $MyInvocation.MyCommand.Path -Parent)
Set-Location $path
$settings = Get-Content -Path .\settings.json | ConvertFrom-Json

# Since pre-commands in sunshine are synchronous, we'll launch this script again in another powershell process
if ($null -eq $async) {
Start-Process powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File `"$($MyInvocation.MyCommand.Path)`" $($MyInvocation.MyCommand.UnboundArguments) -async $true" -WindowStyle Hidden
exit
}

Set-Location (Split-Path $MyInvocation.MyCommand.Path -Parent)
Start-Transcript -Path .\log.txt
. .\ResolutionMatcher-Functions.ps1
$hostResolutions = Get-HostResolution
$lock = $false
Start-Transcript -Path .\log.txt



$mutexName = "ResolutionMatcher"
Expand All @@ -27,28 +29,32 @@ try {

# Asynchronously start the ResolutionMatcher, so we can use a named pipe to terminate it.
Start-Job -Name ResolutionMatcherJob -ScriptBlock {
. .\ResolutionMatcher-Functions.ps1
param($path, $revertDelay)
. $path\ResolutionMatcher-Functions.ps1
$lastStreamed = Get-Date


Register-EngineEvent -SourceIdentifier ResolutionMatcher -Forward
New-Event -SourceIdentifier ResolutionMatcher -MessageData "Start"
while ($true) {
if ((IsCurrentlyStreaming)) {
$lastStreamed = Get-Date
}
else {
if (((Get-Date) - $lastStreamed).TotalSeconds -gt 120) {
Write-Output "Ending the stream script"
New-Event -SourceIdentifier ResolutionMatcher -MessageData "End"
break;
try {
if ((IsCurrentlyStreaming)) {
$lastStreamed = Get-Date
}
else {
if (((Get-Date) - $lastStreamed).TotalSeconds -gt $revertDelay) {
New-Event -SourceIdentifier ResolutionMatcher -MessageData "End"
break;
}

}

}
Start-Sleep -Seconds 1
finally {
Start-Sleep -Seconds 1
}
}

}
} -ArgumentList $path, $settings.revertDelay


# To allow other powershell scripts to communicate to this one.
Expand Down Expand Up @@ -88,15 +94,14 @@ try {
OnStreamEnd $hostResolutions
break;
}
Remove-Event -SourceIdentifier ResolutionMatcher
Remove-Event -EventIdentifier $eventFired.EventIdentifier
}
elseif ($pipeJob.State -eq "Completed") {
Write-Host "Request to terminate has been processed, script will now revert resolution."
OnStreamEnd $hostResolutions
Remove-Job $pipeJob
break;
}
elseif ($eventMessageCount -gt 59) {
elseif($eventMessageCount -gt 59) {
Write-Host "Still waiting for the next event to fire..."
$eventMessageCount = 0
}
Expand Down

0 comments on commit 9ef2409

Please sign in to comment.