import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class GameUpdater {
/*
* Simple Game Updater.
* TODO: getVersionFromURL needs to delete the .TMP after use!
* UPLOAD_DIRECTORY is where the .JAR and .TXT (Version file) are located.
*
* The version.txt file should only contain "0.0.0.1" (your version id)
*
* Notes: .JAR file name (uploaded to the server) should be in the layout provided:
* example: http://www.yourwebsite.co.nf/releases/my_game_0.0.0.1.JAR
*/
// The current version of the game.
private static final String GAME_VERSION = "0.0.0.1";
// The games name
private static final String GAME_NAME = "Block_Tamer_Client_";
// The upload directory
private static final String UPLOAD_DIRECTORY = "http://www.javatools.co.nf/releases/";
// A ByteBuffer for reading bytes.
private static byte[] BYTE_BUFFER = new byte[1024];
/**
* Main entry point into the application.
* @param args The arguments (NONE) passed via the user.
*/
public static void main(String[] args) {
try {
new GameUpdater();
} catch (MalformedURLException e) {
e.printStackTrace(); // malformed url (missing headers?)
} catch (FileNotFoundException e) {
e.printStackTrace(); // the version file or .jar file cannot be located.
} catch (IOException e) {
e.printStackTrace(); // there was a error while reading/writing.
}
}
public GameUpdater() throws MalformedURLException, IOException, FileNotFoundException {
System.out.println("Current client version is: " + GAME_VERSION);
// obtain the current version from the file.
String version = getVersionFromUrl(UPLOAD_DIRECTORY + "version.txt");
System.out.println("Most recent uploaded game jar is version: " + version);
// is the client version out dated?
boolean outdatedClient = !(new String("" + GAME_VERSION).equals(new String("" + version)));
if (!outdatedClient) {
System.out.println("Your client does not need to be updated ^_^");
return;
}
/*
* At this point it's safe to assume the client is out dated.
*/
// The updated .JAR
final String jarLocation = UPLOAD_DIRECTORY + GAME_NAME + version + ".jar";
// The downloaded .JAR save location (Updated)
final String downloadedJarLocation = GAME_NAME + version + ".jar";
System.out.println("Your client is out dated, downloading version " + version+ " updates... (" + getURLSizeInKB(jarLocation) + " kb)");
// download the new .jar
File newlyDownloadedJar = saveJarFromURL(jarLocation, downloadedJarLocation);
System.out.println("New Jar has been downloaded to: "+ newlyDownloadedJar.getAbsolutePath());
}
private File saveJarFromURL(String urlLocation, String saveLocation)
throws MalformedURLException, IOException, FileNotFoundException {
/*
* Initialize the Input Stream. (Downloading from remote system / server)
*/
BufferedInputStream in = new BufferedInputStream(new URL(urlLocation).openStream());
/*
* Initialize the Output Stream. (Downloading on local system / pc)
*/
File savedFile = new File(saveLocation);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savedFile));
/*
* Begin the download.
*/
int inCount;
while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
out.write(BYTE_BUFFER, 0, inCount);
}
/*
* Close the Input/Output streams.
*/
out.flush();
out.close();
in.close();
return savedFile;
}
@SuppressWarnings("resource")
private String getVersionFromUrl(String string) throws MalformedURLException, IOException {
/*
* Initialize the Input Stream. (Downloading from remote system / server)
*/
BufferedInputStream in = new BufferedInputStream(new URL(string).openStream());
/*
* Initialize the Output Stream. (Downloading on local system / pc)
*/
File savedFile = new File("version.txt.tmp");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savedFile));
/*
* Begin the download.
*/
int inCount;
while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
out.write(BYTE_BUFFER, 0, inCount);
}
/*
* Close the Input/Output streams.
*/
out.flush();
out.close();
in.close();
// after the version data has been saved, read it and delete the file.
String version = "";
if (savedFile.exists()) {
System.out.println("Checking version data....");
version = new BufferedReader(new FileReader(savedFile)).readLine();
// TODO: delete the .TMP file
}
return version;
}
/* Obtaining the files size in kilobytes */
public static Double getURLSizeInKB(String urlStr) {
double contentLength = 0;
try {
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
contentLength = httpConn.getContentLength();
httpConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
return contentLength;
}
}