Beispiel #1
0
        public MtaServer(
            string directory,
            string netDllPath,
            Configuration?configuration = null,
            Action <ServiceCollection>?dependencyCallback = null
            )
        {
            this.configuration = configuration ?? new Configuration();

            var validationResults = new List <ValidationResult>();

            if (!Validator.TryValidateObject(this.configuration, new ValidationContext(this.configuration), validationResults, true))
            {
                string invalidProperties = string.Join("\r\n\t", validationResults.Select(r => r.ErrorMessage));
                throw new Exception("An error has occurred while parsing configuration parameters:\r\n " + invalidProperties);
            }

            this.root = new RootElement();

            this.serviceCollection = new ServiceCollection();
            this.SetupDependencies(dependencyCallback);
            this.serviceProvider = this.serviceCollection.BuildServiceProvider();

            this.elementRepository = this.serviceProvider.GetRequiredService <IElementRepository>();
            this.elementRepository.Add(this.root);

            this.packetReducer = new PacketReducer();
            this.clients       = new Dictionary <NetWrapper, Dictionary <uint, Client> >();

            this.netWrapper = CreateNetWrapper(directory, netDllPath, this.configuration.Host, this.configuration.Port);
        }
Beispiel #2
0
    public MtaServer(
        Action <ServerBuilder> builderAction,
        Func <uint, INetWrapper, Client>?clientCreationMethod = null
        )
    {
        this.netWrappers          = new();
        this.clients              = new();
        this.clientCreationMethod = clientCreationMethod;

        this.root = new();
        this.serviceCollection = new();

        var builder = new ServerBuilder();

        builderAction(builder);

        this.configuration = builder.Configuration;
        this.Password      = this.configuration.Password;
        this.SetupDependencies(services => builder.LoadDependencies(services));

        this.serviceProvider = this.serviceCollection.BuildServiceProvider();
        this.packetReducer   = new(this.serviceProvider.GetRequiredService <ILogger>());

        this.resourceServer = this.serviceProvider.GetRequiredService <IResourceServer>();
        this.resourceServer.Start();

        this.elementRepository  = this.serviceProvider.GetRequiredService <IElementRepository>();
        this.elementIdGenerator = this.serviceProvider.GetService <IElementIdGenerator>();

        this.root.AssociateWith(this);

        builder.ApplyTo(this);
    }
Beispiel #3
0
        public MtaServer(string directory, string netDllPath, string host, ushort port, IElementRepository elementRepository)
        {
            this.ElementRepository = elementRepository;

            this.Root = new Element();

            this.packetReducer = new PacketReducer();
            this.clients       = new Dictionary <NetWrapper, Dictionary <uint, Client> >();

            this.netWrapper = CreateNetWrapper(directory, netDllPath, host, port);
        }
Beispiel #4
0
    public MtaServer(
        Configuration?configuration = null,
        Action <ServiceCollection>?dependencyCallback         = null,
        Func <uint, INetWrapper, Client>?clientCreationMethod = null
        )
    {
        this.netWrappers          = new();
        this.clients              = new();
        this.clientCreationMethod = clientCreationMethod;
        this.configuration        = configuration ?? new();
        this.Password             = configuration?.Password;

        this.root = new();
        this.serviceCollection = new();

        var validationResults = new List <ValidationResult>();

        if (!Validator.TryValidateObject(this.configuration, new ValidationContext(this.configuration), validationResults, true))
        {
            string invalidProperties = string.Join("\r\n\t", validationResults.Select(r => r.ErrorMessage));
            throw new Exception($"An error has occurred while parsing configuration parameters:\r\n {invalidProperties}");
        }

        this.SetupDependencies(dependencyCallback);
        this.serviceProvider = this.serviceCollection.BuildServiceProvider();

        this.resourceServer = this.serviceProvider.GetRequiredService <IResourceServer>();
        this.resourceServer.Start();

        this.elementRepository  = this.serviceProvider.GetRequiredService <IElementRepository>();
        this.elementIdGenerator = this.serviceProvider.GetService <IElementIdGenerator>();

        this.root.AssociateWith(this);

        this.packetReducer = new(this.serviceProvider.GetRequiredService <ILogger>());
    }