config-central/tools/Export-MpcHcSettings.ps1

47 lines
2.4 KiB
PowerShell

[CmdletBinding()]
param(
[string]$OutputPath = (Join-Path $PSScriptRoot '..\configs\mpc-hc\settings.json'),
[switch]$IncludePrivateValues
)
Set-StrictMode -Version Latest
$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
$values = [Collections.Generic.List[object]]::new()
foreach ($key in @($root) + @(Get-ChildItem -LiteralPath $rootPath -Recurse)) {
$relativePath = $key.Name.Substring($root.Name.Length).TrimStart('\')
foreach ($name in $key.GetValueNames()) {
if ([string]::IsNullOrWhiteSpace($name)) { 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) }
default { $raw }
}
$values.Add([ordered]@{ path = $relativePath; name = $name; type = $kind; value = $value })
}
}
$document = [ordered]@{ schemaVersion = 1; values = @($values | Sort-Object path, name) }
$fullOutputPath = [IO.Path]::GetFullPath($OutputPath)
[void](New-Item -ItemType Directory -Path (Split-Path -Parent $fullOutputPath) -Force)
$document | ConvertTo-Json -Depth 16 | Set-Content -LiteralPath $fullOutputPath -Encoding utf8NoBOM
Write-Host "Exported $($values.Count) MPC-HC values to '$fullOutputPath'."
Write-Warning 'Review the file for machine-specific paths or private history before committing it.'