config-central/tools/Export-MpcHcSettings.ps1

84 lines
4.1 KiB
PowerShell

[CmdletBinding()]
param(
[string]$OutputPath = (Join-Path $PSScriptRoot '..\configs\mpc-hc\settings.json'),
[switch]$IncludePrivateValues,
[switch]$ThrowOnError
)
# Kept identical in standalone entrypoints; verified by Output.Tests.ps1.
function Write-ConfigCentralMessage {
param([string]$Message, [ValidateSet('Info', 'Success', 'Warning', 'Error')][string]$Level = 'Info')
$color = switch ($Level) {
'Success' { 'Green' }
'Warning' { 'Yellow' }
'Error' { 'Red' }
default { 'Cyan' }
}
$redirected = if ($Level -eq 'Error') { [Console]::IsErrorRedirected } else { [Console]::IsOutputRedirected }
$useColor = -not $redirected -and $null -eq [Environment]::GetEnvironmentVariable('NO_COLOR')
if ($Level -eq 'Error') {
$previous = $null
try {
if ($useColor) { $previous = [Console]::ForegroundColor; [Console]::ForegroundColor = $color }
[Console]::Error.WriteLine($Message)
}
finally { if ($null -ne $previous) { [Console]::ForegroundColor = $previous } }
}
elseif ($useColor) { Write-Host $Message -ForegroundColor $color }
else { Write-Host $Message }
}
try {
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'. Launch MPC-HC, save its settings in Windows registry mode, close both MPC-HC versions, then retry capture." }
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-ConfigCentralMessage -Level Success -Message "Exported $($values.Count) MPC-HC values to '$fullOutputPath'."
Write-ConfigCentralMessage -Level Warning -Message 'Review the file for machine-specific paths or private history before committing it.'
$global:LASTEXITCODE = 0
}
catch {
if ($ThrowOnError) { throw }
Write-ConfigCentralMessage -Level Error -Message "MPC-HC capture could not complete: $($_.Exception.Message)"
Write-Host 'Correct the reported issue and rerun the command. Completed changes are kept.'
$code = if ($_.Exception.Data.Contains('ExitCode')) { [int]$_.Exception.Data['ExitCode'] } else { 2 }
if ($MyInvocation.MyCommand.PSObject.Properties['Path']) { exit $code }
$global:LASTEXITCODE = $code
return
}