using Microsoft.Win32; namespace Plandent.MstscScriptHook.Configurator; internal static class RegistryConfig { public const string ConfigKeyPath = @"Software\Plandent\MstscScriptHook"; public const string AddInKeyPath = @"Software\Microsoft\Terminal Server Client\Default\AddIns\PlandentMstscScriptHook"; public static HookConfig Load() { var config = new HookConfig(); using RegistryKey? key = Registry.CurrentUser.OpenSubKey(ConfigKeyPath, writable: false); if (key is null) { return config; } config.Enabled = ReadBool(key, "Enabled", true); config.ScriptPath = ReadString(key, "ScriptPath"); config.Arguments = ReadString(key, "Arguments"); config.WorkingDirectory = ReadString(key, "WorkingDirectory"); config.StartOnConnect = ReadBool(key, "StartOnConnect", true); config.StopOnDisconnect = ReadBool(key, "StopOnDisconnect", false); config.Hidden = ReadBool(key, "Hidden", true); config.PreventDuplicates = ReadBool(key, "PreventDuplicates", true); config.EnableLogging = ReadBool(key, "EnableLogging", true); return config; } public static void Save(HookConfig config) { using RegistryKey key = Registry.CurrentUser.CreateSubKey(ConfigKeyPath, writable: true) ?? throw new InvalidOperationException("Registry-Schlüssel konnte nicht erstellt werden."); key.SetValue("Enabled", config.Enabled ? 1 : 0, RegistryValueKind.DWord); key.SetValue("ScriptPath", config.ScriptPath ?? string.Empty, RegistryValueKind.ExpandString); key.SetValue("Arguments", config.Arguments ?? string.Empty, RegistryValueKind.String); key.SetValue("WorkingDirectory", config.WorkingDirectory ?? string.Empty, RegistryValueKind.ExpandString); key.SetValue("StartOnConnect", config.StartOnConnect ? 1 : 0, RegistryValueKind.DWord); key.SetValue("StopOnDisconnect", config.StopOnDisconnect ? 1 : 0, RegistryValueKind.DWord); key.SetValue("Hidden", config.Hidden ? 1 : 0, RegistryValueKind.DWord); key.SetValue("PreventDuplicates", config.PreventDuplicates ? 1 : 0, RegistryValueKind.DWord); key.SetValue("EnableLogging", config.EnableLogging ? 1 : 0, RegistryValueKind.DWord); } public static string? GetRegisteredPluginPath() { using RegistryKey? key = Registry.CurrentUser.OpenSubKey(AddInKeyPath, writable: false); return key?.GetValue("Name", null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string; } public static void RegisterPlugin(string dllPath) { using RegistryKey key = Registry.CurrentUser.CreateSubKey(AddInKeyPath, writable: true) ?? throw new InvalidOperationException("MSTSC AddIn-Registry-Schlüssel konnte nicht erstellt werden."); key.SetValue("Name", dllPath, RegistryValueKind.String); } public static void UnregisterPlugin() { Registry.CurrentUser.DeleteSubKeyTree(AddInKeyPath, throwOnMissingSubKey: false); } private static bool ReadBool(RegistryKey key, string name, bool defaultValue) { object? value = key.GetValue(name); return value switch { int i => i != 0, long l => l != 0, _ => defaultValue }; } private static string ReadString(RegistryKey key, string name, string defaultValue = "") { return key.GetValue(name, defaultValue, RegistryValueOptions.DoNotExpandEnvironmentNames) as string ?? defaultValue; } }