From dd2bb7a4b7a36ee133223e9567f9dc6a8bb4e5aa Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 17 Oct 2026 10:07:55 +0200 Subject: [PATCH] Exclude private MPC-HC export values --- tests/ExportMpcHcSettings.Tests.ps1 | 25 +++++++++++++++++++++++++ tools/Export-MpcHcSettings.ps1 | 13 +++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 tests/ExportMpcHcSettings.Tests.ps1 diff --git a/tests/ExportMpcHcSettings.Tests.ps1 b/tests/ExportMpcHcSettings.Tests.ps1 new file mode 100644 index 0000000..42a12e2 --- /dev/null +++ b/tests/ExportMpcHcSettings.Tests.ps1 @@ -0,0 +1,25 @@ +BeforeAll { + $script:exporterPath = "$PSScriptRoot/../tools/Export-MpcHcSettings.ps1" + $tokens = $null + $errors = $null + $ast = [System.Management.Automation.Language.Parser]::ParseFile($exporterPath, [ref]$tokens, [ref]$errors) + if ($errors.Count -gt 0) { throw "MPC-HC exporter does not parse: $($errors[0].Message)" } + $functionAst = $ast.Find({ + param($node) + $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Test-PrivateMpcHcSetting' + }, $true) + . ([scriptblock]::Create($functionAst.Extent.Text)) +} + +Describe 'MPC-HC export filtering' { + It 'excludes history, device identities, paths, and URLs' { + Test-PrivateMpcHcSetting -RelativePath 'PlaylistHistory\item' -Name Position -Value 42 | Should -BeTrue + Test-PrivateMpcHcSetting -RelativePath Settings -Name AudioDevice -Value speakers | Should -BeTrue + Test-PrivateMpcHcSetting -RelativePath Settings -Name Logo -Value 'C:\Users\Alice\logo.png' | Should -BeTrue + Test-PrivateMpcHcSetting -RelativePath Settings -Name Server -Value 'https://example.test' | Should -BeTrue + } + + It 'keeps ordinary portable playback settings' { + Test-PrivateMpcHcSetting -RelativePath Settings -Name Volume -Value 80 | Should -BeFalse + } +} diff --git a/tools/Export-MpcHcSettings.ps1 b/tools/Export-MpcHcSettings.ps1 index df833f9..72a183f 100644 --- a/tools/Export-MpcHcSettings.ps1 +++ b/tools/Export-MpcHcSettings.ps1 @@ -10,20 +10,25 @@ $ErrorActionPreference = 'Stop' $rootPath = 'HKCU:\Software\MPC-HC\MPC-HC' if (-not (Test-Path -LiteralPath $rootPath)) { throw "MPC-HC registry settings were not found at '$rootPath'. Start MPC-HC and save its settings first." } +function Test-PrivateMpcHcSetting { + param([string]$RelativePath, [string]$Name, $Value) + if ($RelativePath -match '(?i)(^|\\)(MediaHistory|PlaylistHistory|Recent File List|Favorites)(\\|$)') { return $true } + if ($Name -match '(?i)(path|directory|folder|filename|recent|url|device|monitor|history|guid|uuid|^last)') { return $true } + $text = if ($Value -is [Array]) { @($Value) -join ' ' } else { [string]$Value } + return $text -match '(?i)([a-z]:[\\/]|https?://|@device:|^\\\\)' +} + $root = Get-Item -LiteralPath $rootPath -$excludedSubkeys = '(?i)(^|\\)(MediaHistory|Recent File List|Favorites)(\\|$)' -$excludedValues = '(?i)(path|directory|folder|filename|lastopen|lastused|recent|url)' $values = [Collections.Generic.List[object]]::new() foreach ($key in @($root) + @(Get-ChildItem -LiteralPath $rootPath -Recurse)) { $relativePath = $key.Name.Substring($root.Name.Length).TrimStart('\') - if (-not $IncludePrivateValues -and $relativePath -match $excludedSubkeys) { continue } foreach ($name in $key.GetValueNames()) { if ([string]::IsNullOrWhiteSpace($name)) { continue } - if (-not $IncludePrivateValues -and $name -match $excludedValues) { continue } $kind = [string]$key.GetValueKind($name) if ($kind -notin @('String', 'ExpandString', 'DWord', 'QWord', 'MultiString', 'Binary')) { continue } $raw = $key.GetValue($name, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) + if (-not $IncludePrivateValues -and (Test-PrivateMpcHcSetting -RelativePath $relativePath -Name $name -Value $raw)) { continue } $value = switch ($kind) { 'Binary' { @([byte[]]$raw) } 'MultiString' { @([string[]]$raw) }