Advertisement
Guest User

Untitled

a guest
Dec 29th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.net.UnknownHostException;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12. import javax.swing.JFrame;
  13. import javax.swing.JOptionPane;
  14.  
  15. public class Loader
  16. extends JFrame
  17. implements Runnable
  18. {
  19. private static final int VERSION = 4;
  20. private static String loadingText = "Launching US2..";
  21. private static int loadingProgress = 95;
  22. private boolean waitingForDownload = true;
  23. private final ExecutorService workService = Executors.newSingleThreadExecutor();
  24. private int expectedVersion;
  25. private String expectedHash;
  26. private String upgradeUrl;
  27. private boolean outOfDate;
  28. private String webclientJar;
  29. private final File localJar = new File(Utilities.findcachedir() + "US2.jar");
  30. private Thread thread = new Thread(this, "US2 Launcher");
  31.  
  32. public void startThread()
  33. {
  34. this.thread.start();
  35. }
  36.  
  37. public static void setLoadingText(int loadingProgress, String loadingText)
  38. {
  39. loadingProgress = loadingProgress;
  40. loadingText = loadingText;
  41. }
  42.  
  43. public String generateHash()
  44. {
  45. File file = new File(Utilities.findcachedir() + "US2.jar");
  46. byte[] bytes = null;
  47. try
  48. {
  49. bytes = Utilities.getBytesFromFile(file);
  50. }
  51. catch (IOException e)
  52. {
  53. e.printStackTrace();
  54. return "unknown";
  55. }
  56. return MD5HashGenerator.getHash(bytes);
  57. }
  58.  
  59. public boolean readWebsiteConfigurations()
  60. {
  61. BufferedReader reader = null;
  62. try
  63. {
  64. URL url = new URL("http://server.ultimatescape2.com/client_config.cfg");
  65. conn = url.openConnection();
  66. conn.addRequestProperty("User-Agent",
  67. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
  68. conn.setConnectTimeout(10000);
  69. conn.setReadTimeout(10000);
  70. reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  71. }
  72. catch (Exception e)
  73. {
  74. setLoadingText(100, "Error connecting to server. Please try again later.");
  75. e.printStackTrace();
  76. }
  77. try
  78. {
  79. if (reader != null)
  80. {
  81. String line = null;
  82. while ((line = reader.readLine()) != null) {
  83. if (!line.startsWith("#"))
  84. {
  85. line = line.trim();
  86. if (line.startsWith("loader_version")) {
  87. this.expectedVersion = Integer.parseInt(line.substring(line.indexOf('=') + 1));
  88. } else if (line.startsWith("loader_hash")) {
  89. this.expectedHash = line.substring(line.indexOf('=') + 1);
  90. } else if (line.startsWith("upgrade_url")) {
  91. this.upgradeUrl = line.substring(line.indexOf('=') + 1);
  92. } else if (line.startsWith("webclient_jar")) {
  93. this.webclientJar = line.substring(line.indexOf('=') + 1);
  94. }
  95. }
  96. }
  97. setLoadingText(100, "Lauching US2 Client..");
  98. return true;
  99. }
  100. }
  101. catch (Exception e)
  102. {
  103. e.printStackTrace();
  104. }
  105. finally
  106. {
  107. if (reader != null) {
  108. try
  109. {
  110. reader.close();
  111. }
  112. catch (IOException e)
  113. {
  114. e.printStackTrace();
  115. }
  116. }
  117. }
  118. if (reader != null) {
  119. try
  120. {
  121. reader.close();
  122. }
  123. catch (IOException e)
  124. {
  125. e.printStackTrace();
  126. }
  127. }
  128. return false;
  129. }
  130.  
  131. public static void main(String[] args)
  132. {
  133. Loader loader = new Loader();
  134. loader.startThread();
  135.  
  136. loader.setVisible(true);
  137. }
  138.  
  139. public void run()
  140. {
  141. for (;;)
  142. {
  143. try
  144. {
  145. Thread.sleep(50L);
  146. }
  147. catch (InterruptedException e)
  148. {
  149. break;
  150. }
  151. super.repaint();
  152. }
  153. }
  154.  
  155. public void paint(Graphics g)
  156. {
  157. int x = 33;
  158. int y = 25;
  159. int width = 773;
  160. int height = 538;
  161. g.setColor(Color.BLACK);
  162. g.fillRect(0, 0, width, height);
  163. g.setColor(Color.RED);
  164. g.drawRect(x, y, width - 72, height - 508);
  165. g.fillRect(x + 2, y + 2, loadingProgress * ((width - 3) / 100) - 2, 27);
  166. g.setColor(Color.WHITE);
  167. g.drawString(loadingText, 310, 45);
  168. }
  169.  
  170. public Loader()
  171. {
  172. setTitle("US2 Launcher - http://ultimatescape2.com");
  173. setDefaultCloseOperation(3);
  174. setUndecorated(true);
  175. setSize(760, 80);
  176. setLocationRelativeTo(null);
  177. setVisible(true);
  178.  
  179.  
  180.  
  181.  
  182. final Loader instance = this;
  183.  
  184.  
  185.  
  186.  
  187. setLoadingText(95, "Launching US2 Client...");
  188.  
  189.  
  190.  
  191.  
  192. this.workService.submit(new Runnable()
  193. {
  194. boolean initiateDownload = Loader.this.readWebsiteConfigurations();
  195.  
  196. public void run()
  197. {
  198. if (4 < Loader.this.expectedVersion)
  199. {
  200. int option = JOptionPane.showOptionDialog(instance, "A newer version of the client loader is available.", "Update Available", -1, 1, null, new Object[] { "Download (Recommended)", "Continue" }, Integer.valueOf(0));
  201. Loader.this.outOfDate = true;
  202. if (option == 0)
  203. {
  204. Utilities.launchURL(Loader.this.upgradeUrl);
  205. System.exit(0);
  206. return;
  207. }
  208. }
  209. if ((Loader.this.expectedHash != null) && (Loader.this.localJar.exists()))
  210. {
  211. byte[] bytes = null;
  212. try
  213. {
  214. bytes = Utilities.getBytesFromFile(Loader.this.localJar);
  215. }
  216. catch (IOException e)
  217. {
  218. Loader.setLoadingText(100, "Error code 106");
  219. e.printStackTrace();
  220. }
  221. if (bytes != null)
  222. {
  223. String hash = MD5HashGenerator.getHash(bytes);
  224. if (!hash.equals(Loader.this.expectedHash)) {
  225. Loader.this.localJar.delete();
  226. } else {
  227. this.initiateDownload = false;
  228. }
  229. }
  230. }
  231. if (this.initiateDownload) {
  232. try
  233. {
  234. Utilities.download(Loader.this.webclientJar, Loader.this.localJar);
  235. }
  236. catch (UnknownHostException e)
  237. {
  238. Loader.setLoadingText(0, "Error connecting to server!");
  239. }
  240. catch (Exception e)
  241. {
  242. Loader.setLoadingText(0, "Error code 100");
  243. e.printStackTrace();
  244. }
  245. }
  246. Loader.this.waitingForDownload = false;
  247. }
  248. });
  249. while (this.waitingForDownload) {
  250. try
  251. {
  252. Thread.sleep(50L);
  253. }
  254. catch (InterruptedException e)
  255. {
  256. e.printStackTrace();
  257. }
  258. }
  259. this.workService.submit(new Runnable()
  260. {
  261. public void run()
  262. {
  263. try
  264. {
  265. Runtime.getRuntime().exec("java -Xmx1024m -jar " + Loader.this.localJar.getAbsolutePath());
  266. }
  267. catch (IOException e1)
  268. {
  269. e1.printStackTrace();
  270. }
  271. Loader.this.workService.shutdown();
  272. System.exit(0);
  273. }
  274. });
  275. }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement