public Server(ServerSettings settings)
        {
            // Attempt to load world
            if (!File.Exists(settings.SavePath))
            {
                throw new FileNotFoundException("Save file not found.");
            }

            string         worldData      = File.ReadAllText(settings.SavePath);
            WorldStateData worldStateData = new WorldStateData(worldData);

            this.settings = settings;
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            processorContext = new ServerProcessorContext(this);

            router = new PacketRouter(processorContext);
            foreach (PacketProcessor packetProcessor in PacketProcessor.GetProcessors())
            {
                router.RegisterPacketProcessor(packetProcessor);
            }

            playerConnections        = new Dictionary <Guid, long>();
            playerManager            = new PlayerManager(this);
            simulationManager        = new SimulationManager(this);
            worldStateManager        = new WorldStateManager(this, settings.SavePath, worldStateData);
            worldRequestQueueManager = new WorldRequestQueueManager(this);
            timeManager        = new TimeManager(this);
            environmentManager = new EnvironmentManager(this);
            disasterManager    = new DisasterManager(this);
            Initialize();
        }
Example #2
0
        public override void ProcessPacket(Guid sourcePlayerId, Packet packet, IProcessorContext context)
        {
            WorldDataRequestPacket   worldDataRequestPacket   = (WorldDataRequestPacket)packet;
            ServerProcessorContext   processorContext         = (ServerProcessorContext)context;
            PlayerManager            playerManager            = processorContext.Server.PlayerManager;
            SimulationManager        simulationManager        = processorContext.Server.SimulationManager;
            TimeManager              timeManager              = processorContext.Server.TimeManager;
            WorldRequestQueueManager worldRequestQueueManager = processorContext.Server.WorldRequestQueueManager;
            WorldStateManager        worldStateManager        = processorContext.Server.WorldStateManager;

            if (!playerManager.PlayerExists(sourcePlayerId))
            {
                // Players can not ask for a copy of the world before they authenticate
                return;
            }

            Player sourcePlayer = playerManager.GetPlayer(sourcePlayerId);

            if (sourcePlayer.State != PlayerState.ConnectedMainMenu)
            {
                // Invalid state
                return;
            }

            // We pause the game and lock time management until everyone has finished loading
            timeManager.FreezeTime();

            Player?simulationOwner = simulationManager.GetSimulationOwner();

            if (simulationOwner != null && simulationOwner.Value != sourcePlayer && worldStateManager.RequestWorldData())
            {
                // The server can get a newer world state
                // add the client to the queue and wait
                worldRequestQueueManager.EnqueuePlayer(sourcePlayer);
            }
            else
            {
                // The state we have is already the newest
                WorldStateData  worldStateData  = worldStateManager.GetWorldData();
                WorldDataPacket worldDataPacket = new WorldDataPacket(worldStateData);
                processorContext.Server.SendPacketToPlayer(worldDataPacket, sourcePlayerId);
            }
        }