using qtc_net_client_2.ClientModel; using qtc_net_client_2.Services; using QtCNETAPI.Services.ApiService; using QtCNETAPI.Services.GatewayService; using System.Text.Json; using System.Threading.Tasks; namespace qtc_net_client_2 { internal static class Program { /// /// The main entry point for the application. /// [STAThread] static async Task Main() { Config clientConfig = new Config(); if(System.Diagnostics.Debugger.IsAttached) { // use localhost clientConfig.ApiEndpoint = "http://localhost:5268/api"; clientConfig.GatewayEndpoint = "http://localhost:5268/chat"; } // find config file if(!File.Exists("./config.json")) { // create it using default config model string configJson = JsonSerializer.Serialize(clientConfig, options: new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText("./config.json", configJson); } else { try { // use config in file Config? fileConfig = JsonSerializer.Deserialize(File.ReadAllText("./config.json")); if (fileConfig != null) { clientConfig = fileConfig; } } catch (JsonException) { // there was a missing property, reset config to default string configJson = JsonSerializer.Serialize(clientConfig, options: new JsonSerializerOptions { WriteIndented = true }); File.WriteAllText("./config.json", configJson); } } // instantiate new ApiService and GatewayService for this session // this will keep the gateway thread in the main thread (i think) IApiService api = new ApiService(clientConfig.ApiEndpoint); IGatewayService gateway = new GatewayService(clientConfig.GatewayEndpoint, api); // ping api var pingResult = await api.PingServerAsync(); if (!pingResult.Success) { MessageBox.Show("The Server Specified In The Config Could Not Be Reached.\nCheck The URL Specified, Otherwise Contact The Server Admin.", "Uh Oh.", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(1); } // check for updates UpdateService updateService = new(); await updateService.CheckForUpdatesAsync(); // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); Application.Run(new Main(api, gateway, clientConfig)); // if application loop is exited, dispose everything await gateway.DisposeAsync(); api = null!; // shut up compiler >:( gateway = null!; Environment.Exit(0); } } }