diff --git a/TASKS.md b/TASKS.md index 6bdd9f5..d8b9a8c 100644 --- a/TASKS.md +++ b/TASKS.md @@ -143,3 +143,4 @@ open until it is run on disposable machines with the required account setups. - [x] Verify the shared MPC-HC x86/x64 registry target and block apply while either player is running to prevent settings being overwritten on exit. - [x] Hide Ontime clock seconds and slow background blink to a three-second fade with a steady reduced-motion fallback. - [x] Explain first-launch/registry-mode steps for missing MPC-HC capture settings and key-creation failures without suppressing the underlying error. +- [x] Continue MPC-HC registry writes after individual failures, warn with key names on partial success, and fail only when no values were written; preserve diagnostics and retry state. diff --git a/docs/desktop-app-configs.md b/docs/desktop-app-configs.md index 667b9bc..5771933 100644 --- a/docs/desktop-app-configs.md +++ b/docs/desktop-app-configs.md @@ -28,10 +28,15 @@ views; do not add a speculative Wow6432Node or "MPC-HC x64" target. See [MPC-HC's settings store](https://github.com/clsid2/mpc-hc/blob/develop/src/mpc-hc/Profile.cpp) and [Windows registry sharing rules](https://learn.microsoft.com/en-us/windows/win32/winprog64/shared-registry-keys). -Apply attempts to create missing registry keys automatically. If Windows rejects -key creation, launch MPC-HC once, save settings in Windows registry mode, close -it, and retry; the error also retains the Windows diagnostic. Capture cannot -export settings that do not exist and gives the same first-launch guidance. +Apply attempts to create missing registry keys and writes each declared value +independently. If some writes fail, the summary is a yellow warning listing only +the keys that could not be written; other values continue to apply. If every +write fails, the config fails. The existing running-player and backup checks +still apply. Full Windows errors are saved alongside the registry backup in a +`*.write-results.json` file. Partial deployments track the observed registry +state, so a later update retries the missing values instead of claiming that +all desired settings are current. Capture still explains how to initialize +settings when no registry profile exists. Close **both** `mpc-hc.exe` and `mpc-hc64.exe` before applying. The adapter refuses writes while either process is running, to prevent an open player from saving diff --git a/src/Adapters.psm1 b/src/Adapters.psm1 index 6c96ef2..b8c5fa5 100644 --- a/src/Adapters.psm1 +++ b/src/Adapters.psm1 @@ -196,23 +196,28 @@ function Set-RegistryDeclaration { if (Get-Process -Name 'mpc-hc', 'mpc-hc64' -ErrorAction SilentlyContinue) { throw 'Close both MPC-HC and MPC-HC x64 before applying settings, then rerun the update.' } + $written = 0 + $failures = [Collections.Generic.List[object]]::new() foreach ($entry in $Declaration.values) { - $path = if ([string]::IsNullOrWhiteSpace([string]$entry.path)) { $Root } else { Join-Path $Root $entry.path } - if (-not (Test-Path -LiteralPath $path)) { - try { [void](New-Item -Path $path -ErrorAction Stop) } - catch { - throw "Could not create MPC-HC settings key '$path'. Launch MPC-HC once, save its settings in Windows registry mode, then close both MPC-HC versions and retry. Check permissions if it still fails. Windows reported: $($_.Exception.Message)" + try { + $path = if ([string]::IsNullOrWhiteSpace([string]$entry.path)) { $Root } else { Join-Path $Root $entry.path } + if (-not (Test-Path -LiteralPath $path)) { [void](New-Item -Path $path -ErrorAction Stop) } + $value = switch ($entry.type) { + 'DWord' { if ($entry.value -lt 0) { [int32]$entry.value } else { [uint32]$entry.value } } + 'QWord' { if ($entry.value -lt 0) { [int64]$entry.value } else { [uint64]$entry.value } } + 'Binary' { [byte[]]@($entry.value) } + 'MultiString' { [string[]]@($entry.value) } + default { [string]$entry.value } } + [void](New-ItemProperty -LiteralPath $path -Name $entry.name -PropertyType $entry.type -Value $value -Force -ErrorAction Stop) + $written++ } - $value = switch ($entry.type) { - 'DWord' { if ($entry.value -lt 0) { [int32]$entry.value } else { [uint32]$entry.value } } - 'QWord' { if ($entry.value -lt 0) { [int64]$entry.value } else { [uint64]$entry.value } } - 'Binary' { [byte[]]@($entry.value) } - 'MultiString' { [string[]]@($entry.value) } - default { [string]$entry.value } + catch { + $failures.Add([pscustomobject]@{ path = $entry.path; name = $entry.name; error = $_.Exception.Message }) } - [void](New-ItemProperty -LiteralPath $path -Name $entry.name -PropertyType $entry.type -Value $value -Force) } + [pscustomobject]@{ written = $written; failures = @($failures) } + } function Set-HostsBlockContent { diff --git a/src/Configs.psm1 b/src/Configs.psm1 index 0cb0a36..485e946 100644 --- a/src/Configs.psm1 +++ b/src/Configs.psm1 @@ -235,6 +235,8 @@ function Invoke-ConfigOperation { } $backup = $null $temporary = $null + $registryWarning = $null + $deployedHash = $Operation.sourceHash try { if ($Operation.action -ne 'Adopt') { if ($Operation.mode -eq 'registry') { @@ -242,7 +244,15 @@ function Invoke-ConfigOperation { [void](New-Item -ItemType Directory -Path $folder -Force) $backup = Join-Path $folder "$([DateTimeOffset]::UtcNow.ToString('yyyyMMddTHHmmssfffZ')).json" (Get-RegistrySnapshot -Root $Operation.target -Declaration $Operation.desired).values | ConvertTo-Json -Depth 16 | Set-Content -LiteralPath $backup -Encoding utf8NoBOM - Set-RegistryDeclaration -Root $Operation.target -Declaration $Operation.desired + $writes = Set-RegistryDeclaration -Root $Operation.target -Declaration $Operation.desired + if ($writes.failures.Count -gt 0) { + # Keep detailed causes beside the backup; the terminal gets only key names. + Write-JsonAtomic -Path "$backup.write-results.json" -Value $writes + $failedKeys = @($writes.failures | ForEach-Object { if ($_.path) { "$($_.path)\$($_.name)" } else { $_.name } }) + $registryWarning = "Could not write registry key(s): $($failedKeys -join ', ')" + if ($writes.written -eq 0) { throw $registryWarning } + $deployedHash = (Get-RegistrySnapshot -Root $Operation.target -Declaration $Operation.desired).hash + } } else { $backup = Backup-ConfigTarget -Operation $Operation -Paths $Paths @@ -259,13 +269,14 @@ function Invoke-ConfigOperation { if (-not $State.Contains('configs')) { $State.configs = [ordered]@{} } $State.configs[$Operation.name] = [ordered]@{ target = $Operation.target - deployedHash = $Operation.sourceHash + deployedHash = $deployedHash sourceHash = $Operation.sourceHash mode = $Operation.mode deployedAt = [DateTimeOffset]::UtcNow.ToString('o') lastBackup = $backup } Write-JsonAtomic -Path $Paths.State -Value $State + if ($registryWarning) { return [pscustomobject]@{ name = $Operation.name; status = 'Warning'; message = $registryWarning } } return [pscustomobject]@{ name = $Operation.name; status = 'Succeeded'; message = if ($Operation.action -eq 'Adopt') { 'Adopted matching managed values' } elseif ($backup) { "Deployed; backup: $backup" } else { 'Deployed' } } } catch { diff --git a/tests/Adapters.Tests.ps1 b/tests/Adapters.Tests.ps1 index d3585dd..582e804 100644 --- a/tests/Adapters.Tests.ps1 +++ b/tests/Adapters.Tests.ps1 @@ -84,7 +84,7 @@ Describe 'Registry integer deployment' { $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Invalid'; type = 'DWord'; value = -2147483649 } ) } - { Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration } | Should -Throw + (Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration).failures.Count | Should -Be 1 Should -Invoke New-ItemProperty -Times 0 } } @@ -140,7 +140,7 @@ Describe 'MPC-HC running player protection' { } Describe 'Missing MPC-HC settings guidance' { - It 'explains first launch and retains the key-creation error' { + It 'collects key-creation errors without stopping the declaration' { InModuleScope Adapters { Mock Get-Process { $null } Mock Test-Path { $false } @@ -149,8 +149,28 @@ Describe 'Missing MPC-HC settings guidance' { $declaration = [pscustomobject]@{ values = @( [pscustomobject]@{ path = ''; name = 'Volume'; type = 'DWord'; value = 80 } ) } - { Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration } | Should -Throw '*Launch MPC-HC once*Access denied*' + $result = Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration + $result.written | Should -Be 0 + $result.failures[0].error | Should -Be 'Access denied' Should -Invoke New-ItemProperty -Times 0 } } } + +Describe 'Partial registry writes' { + It 'continues after HWAcceleration fails and writes subsequent keys' { + InModuleScope Adapters { + Mock Get-Process { $null } + Mock Test-Path { $true } + Mock New-ItemProperty { if ($Name -eq 'HWAcceleration') { throw 'Unsupported value' } } + $declaration = [pscustomobject]@{ values = @( + [pscustomobject]@{ path = ''; name = 'HWAcceleration'; type = 'DWord'; value = 1 }, + [pscustomobject]@{ path = ''; name = 'Volume'; type = 'DWord'; value = 80 } + ) } + $result = Set-RegistryDeclaration -Root 'TestRoot' -Declaration $declaration + $result.written | Should -Be 1 + $result.failures[0].name | Should -Be 'HWAcceleration' + Should -Invoke New-ItemProperty -Times 1 -ParameterFilter { $Name -eq 'Volume' } + } + } +} diff --git a/tests/Configs.Tests.ps1 b/tests/Configs.Tests.ps1 index 781bd38..4c6d344 100644 --- a/tests/Configs.Tests.ps1 +++ b/tests/Configs.Tests.ps1 @@ -131,6 +131,33 @@ Describe 'Configuration deployment' { $result.operations[0].message | Should -Match 'Launch MPC-HC.*close both MPC-HC versions' } + It 'reports partial registry writes as warnings and records actual deployed state' { + Mock Get-RegistrySnapshot { [pscustomobject]@{ values = @{ Volume = 80 }; hash = 'partial-state' } } -ModuleName Configs + Mock Set-RegistryDeclaration { + [pscustomobject]@{ written = 1; failures = @([pscustomobject]@{ path = ''; name = 'HWAcceleration'; error = 'Access denied' }) } + } -ModuleName Configs + $operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired-state'; desired = @{ values = @() } } + $result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state + $result.status | Should -Be 'Warning' + $result.message | Should -Be 'Could not write registry key(s): HWAcceleration' + $state.configs['mpc-hc-settings'].deployedHash | Should -Be 'partial-state' + $state.configs['mpc-hc-settings'].sourceHash | Should -Be 'desired-state' + $details = Get-Content ($state.configs['mpc-hc-settings'].lastBackup + '.write-results.json') -Raw | ConvertFrom-Json + $details.failures[0].error | Should -Be 'Access denied' + } + + It 'fails when no registry values could be written and leaves deployment state untouched' { + Mock Get-RegistrySnapshot { [pscustomobject]@{ values = @{}; hash = 'old-state' } } -ModuleName Configs + Mock Set-RegistryDeclaration { + [pscustomobject]@{ written = 0; failures = @([pscustomobject]@{ path = ''; name = 'HWAcceleration'; error = 'Access denied' }) } + } -ModuleName Configs + $operation = [pscustomobject]@{ name = 'mpc-hc-settings'; action = 'Update'; mode = 'registry'; target = 'TestRoot'; sourceHash = 'desired-state'; desired = @{ values = @() } } + $result = Invoke-ConfigOperation -Operation $operation -Paths $paths -State $state + $result.status | Should -Be 'Failed' + $result.message | Should -Be 'Could not write registry key(s): HWAcceleration' + $state.configs.ContainsKey('mpc-hc-settings') | Should -BeFalse + } + It 'refuses unsafe automatic restoration of registry and hosts backups' { $registryState = @{ configs = @{ 'mpc-hc-settings' = @{ mode = 'registry'; target = 'HKCU:\Software\MPC-HC\MPC-HC' } } } { Restore-ConfigBackup -Name mpc-hc-settings -Paths $paths -State $registryState } | Should -Throw '*not supported for registry*' diff --git a/tests/Core.Tests.ps1 b/tests/Core.Tests.ps1 index bf1b480..7b126dd 100644 --- a/tests/Core.Tests.ps1 +++ b/tests/Core.Tests.ps1 @@ -18,6 +18,19 @@ Describe 'Core workflow policy' { } } + It 'does not fail the run for a partial registry warning' { + InModuleScope Core -Parameters @{ Working = $TestDrive } { + Mock Invoke-ConfigOperation { [pscustomobject]@{ name = 'mpc-hc-settings'; status = 'Warning'; message = 'Could not write registry key(s): HWAcceleration' } } + $paths = Get-ConfigCentralPaths -DataRoot (Join-Path $Working 'warning-data') + $operation = [pscustomobject]@{ name = 'mpc-hc-settings'; requiresPackage = $null; requiresAdmin = $false; action = 'Update' } + $plan = [pscustomobject]@{ packages = @(); configs = @($operation); state = @{ schemaVersion = 1; configs = @{} } } + $result = Invoke-ApplyPlan -Plan $plan -Machine test -RepositoryRoot $Working -Paths $paths -Revision abc + $result.succeeded | Should -BeTrue + $result.exitCode | Should -Be 0 + $result.operations[0].status | Should -Be 'Warning' + } + } + It 'returns the stable reboot-required exit code after successful work' { InModuleScope Core -Parameters @{ Working = $TestDrive } { Mock Invoke-PackageOperation { [pscustomobject]@{ name = 'Reboot.App'; status = 'Succeeded'; rebootRequired = $true; message = 'Installed; reboot required' } }