LANCommander/LANCommander.Launcher/Program.cs

152 lines
6.1 KiB
C#
Raw Permalink Normal View History

using LANCommander.Launcher.Data;
using LANCommander.Launcher.Services;
using LANCommander.Launcher.Services.Extensions;
using LANCommander.Launcher.Settings;
using LANCommander.Launcher.Startup;
using LANCommander.Launcher.UI;
using LANCommander.SDK.Extensions;
using LANCommander.SDK.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2024-06-12 23:20:31 -05:00
using Microsoft.Extensions.Logging;
using Photino.Blazor;
using Photino.Blazor.CustomWindow.Extensions;
2026-01-16 22:43:10 -06:00
using System.Runtime.InteropServices;
2026-01-19 16:54:31 -06:00
using LANCommander.Launcher.Enums;
using LANCommander.Launcher.Models;
2026-01-16 22:43:10 -06:00
using LANCommander.UI.Extensions;
2026-03-15 17:06:37 -05:00
using Microsoft.Extensions.Configuration;
2026-01-16 22:43:10 -06:00
namespace LANCommander.Launcher;
2026-01-16 22:43:10 -06:00
class Program
{
[STAThread]
static void Main(string[] args)
{
2026-03-10 18:31:54 -05:00
if (args.Any(a => a.Equals("RunScript", StringComparison.OrdinalIgnoreCase)))
{
RunHeadlessAsync(args).GetAwaiter().GetResult();
return;
}
2026-01-19 16:54:31 -06:00
WindowService.CreateWindow<UI.App_Main>(new WindowOptions
{
Title = "LANCommander",
Type = WindowType.Main,
}, null, async (app) =>
{
app.RegisterImportHandler();
2026-03-10 18:31:54 -05:00
2026-01-19 16:54:31 -06:00
var logger = app.Services.GetRequiredService<ILogger<Program>>();
2026-01-19 16:54:31 -06:00
logger.LogInformation("Starting launcher | Version: {Version}", UpdateService.GetCurrentVersion());
2026-03-10 18:31:54 -05:00
2026-01-19 16:54:31 -06:00
// Initialize application
using var scope = app.Services.CreateScope();
2026-01-19 16:54:31 -06:00
var connectionClient = scope.ServiceProvider.GetRequiredService<IConnectionClient>();
var settingsProvider = scope.ServiceProvider.GetRequiredService<SettingsProvider<Settings.Settings>>();
var databaseContext = scope.ServiceProvider.GetRequiredService<DatabaseContext>();
2026-01-19 16:54:31 -06:00
await connectionClient.ConnectAsync();
2026-01-16 22:43:10 -06:00
if (!await connectionClient.PingAsync())
await connectionClient.EnableOfflineModeAsync();
2026-01-19 16:54:31 -06:00
if (settingsProvider.CurrentValue.Games.InstallDirectories.Length == 0)
2026-01-16 22:43:10 -06:00
{
2026-01-19 16:54:31 -06:00
settingsProvider.Update(static s => s.Games.InstallDirectories = GetOSPlatform() switch
{
var platform when platform == OSPlatform.Windows => [Path.Combine(Path.GetPathRoot(AppContext.BaseDirectory) ?? "C:", "Games")],
var platform when platform == OSPlatform.Linux => [Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Games")],
var platform when platform == OSPlatform.OSX => [Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Games")],
_ => throw new NotSupportedException("Unsupported OS platform")
});
}
2026-03-10 18:31:54 -05:00
2026-01-19 16:54:31 -06:00
if ((await databaseContext.Database.GetPendingMigrationsAsync()).Any())
await databaseContext.Database.MigrateAsync();
}, args);
2026-01-16 22:43:10 -06:00
static OSPlatform GetOSPlatform()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
return OSPlatform.Windows;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
return OSPlatform.Linux;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return OSPlatform.OSX;
throw new NotSupportedException("Unsupported OS platform");
}
}
2026-03-10 18:31:54 -05:00
static async Task RunHeadlessAsync(string[] args)
{
2026-03-15 17:06:37 -05:00
IConfiguration configuration = new ConfigurationBuilder().ReadFromFile<Settings.Settings>();
var settings = new Settings.Settings();
configuration.Bind(settings);
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
if (settings.Debug.EnableScriptDebugging)
{
WindowService.CreateWindow<UI.App_Debugger>(new WindowOptions
{
Title = "LANCommander",
Type = WindowType.Debugger,
CustomWindow = false,
Width = 1024,
Height = 576,
}, null, async (app) =>
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();
logger.LogInformation("Starting debugger | Version: {Version}", UpdateService.GetCurrentVersion());
// Initialize application
using var scope = app.Services.CreateScope();
var connectionClient = scope.ServiceProvider.GetRequiredService<IConnectionClient>();
var commandLineService = scope.ServiceProvider.GetRequiredService<CommandLineService>();
var scriptDebugger = scope.ServiceProvider.GetRequiredService<LANCommander.Launcher.Services.PowerShell.ScriptDebugger>();
var scriptClient = scope.ServiceProvider.GetRequiredService<ScriptClient>();
scriptClient.Debug = true;
await connectionClient.ConnectAsync();
if (!await connectionClient.PingAsync())
await connectionClient.EnableOfflineModeAsync();
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
await scriptDebugger.WaitForReadyAsync();
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
await commandLineService.ParseCommandLineAsync(args);
}, args);
}
else
{
var builder = Host.CreateApplicationBuilder(args);
builder.AddServiceDefaults();
builder.Configuration.ReadFromFile<Settings.Settings>();
builder.Services.Configure<Settings.Settings>(builder.Configuration);
builder.Services.AddLANCommanderClient<Settings.Settings>();
builder.Services.AddLANCommanderLauncher(options => { });
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
using var host = builder.Build();
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
host.Services.InitializeLANCommander();
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
using var scope = host.Services.CreateScope();
var connectionClient = scope.ServiceProvider.GetRequiredService<IConnectionClient>();
var commandLineService = scope.ServiceProvider.GetRequiredService<CommandLineService>();
2026-03-10 18:31:54 -05:00
2026-03-15 17:06:37 -05:00
if (!await connectionClient.PingAsync())
await connectionClient.EnableOfflineModeAsync();
await commandLineService.ParseCommandLineAsync(args);
}
2026-03-10 18:31:54 -05:00
}
}