using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Controls.Primitives; using Microsoft.UI.Xaml.Data; using Microsoft.UI.Xaml.Input; using Microsoft.UI.Xaml.Media; using Microsoft.UI.Xaml.Navigation; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.UI.ApplicationSettings; using WinUI3Localizer; using WinWARP.Core.ViewModels; using WinWARP.Core.ViewModels.ConfigSettings; using WinWARP.Core.ViewModels.Settings; using WinWARP.Core.Data; using WinWARP.Ui.Services; using WinWARP.Ui.Views; using WinWARP.Ui.Views.Settings; using WinWARP.Core.Services.Implementations; using WinWARP.Core.Services.Interfaces; using System.Diagnostics; // To learn more about WinUI, the WinUI project structure, // and more about our project templates, see: http://aka.ms/winui-project-info. namespace WinWARP.Ui; /// /// Provides application-specific behavior to supplement the default Application class. /// public partial class App : Application { public static IServiceProvider Services { get; private set; } private Window? _window; /// /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// public App() { InitializeComponent(); Services = ConfigureServices(); using var context = Services.GetRequiredService>().CreateDbContext(); context.Database.EnsureCreated(); } /// /// Configures the services for dependency injection. /// /// private static ServiceProvider ConfigureServices() { var services = new ServiceCollection(); // DB Debug.WriteLine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP", "app.db")); services.AddDbContextFactory(options => { var folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "WinWARP"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } var dbPath = Path.Combine(folder, "app.db"); options.UseSqlite($"Data Source={dbPath}"); }); services.AddTransient(); // ViewModels services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddSingleton(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); // Views services.AddTransient(); // UI services var navigationService = new NavigationService(); navigationService.Configure(typeof(HomeViewModel), typeof(HomePage)); navigationService.Configure(typeof(SettingsViewModel), typeof(SettingsPage)); navigationService.Configure(typeof(SubsSettingsViewModel), typeof(SubsSettingsPage)); navigationService.Configure(typeof(ConfigSettingsViewModel), typeof(ConfigSettingsPage)); navigationService.Configure(typeof(GeneralSettingsViewModel), typeof(GeneralSettingsPage)); navigationService.Configure(typeof(UpdateSettingsViewModel), typeof(UpdateSettingsPage)); navigationService.Configure(typeof(InboundSettingsViewModel), typeof(InboundSettingsPage)); navigationService.Configure(typeof(RoutingSettingsViewModel), typeof(RoutingSettingsPage)); navigationService.Configure(typeof(DnsSettingsViewModel), typeof(DnsSettingsPage)); navigationService.Configure(typeof(TlsSettingsViewModel), typeof(TlsSettingsPage)); navigationService.Configure(typeof(WarpSettingsViewModel), typeof(WarpSettingsPage)); services.AddSingleton(navigationService); services.AddSingleton(); return services.BuildServiceProvider(); } /// /// Invoked when the application is launched. /// /// Details about the launch request and process. protected async override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args) { var stringsFolderPath = System.IO.Path.Combine(AppContext.BaseDirectory, "Strings"); var localizer = await new LocalizerBuilder() .AddStringResourcesFolderForLanguageDictionaries(stringsFolderPath) .SetOptions(options => { options.DefaultLanguage = "en-US"; }) .Build(); _window = Services.GetRequiredService(); _window.Activate(); } }