Ejemplo n.º 1
0
        public IClient Build()
        {
            var packetHandlers = new PacketHandlers();

            foreach (var packetHandlerModule in this.modules)
            {
                foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                {
                    this.serviceCollection.AddSingleton(packetHandler.Value);
                }
            }

            this.serviceCollection.AddSingleton(this.options);
            this.serviceCollection.AddSingleton <IClient, Client>();
            this.serviceCollection.AddSingleton <IClientPacketProcessor, ClientPacketProcessor>();
            this.serviceCollection.AddSingleton <IPacketHandlers>(packetHandlers);
            this.serviceCollection.AddSingleton <ILogLevelProvider, LogLevelProvider>();

            if (this.logger == null)
            {
                this.serviceCollection.AddSingleton <ILogger, NoOpLogger>();
            }

            var serviceProvider = this.serviceCollection.BuildServiceProvider();

            PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>();
            serviceProvider.GetService <ILogLevelProvider>().SetLogLevel(this.options.LogLevel);

            foreach (var packetHandlerModule in this.modules)
            {
                foreach (var packetHandler in packetHandlerModule.GetPacketHandlers())
                {
                    packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default",
                                       (IPacketHandler)serviceProvider.GetService(packetHandler.Value));
                }
            }

            if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1)
            {
                throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type");
            }

            return(serviceProvider.GetService <IClient>());
        }