2023-01-22 18:35:57 +00:00
using System.Text.Json ;
2022-07-09 13:08:25 +00:00
using DataModel ;
using DataModel.Database.Common ;
using Microsoft.Extensions.DependencyInjection ;
using Microsoft.Extensions.Hosting ;
2022-07-17 13:15:55 +00:00
using Processor ;
2022-07-09 13:08:25 +00:00
2023-01-22 18:35:57 +00:00
using static UI_WinForms . Components . LoaderStart . LoaderAction ;
2022-06-06 12:28:36 +00:00
namespace UI_WinForms ;
internal static class Program
{
2022-07-11 15:58:37 +00:00
internal static IServiceProvider ? SERVICE_PROVIDER ;
2022-07-09 13:08:25 +00:00
2022-09-20 19:36:45 +00:00
internal static bool RestartMainApp = false ;
2022-06-06 12:28:36 +00:00
[STAThread]
private static void Main ( )
{
ApplicationConfiguration . Initialize ( ) ;
2022-07-09 13:08:25 +00:00
Application . EnableVisualStyles ( ) ;
2022-06-06 20:07:17 +00:00
// Start the loader screen:
var loader = new Loader ( ) ;
Application . Run ( loader ) ;
2022-07-09 14:00:23 +00:00
// Check, if the user closes the loader screen:
if ( loader . DialogResult ! = DialogResult . OK )
return ;
2023-01-22 18:35:57 +00:00
2022-07-09 13:08:25 +00:00
//
// Create the DI system
//
var builder = new HostBuilder ( ) ;
//
// Add services
//
builder . ConfigureServices ( ( hostContext , serviceCollection ) = >
{
2023-01-22 18:35:57 +00:00
if ( loader . Result . Action is LOAD_PROJECT or CREATE_NEW_PROJECT )
serviceCollection . AddDatabase ( loader . Result . DataFile , true ) ;
else if ( loader . Result . Action is IMPORT_JSON )
{
try
{
2023-01-22 20:04:48 +00:00
serviceCollection . ImportDataAndAddDatabase ( loader . Result . DataFile ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
2023-01-22 18:35:57 +00:00
}
2023-01-22 20:05:13 +00:00
catch ( JsonException e )
2023-01-22 18:35:57 +00:00
{
2023-01-22 20:05:13 +00:00
MessageBox . Show ( $"The JSON file '{loader.Result.DataFile}' is invalid and cannot be imported: {e.Message}" , "Invalid JSON file" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2023-01-22 18:35:57 +00:00
Environment . Exit ( 100 ) ;
}
2023-01-22 20:05:13 +00:00
catch ( Exception e )
{
MessageBox . Show ( $"An error occurred while importing the JSON file '{loader.Result.DataFile}': {e.Message}" , "Error while importing JSON file" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
Environment . Exit ( 101 ) ;
}
2023-01-22 18:35:57 +00:00
}
2022-07-09 13:08:25 +00:00
} ) ;
// Get the host out of the DI system:
2023-02-11 22:03:14 +00:00
using ( var host = builder . Build ( ) )
2022-07-09 13:08:25 +00:00
{
2023-02-11 22:03:14 +00:00
// Create a service scope:
using ( var scope = host . Services . CreateScope ( ) )
{
// Get a service provider:
SERVICE_PROVIDER = scope . ServiceProvider ;
// Set the service provider to the processor:
ProcessorMeta . ServiceProvider = SERVICE_PROVIDER ;
2022-07-17 13:15:55 +00:00
2023-02-11 22:03:14 +00:00
// Apply database migrations:
using ( var database = SERVICE_PROVIDER . GetRequiredService < DataContext > ( ) )
Setup . PerformDataMigration ( database ) . Wait ( ) ;
2022-07-09 13:08:25 +00:00
2023-02-11 22:03:14 +00:00
// Start the app:
do
{
2023-02-12 11:01:27 +00:00
Processor . DeepL . ResetState ( ) ;
2023-02-11 22:03:14 +00:00
Application . Run ( new Main ( ) ) ;
} while ( Program . RestartMainApp ) ;
}
2022-07-09 13:08:25 +00:00
}
2023-02-11 22:03:14 +00:00
// Tear down the setup:
AppDomain . CurrentDomain . ProcessExit + = ( sender , eventArgs ) = > Setup . SETUP_MAINTENANCE . Dispose ( ) ;
2022-06-06 12:28:36 +00:00
}
}