feat: add config file

This commit is contained in:
k0t9i 2023-01-28 17:43:41 +04:00
parent 4778f1c478
commit 16630de6a4
3 changed files with 52 additions and 28 deletions

View File

@ -11,6 +11,8 @@ 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
{
@ -20,38 +22,14 @@ namespace Client
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) => {
services
.AddSingleton<MainWindow>()
.AddSingleton(
typeof(Application),
x => new Application(
x.GetRequiredService<TransportInterface>(),
x.GetRequiredService<MessageParserInterface>(),
x.GetRequiredService<EntityHandlerFactoryInterface>(),
"L2BotDll.dll"
.ConfigureServices(
(hostContext, services) => ConfigureServices(services)
)
)
.AddSingleton(typeof(EntityHandlerFactoryInterface), typeof(EntityHandlerFactory))
.AddSingleton(typeof(MessageParserInterface), typeof(JsonMessageParser))
.AddSingleton(typeof(TransportInterface), x => new NamedPipeTransport("PipeL2Bot"))
.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>();
})
.Build();
}
@ -74,5 +52,45 @@ namespace Client
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>();
}
}
}

View File

@ -8,6 +8,12 @@
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Content Include="config.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />

BIN
Client/config.json Normal file

Binary file not shown.