Skip to content

Commit

Permalink
bugfix: fixed an issue that prevented the script from working on gfe
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonary committed Aug 11, 2023
1 parent a18d991 commit a94e4de
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 339 deletions.
2 changes: 1 addition & 1 deletion Install.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
powershell.exe -executionpolicy bypass -file ./Installer.ps1 True
powershell.exe -noexit -executionpolicy bypass -file "./InstallTask.ps1" true
42 changes: 42 additions & 0 deletions InstallTask.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
param($install, $start)

if($install -eq $true -or $install -eq "true"){
Write-Host "Installing Task"
$file_location = Get-Item ".\ResolutionMatcher.ps1"

$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-windowstyle hidden -executionpolicy bypass -file `"$($file_location.FullName)`"" -WorkingDirectory $file_location.Directory
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable -DontStopOnIdleEnd -ExecutionTimeLimit 0 -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 5
$trigger = New-ScheduledTaskTrigger -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 15) -Once

#Reinstall Task

$taskExists = Get-ScheduledTask -TaskName "Match GameStream Resolution" -ErrorAction Ignore
if($taskExists){
Write-Host "Existing task was found, deleting this task so it can be recreated again"
# If user moves folder where script is at, they will have to install again, so let's remove existing task if exists.
$taskExists | Unregister-ScheduledTask -Confirm:$false
}


$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings

Register-ScheduledTask -InputObject $task -TaskName "Match GameStream Resolution" | Out-Null

Write-Host "Task was installed sucessfully."
Start-ScheduledTask -TaskName "Match GameStream Resolution" | Out-Null

# We can't make a scheduled task start at logon without admin, so this is a workaround to that.
New-Item -Name "ResolutionMatcher.bat" -Value "powershell.exe -windowstyle hidden -executionpolicy bypass -command `"Start-ScheduledTask -TaskName 'Match GameStream Resolution' | Out-Null`"" -Path "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup" -Force | Out-Null

}


elseif($install -eq $false -or $install -eq "false") {
Write-Host "Uninstalling Task"
Get-ScheduledTask -TaskName "Match GameStream Resolution" | Stop-ScheduledTask
Get-ScheduledTask -TaskName "Match GameStream Resolution" | Unregister-ScheduledTask -Confirm:$false
Remove-Item -Path "$env:APPDATA\Microsoft\Windows\Start Menu\Programs\Startup\ResolutionMatcher.bat" -Force -Confirm:$false | Out-Null
Write-Host "Task was removed successfully."
}


174 changes: 0 additions & 174 deletions Installer.ps1

This file was deleted.

100 changes: 52 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,57 +1,61 @@
## Host Resolution Matching for Moonlight Streaming

This script changes your host resolution to match exactly with Moonlight's resolution. This is mostly used for users who have different aspect ratios between the client and host, or anyone who wishes to match the resolution while streaming.

### Requirements

#### For Sunshine Users
- Host must be Windows.
- Sunshine must be installed as a service (it does not work with the zip version of Sunshine).
- Sunshine logging level must be set to Debug.
- Users must have read permissions to `%WINDIR%/Temp/Sunshine.log` (do not change other permissions, just make sure Users has at least read permissions).

### Caveats
- If using Windows 11, you'll need to set the default terminal to Windows Console Host as there is currently a bug in Windows Terminal that prevents hidden consoles from working properly.
* That can be changed at Settings > Privacy & security > Security > For developers > Terminal [Let Windows decide] >> (change to) >> Terminal [Windows Console Host]
- Prepcommands do not work from cold reboots, and will prevent Sunshine from working until you logon locally.
* You should add a new application (with any name you'd like) in the WebUI and leave **both** the command and detached command empty.
* When adding this new application, make sure global prep command option is disabled.
* That will serve as a fallback option when you have to remote into your computer from a cold start.
* Normal reboots issued from start menu, will still work without the workaround above as long as Settings > Accounts > Sign-in options and "Use my sign-in info to automatically finish setting up after an update" is enabled which is default in Windows 10 & 11.
- The script will stop working if you move the folder, simply reinstall it to resolve that issue.

#### GFE Users
- You'll need to use the Geforce Experience version of this script instead.
- The current release for Geforce Experience users is: https://github.com/Nonary/ResolutionAutomation/releases/tag/2.0.14_gfe

### Installation Instructions
1. Store the downloaded folder in a location you intend to keep. If you delete this folder or move it, the automation will stop working.
2. To install, double click the Install.bat file.
3. To uninstall, double click the Uninstall.bat file.

This script will ask for elevated rights because in the coming future, Sunshine configuration will be locked from modifications for non-administrator users.

### How it Works
1. When you start streaming any application in Sunshine, it will start the script.
2. The script reads the `Sunshine.log` file to capture Moonlight's resolution.
3. It sets the host's resolution to match the Moonlight resolution (including refresh rate), unless overridden with the `overrides` file.
4. The script waits for Sunshine to be suspended for more than 120 seconds or until the user ends the stream.
5. It sets the host resolution back to the same resolution it was prior to starting the stream (including refresh rate).
## Note
This version is meant exclusively for GeForce Experience users, if you're using Sunshine please download the latest release by clicking the link below instead.

https://github.com/Nonary/ResolutionAutomation/releases/latest



# ELI5
Changes your host resolution to match exactly with Moonlight's resolution.

# Why
The biggest use case for matching resolution is for people with different aspect ratios on the client and host, such as Steam Deck and Widescreen Users.
If you do not match the resolution/aspect ratio, it will either squish or stretch the stream and or letterbox... or in some cases both!
This can also be used to supersample games on client device (such as streaming 1080p and keeping host at 4k).


# Requirements

## GFE Users
None, should work "out of the box"

# Install instructions
First, store this folder in a location you intend to keep. If you delete this folder or move it, the automation will stop working.

If you have to move the folder, move it, then run the installation script again.

To install, simply double click the Install Script.bat file.

To uninstall, simply double click the Uninstall Script.bat file.

If you get a SmartScreen warning, tell it to proceed anyway, this will only happen once.

# How it works

1. Waits for NVStreamer process to be launched if GFE, otherwise it waits for a connection from Sunshine.
2. Reads the NVStreamerCurrentLog.txt or Sunshine.log file to capture the hosts resolution and moonlights resolution.
3. Sets the hosts resolution to match the Moonlight resolution (including refresh rate).
4. Waits for NVStreamer Process to end or Sunshine connection to either suspend or terminate.
5. Sets the host resolution back to the same resolution it was prior to starting the stream (including refresh rate).


This will only work if the resolution is available to be used, so you will need to make sure to use NVIDIA Custom Resolution or CRU to add the client resolution first.

### Overrides File
You may have a mobile device that you wish to stream at a lower resolution to save bandwidth or some devices may perform better when streaming at a lower resolution. If you want your host to change the resolution to something higher than the client, use the `overrides` file to do this.
# Overrides File
You may have a mobile device that you wish to stream at a lower resolution to save bandwidth.

Or, some devices may perform better when streaming at a lower resolution.

If you want your host to change the resolution to something higher than the client, use the overrides file to do this.

#### Format
```
WidthxHeightxRefresh=WidthxHeightxRefresh
Format: WidthxHeightxRefresh=WidthxHeightxRefresh
```

Resolution on the left is what triggers the override, the one on the right is what the host will be set to.

The resolution on the left is what triggers the override, and the one on the right is what the host will be set to.

#### Example
To stream at 720p and keep the host at 4k resolution, you would add this line:
For example, to stream at 720p and keep the host at 4k resolution you would add this line:
```
1280x700x60=3840x2160x60
```
```

11 changes: 3 additions & 8 deletions ResolutionMatcher-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function Assert-ResolutionChange($originalRes) {
}

function Get-ClientResolution() {
$log_path = "$env:WINDIR\Temp\sunshine.log"
$log_path = "$env:PROGRAMDATA\NVIDIA Corporation\NvStream\NvStreamerCurrent.log"

# Initialize a hash table to store the client resolution values
$clientRes = @{
Expand All @@ -76,16 +76,11 @@ function Get-ClientResolution() {
}

# Define combined regular expressions to match the height, width, and refresh rate values in the log file
$regex = [regex] "a=x-nv-video\[0\]\.(clientViewportWd:(?<wd>\d+)|clientViewportHt:(?<ht>\d+)|maxFPS:(?<hz>\d+))"
$regex = [regex] "video\[0\]\.(clientViewportWd:\s(?<wd>\d+)|clientViewportHt:\s(?<ht>\d+)|maxFPS:\s(?<hz>\d+))"

$reader = New-Object ReverseFileReader -ArgumentList $log_path

while ($null -ne ($line = $reader.ReadLine())) {
# Skip to the next line if the line doesn't start with "a=x"
# This is a performance optimization, this will match much faster than regular expressions.
if (-not $line.StartsWith("a=x")) {
continue;
}

# Attempt to match the values in the line
$match = $regex.Match($line)
Expand Down Expand Up @@ -152,7 +147,7 @@ function Join-Overrides($resolution) {


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


Expand Down
Loading

0 comments on commit a94e4de

Please sign in to comment.