98 lines
3.6 KiB
C#
98 lines
3.6 KiB
C#
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
using Microsoft.Win32;
|
|
|
|
namespace AeroMCSetup;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private static Mutex? _single;
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
bool has(string arg) => e.Args.Any(a => a.Equals(arg, StringComparison.OrdinalIgnoreCase));
|
|
|
|
// Das Deinstallationsprogramm ist das Setup ohne angehängte App → ohne Anhang immer Deinstallation
|
|
bool uninstall = has("--uninstall") || !Payload.Exists;
|
|
bool silent = has("/S");
|
|
bool update = has("--update");
|
|
bool launch = has("/launch");
|
|
var dirArg = e.Args.FirstOrDefault(a => a.StartsWith("/D=", StringComparison.OrdinalIgnoreCase))?.Substring(3);
|
|
|
|
_single = new Mutex(true, "AeroMC-Setup", out bool first);
|
|
if (!first)
|
|
{
|
|
Shutdown(0);
|
|
return;
|
|
}
|
|
|
|
// Stille Installation/Deinstallation für Skripte: AeroMC-Setup.exe /S [/D=Ordner] [/launch] bzw. --uninstall /S
|
|
if (silent)
|
|
{
|
|
var progress = new Progress<(double, string)>(_ => { });
|
|
try
|
|
{
|
|
if (uninstall) Installer.Uninstall(removeData: false, progress);
|
|
else
|
|
{
|
|
var dir = dirArg ?? Installer.InstalledDir ?? Installer.DefaultDir;
|
|
Installer.Install(dir, desktopLink: Installer.InstalledDir is null || Installer.DesktopLinkExists,
|
|
progress, waitForExit: true);
|
|
if (launch) System.Diagnostics.Process.Start(Path.Combine(dir, Installer.ExeName));
|
|
}
|
|
Shutdown(0);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// ohne Oberfläche: Fehler für Skripte nachvollziehbar ablegen
|
|
try { File.WriteAllText(Path.Combine(Path.GetTempPath(), "AeroMC-Setup.log"), $"[{DateTime.Now}] {ex}"); } catch { }
|
|
Shutdown(1);
|
|
}
|
|
return;
|
|
}
|
|
|
|
ApplyTheme();
|
|
var mode = uninstall ? SetupMode.Uninstall : update ? SetupMode.Update : SetupMode.Install;
|
|
new MainWindow(mode).Show();
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
try { _single?.ReleaseMutex(); } catch { }
|
|
base.OnExit(e);
|
|
}
|
|
|
|
/// <summary>Hell/Dunkel wie in Windows eingestellt.</summary>
|
|
private void ApplyTheme()
|
|
{
|
|
bool light = ReadDword(@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "AppsUseLightTheme") != 0;
|
|
Resources.MergedDictionaries[0] = new ResourceDictionary
|
|
{
|
|
Source = new Uri($"pack://application:,,,/Themes/Colors.{(light ? "Light" : "Dark")}.xaml"),
|
|
};
|
|
if (!light)
|
|
{
|
|
// im Dunkeln etwas heller, damit Text und Fortschrittsbalken gut lesbar bleiben
|
|
var a = Color.FromRgb(0x4A, 0x86, 0xEA);
|
|
Resources["AccentBrush"] = new SolidColorBrush(a);
|
|
Resources["AccentHoverBrush"] = new SolidColorBrush(Color.FromRgb(0x5E, 0x94, 0xEE));
|
|
Resources["AccentSubtleBrush"] = new SolidColorBrush(Color.FromArgb(0x30, a.R, a.G, a.B));
|
|
Resources["AccentSubtleStrokeBrush"] = new SolidColorBrush(Color.FromArgb(0x60, a.R, a.G, a.B));
|
|
}
|
|
}
|
|
|
|
private static int ReadDword(string key, string name)
|
|
{
|
|
try
|
|
{
|
|
using var k = Registry.CurrentUser.OpenSubKey(key);
|
|
return k?.GetValue(name) is int i ? i : -1;
|
|
}
|
|
catch { return -1; }
|
|
}
|
|
}
|
|
|
|
public enum SetupMode { Install, Update, Uninstall }
|