feat(setup): add one-screen installer, uninstaller and silent mode
Titano.Setup reads the TPK1 payload from its own executable and installs per user (%LOCALAPPDATA%\Programs) or for all users (Program Files, relaunched elevated through ShellExecuteEx "runas"). Extraction is hash-verified into a staging folder and committed with rollback; upgrades replace files in place, remove obsolete ones and never touch %LOCALAPPDATA%\Titano. - Components, scope, folder (IFileOpenDialog picker), desktop and Start menu shortcuts (IShellLinkW), start-after-install, language picker - Add/Remove Programs entry, .titano file type, install log - /uninstall [/S] removes only logged files, shortcuts and registry entries; user data only when "Remove my data" is ticked; the uninstaller deletes itself through a temporary helper copy - Silent switches /S /DIR= /COMPONENTS= /ALLUSERS /NODESKTOP /NOSTARTMENU /LAUNCH /LANG= with documented exit codes; running programs are asked to close, never killed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
25 files changed
+2183
-11
No files matched your search
@@ -0,0 +1,110 @@
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace Titano.Setup.Engine;
|
||||
|
||||
/// <summary>An installation found in Add/Remove Programs.</summary>
|
||||
internal sealed record ExistingInstall(InstallScope Scope, string Directory, string Version);
|
||||
|
||||
/// <summary>Values written to the Add/Remove Programs entry.</summary>
|
||||
internal sealed record UninstallEntry(string DisplayName, string Version, string Publisher, string Directory, string UninstallerPath, string DisplayIcon, long EstimatedBytes, string? AboutUrl);
|
||||
|
||||
/// <summary>
|
||||
/// Registry state owned by setup: the Add/Remove Programs entry under
|
||||
/// <c>Software\Microsoft\Windows\CurrentVersion\Uninstall\<key></c> and file types under <c>Software\Classes</c>,
|
||||
/// in HKCU for per-user installs and HKLM for all-users installs.
|
||||
/// </summary>
|
||||
internal static partial class SetupRegistry
|
||||
{
|
||||
private const string UninstallRoot = @"Software\Microsoft\Windows\CurrentVersion\Uninstall\";
|
||||
private const string ClassesRoot = @"Software\Classes\";
|
||||
|
||||
private static RegistryKey Hive(InstallScope scope) => scope == InstallScope.AllUsers ? Registry.LocalMachine : Registry.CurrentUser;
|
||||
|
||||
/// <summary>The installation registered under <paramref name="productKey"/> whose folder still exists, current user first.</summary>
|
||||
public static ExistingInstall? FindInstall(string productKey)
|
||||
{
|
||||
foreach (var scope in (ReadOnlySpan<InstallScope>)[InstallScope.CurrentUser, InstallScope.AllUsers])
|
||||
{
|
||||
using var key = Hive(scope).OpenSubKey(UninstallRoot + productKey);
|
||||
if (key?.GetValue("InstallLocation") is string directory && Directory.Exists(directory))
|
||||
return new ExistingInstall(scope, directory, key.GetValue("DisplayVersion") as string ?? string.Empty);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void WriteUninstallEntry(InstallScope scope, string productKey, UninstallEntry entry)
|
||||
{
|
||||
using var key = Hive(scope).CreateSubKey(UninstallRoot + productKey, writable: true);
|
||||
var quoted = "\"" + entry.UninstallerPath + "\"";
|
||||
key.SetValue("DisplayName", entry.DisplayName);
|
||||
key.SetValue("DisplayVersion", entry.Version);
|
||||
key.SetValue("Publisher", entry.Publisher);
|
||||
key.SetValue("DisplayIcon", entry.DisplayIcon + ",0");
|
||||
key.SetValue("InstallLocation", entry.Directory);
|
||||
key.SetValue("InstallDate", DateTime.Now.ToString("yyyyMMdd", CultureInfo.InvariantCulture));
|
||||
key.SetValue("UninstallString", quoted + " /uninstall");
|
||||
key.SetValue("QuietUninstallString", quoted + " /uninstall /S");
|
||||
key.SetValue("EstimatedSize", (int)Math.Min(int.MaxValue, (entry.EstimatedBytes + 1023) / 1024), RegistryValueKind.DWord);
|
||||
key.SetValue("NoModify", 1, RegistryValueKind.DWord);
|
||||
key.SetValue("NoRepair", 1, RegistryValueKind.DWord);
|
||||
if (entry.AboutUrl is not null) key.SetValue("URLInfoAbout", entry.AboutUrl);
|
||||
}
|
||||
|
||||
public static void DeleteUninstallEntry(InstallScope scope, string productKey) =>
|
||||
Hive(scope).DeleteSubKeyTree(UninstallRoot + productKey, throwOnMissingSubKey: false);
|
||||
|
||||
/// <summary>Registers <paramref name="extension"/> to open with <c>"program" "%1"</c> under <paramref name="progId"/>.</summary>
|
||||
public static void RegisterFileType(InstallScope scope, string extension, string progId, string description, string program)
|
||||
{
|
||||
EnsureValid(extension, progId);
|
||||
var hive = Hive(scope);
|
||||
using (var type = hive.CreateSubKey(ClassesRoot + progId, writable: true))
|
||||
{
|
||||
type.SetValue(string.Empty, description);
|
||||
using (var icon = type.CreateSubKey("DefaultIcon", writable: true)) icon.SetValue(string.Empty, "\"" + program + "\",0");
|
||||
using var command = type.CreateSubKey(@"shell\open\command", writable: true);
|
||||
command.SetValue(string.Empty, "\"" + program + "\" \"%1\"");
|
||||
}
|
||||
using var ext = hive.CreateSubKey(ClassesRoot + extension, writable: true);
|
||||
ext.SetValue(string.Empty, progId);
|
||||
using var openWith = ext.CreateSubKey("OpenWithProgids", writable: true);
|
||||
openWith.SetValue(progId, string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>Removes the ProgID and detaches the extension, leaving other programs' registrations intact.</summary>
|
||||
public static void UnregisterFileType(InstallScope scope, string extension, string progId)
|
||||
{
|
||||
EnsureValid(extension, progId);
|
||||
var hive = Hive(scope);
|
||||
hive.DeleteSubKeyTree(ClassesRoot + progId, throwOnMissingSubKey: false);
|
||||
using (var ext = hive.OpenSubKey(ClassesRoot + extension, writable: true))
|
||||
{
|
||||
if (ext is null) return;
|
||||
if (ext.GetValue(string.Empty) is string current && current.Equals(progId, StringComparison.OrdinalIgnoreCase)) ext.DeleteValue(string.Empty, false);
|
||||
using (var openWith = ext.OpenSubKey("OpenWithProgids", writable: true))
|
||||
{
|
||||
openWith?.DeleteValue(progId, false);
|
||||
if (openWith is { ValueCount: 0, SubKeyCount: 0 }) ext.DeleteSubKey("OpenWithProgids", false);
|
||||
}
|
||||
if (ext.ValueCount > 0 || ext.SubKeyCount > 0) return;
|
||||
}
|
||||
hive.DeleteSubKey(ClassesRoot + extension, throwOnMissingSubKey: false);
|
||||
}
|
||||
|
||||
/// <summary>Guards registry paths built from a log file: an extension and a dotted ProgID, nothing else.</summary>
|
||||
public static bool IsValidFileType(string extension, string progId) => ExtensionRegex().IsMatch(extension) && ProgIdRegex().IsMatch(progId);
|
||||
|
||||
private static void EnsureValid(string extension, string progId)
|
||||
{
|
||||
if (!IsValidFileType(extension, progId)) throw new ArgumentException($"Invalid file type '{extension}' / '{progId}'.");
|
||||
}
|
||||
|
||||
[GeneratedRegex(@"^\.[A-Za-z0-9]{1,16}$")]
|
||||
private static partial Regex ExtensionRegex();
|
||||
|
||||
[GeneratedRegex(@"^[A-Za-z][A-Za-z0-9]*(\.[A-Za-z0-9]+)+$")]
|
||||
private static partial Regex ProgIdRegex();
|
||||
}
|
||||
Reference in new issue
Block a user