From 73136bbf60a7e52fda352dce1481f0d1fbad9945 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Thu, 29 Jun 2023 19:03:31 +0200 Subject: [PATCH] Allow storing settings using the context --- scripts/Windows/Scripts/Context.ps1 | 30 ++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index 789376f4..2422078c 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -4,11 +4,17 @@ class Context { [string]$BackupName; [string]$UserName; [string]$AdminName = "Admin"; + [string]$ConfigRoot = "HKLM:\Software\PortValhalla"; [string]$RunOnceName = "PortValhalla"; + + [string] ProjectRoot() { + return Resolve-Path (Join-Path $PSScriptRoot ".." ".." ".."); + } + [string] BackupRoot() { if (-not $this.RootDir) { - return Join-Path $PSScriptRoot ".." ".." ".." "backup" $this.BackupName; + return Join-Path $this.ProjectRoot() $this.BackupName; } 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) { return Join-Path $this.BackupRoot() "$name.7z"; }