public void AllPacketsAreHandled() { List <Type> packetTypes = typeof(DefaultServerPacketProcessor).Assembly.GetTypes() .Where(p => typeof(PacketProcessor).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList(); NitroxServiceLocator.InitializeDependencyContainer(new ClientAutoFacRegistrar(), new SubnauticaServerAutoFacRegistrar(), new TestAutoFacRegistrar()); NitroxServiceLocator.BeginNewLifetimeScope(); foreach (Type packet in typeof(Packet).Assembly.GetTypes() .Where(p => typeof(Packet).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList()) { Type clientPacketProcessorType = typeof(ClientPacketProcessor <>); Type authenticatedPacketProcessorType = typeof(AuthenticatedPacketProcessor <>); Type unauthenticatedPacketProcessorType = typeof(UnauthenticatedPacketProcessor <>); Type clientProcessorType = clientPacketProcessorType.MakeGenericType(packet); Type authProcessorType = authenticatedPacketProcessorType.MakeGenericType(packet); Type unauthProcessorType = unauthenticatedPacketProcessorType.MakeGenericType(packet); Console.WriteLine($@"Checking handler for packet {packet}..."); (packetTypes.Contains(packet) || NitroxServiceLocator.LocateOptionalService(clientProcessorType).HasValue || NitroxServiceLocator.LocateOptionalService(authProcessorType).HasValue || NitroxServiceLocator.LocateOptionalService(unauthProcessorType).HasValue).Should() .BeTrue($"Packet of type '{packet}' should have at least one processor."); } }
public void AllPacketsAreHandled() { World world = new World(); ContainerBuilder serverContainerBuilder = new ContainerBuilder(); ServerAutoFacRegistrar serverDependencyRegistrar = new ServerAutoFacRegistrar(); NitroxServiceLocator.InitializeDependencyContainer(serverDependencyRegistrar); NitroxServiceLocator.BeginNewLifetimeScope(); List <Type> packetTypes = typeof(DefaultServerPacketProcessor).Assembly.GetTypes() .Where(p => typeof(PacketProcessor).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList(); ContainerBuilder containerBuilder = new ContainerBuilder(); ClientAutoFacRegistrar clientDependencyRegistrar = new ClientAutoFacRegistrar(); NitroxServiceLocator.InitializeDependencyContainer(clientDependencyRegistrar); NitroxServiceLocator.BeginNewLifetimeScope(); typeof(Packet).Assembly.GetTypes() .Where(p => typeof(Packet).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList() .ForEach(packet => { Type clientPacketProcessorType = typeof(ClientPacketProcessor <>); Type clientProcessorType = clientPacketProcessorType.MakeGenericType(packet); Console.WriteLine("Checking handler for packet {0}...", packet); Assert.IsTrue(packetTypes.Contains(packet) || NitroxServiceLocator.LocateOptionalService(clientProcessorType).IsPresent(), $"Runtime has not detected a handler for {packet}!"); } ); }
public void RuntimeDetectsAllClientPacketProcessors() { ContainerBuilder containerBuilder = new ContainerBuilder(); ClientAutoFacRegistrar clientDependencyRegistrar = new ClientAutoFacRegistrar(); NitroxServiceLocator.InitializeDependencyContainer(clientDependencyRegistrar); NitroxServiceLocator.BeginNewLifetimeScope(); // Check if every PacketProcessor has been detected: typeof(Multiplayer).Assembly.GetTypes() .Where(p => typeof(PacketProcessor).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList() .ForEach(processor => { try { Assert.IsTrue(NitroxServiceLocator.LocateOptionalService(processor.BaseType).IsPresent(), $"{processor} has not been discovered by the runtime code!"); } catch (Autofac.Core.DependencyResolutionException ex) { if (ex.InnerException.GetType() != typeof(System.Security.SecurityException)) { throw ex; } } } ); }
private void ProcessAuthenticated(Packet packet, Player player) { Type serverPacketProcessorType = typeof(AuthenticatedPacketProcessor <>); Type packetType = packet.GetType(); Type packetProcessorType = serverPacketProcessorType.MakeGenericType(packetType); Optional <object> opProcessor = NitroxServiceLocator.LocateOptionalService(packetProcessorType); if (opProcessor.HasValue) { PacketProcessor processor = (PacketProcessor)opProcessor.Value; processor.ProcessPacket(packet, player); } else { defaultServerPacketProcessor.ProcessPacket(packet, player); } }
public void RuntimeDetectsAllServerPacketProcessors() { World world = new World(); ContainerBuilder containerBuilder = new ContainerBuilder(); ServerAutoFacRegistrar serverDependencyRegistrar = new NitroxServer_Subnautica.SubnauticaServerAutoFacRegistrar(); NitroxServiceLocator.InitializeDependencyContainer(serverDependencyRegistrar); NitroxServiceLocator.BeginNewLifetimeScope(); // Check if every PacketProcessor has been detected: typeof(DefaultServerPacketProcessor).Assembly.GetTypes() .Where(p => typeof(PacketProcessor).IsAssignableFrom(p) && p.IsClass && !p.IsAbstract) .ToList() .ForEach(processor => Assert.IsTrue(NitroxServiceLocator.LocateOptionalService(processor.BaseType).IsPresent(), $"{processor} has not been discovered by the runtime code!") ); }