Advertisement
Guest User

Untitled

a guest
May 10th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1.     public void read() throws IOException {
  2.         while(connection.available() >= 0) {
  3.             switch(state) {
  4.             case READ_OPCODE:
  5.                 if(connection.available() >= 1) {
  6.                     int opcode = connection.readByte() & 0xFF;
  7.                     if(opcode == Opcodes.PROTOCOL_UPDATE) {
  8.                         state = State.UPDATE_READ_VERSION;
  9.                     } else if(opcode == Opcodes.PROTOCOL_WORLD_LIST) {
  10.                         state = State.WORLD_LIST_READ_OPCODE;
  11.                     } else if(opcode == Opcodes.PROTOCOL_GAME) {
  12.                         state = State.GAME_READ_NAME_HASH;
  13.                     } else {
  14.                         return;
  15.                     }
  16.                 } else {
  17.                     return;
  18.                 }
  19.                 break;
  20.             case GAME_READ_NAME_HASH:
  21.                 if(connection.available() >= 1) {
  22.                     int hash = connection.readByte() & 0xFF;
  23.                     serverKey = (((int) (Math.random() * 999999D)) << 32) + ((int) (Math.random() * 999999D));
  24.                     connection.write((byte) 0);
  25.                     connection.write((long) serverKey);
  26.                     connection.flush();
  27.                     state = State.GAME_READ_TYPE;
  28.                 } else {
  29.                     return;
  30.                 }
  31.                 break;
  32.             case GAME_READ_TYPE:
  33.                 if(connection.available() >= 3) {
  34.                     int type        = connection.readByte()  & 0xFF;
  35.                     loginPacketSize = connection.readShort() & 0xFFFF;
  36.                     state = State.GAME_READ_LOGIN_BLOCK;
  37.                 } else {
  38.                     return;
  39.                 }
  40.                 break;
  41.             case GAME_READ_LOGIN_BLOCK:
  42.                 if(connection.available() >= loginPacketSize) {
  43.                     int version = connection.readInt();
  44.                     connection.readByte();
  45.                     byte lowMemory = connection.readByte();
  46.                     connection.readByte();
  47.                     connection.readByte();
  48.                     short s1 = connection.readShort();
  49.                     short s2 = connection.readShort();
  50.                     connection.readByte();
  51.                     for(int i = 0; i < 24; i++) {
  52.                         connection.readByte();
  53.                     }
  54.                     readString(connection);
  55.                     connection.readInt();
  56.                     connection.readInt();
  57.                     connection.readShort();
  58.                     for(int i = 0; i < 28; i++) {
  59.                         connection.readInt();
  60.                     }
  61.                     connection.readShort();
  62.                     long clientKey = connection.readLong();
  63.                     long serverKey = connection.readLong();
  64.                     String user = Conversions.longToString(connection.readLong());
  65.                     String pass = readString(connection);
  66.                     returnCode = login(user, pass);
  67.                     connection.write((byte) returnCode);
  68.                     connection.flush();
  69.                     if(returnCode != 2) {
  70.                         connection.close();
  71.                         return;
  72.                     } else {
  73.                         int sessionKey[] = new int[4];
  74.                         sessionKey[0] = (int) (clientKey >> 32);
  75.                         sessionKey[1] = (int) clientKey;
  76.                         sessionKey[2] = (int) (serverKey >> 32);
  77.                         sessionKey[3] = (int) serverKey;
  78.                         state = State.GAME_STANDARD;
  79.                         inCipher = new IsaacCipher(sessionKey);
  80.                         for(int i = 0; i < 4; i++) {
  81.                             sessionKey[i] += 50;
  82.                         }
  83.                         outCipher = new IsaacCipher(sessionKey);
  84.                     }
  85.                 } else {
  86.                     return;
  87.                 }
  88.                 break;
  89.             case WORLD_LIST_READ_OPCODE:
  90.                 if(connection.available() >= 4) {
  91.                     int opcode = connection.readInt() & 0xFFFFFFFF;
  92.                     if(opcode == Opcodes.WORLD_LIST_GET_DATA) {
  93.                         connection.write((byte) 0);
  94.                         ByteBuffer worldListData = WorldList.getData();
  95.                         connection.write((short) worldListData.limit());
  96.                         connection.write(worldListData);
  97.                         connection.flush();
  98.                         connection.close();
  99.                         return;
  100.                     } else if(opcode == Opcodes.WORLD_LIST_GET_STATUS) {
  101.                         connection.write((byte) 0);
  102.                         ByteBuffer worldListData = WorldList.getStatus();
  103.                         connection.write((short) worldListData.limit());
  104.                         connection.write(worldListData);
  105.                         connection.flush();
  106.                         connection.close();
  107.                         return;
  108.                     } else {
  109.                         connection.close();
  110.                         return;
  111.                     }
  112.                 } else {
  113.                     return;
  114.                 }
  115.             case UPDATE_READ_VERSION:
  116.                 if(connection.available() >= 4) {
  117.                     int version = connection.readInt() & 0xFFFF;
  118.                     if(version == Main.VERSION) {
  119.                         connection.write((byte) 0);
  120.                         connection.flush();
  121.                         state = State.UPDATE_READ_INTS;
  122.                     } else {
  123.                         connection.close();
  124.                         return;
  125.                     }
  126.                 } else {
  127.                     return;
  128.                 }
  129.                 break;
  130.             case UPDATE_READ_INTS:
  131.                 if(connection.available() >= 4) {
  132.                     connection.readInt();
  133.                     ByteBuffer buf = ByteBuffer.allocate(Main.UPDATE_KEYS.length);
  134.                     for(int key : Main.UPDATE_KEYS) {
  135.                         buf.put((byte) key);
  136.                     }
  137.                     buf.flip();
  138.                     connection.write(buf);
  139.                     connection.flush();
  140.                     state = State.UPDATE_READ_CONFIRMATION;
  141.                 } else {
  142.                     return;
  143.                 }
  144.                 break;
  145.             case UPDATE_READ_CONFIRMATION:
  146.                 if(connection.available() >= 4) {
  147.                     connection.readInt();
  148.                     state = State.READ_OPCODE;
  149.                     return;
  150.                 } else {
  151.                     return;
  152.                 }
  153.             }
  154.         }
  155.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement