function saveConfig { [IO.File]::WriteAllLines($config_file, ($cfg | ConvertTo-Json -Depth 100)) } function loadConfig { $def_cfg = Get-Content $config_def_file | ConvertFrom-Json if (Test-Path -Path $config_file -PathType leaf) { $cfg = Get-Content $config_file | ConvertFrom-Json fill -cfg $cfg -def_cfg $def_cfg } return $def_cfg } function fill($cfg, $def_cfg) { foreach ($prop in $def_cfg.PsObject.Properties) { if ($prop.Value.GetType().Name -eq "PSCustomObject") { fill -cfg $cfg.$($prop.Name) -def_cfg $def_cfg.$($prop.Name) } else { if ($cfg.$($prop.Name) -ne $null) { $def_cfg.$($prop.Name) = $cfg.$($prop.Name) } elseif ($prop.Value -is [string] -and $prop.Value.StartsWith("#")) { $def_cfg.$($prop.Name) = getCustomVal -name $prop.Value } } } } function getCustomVal($name) { $res = switch ($name) { "#desktop" { [Environment]::GetFolderPath("Desktop") } "#microphone" { (Get-AudioDevice -Recording).Name } "#background" { "$root\res\fondo-1.jpg" } } return $res }