Skip to content

Commit 5a3177b

Browse files
committed
Fix override code
1 parent aff976e commit 5a3177b

File tree

1 file changed

+94
-14
lines changed

1 file changed

+94
-14
lines changed

Events.ps1

Lines changed: 94 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,111 @@ function Assert-ResolutionChange($width, $height, $refresh) {
129129
}
130130

131131

132-
function Join-Overrides($width, $height, $refresh) {
133-
Write-Debug "Function Join-Overrides called with Width: $width, Height: $height, Refresh: $refresh"
132+
function Join-Overrides {
133+
param (
134+
[int]$Width,
135+
[int]$Height,
136+
[int]$Refresh
137+
)
134138

135-
Write-Host "Before Overriding: $width x $height x $refresh"
139+
Write-Debug "Function Join-Overrides called with Width: $Width, Height: $Height, Refresh: $Refresh"
140+
141+
Write-Host "Before Overriding: $Width x $Height x $Refresh"
142+
143+
# Initialize variables to hold the best matching override
144+
$matchedOverride = $null
145+
$matchedSpecificity = -1 # Higher value means more specific
136146

137147
foreach ($override in $settings.overrides) {
138-
Write-Debug "Checking override with client width: $($override.client.width), height: $($override.client.height), refresh: $($override.client.refresh)"
139-
if ($override.client.width -eq $width -and $override.client.height -eq $height -and $override.client.refresh -eq $refresh) {
140-
Write-Debug "Match found. Overriding with host width: $($override.host.width), height: $($override.host.height), refresh: $($override.host.refresh)"
141-
$width = $override.host.width
142-
$height = $override.host.height
143-
$refresh = $override.host.refresh
144-
break
148+
Write-Debug "Processing override: $override"
149+
150+
# Split the override into client and host parts
151+
$parts = $override -split '='
152+
if ($parts.Count -ne 2) {
153+
Write-Warning "Invalid override format (missing '='): $override"
154+
continue
155+
}
156+
157+
$clientPart = $parts[0].Trim()
158+
$hostPart = $parts[1].Trim()
159+
160+
# Function to parse a dimension string into a hashtable
161+
function Parse-Dimension {
162+
param (
163+
[string]$dimensionStr
164+
)
165+
$dimParts = $dimensionStr -split 'x'
166+
if ($dimParts.Count -lt 2 -or $dimParts.Count -gt 3) {
167+
return $null
168+
}
169+
170+
$parsed = @{
171+
Width = [int]$dimParts[0]
172+
Height = [int]$dimParts[1]
173+
Refresh = if ($dimParts.Count -eq 3) { [int]$dimParts[2] } else { $null }
174+
}
175+
return $parsed
176+
}
177+
178+
# Parse client and host dimensions
179+
$client = Parse-Dimension -dimensionStr $clientPart
180+
$host_res = Parse-Dimension -dimensionStr $hostPart
181+
182+
if (-not $client -or -not $host_res) {
183+
Write-Warning "Invalid dimension format in override: $override"
184+
continue
185+
}
186+
187+
Write-Debug "Parsed Client: Width=$($client.Width), Height=$($client.Height), Refresh=$($client.Refresh)"
188+
Write-Debug "Parsed Host: Width=$($host_res.Width), Height=$($host_res.Height), Refresh=$($host_res.Refresh)"
189+
190+
# Check if the client dimensions match the input dimensions
191+
if ($client.Width -eq $Width -and $client.Height -eq $Height) {
192+
# Determine specificity: 2 if both width/height and refresh match,
193+
# 1 if only width and height match, 0 otherwise
194+
$specificity = 1
195+
if ($null -ne $client.Refresh) {
196+
if ($client.Refresh -eq $Refresh) {
197+
$specificity = 2
198+
} else {
199+
# Refresh specified in override but does not match input
200+
continue
201+
}
202+
}
203+
204+
Write-Debug "Override specificity: $specificity"
205+
206+
# Select the override with the highest specificity
207+
if ($specificity -gt $matchedSpecificity) {
208+
$matchedOverride = $host_res
209+
$matchedSpecificity = $specificity
210+
Write-Debug "Selected override: $override with specificity $specificity"
211+
}
145212
}
146213
}
147214

148-
Write-Host "After Overriding: $width x $height x $refresh"
215+
# Apply the matched override if any
216+
if ($null -ne $matchedOverride) {
217+
Write-Debug "Applying override: Width=$($matchedOverride.Width), Height=$($matchedOverride.Height), Refresh=$($matchedOverride.Refresh)"
218+
$Width = $matchedOverride.Width
219+
$Height = $matchedOverride.Height
220+
if ($null -ne $matchedOverride.Refresh) {
221+
$Refresh = $matchedOverride.Refresh
222+
}
223+
} else {
224+
Write-Debug "No matching override found."
225+
}
226+
227+
Write-Host "After Overriding: $Width x $Height x $Refresh"
149228

150229
return @{
151-
Width = $width
152-
Height = $height
153-
Refresh = $refresh
230+
Width = $Width
231+
Height = $Height
232+
Refresh = $Refresh
154233
}
155234
}
156235

236+
157237
function Set-10bitCompatibilityIfApplicable($width, $height, $refresh) {
158238
Write-Debug "Function Set-10bitCompatibilityIfApplicable called with Width: $width, Height: $height, Refresh: $refresh"
159239
Write-Debug "SUNSHINE_CLIENT_HDR environment variable: $($env:SUNSHINE_CLIENT_HDR)"

0 commit comments

Comments
 (0)