3
\$\begingroup\$

I have been creating a game with java, and before I release it I wanted to make a launcher that is connected to the game's website.

Basically I need to make a launcher that updates itself when I have changed the content.

Unfortunately I have no idea on how to do this. There would be a description in the main launcher which only I can change, and a large play button that will close the launcher and run the game. If I have changed the description, then it will change on the website download, and on the copy that anyone else owns (anywhere).

So, because of my lack of knowledge on this topic, does anyone know how I'm supposed to do this, and how it would work?

\$\endgroup\$
  • 2
    \$\begingroup\$ You would need to check for the version number on the server. If it's different from the local version then download the files that are different than the local ones (don't update things you don't need to). Also you'll need a way to update the launcher as well. Implementation depends from your project... \$\endgroup\$ – zoran404 Jun 25 '15 at 0:32
  • \$\begingroup\$ Does that not change the things in the launcher? \$\endgroup\$ – TechyEngineer Jun 25 '15 at 0:40
  • \$\begingroup\$ What do you mean? \$\endgroup\$ – zoran404 Jun 25 '15 at 1:05
  • 1
    \$\begingroup\$ To update the launcher you'll need another program. And they can update each other. But if you don't know enough to do this you should find an experienced programmer to do this for you. \$\endgroup\$ – zoran404 Jun 25 '15 at 9:59
  • 1
    \$\begingroup\$ Do you want to update the code of the actual launcher, or the code of the main game that the launcher launches, or simply update information that is displayed in the launcher? If it's the last, you can do something rather easily like pull the HTML from a site, if not connected show the latest cached, and display the HTML in a component that can render HTML well. Minecraft does this if I remember correctly (at least, it did years ago). \$\endgroup\$ – NESPowerGlove Jun 25 '15 at 18:09
1
\$\begingroup\$

Here's a launcher I made about a year ago:

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;
    }
}

It pings http://www.endcraft.net/webstart/ver and sees if it's different than a current version stored in a file - If it is, download the updated JAR.

It also downloads all files listed at http://www.endcraft.net/webstart/download - Unfortunately I lost that file, so vising it will point to a blank URL.

\$\endgroup\$
  • \$\begingroup\$ So how would I setup the game with that? I mean... Is there a way that I can put it on a webserver then see if it's different from there? And if so, how? Also thanks so much for the help, it will help me a lot. \$\endgroup\$ – TechyEngineer Jun 25 '15 at 14:20
  • \$\begingroup\$ Hi. Sorry, just had a look at the code in Eclipse Indigo and the amount of errors is unbelievable. I tried to fix some, but there is just too many :-( \$\endgroup\$ – TechyEngineer Jun 25 '15 at 14:40
  • \$\begingroup\$ @TechyEngineer You just need to import some stuff. I'll amend my code with the imports. \$\endgroup\$ – joehot200 Jun 25 '15 at 14:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.