Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions Installer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,48 @@ if (-not $isAdmin) {
exit
}

function Test-AndRequest-SunshineConfig {
param(
[string]$InitialPath
)

# Check if the initial path exists
if (Test-Path $InitialPath) {
Write-Host "File found at: $InitialPath"
return $InitialPath
}
else {
# Show error message dialog
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Windows.Forms.MessageBox]::Show("Sunshine configuration could not be found. Please locate it.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) | Out-Null

# Open file dialog
$fileDialog = New-Object System.Windows.Forms.OpenFileDialog
$fileDialog.Title = "Open sunshine.conf"
$fileDialog.Filter = "Configuration files (*.conf)|*.conf"
$fileDialog.InitialDirectory = [System.IO.Path]::GetDirectoryName($InitialPath)

if ($fileDialog.ShowDialog() -eq "OK") {
$selectedPath = $fileDialog.FileName
# Check if the selected path is valid
if (Test-Path $selectedPath) {
Write-Host "File selected: $selectedPath"
return $selectedPath
}
else {
Write-Error "Invalid file path selected."
}

}
else {
Write-Error "Sunshine Configuiration file dialog was canceled or no valid file was selected."
exit 1
}
}
}

# Define the path to the Sunshine configuration file
$confPath = "C:\Program Files\Sunshine\config\sunshine.conf"
$confPath = Test-AndRequest-SunshineConfig -InitialPath "C:\Program Files\Sunshine\config\sunshine.conf"
$scriptRoot = Split-Path $scriptPath -Parent


Expand Down Expand Up @@ -165,9 +205,10 @@ else {
$commands = Remove-ResolutionMatcherCommand
}


Set-GlobalPrepCommand $commands

$sunshineService = Get-Service -ErrorAction Ignore | Where-Object {$_.Name -eq 'sunshinesvc' -or $_.Name -eq 'SunshineService'}
$sunshineService = Get-Service -ErrorAction Ignore | Where-Object { $_.Name -eq 'sunshinesvc' -or $_.Name -eq 'SunshineService' }
# In order for the commands to apply we have to restart the service
$sunshineService | Restart-Service -WarningAction SilentlyContinue
Write-Host "If you didn't see any errors, that means the script installed without issues! You can close this window."
Expand Down
32 changes: 24 additions & 8 deletions ResolutionMatcher-Functions.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Function Set-ScreenResolution($width, $height, $frequency) {
Write-Host "Resolution changed successfully."
}
else {
throw "Failed to change resolution. Error code: $result"
Write-Host "Failed to change resolution. Error code: $result"
}
break
}
Expand All @@ -41,12 +41,27 @@ function Get-HostResolution {

while ([DisplaySettings]::EnumDisplaySettings([NullString]::Value, $modeNum, [ref]$devMode)) {
return @{
CurrentHorizontalResolution = $devMode.dmPelsWidth
CurrentVerticalResolution = $devMode.dmPelsHeight
CurrentRefreshRate = $devMode.dmDisplayFrequency
Width = $devMode.dmPelsWidth
Height = $devMode.dmPelsHeight
Refresh = $devMode.dmDisplayFrequency
}
}
}
function Assert-ResolutionChange($width, $height, $refresh) {
# Attempt to set the resolution up to 6 times, in event of failures
for ($i = 0; $i -lt 12; $i++) {
$hostResolution = Get-HostResolution
$refreshDiff = [Math]::Abs($hostResolution.Refresh - $refresh)
if (($width -ne $hostResolution.Width) -or ($height -ne $hostResolution.Height) -or ($refreshDiff -ge 2)) {
# If the resolutions don't match, set the screen resolution to the current client's resolution
Write-Host "Current Resolution: $($hostResolution.Width) x $($hostResolution.Height) x $($hostResolution.Refresh)"
Write-Host "Expected Requested Resolution: $width x $height x $refresh"
Set-ScreenResolution $width $height $refresh
}
# Wait for a while before checking the resolution again
Start-Sleep -Milliseconds 500
}
}


function Join-Overrides($width, $height, $refresh) {
Expand Down Expand Up @@ -106,18 +121,19 @@ function Stop-ResolutionMatcherScript() {
function OnStreamStart($width, $height, $refresh) {
$expectedRes = Join-Overrides -width $width -height $height -refresh $refresh
Set-ScreenResolution -Width $expectedRes.Width -Height $expectedRes.Height -Freq $expectedRes.Refresh
Assert-ResolutionChange -width $expectedRes.Width -height $expectedRes.Height -refresh $expectedRes.Refresh
}

function OnStreamEnd($hostResolution) {

if (($host_resolution_override.Values | Measure-Object -Sum).Sum -gt 1000) {
$hostResolution = @{
CurrentHorizontalResolution = $host_resolution_override['Width']
CurrentVerticalResolution = $host_resolution_override['Height']
CurrentRefreshRate = $host_resolution_override['Refresh']
Width = $host_resolution_override['Width']
Height = $host_resolution_override['Height']
Refresh = $host_resolution_override['Refresh']
}
}
Set-ScreenResolution -Width $hostResolution.CurrentHorizontalResolution -Height $hostResolution.CurrentVerticalResolution -Freq $hostResolution.CurrentRefreshRate
Set-ScreenResolution -Width $hostResolution.Width -Height $hostResolution.Height -Freq $hostResolution.Refresh
}


Expand Down