Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. package com.rs.entities;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. import com.rs2.jindex.*;
  7.  
  8. public class World extends Thread
  9. {
  10.  
  11. private final static int buffersize = 5000;
  12.  
  13. private java.net.Socket socket;
  14.  
  15. private InputStream in;
  16.  
  17. private OutputStream out;
  18.  
  19. public byte buffer[];
  20.  
  21. private int playerid;
  22.  
  23. private int readptr;
  24.  
  25. private int writeptr;
  26.  
  27. private stream instream;
  28.  
  29. private stream outstream;
  30.  
  31. private Cryption instreamdecryption;
  32.  
  33. private Cryption outstreamdecryption;
  34.  
  35. public int packetsize = 0, packettype = -1;
  36.  
  37. private int returncode = 2;
  38.  
  39. private int rights;
  40.  
  41. private boolean isrunning = true;
  42.  
  43. private String username;
  44.  
  45. private String password;
  46.  
  47. private Player player;
  48.  
  49. public World()
  50. {
  51. new World(socket, playerid);
  52. username = player.username;
  53. password = player.password;
  54. }
  55.  
  56. public World(Player player)
  57. {
  58. this.player = player;
  59. }
  60.  
  61. public World(java.net.Socket socket, int playerid)
  62. {
  63. this.playerid = playerid;
  64. this.socket = socket;
  65. try
  66. {
  67. in = socket.getInputStream();
  68. out = socket.getOutputStream();
  69. } catch(IOException ioe)
  70. {
  71. System.err.println(ioe.getMessage());
  72. }
  73. outstream = new stream(new byte[5000]);
  74. outstream.currentOffset = 0;
  75. instream = new stream(new byte[5000]);
  76. instream.currentOffset = 0;
  77.  
  78. readptr = writeptr = 0;
  79. buffer = buffer = new byte[5000];
  80. }
  81.  
  82. public void run()
  83. {
  84. long serversessionkey = 0;
  85. long clientsessionkey = 0;
  86. serversessionkey = ((long)(Math.random() * 99999999D) << 32) + (long)(Math.random() * 99999999D);
  87.  
  88. try
  89. {
  90. fillinstream(2);
  91. if(instream.readUnsignedByte() != 14)
  92. {
  93. System.err.println("Needed 14 login id, received " + instream.readUnsignedByte());
  94. return;
  95. }
  96. int namepart = instream.readUnsignedByte();
  97. for(int i = 0; i < 8; i++)
  98. {
  99. out.write(0);
  100. }
  101. out.write(0);
  102. outstream.writeQWord(serversessionkey);
  103. directFlushoutstream();
  104. fillinstream(2);
  105. int loginType = instream.readUnsignedByte();
  106. if(loginType != 16 && loginType != 18)
  107. {
  108. System.out.println("Needed 16 or 18 login response, received " + loginType);
  109. return;
  110. }
  111. int loginpacketsize = instream.readUnsignedByte();
  112. int loginencryptpacketsize = loginpacketsize-(36+1+1+2);
  113. fillinstream(loginpacketsize);
  114. if(instream.readUnsignedByte() != 255 || instream.readUnsignedWord() != 317)
  115. {
  116. System.out.println("Wrong login packet id, needed 255, 317");
  117. return;
  118. }
  119. int lowmemoryversion = instream.readUnsignedByte();
  120. loginencryptpacketsize--;
  121. int temp = instream.readUnsignedByte();
  122. if(loginencryptpacketsize != temp)
  123. {
  124. System.out.println("Encrypted packet data length " + loginencryptpacketsize + " different from length byte of " + temp);
  125. return;
  126. }
  127. temp = instream.readUnsignedByte();
  128. if(temp != 10)
  129. {
  130. System.out.println("Tempt is not equal to 10");
  131. return;
  132. }
  133. clientsessionkey = instream.readQWord();
  134. serversessionkey = instream.readQWord();
  135. username = instream.readString();
  136. password = instream.readString();
  137. int sessionkey[] = new int[4];
  138. sessionkey[0] = (int)(clientsessionkey >> 32);
  139. sessionkey[1] = (int)clientsessionkey;
  140. sessionkey[2] = (int)(serversessionkey >> 32);
  141. sessionkey[3] = (int)serversessionkey;
  142. instreamdecryption = new Cryption(sessionkey);
  143. outstreamdecryption = new Cryption(sessionkey);
  144. outstream.packetEncryption = outstreamdecryption;
  145.  
  146. returncode = 2;
  147.  
  148. rights = 2;
  149.  
  150. PlayerHandler handler = new PlayerHandler();
  151. boolean bool = true;
  152. if(bool)
  153. {
  154. player.x = 3200;
  155. player.y = 3200;
  156. player.h = 0;
  157. }
  158. out.write(returncode);
  159. out.write(rights);
  160. out.write(0);
  161. } catch(Exception exp)
  162. {
  163. System.out.println(exp.getMessage());
  164. return;
  165. }
  166. if(playerid == -1 || returncode != 2)
  167. {
  168. return;
  169. }
  170. packetsize = 0;
  171. packettype = -1;
  172. readptr = 0;
  173. writeptr = 0;
  174. int numbytesinbuffer;
  175. int offset;
  176. while(isrunning)
  177. {
  178. synchronized(this)
  179. {
  180. if(writeptr == readptr)
  181. {
  182. try
  183. {
  184. wait();
  185. } catch(Exception ex)
  186. {
  187. System.out.println(ex.getMessage());
  188. }
  189. }
  190. offset = readptr;
  191. if(writeptr >= readptr)
  192. {
  193. numbytesinbuffer = writeptr - readptr;
  194. } else
  195. {
  196. numbytesinbuffer = 5000 - readptr;
  197. }
  198. }
  199. if(numbytesinbuffer > 0)
  200. {
  201. try
  202. {
  203. out.write(buffer, offset, numbytesinbuffer);
  204. readptr = (readptr + numbytesinbuffer) % buffersize;
  205. if(writeptr == readptr)
  206. {
  207. out.flush();
  208. }
  209. } catch(Exception ex)
  210. {
  211. System.out.println(ex.getMessage());
  212. }
  213. }
  214. }
  215. return;
  216. }
  217.  
  218. private void fillinstream(int forceRead) throws IOException
  219. {
  220. instream.currentOffset = 0;
  221. in.read(instream.buffer, 0, forceRead);
  222. }
  223.  
  224. private void directFlushoutstream() throws java.io.IOException
  225. {
  226. out.write(outstream.buffer, 0, outstream.currentOffset);
  227. outstream.currentOffset = 0; // reset
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement