Esempio n. 1
0
        public void Write( SMPPacketWriter writer )
        {
            writer.Write( PacketType.KEEP_ALIVE );
            writer.Write( ID );

            writer.Flush();
        }
Esempio n. 2
0
        public void Write(SMPPacketWriter writer)
        {
            writer.Write(PacketType.KICK);
            writer.Write(Message);

            writer.Flush();
        }
        public void Write( SMPPacketWriter writer )
        {
            writer.Write( PacketType.SPAWN_POSITION );
            writer.Write( X );
            writer.Write( Y );
            writer.Write( Z );

            writer.Flush();
        }
        public void Write( SMPPacketWriter writer )
        {
            writer.Write( PacketType.ENTITY_EQUIPMENT );
            writer.Write( ID );
            writer.Write( Slot );
            writer.Write( Z );

            writer.Flush();
        }
        public void Write(SMPPacketWriter writer)
        {
            writer.Write(PacketType.ENCRYPTION_KEY_RESPONSE);
            writer.Write(SharedSecretLength);
            writer.Write(SharedSecret);
            writer.Write(VerifyTokenLength);
            writer.Write(VerifyTokenResponse);

            writer.Flush();
        }
        public void Write(SMPPacketWriter writer)
        {
            writer.Write(PacketType.ENCRYPTION_KEY_REQUEST);
            writer.Write(ServerID);
            writer.Write(PublicKeyLength);
            writer.Write(PublicKey);
            writer.Write(VerifyTokenLength);
            writer.Write(VerifyToken);

            writer.Flush();
        }
Esempio n. 7
0
        public void Write( SMPPacketWriter writer )
        {
            writer.Write( PacketType.INITCHUNK );
            writer.Write( X );
            writer.Write( Z );
            writer.Write( GroundUp );
            writer.Write( PrimaryBitMap );
            writer.Write( AddBitMap );
            writer.Write( Sunlight );

            writer.Flush();
        }
        public void Write( SMPPacketWriter writer )
        {
            writer.Write( PacketType.POSITIONANDLOOK );
            writer.Write( X );
            writer.Write( Y );
            writer.Write( Stance );
            writer.Write( Z );
            writer.Write( R );
            writer.Write( L );
            writer.Write( OnGround );

            writer.Flush();
        }
        public void Write(SMPPacketWriter writer)
        {
            writer.Write(PacketType.LOGIN_REQUEST);
            writer.Write(EntityID);
            writer.Write(LevelType);
            writer.Write(Gamemode);
            writer.Write(Dimension);
            writer.Write(Difficulty);
            writer.Write(NotUsed);
            writer.Write(MaxPlayers);

            writer.Flush();
        }
Esempio n. 10
0
        public static void MakeMapChunkPacket( Chunk chunk, SMPPacketWriter Writer )
        {
            var X = ( int )chunk.X;
            var Z = ( int )chunk.Z;

            byte[] blockData;
            byte[] metadata;
            byte[] blockLight;
            byte[] skyLight;

            ushort mask = 1, chunkY = 0;
            bool nonAir = true;

            // First pass calculates number of sections to send
            int totalSections = 0;
            for ( int i = 15; i >= 0; i-- ) {
                Section s = chunk.Sections[chunkY++];

                if ( s.IsAir )
                    nonAir = false;
                if ( nonAir )
                    totalSections++;
            }

            mask = 1;
            chunkY = 0;
            nonAir = true;
            blockData = new byte[totalSections * BlockDataLength];
            metadata = new byte[totalSections * NibbleDataLength];
            blockLight = new byte[totalSections * NibbleDataLength];
            skyLight = new byte[totalSections * NibbleDataLength];

            ushort PrimaryBitMap = 0, AddBitMap = 0;

            // Second pass produces the arrays
            for ( int i = 15; i >= 0; i-- ) {
                Section s = chunk.Sections[chunkY++];

                if ( s.IsAir )
                    nonAir = false;
                if ( nonAir ) {
                    Array.Copy( s.Blocks, 0, blockData, ( chunkY - 1 ) * BlockDataLength, BlockDataLength );
                    Array.Copy( s.Metadata.Data, 0, metadata, ( chunkY - 1 ) * NibbleDataLength, NibbleDataLength );
                    Array.Copy( s.BlockLight.Data, 0, blockLight, ( chunkY - 1 ) * NibbleDataLength, NibbleDataLength );
                    Array.Copy( s.SkyLight.Data, 0, skyLight, ( chunkY - 1 ) * NibbleDataLength, NibbleDataLength );

                    PrimaryBitMap |= mask;
                }

                mask <<= 1;
            }

            // Create the final array
            // TODO: Merge this into the other loop, reduce objects
            byte[] data = new byte[blockData.Length + metadata.Length +
                blockLight.Length + skyLight.Length + chunk.Biomes.Length];
            int index = 0;
            Array.Copy( blockData, 0, data, index, blockData.Length );
            index += blockData.Length;
            Array.Copy( metadata, 0, data, index, metadata.Length );
            index += metadata.Length;
            Array.Copy( blockLight, 0, data, index, blockLight.Length );
            index += blockLight.Length;
            Array.Copy( skyLight, 0, data, index, skyLight.Length );
            index += skyLight.Length;
            Array.Copy(chunk.Biomes, 0, data, index, chunk.Biomes.Length);

            // Compress the array
            var result = Ionic.Zlib.ZlibStream.CompressBuffer( data );
            var GroundUpContiguous = true;
            Logger.LogToConsole( "Chunk length: "+result.Length.ToString() );
            Writer.Write( 0x38 );
            Writer.Write( X );
            Writer.Write( Z );
            Writer.Write( GroundUpContiguous );
            Writer.Write( PrimaryBitMap );
            Writer.Write( AddBitMap );
            Writer.Write( result.Length );
            Writer.Write( result );

            Writer.Flush();
        }
