Allow storing settings using the context

This commit is contained in:
Manuel Thalmann 2023-06-29 19:03:31 +02:00
parent 171ad324b5
commit 73136bbf60

View file

@ -4,11 +4,17 @@ class Context {
[string]$BackupName; [string]$BackupName;
[string]$UserName; [string]$UserName;
[string]$AdminName = "Admin"; [string]$AdminName = "Admin";
[string]$ConfigRoot = "HKLM:\Software\PortValhalla";
[string]$RunOnceName = "PortValhalla"; [string]$RunOnceName = "PortValhalla";
[string] ProjectRoot() {
return Resolve-Path (Join-Path $PSScriptRoot ".." ".." "..");
}
[string] BackupRoot() { [string] BackupRoot() {
if (-not $this.RootDir) if (-not $this.RootDir)
{ {
return Join-Path $PSScriptRoot ".." ".." ".." "backup" $this.BackupName; return Join-Path $this.ProjectRoot() $this.BackupName;
} }
else else
{ {
@ -16,6 +22,28 @@ class Context {
} }
} }
[Microsoft.Win32.RegistryKey] EnsureConfigKey() {
if (-not (Test-Path $this.ConfigRoot)) {
New-Item $this.ConfigRoot;
}
return Get-Item $this.ConfigRoot;
}
[void] Set([string] $key, $value, [Microsoft.Win32.RegistryValueKind] $type) {
$configKey = $this.EnsureConfigKey();
$configKey.SetValue($key, $value, $type);
}
[object] Get([string] $key) {
$configKey = $this.EnsureConfigKey();
if ($configKey.GetValueNames().Contains($key)) {
return $configKey.GetValue($key);
} else {
return $null;
}
}
[string] ArchivePath($name) { [string] ArchivePath($name) {
return Join-Path $this.BackupRoot() "$name.7z"; return Join-Path $this.BackupRoot() "$name.7z";
} }