L2Bot2.0/Client/App.xaml.cs
2023-01-28 17:43:41 +04:00

97 lines
3.6 KiB
C#

using Client.Domain.Factories;
using Client.Infrastructure.Factories;
using Client.Domain.Parsers;
using Client.Infrastructure.Parsers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Windows;
using Client.Domain.Transports;
using Client.Infrastructure.Transports;
using Client.Domain.Entities;
using Client.Domain.Service;
using BaseApp = System.Windows.Application;
using Client.Domain.ValueObjects;
using Microsoft.Extensions.Configuration;
using System.Reflection;
namespace Client
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : BaseApp
{
public static IHost? AppHost { get; private set; }
public static IConfiguration? AppConfig { get; private set; }
public App()
{
AppHost = Host.CreateDefaultBuilder()
.ConfigureServices(
(hostContext, services) => ConfigureServices(services)
)
.Build();
}
protected override async void OnStartup(StartupEventArgs e)
{
await AppHost!.StartAsync();
var startupForm = AppHost.Services.GetRequiredService<MainWindow>();
startupForm.Show();
var application = AppHost.Services.GetRequiredService<Application>();
application.StartAsync();
base.OnStartup(e);
}
protected override async void OnExit(ExitEventArgs e)
{
await AppHost!.StopAsync();
base.OnExit(e);
}
private void ConfigureServices(IServiceCollection services)
{
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true, reloadOnChange: false)
.Build();
services
.AddSingleton(typeof(IConfiguration), config)
.AddSingleton<MainWindow>()
.AddSingleton(
typeof(Application),
x => new Application(
x.GetRequiredService<TransportInterface>(),
x.GetRequiredService<MessageParserInterface>(),
x.GetRequiredService<EntityHandlerFactoryInterface>(),
config.GetValue<string>("DLLName") ?? ""
)
)
.AddSingleton(typeof(EntityHandlerFactoryInterface), typeof(EntityHandlerFactory))
.AddSingleton(typeof(MessageParserInterface), typeof(JsonMessageParser))
.AddSingleton(
typeof(TransportInterface),
x => new NamedPipeTransport(
config.GetValue<string>("ConnectionPipeName") ?? ""
)
)
.AddTransient(typeof(EntityFactoryInterface<Hero>), typeof(EntityFactory<Hero>))
.AddTransient(typeof(EntityFactoryInterface<Drop>), typeof(EntityFactory<Drop>))
.AddTransient(typeof(EntityFactoryInterface<NPC>), typeof(EntityFactory<NPC>))
.AddTransient(typeof(EntityFactoryInterface<Player>), typeof(EntityFactory<Player>))
.AddTransient(typeof(EntityFactoryInterface<ChatMessage>), typeof(EntityFactory<ChatMessage>))
.AddSingleton<EntityHandler<Hero>>()
.AddSingleton<EntityHandler<Drop>>()
.AddSingleton<EntityHandler<NPC>>()
.AddSingleton<EntityHandler<Player>>()
.AddSingleton<ChatMessageHandler>();
}
}
}