Files
rssotto-csharp-client/ModVersionChecker/Main.cs
2025-09-04 10:14:30 +02:00

144 lines
5.3 KiB
C#

using Microsoft.Extensions.DependencyInjection;
using ModVersionChecker.forms;
using Microsoft.Extensions.Hosting;
using ModVersionChecker.managers.interfaces;
using ModVersionChecker.managers.filesystem;
using ModVersionChecker.managers.litedb;
namespace ModVersionChecker
{
class Program
{
[STAThread]
static void Main()
{
var builder = Host.CreateDefaultBuilder();
var program = new Program();
builder.ConfigureServices(services =>
{
services.AddSingleton<IConfigManager, ConfigLiteDb>();
services.AddSingleton<IAppsManager, AppConfigLiteDb>();
services.AddSingleton<ISourcesDefManager, SourcesLiteDb>();
services.AddSingleton<ICheckerTypesDefManager, CheckerTypesDefManager>();
services.AddSingleton<IFlightSimsManager, FlightSimsLiteDb>();
services.AddSingleton<IFormFactory, FormFactory>();
services.AddSingleton<IAppStatusManager, AppStatusManager>();
services.AddSingleton<INotifyIconService, NotifyIconService>();
services.AddTransient<MainForm>();
services.AddTransient<AppDetailsForm>();
services.AddTransient<VersionChecker>();
});
using var host = builder.Build();
var lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
lifetime.ApplicationStarted.Register(() =>
{
Console.WriteLine("Application is shutting down...");
});
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (!SystemTray.IsSupported())
{
MessageBox.Show("System tray not supported", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
var serviceProvider = host.Services;
var configForm = serviceProvider.GetService<MainForm>();
var versionChecker = serviceProvider.GetService<VersionChecker>();
var notifyIconService = serviceProvider.GetRequiredService<INotifyIconService>();
var configManager = serviceProvider.GetRequiredService<IConfigManager>();
var config = configManager.GetConfig();
EventHandler openFormHandler = (s, e) =>
{
if (configForm == null) return;
configForm.UpdateListView();
if (configForm.Visible)
{
configForm.BringToFront();
return;
}
configForm.ShowDialog();
};
using (var notifyIcon = new NotifyIcon())
{
notifyIcon.Icon = new Icon("Resources/MVC-Icon.ico"); // Place MVC-Icon.ico in Resources
notifyIcon.Text = "Update Checker";
notifyIcon.Visible = true;
notifyIconService.SetNotifyIcon(notifyIcon);
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Configure", null, openFormHandler);
contextMenu.Items.Add("Exit", null, (s, e) => Application.Exit());
notifyIcon.ContextMenuStrip = contextMenu;
notifyIcon.DoubleClick += openFormHandler;
bool checkOnInitialStart = config.CheckOnStartup;
if (checkOnInitialStart && versionChecker != null)
{
versionChecker.StartVersionChecking(notifyIcon);
versionChecker.OnFinished += (s, e) => {
if (configForm != null)
{
if (configForm.InvokeRequired)
{
configForm.Invoke(() => configForm.UpdateListView());
}
else
{
configForm.UpdateListView();
}
}
};
}
if (versionChecker != null)
{
if (configForm != null)
{
configForm.OnRecheck += (s, e) =>
{
if (versionChecker != null)
{
versionChecker.CheckAsync();
}
};
}
}
// Add to startup
// AddToStartup();
Application.Run(); // Keep app running for tray icon
}
host.RunAsync().GetAwaiter().GetResult();
}
static void AddToStartup()
{
using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true))
{
key?.SetValue("XintanalabsUpdateChecker", $"\"{Application.ExecutablePath}\"");
}
}
}
public class SystemTray
{
public static bool IsSupported() => System.Windows.Forms.SystemInformation.TerminalServerSession == false;
}
}