Esempio n. 11
0
        public void SessionLoop( byte firstPacket )
        {
            try {
                smpReader = new PacketReader( stream );
                smpWriter = new SMPPacketWriter( stream );
                // See if we have a packet where no real loop should be created
                PacketType packet = ( PacketType )firstPacket;// smpReader.ReadByte();

                switch ( packet ) {
                    case PacketType.SERVER_LIST_PING:
                        // Kick player with server list ping message
                        new DisconnectPacket() {
                            Message = ""//Server.ServerListPingMessage
                        }.Write( smpWriter );

                        // Close connection
                        stream.Close();

                        break;
                    case PacketType.HANDSHAKE:
                        Logger.LogToConsole( "Handshake packet received" );

                        // Initialize handshake packet to be read
                        HandshakePacket handshakePacket = new HandshakePacket();

                        // Read the packet
                        handshakePacket.Read( smpReader );

                        string Name = handshakePacket.Username;
                        IP = IPAddress.Parse( handshakePacket.ServerHost );
                        // Send encryption request
                        KeyPair keyPair = GenerateRSAKeyPair();
                        byte[] publicKey = keyPair.getPublic().getEncoded();
                        byte[] verifyToken = GetRandomBytes( 4 );

                        EncryptionKeyRequestPacket encRequestPacket = new EncryptionKeyRequestPacket() {
                            ServerID = "-",
                            PublicKey = publicKey,
                            PublicKeyLength = ( short )publicKey.Length,
                            VerifyToken = verifyToken,
                            VerifyTokenLength = ( short )verifyToken.Length
                        };

                        // Send the packet
                        Logger.LogToConsole( "Sending encryption request packet" );
                        encRequestPacket.Write( smpWriter );

                        // Initialize encrytion response packet
                        EncryptionKeyResponsePacket encResponsePacket = new EncryptionKeyResponsePacket();

                        // Read the packet
                        Logger.LogToConsole( "Reading encryption response packet" );
                        encResponsePacket.Read( smpReader );

                        // Verify token
                        byte[] decryptedVerifyToken = RSADecrypt( encResponsePacket.VerifyTokenResponse, keyPair.getPrivate() );

                        if ( BitConverter.ToInt32( verifyToken, 0 ).Equals( BitConverter.ToInt32( decryptedVerifyToken, 0 ) ) ) {
                            // Send encryption response with empty payload
                            Logger.LogToConsole( "Sending encryption response packet" );

                            new EncryptionKeyResponsePacket() {
                                SharedSecret = new byte[0],
                                SharedSecretLength = 0,
                                VerifyTokenResponse = new byte[0],
                                VerifyTokenLength = 0
                            }.Write( smpWriter );

                            // Decrypt client's shared key
                            byte[] decryptedSharedKey = RSADecrypt( encResponsePacket.SharedSecret, keyPair.getPrivate() );

                            // Switch to encrypted stream
                            Logger.LogToConsole( "Switching to an encrypted stream" );
                            EncryptedStream = new AesStream( stream, decryptedSharedKey );
                            smpReader = new PacketReader( EncryptedStream );
                            smpWriter = new SMPPacketWriter( EncryptedStream );

                            Logger.LogToConsole( "Sending client statuses packet" );
                            ClientStatusesPacket clientStatusesPacket = new ClientStatusesPacket();
                            smpReader.ReadByte();
                            clientStatusesPacket.Read( smpReader );

                            Logger.LogToConsole( "Sending Login request" );
                            new LoginRequestPacket() {
                                EntityID = 15,
                                LevelType = LevelType.FLAT,
                                Gamemode = ( byte )1,
                                Dimension = ( byte )0,
                                Difficulty = ( byte )0,
                                NotUsed = ( byte )0,
                                MaxPlayers = ( byte )8
                            }.Write( smpWriter );

                            Logger.LogToConsole( "Sending Spawn-Position Packet" );
                            new SpawnPositionPacket() { X = 120, Y = 200, Z = 120 }.Write( smpWriter );

                            Logger.LogToConsole( "Sending time" );
                            smpWriter.Write( PacketType.TIME_UPDATE );
                            long l = 5000;
                            long l2 = 5;
                            smpWriter.Write( l );
                            smpWriter.Write( l2 );
                            smpWriter.Flush();

                            Logger.LogToConsole( "Sending locale" );
                            LocaleAndViewDistancePacket lavdPacket = new LocaleAndViewDistancePacket();
                            smpReader.ReadByte();
                            lavdPacket.Read( smpReader );
                            PacketType type = ( PacketType )smpReader.ReadByte();
                            Logger.LogToConsole( type.ToString() );

                            /*Logger.LogToConsole( "Sending Init" );
                            State = SessionState.LoadingMain;
                            SMPPacketWriter.MakeInitChunk( new Chunk( 1, 1, WorldManager.MainWorld ), smpWriter );

                            Logger.LogToConsole( "Sending Map" );
                            SMPPacketWriter.MakeMapChunkPacket( new Chunk( 1, 1, WorldManager.MainWorld ), smpWriter );*/

                            UpdateChunks();

                            Logger.LogToConsole( "Sending Pos+Look Packet" );
                            new PositionAndLookPacket() {
                                X = 120,
                                L = 0,
                                R = 0,
                                Z = 120,
                                Stance = 120,
                                Y = 200,
                                OnGround = true
                            }.Write( smpWriter );
                            Position = new fCraft.Position( 120, 120, 120 );
                            //WorldManager.MainWorld.Players.Send( PacketWriter.MakeAddEntity( 15, "Jonty800", Position ) );

                            new PositionAndLookPacket().Read( smpReader );

                            // Info.ProcessLogin( this ); <-----throws null, find out why
                            World = WorldManager.MainWorld;
                            Position = WorldManager.MainWorld.Map.Spawn;

                            Info = PlayerDB.FindOrCreateInfoForPlayer( Name, IP );
                            ResetAllBinds();
                            if ( RaisePlayerConnectingEvent( this ) ) return;
                            HasFullyConnected = true;
                            State = SessionState.Online;
                            Server.UpdatePlayerList();
                            RaisePlayerReadyEvent( this );

                        } else {
                            new DisconnectPacket() {
                                Message = "Verification tokens don't match."
                            }.Write( smpWriter );

                            stream.Close();
                        }

                        break;
                    default:
                        Logger.LogToConsole( packet.ToString() );
                        Logger.LogToConsole( "Unknown packet received" );
                        break;
                }
            } catch ( Exception ex ) {
                Logger.LogToConsole( "Error in player's session: " + ex + ex.StackTrace );

                stream.Close();
            }
        }