Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void read() throws IOException {
- while(connection.available() >= 0) {
- switch(state) {
- case READ_OPCODE:
- if(connection.available() >= 1) {
- int opcode = connection.readByte() & 0xFF;
- if(opcode == Opcodes.PROTOCOL_UPDATE) {
- state = State.UPDATE_READ_VERSION;
- } else if(opcode == Opcodes.PROTOCOL_WORLD_LIST) {
- state = State.WORLD_LIST_READ_OPCODE;
- } else if(opcode == Opcodes.PROTOCOL_GAME) {
- state = State.GAME_READ_NAME_HASH;
- } else {
- return;
- }
- } else {
- return;
- }
- break;
- case GAME_READ_NAME_HASH:
- if(connection.available() >= 1) {
- int hash = connection.readByte() & 0xFF;
- serverKey = (((int) (Math.random() * 999999D)) << 32) + ((int) (Math.random() * 999999D));
- connection.write((byte) 0);
- connection.write((long) serverKey);
- connection.flush();
- state = State.GAME_READ_TYPE;
- } else {
- return;
- }
- break;
- case GAME_READ_TYPE:
- if(connection.available() >= 3) {
- int type = connection.readByte() & 0xFF;
- loginPacketSize = connection.readShort() & 0xFFFF;
- state = State.GAME_READ_LOGIN_BLOCK;
- } else {
- return;
- }
- break;
- case GAME_READ_LOGIN_BLOCK:
- if(connection.available() >= loginPacketSize) {
- int version = connection.readInt();
- connection.readByte();
- byte lowMemory = connection.readByte();
- connection.readByte();
- connection.readByte();
- short s1 = connection.readShort();
- short s2 = connection.readShort();
- connection.readByte();
- for(int i = 0; i < 24; i++) {
- connection.readByte();
- }
- readString(connection);
- connection.readInt();
- connection.readInt();
- connection.readShort();
- for(int i = 0; i < 28; i++) {
- connection.readInt();
- }
- connection.readShort();
- long clientKey = connection.readLong();
- long serverKey = connection.readLong();
- String user = Conversions.longToString(connection.readLong());
- String pass = readString(connection);
- returnCode = login(user, pass);
- connection.write((byte) returnCode);
- connection.flush();
- if(returnCode != 2) {
- connection.close();
- return;
- } else {
- int sessionKey[] = new int[4];
- sessionKey[0] = (int) (clientKey >> 32);
- sessionKey[1] = (int) clientKey;
- sessionKey[2] = (int) (serverKey >> 32);
- sessionKey[3] = (int) serverKey;
- state = State.GAME_STANDARD;
- inCipher = new IsaacCipher(sessionKey);
- for(int i = 0; i < 4; i++) {
- sessionKey[i] += 50;
- }
- outCipher = new IsaacCipher(sessionKey);
- }
- } else {
- return;
- }
- break;
- case WORLD_LIST_READ_OPCODE:
- if(connection.available() >= 4) {
- int opcode = connection.readInt() & 0xFFFFFFFF;
- if(opcode == Opcodes.WORLD_LIST_GET_DATA) {
- connection.write((byte) 0);
- ByteBuffer worldListData = WorldList.getData();
- connection.write((short) worldListData.limit());
- connection.write(worldListData);
- connection.flush();
- connection.close();
- return;
- } else if(opcode == Opcodes.WORLD_LIST_GET_STATUS) {
- connection.write((byte) 0);
- ByteBuffer worldListData = WorldList.getStatus();
- connection.write((short) worldListData.limit());
- connection.write(worldListData);
- connection.flush();
- connection.close();
- return;
- } else {
- connection.close();
- return;
- }
- } else {
- return;
- }
- case UPDATE_READ_VERSION:
- if(connection.available() >= 4) {
- int version = connection.readInt() & 0xFFFF;
- if(version == Main.VERSION) {
- connection.write((byte) 0);
- connection.flush();
- state = State.UPDATE_READ_INTS;
- } else {
- connection.close();
- return;
- }
- } else {
- return;
- }
- break;
- case UPDATE_READ_INTS:
- if(connection.available() >= 4) {
- connection.readInt();
- ByteBuffer buf = ByteBuffer.allocate(Main.UPDATE_KEYS.length);
- for(int key : Main.UPDATE_KEYS) {
- buf.put((byte) key);
- }
- buf.flip();
- connection.write(buf);
- connection.flush();
- state = State.UPDATE_READ_CONFIRMATION;
- } else {
- return;
- }
- break;
- case UPDATE_READ_CONFIRMATION:
- if(connection.available() >= 4) {
- connection.readInt();
- state = State.READ_OPCODE;
- return;
- } else {
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement