IW4M-Admin/Application/Factories/RConConnectionFactory.cs

46 lines
1.7 KiB
C#
Raw Permalink Normal View History

2021-06-03 11:51:03 -04:00
using System;
2021-07-11 18:26:30 -04:00
using System.Net;
using SharedLibraryCore.Interfaces;
using System.Text;
2021-06-03 11:51:03 -04:00
using Integrations.Cod;
using Integrations.Source;
using Integrations.Source.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using SharedLibraryCore.Configuration;
namespace IW4MAdmin.Application.Factories
{
/// <summary>
/// implementation of IRConConnectionFactory
/// </summary>
internal class RConConnectionFactory : IRConConnectionFactory
{
2021-06-03 11:51:03 -04:00
private static readonly Encoding GameEncoding = Encoding.GetEncoding("windows-1252");
private readonly IServiceProvider _serviceProvider;
/// <summary>
/// Base constructor
/// </summary>
/// <param name="logger"></param>
2021-06-03 11:51:03 -04:00
public RConConnectionFactory(IServiceProvider serviceProvider)
{
2021-06-03 11:51:03 -04:00
_serviceProvider = serviceProvider;
}
2021-07-11 18:26:30 -04:00
/// <inheritdoc/>
public IRConConnection CreateConnection(IPEndPoint ipEndpoint, string password, string rconEngine)
{
2021-06-03 11:51:03 -04:00
return rconEngine switch
{
2021-07-11 18:26:30 -04:00
"COD" => new CodRConConnection(ipEndpoint, password,
_serviceProvider.GetRequiredService<ILogger<CodRConConnection>>(), GameEncoding,
_serviceProvider.GetRequiredService<ApplicationConfiguration>()?.ServerConnectionAttempts ?? 6),
"Source" => new SourceRConConnection(
_serviceProvider.GetRequiredService<ILogger<SourceRConConnection>>(),
2021-07-11 18:26:30 -04:00
_serviceProvider.GetRequiredService<IRConClientFactory>(), ipEndpoint, password),
2021-06-03 11:51:03 -04:00
_ => throw new ArgumentException($"No supported RCon engine available for '{rconEngine}'")
};
}
}
2021-06-03 11:51:03 -04:00
}