Advertisement
Guest User

Java SimpleGameUpdater

a guest
Jun 7th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.24 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.BufferedOutputStream;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.net.HttpURLConnection;
  10. import java.net.MalformedURLException;
  11. import java.net.URL;
  12.  
  13. public class GameUpdater {
  14.     /*
  15.      * Simple Game Updater.
  16.      * TODO: getVersionFromURL needs to delete the .TMP after use!
  17.      * UPLOAD_DIRECTORY is where the .JAR and .TXT (Version file) are located.
  18.      *
  19.      * The version.txt file should only contain "0.0.0.1" (your version id)
  20.      *
  21.      * Notes: .JAR file name (uploaded to the server) should be in the layout provided:
  22.      * example: http://www.yourwebsite.co.nf/releases/my_game_0.0.0.1.JAR
  23.      */
  24.  
  25.     // The current version of the game.
  26.     private static final String GAME_VERSION = "0.0.0.1";
  27.  
  28.     // The games name
  29.     private static final String GAME_NAME = "Block_Tamer_Client_";
  30.  
  31.     // The upload directory
  32.     private static final String UPLOAD_DIRECTORY = "http://www.javatools.co.nf/releases/";
  33.  
  34.     // A ByteBuffer for reading bytes.
  35.     private static byte[] BYTE_BUFFER = new byte[1024];
  36.  
  37.     /**
  38.      * Main entry point into the application.
  39.      * @param args The arguments (NONE) passed via the user.
  40.      */
  41.     public static void main(String[] args) {
  42.         try {
  43.             new GameUpdater();
  44.         } catch (MalformedURLException e) {
  45.             e.printStackTrace(); // malformed url (missing headers?)
  46.         } catch (FileNotFoundException e) {
  47.             e.printStackTrace(); // the version file or .jar file cannot be located.
  48.         } catch (IOException e) {
  49.             e.printStackTrace(); // there was a error while reading/writing.
  50.         }
  51.     }
  52.  
  53.     public GameUpdater() throws MalformedURLException, IOException, FileNotFoundException {
  54.         System.out.println("Current client version is: " + GAME_VERSION);
  55.        
  56.         // obtain the current version from the file.
  57.         String version = getVersionFromUrl(UPLOAD_DIRECTORY + "version.txt");
  58.        
  59.         System.out.println("Most recent uploaded game jar is version: " + version);
  60.  
  61.         // is the client version out dated?
  62.         boolean outdatedClient = !(new String("" + GAME_VERSION).equals(new String("" + version)));
  63.  
  64.         if (!outdatedClient) {
  65.             System.out.println("Your client does not need to be updated ^_^");
  66.             return;
  67.         }
  68.         /*
  69.          * At this point it's safe to assume the client is out dated.
  70.          */
  71.  
  72.         // The updated .JAR
  73.         final String jarLocation = UPLOAD_DIRECTORY + GAME_NAME + version + ".jar";
  74.  
  75.         // The downloaded .JAR save location (Updated)
  76.         final String downloadedJarLocation = GAME_NAME + version + ".jar";
  77.  
  78.         System.out.println("Your client is out dated, downloading version " + version+ " updates... (" + getURLSizeInKB(jarLocation) + " kb)");
  79.  
  80.         // download the new .jar
  81.         File newlyDownloadedJar = saveJarFromURL(jarLocation, downloadedJarLocation);
  82.         System.out.println("New Jar has been downloaded to: "+ newlyDownloadedJar.getAbsolutePath());
  83.     }
  84.  
  85.     private File saveJarFromURL(String urlLocation, String saveLocation)
  86.             throws MalformedURLException, IOException, FileNotFoundException {
  87.         /*
  88.          * Initialize the Input Stream. (Downloading from remote system / server)
  89.          */
  90.         BufferedInputStream in = new BufferedInputStream(new URL(urlLocation).openStream());
  91.  
  92.         /*
  93.          * Initialize the Output Stream. (Downloading on local system / pc)
  94.          */
  95.         File savedFile = new File(saveLocation);
  96.         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savedFile));
  97.  
  98.         /*
  99.          * Begin the download.
  100.          */
  101.         int inCount;
  102.         while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
  103.             out.write(BYTE_BUFFER, 0, inCount);
  104.         }
  105.  
  106.         /*
  107.          * Close the Input/Output streams.
  108.          */
  109.         out.flush();
  110.         out.close();
  111.         in.close();
  112.         return savedFile;
  113.     }
  114.  
  115.     @SuppressWarnings("resource")
  116.     private String getVersionFromUrl(String string) throws MalformedURLException, IOException {
  117.         /*
  118.          * Initialize the Input Stream. (Downloading from remote system / server)
  119.          */
  120.         BufferedInputStream in = new BufferedInputStream(new URL(string).openStream());
  121.  
  122.         /*
  123.          * Initialize the Output Stream. (Downloading on local system / pc)
  124.          */
  125.         File savedFile = new File("version.txt.tmp");
  126.         BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(savedFile));
  127.  
  128.         /*
  129.          * Begin the download.
  130.          */
  131.         int inCount;
  132.         while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
  133.             out.write(BYTE_BUFFER, 0, inCount);
  134.         }
  135.  
  136.         /*
  137.          * Close the Input/Output streams.
  138.          */
  139.         out.flush();
  140.         out.close();
  141.         in.close();
  142.  
  143.         // after the version data has been saved, read it and delete the file.
  144.         String version = "";
  145.         if (savedFile.exists()) {
  146.             System.out.println("Checking version data....");
  147.             version = new BufferedReader(new FileReader(savedFile)).readLine();
  148.             // TODO: delete the .TMP file
  149.         }
  150.         return version;
  151.     }
  152.  
  153.     /* Obtaining the files size in kilobytes */
  154.     public static Double getURLSizeInKB(String urlStr) {
  155.         double contentLength = 0;
  156.         try {
  157.             URL url = new URL(urlStr);
  158.             HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
  159.             contentLength = httpConn.getContentLength();
  160.             httpConn.disconnect();
  161.         } catch (Exception ex) {
  162.             ex.printStackTrace();
  163.         }
  164.         return contentLength;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement