Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.UnknownHostException;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import javax.swing.JFrame;
- import javax.swing.JOptionPane;
- public class Loader
- extends JFrame
- implements Runnable
- {
- private static final int VERSION = 4;
- private static String loadingText = "Launching US2..";
- private static int loadingProgress = 95;
- private boolean waitingForDownload = true;
- private final ExecutorService workService = Executors.newSingleThreadExecutor();
- private int expectedVersion;
- private String expectedHash;
- private String upgradeUrl;
- private boolean outOfDate;
- private String webclientJar;
- private final File localJar = new File(Utilities.findcachedir() + "US2.jar");
- private Thread thread = new Thread(this, "US2 Launcher");
- public void startThread()
- {
- this.thread.start();
- }
- public static void setLoadingText(int loadingProgress, String loadingText)
- {
- loadingProgress = loadingProgress;
- loadingText = loadingText;
- }
- public String generateHash()
- {
- File file = new File(Utilities.findcachedir() + "US2.jar");
- byte[] bytes = null;
- try
- {
- bytes = Utilities.getBytesFromFile(file);
- }
- catch (IOException e)
- {
- e.printStackTrace();
- return "unknown";
- }
- return MD5HashGenerator.getHash(bytes);
- }
- public boolean readWebsiteConfigurations()
- {
- BufferedReader reader = null;
- try
- {
- URL url = new URL("http://server.ultimatescape2.com/client_config.cfg");
- conn = url.openConnection();
- conn.addRequestProperty("User-Agent",
- "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
- conn.setConnectTimeout(10000);
- conn.setReadTimeout(10000);
- reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- }
- catch (Exception e)
- {
- setLoadingText(100, "Error connecting to server. Please try again later.");
- e.printStackTrace();
- }
- try
- {
- if (reader != null)
- {
- String line = null;
- while ((line = reader.readLine()) != null) {
- if (!line.startsWith("#"))
- {
- line = line.trim();
- if (line.startsWith("loader_version")) {
- this.expectedVersion = Integer.parseInt(line.substring(line.indexOf('=') + 1));
- } else if (line.startsWith("loader_hash")) {
- this.expectedHash = line.substring(line.indexOf('=') + 1);
- } else if (line.startsWith("upgrade_url")) {
- this.upgradeUrl = line.substring(line.indexOf('=') + 1);
- } else if (line.startsWith("webclient_jar")) {
- this.webclientJar = line.substring(line.indexOf('=') + 1);
- }
- }
- }
- setLoadingText(100, "Lauching US2 Client..");
- return true;
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- finally
- {
- if (reader != null) {
- try
- {
- reader.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- if (reader != null) {
- try
- {
- reader.close();
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- return false;
- }
- public static void main(String[] args)
- {
- Loader loader = new Loader();
- loader.startThread();
- loader.setVisible(true);
- }
- public void run()
- {
- for (;;)
- {
- try
- {
- Thread.sleep(50L);
- }
- catch (InterruptedException e)
- {
- break;
- }
- super.repaint();
- }
- }
- public void paint(Graphics g)
- {
- int x = 33;
- int y = 25;
- int width = 773;
- int height = 538;
- g.setColor(Color.BLACK);
- g.fillRect(0, 0, width, height);
- g.setColor(Color.RED);
- g.drawRect(x, y, width - 72, height - 508);
- g.fillRect(x + 2, y + 2, loadingProgress * ((width - 3) / 100) - 2, 27);
- g.setColor(Color.WHITE);
- g.drawString(loadingText, 310, 45);
- }
- public Loader()
- {
- setTitle("US2 Launcher - http://ultimatescape2.com");
- setDefaultCloseOperation(3);
- setUndecorated(true);
- setSize(760, 80);
- setLocationRelativeTo(null);
- setVisible(true);
- final Loader instance = this;
- setLoadingText(95, "Launching US2 Client...");
- this.workService.submit(new Runnable()
- {
- boolean initiateDownload = Loader.this.readWebsiteConfigurations();
- public void run()
- {
- if (4 < Loader.this.expectedVersion)
- {
- 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));
- Loader.this.outOfDate = true;
- if (option == 0)
- {
- Utilities.launchURL(Loader.this.upgradeUrl);
- System.exit(0);
- return;
- }
- }
- if ((Loader.this.expectedHash != null) && (Loader.this.localJar.exists()))
- {
- byte[] bytes = null;
- try
- {
- bytes = Utilities.getBytesFromFile(Loader.this.localJar);
- }
- catch (IOException e)
- {
- Loader.setLoadingText(100, "Error code 106");
- e.printStackTrace();
- }
- if (bytes != null)
- {
- String hash = MD5HashGenerator.getHash(bytes);
- if (!hash.equals(Loader.this.expectedHash)) {
- Loader.this.localJar.delete();
- } else {
- this.initiateDownload = false;
- }
- }
- }
- if (this.initiateDownload) {
- try
- {
- Utilities.download(Loader.this.webclientJar, Loader.this.localJar);
- }
- catch (UnknownHostException e)
- {
- Loader.setLoadingText(0, "Error connecting to server!");
- }
- catch (Exception e)
- {
- Loader.setLoadingText(0, "Error code 100");
- e.printStackTrace();
- }
- }
- Loader.this.waitingForDownload = false;
- }
- });
- while (this.waitingForDownload) {
- try
- {
- Thread.sleep(50L);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- this.workService.submit(new Runnable()
- {
- public void run()
- {
- try
- {
- Runtime.getRuntime().exec("java -Xmx1024m -jar " + Loader.this.localJar.getAbsolutePath());
- }
- catch (IOException e1)
- {
- e1.printStackTrace();
- }
- Loader.this.workService.shutdown();
- System.exit(0);
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement