Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Joehot200;
- import java.awt.Dimension;
- import java.awt.EventQueue;
- import java.awt.Toolkit;
- import java.io.BufferedReader;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.net.HttpURLConnection;
- import java.net.ProtocolException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.List;
- import javax.swing.JFrame;
- import javax.swing.JProgressBar;
- public class Main extends JFrame{
- String version = "0.0";
- boolean needDownload = false;
- static JFrame fr;
- public static void main(String[] args){
- fr = new Main();
- }
- public void needDownload(){
- {
- File f = new File(System.getProperty("user.dir") + "/ver.txt");
- try {
- List<String> lines = Files.readAllLines(Paths.get(f.getAbsolutePath()), StandardCharsets.UTF_8);
- for (String str : lines){
- version = str;
- System.out.println("Version: " + str);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- progressBar.setValue(25);
- {
- StringBuilder page = new StringBuilder();
- URL google;
- try {
- google = new URL("http://endcraft.net/ver");
- //URL google = new URL("www.endcraft.net/Post");
- URLConnection yc = google.openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
- String inputLine;
- progressBar.setValue(50);
- while ((inputLine = in.readLine()) != null) {
- System.out.println(inputLine + " || " + version);
- if (!inputLine.contains(version)){
- needDownload = true;
- System.out.println("Downloading!");
- File f = new File(System.getProperty("user.dir") + "/ver.txt");
- if (f.exists()) f.delete();
- f.createNewFile();
- PrintWriter writer = new PrintWriter(f.getAbsoluteFile(), "UTF-8");
- writer.println(inputLine);
- System.out.println("Updated to version " + inputLine + " from version " + version);
- writer.close();
- }else{
- System.out.println("No downloaded needed!");
- }
- }
- progressBar.setValue(100);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //RES TODO
- {
- /*StringBuilder page = new StringBuilder();
- URL google;
- try {
- google = new URL("http://endcraft.net/verres");
- //URL google = new URL("www.endcraft.net/Post");
- URLConnection yc = google.openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
- String inputLine;
- while ((inputLine = in.readLine()) != null) {
- System.out.println(inputLine + " || " + versionRes);
- if (!inputLine.contains(versionRes)){
- downloadRes = true;
- System.out.println("Downloading res!");
- }else{
- System.out.println("No res downloaded needed!");
- }
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }*/
- }
- }
- //boolean downloadRes = false;
- JProgressBar progressBar;
- JProgressBar oprogressBar;
- int i = 0;
- List<Thread> threads = new ArrayList<Thread>();
- long lastTime = 0;
- public Main() {
- fr = this;
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- int width = (int) screenSize.getWidth();
- int height = (int) screenSize.getHeight();
- fr.setTitle("Downloading game...");
- fr.setBounds(width / 2 - 200, height / 2 - 50, 400, 100);
- fr.setVisible(true);
- progressBar = new JProgressBar(0);
- oprogressBar = new JProgressBar(0);
- progressBar.setBounds(0, 0, 400, 50);
- oprogressBar.setBounds(0, 50, 400, 50);
- fr.add(progressBar);
- fr.add(oprogressBar);
- progressBar.setValue(0);
- oprogressBar.setValue(0);
- needDownload();
- //if (downloadRes){
- {
- {
- File f = new File("res/");
- if (!f.exists()){
- if (!f.mkdir()){
- System.out.println("Unable to create dir!!");
- System.exit(1);
- }else{
- System.out.println("Created dir!");
- }
- }else{
- System.out.println("Dir already exists");
- }
- }
- {
- File f = new File("res/natives-win");
- if (!f.exists()){
- if (!f.mkdir()){
- System.out.println("Unable to create dir!!");
- System.exit(1);
- }else{
- System.out.println("Created dir!");
- }
- }else{
- System.out.println("Dir already exists");
- }
- }
- progressBar.setValue(10);
- try{
- {
- URL url = new URL("http://www.endcraft.net/webstart/res/download.txt");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("HEAD");
- File fi = new File("res/download.txt");
- System.out.println((fi.length() != conn.getContentLength()) + " " + fi.length() + " || " + conn.getContentLength());
- if (!fi.exists() || fi.length() != conn.getContentLength()){
- System.out.println("Downloading resource!");
- download("http://www.endcraft.net/webstart/res/download.txt", "res/download.txt", conn.getContentLength());
- }else{
- System.out.println("No need to download resource!");
- }
- }
- progressBar.setValue(15);
- ArrayList<String> download = readTextFile("res/download.txt");
- for (final String str : download){
- URL url = new URL("http://www.endcraft.net/webstart/res/" + str);
- final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- progressBar.setValue(30);
- conn.setRequestMethod("HEAD");
- File fi = new File("res/" + str);
- if (!fi.getParentFile().exists()){
- fi.getParentFile().mkdirs();
- }
- System.out.println((fi.length() != conn.getContentLength()) + " " + fi.length() + " || " + conn.getContentLength());
- if (!fi.exists() || fi.length() != conn.getContentLength()){
- System.out.println("Downloading resource!");
- progressBar.setValue(50);
- Thread t = new Thread() {
- @Override public void run() {
- download("http://www.endcraft.net/webstart/res/" + str, "res/" + str, conn.getContentLength());
- }
- };
- t.start();
- threads.add(t);
- }else{
- System.out.println("No need to download resource!");
- progressBar.setValue(100);
- }
- }
- i++;
- oprogressBar.setValue((int) ((i * 100.0f) / download.size() - threads.size()));
- }catch (Exception e){
- }
- }
- progressBar.setValue(50);
- File f = new File("privateers.jar");
- System.out.println("Exists: " + f.exists());
- try{
- URL url = new URL("http://www.endcraft.net/webstart/privateers.jar");
- final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("HEAD");
- if (needDownload || !f.exists() || f.length() != conn.getContentLength()){
- Thread t = new Thread() {
- @Override public void run() {
- download("http://www.endcraft.net/webstart/privateers.jar", "privateers.jar", conn.getContentLength());
- }
- };
- t.start();
- threads.add(t);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- for (Thread t : threads) {
- try {
- t.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- progressBar.setValue(95);
- try {
- File dir = new File("settings");
- if (!dir.exists()){
- dir.mkdirs();
- }
- String exec = (System.getProperty("user.dir") + java.io.File.separator + "privateers.jar");
- String[] command = {"java", "-jar", exec};
- ProcessBuilder pb =
- new ProcessBuilder(command[0], command[1], command[2]);
- progressBar.setValue(100);
- System.out.println(pb.command());
- pb.start();
- //final Process process = Runtime.getRuntime().exec(command);
- System.out.println("Running " + exec);
- //while (true){
- //System.out.println(process.waitFor() + " || " + process.getOutputStream());
- System.exit(0);
- //}
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- void download(String source, String destination, int size) {
- // ten percent of the total download size
- File ofile = new File(System.getProperty("user.dir") + "", destination);
- System.out.printf("\nDownloading\n\t%s\nTo\n\t%s\n", source, destination);
- try {
- if (ofile.exists()) ofile.delete();
- if (!ofile.createNewFile()) {
- throw new IOException("Can't create " + ofile.getAbsolutePath());
- }
- int inChar = 0;
- URL url = new URL(source);
- InputStream input = url.openStream();
- FileOutputStream fos = new FileOutputStream(ofile);
- for (int i = 0; i < size && inChar != -1; i++) {
- if (System.nanoTime() > lastTime + 2000000000){
- lastTime = System.nanoTime();
- int percentage = (int) ((i * 100.0f) / size);
- progressBar.setValue(((int) ((percentage * 100.0f) / 100)));
- fr.setTitle(ofile.getName() + ": " + progressBar.getValue() + "%" + " Total: " + oprogressBar.getValue() + "%");
- }
- inChar = input.read();
- fos.write(inChar);
- }
- i++;
- oprogressBar.setValue((int) ((i * 100.0f) / threads.size()));
- input.close();
- fos.close();
- System.out.println("Downloaded " + ofile.getAbsolutePath());
- } catch (EOFException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static ArrayList<String> readTextFile(String fileName) {
- ArrayList<String> values = new ArrayList<String>();
- FileReader file = null;
- try {
- file = new FileReader(fileName);
- BufferedReader reader = new BufferedReader(file);
- String line = "";
- while ((line = reader.readLine()) != null) {
- //////System.out.println(line);
- //line = line.replace("~", "");
- //if (!line.endsWith("~")) line = line + "~";
- //if (line == "" || line.startsWith("~") || line.length() <= 1){
- // line = ChatColor.GREEN + " ~";
- //}
- values.add(line);
- //////System.out.println(line);
- }
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException(e);
- } finally {
- if (file != null) {
- try {
- file.close();
- } catch (IOException e) {
- // Ignore issues during closing
- }
- }
- }
- //String[] returnValue = new String[values.size()];
- //for ()
- return values;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement