|
| 1 | +//************************************************************************************************ |
| 2 | +// Copyright © 2025 Steven M Cohn. All rights reserved. |
| 3 | +//************************************************************************************************ |
| 4 | + |
| 5 | +namespace OneMoreSetupActions |
| 6 | +{ |
| 7 | + using Microsoft.Win32; |
| 8 | + using System; |
| 9 | + using System.Runtime.InteropServices; |
| 10 | + using System.Text.RegularExpressions; |
| 11 | + |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Ensures the OneMore Registry settings are defined per the config file. |
| 15 | + /// </summary> |
| 16 | + internal class RegistryAction : CustomAction |
| 17 | + { |
| 18 | + private readonly Architecture architecture; |
| 19 | + |
| 20 | + public RegistryAction(Logger logger, Stepper stepper, Architecture onArchitecture) |
| 21 | + : base(logger, stepper) |
| 22 | + { |
| 23 | + architecture = onArchitecture; |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | + //======================================================================================== |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// </summary> |
| 31 | + /// <returns></returns> |
| 32 | + public override int Install() |
| 33 | + { |
| 34 | + logger.WriteLine(); |
| 35 | + logger.WriteLine($"RegistryAction.Install --- {architecture}"); |
| 36 | + |
| 37 | + var config = GetRegistryConfig(); |
| 38 | + RegistryKey key = null; |
| 39 | + |
| 40 | + try |
| 41 | + { |
| 42 | + foreach (var line in config.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) |
| 43 | + { |
| 44 | + if (line.Length == 0 || line[0] == ';') |
| 45 | + { |
| 46 | + continue; // skip empty lines and comments |
| 47 | + } |
| 48 | + |
| 49 | + if (line[0] == '[') |
| 50 | + { |
| 51 | + key?.Dispose(); |
| 52 | + key = OpenOrCreateKey(line); |
| 53 | + } |
| 54 | + else if (key is not null && (line[0] == '@' || line[0] == '"')) |
| 55 | + { |
| 56 | + SetValue(key, line); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + catch (Exception exc) |
| 61 | + { |
| 62 | + logger.WriteLine("RegistryAction.Install failed:"); |
| 63 | + logger.WriteLine(exc); |
| 64 | + return FAILURE; |
| 65 | + } |
| 66 | + finally |
| 67 | + { |
| 68 | + key?.Dispose(); |
| 69 | + } |
| 70 | + |
| 71 | + return SUCCESS; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + private string GetRegistryConfig() |
| 76 | + { |
| 77 | + var env = architecture == Architecture.X86 ? "ProgramFiles(x86)" : "ProgramFiles"; |
| 78 | + |
| 79 | + var config = Properties.Resource.Registry |
| 80 | + .Replace("{OneMoreID}", RegistryHelper.OneMoreID) |
| 81 | + .Replace("{ProgramFiles}", Environment.GetEnvironmentVariable(env)) |
| 82 | + .Replace("{Version}", AssemblyInfo.Version); |
| 83 | + |
| 84 | + return config; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + private RegistryKey OpenOrCreateKey(string line) |
| 89 | + { |
| 90 | + var raw = line.Trim('[', ']'); |
| 91 | + |
| 92 | + // extract hive name, ending up with something like "HKEY_CLASSES_ROOT" |
| 93 | + var hiveName = raw.Substring(0, raw.IndexOf('\\')); |
| 94 | + |
| 95 | + // we only care about these two! |
| 96 | + var hive = hiveName.EndsWith("ROOT") ? Registry.ClassesRoot : Registry.CurrentUser; |
| 97 | + |
| 98 | + // extract key path, ending up with something like "Software\OneMore" |
| 99 | + var keyName = raw.Substring(raw.IndexOf('\\') + 1); |
| 100 | + |
| 101 | + var key = hive.OpenSubKey(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree); |
| 102 | + if (key is null) |
| 103 | + { |
| 104 | + logger.WriteLine($"creating key: {hive.Name}\\{keyName}"); |
| 105 | + key = hive.CreateSubKey(keyName, RegistryKeyPermissionCheck.ReadWriteSubTree); |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + logger.WriteLine($"opened key: {hive.Name}\\{keyName}"); |
| 110 | + } |
| 111 | + |
| 112 | + return key; |
| 113 | + } |
| 114 | + |
| 115 | + |
| 116 | + private void SetValue(RegistryKey key, string line) |
| 117 | + { |
| 118 | + var matches = Regex.Match(line, @"^(?<name>@|""\w+"")=(?<type>[^""]\w+:)?(?<value>.+)$"); |
| 119 | + if (matches.Success) |
| 120 | + { |
| 121 | + var name = matches.Groups["name"].Value.Trim('"'); |
| 122 | + var type = matches.Groups["type"].Value.TrimEnd(':'); |
| 123 | + var value = matches.Groups["value"].Value.Trim('"'); |
| 124 | + |
| 125 | + if (name == "@") |
| 126 | + { |
| 127 | + name = string.Empty; // use empty string for default value |
| 128 | + } |
| 129 | + |
| 130 | + if (type.Length == 0) |
| 131 | + { |
| 132 | + type = "string"; // default type is string |
| 133 | + } |
| 134 | + |
| 135 | + var v = key.GetValue(name); |
| 136 | + if (v is not null && v.ToString() == value) |
| 137 | + { |
| 138 | + logger.WriteLine($"value already set: {name} = {value} ({type})"); |
| 139 | + return; // no change needed |
| 140 | + } |
| 141 | + |
| 142 | + logger.WriteLine($"setting value: {name} = {value} ({type})"); |
| 143 | + |
| 144 | + if (type == "dword") |
| 145 | + { |
| 146 | + var dword = int.Parse(value, System.Globalization.NumberStyles.HexNumber); |
| 147 | + key.SetValue(name, dword, RegistryValueKind.DWord); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + key.SetValue(name, value, RegistryValueKind.String); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + //======================================================================================== |
| 157 | + |
| 158 | + public override int Uninstall() |
| 159 | + { |
| 160 | + logger.WriteLine(); |
| 161 | + logger.WriteLine($"RegistryAction.Uninstall --- {architecture}"); |
| 162 | + |
| 163 | + var config = GetRegistryConfig(); |
| 164 | + |
| 165 | + string marker = null; |
| 166 | + |
| 167 | + try |
| 168 | + { |
| 169 | + foreach (var line in config.Split(new[] { Environment.NewLine }, StringSplitOptions.None)) |
| 170 | + { |
| 171 | + if (line.Length > 0 && line[0] == '[') |
| 172 | + { |
| 173 | + var raw = line.Trim('[', ']'); |
| 174 | + if (marker is null || !raw.StartsWith(marker)) |
| 175 | + { |
| 176 | + var hiveName = raw.Substring(0, raw.IndexOf('\\')); |
| 177 | + var hive = hiveName.EndsWith("ROOT") ? Registry.ClassesRoot : Registry.CurrentUser; |
| 178 | + var keyName = raw.Substring(raw.IndexOf('\\') + 1); |
| 179 | + |
| 180 | + logger.WriteLine($"deleting tree: {raw}"); |
| 181 | + hive.DeleteSubKeyTree(keyName, false); |
| 182 | + |
| 183 | + // remember for next pass; only need to delete root of subtree |
| 184 | + marker = raw; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + catch (Exception exc) |
| 190 | + { |
| 191 | + logger.WriteLine("RegistryAction.Uninstall failed:"); |
| 192 | + logger.WriteLine(exc); |
| 193 | + return FAILURE; |
| 194 | + } |
| 195 | + |
| 196 | + return SUCCESS; |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments