Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: GabrielBailey74 (posted 2013-06-08 17:37:18, viewed 196 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258  
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
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 {
   /*
    * GameUpdater.Java
    * Checks if the games version is out dated / downloads new updates (.JAR).
    * Author: GabrielBailey74
    */
   private static byte[] BYTE_BUFFER = new byte[1024];
   private String currentClientVersion;
   private String versionLocation;
   private String versionFileNameAndExt;
   private String gameName;
   private String mostRecentVersion;
   private boolean needsUpdating;
   private int downloadPercentage;
   private int kbs;
   private double ETA;

   /**
    * Main entry point into the application
    * @param args The arguments passed via the user (NONE)
    * @throws IOException There was a error during reading / writing.
    * @throws MalformedURLException URL is missing headers?
    */
   public static void main(String[] args) throws MalformedURLException, IOException {
      /* Parameters for the GameUpdater constructor */
      String currentClientVersion = "0.0.0.1";
      String versionDirectoryURL = "http://www.javatools.co.nf/releases/";
      String versionFileNameAndExt = "version.txt";
      String gameName = "Block_Tamer_Client_";
      GameUpdater updater = new GameUpdater(currentClientVersion, versionDirectoryURL, versionFileNameAndExt, gameName);
      if (updater.needsUpdating()) {
         updater.downloadUpdatedJar(true); // true to print percentage while downloading
      }
   }

   public GameUpdater(String currentClientVersion, String versionLocation,
         String versionFileNameAndExtension, String gameName) throws MalformedURLException,
         IOException {
      setCurrentClientVersion(currentClientVersion);
      setVersionLocation(versionLocation);
      setVersionFileNameAndExt(versionFileNameAndExtension);
      setGameName(gameName);
      String version = getVersionFromUrl(versionLocation + versionFileNameAndExtension);
      setMostRecentVersion(version);
      System.out.println("Current client version: " + currentClientVersion);
      System.out.println("Actual client version: " + version);
      setNeedsUpdating(!currentClientVersion.equals(version));
      System.out.println("Client needs updating: " + needsUpdating());

   }

   private File updateJar(String jarLocation, boolean printPercentage)
         throws MalformedURLException, IOException {
      return getJarFromURL(jarLocation, getGameName() + getMostRecentVersion() + ".jar",
            printPercentage);
   }

   private File downloadUpdatedJar(boolean printPercentage) throws MalformedURLException,
         IOException {
      return updateJar(getVersionLocation() + getGameName() + getMostRecentVersion() + ".jar",
            printPercentage);
   }

   private String getVersionFromUrl(String versionFileURL) throws MalformedURLException,
         IOException {
      /*
       * Initialize the Input Stream. (Downloading from remote system / server)
       */
      BufferedInputStream in = new BufferedInputStream(new URL(versionFileURL).openStream());

      /*
       * Initialize the Output Stream. (Downloading on local system / pc)
       */
      File savedFile = new File("version.txt.tmp");
      savedFile.deleteOnExit();
      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 .TMP file.
      String version = "";
      if (savedFile.exists()) {
         BufferedReader br = new BufferedReader(new FileReader(savedFile));
         version = br.readLine();
         br.close();
      }
      return version;
   }

   private File getJarFromURL(String urlLocation, String saveLocation, boolean printPercentage)
         throws MalformedURLException, IOException {
      double numWritten = 0;
      double length = getURLSizeInKB(urlLocation);

      /*
       * 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));

      /*
       * Keeping track of when we started.
       */
      long startTime = System.currentTimeMillis();

      /*
       * Begin the download.
       */
      int inCount;
      while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
         out.write(BYTE_BUFFER, 0, inCount);
         numWritten += inCount;

         /*
          * Calculate the Percentage.
          */
         setDownloadPercentage((int) (((double) numWritten / (double) length) * 100D));
         if (printPercentage) {
            System.out.println("Download is " + getDownloadPercentage() + "% complete, "
                  + (int) getKbs() + " seconds left");
         }

         /*
          * Calculate the KBS.
          */
         setKbs((int) ((numWritten / BYTE_BUFFER.length) / (1 + ((System.currentTimeMillis() - startTime) / 1000))));

         /*
          * Calculate the ETA.
          */
         setETA((length - numWritten) / kbs / 1000D);
      }

      /*
       * Close the Input/Output streams.
       */
      out.flush();
      out.close();
      in.close();
      return savedFile;
   }

   /* Obtaining the files size in kilobytes */
   private 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;
   }

   private boolean needsUpdating() {
      return needsUpdating;
   }

   public String getCurrentClientVersion() {
      return currentClientVersion;
   }

   public void setCurrentClientVersion(String currentClientVersion) {
      this.currentClientVersion = currentClientVersion;
   }

   public String getVersionLocation() {
      return versionLocation;
   }

   public void setVersionLocation(String versionLocation) {
      this.versionLocation = versionLocation;
   }

   public String getVersionFileNameAndExt() {
      return versionFileNameAndExt;
   }

   public void setVersionFileNameAndExt(String versionFileNameAndExt) {
      this.versionFileNameAndExt = versionFileNameAndExt;
   }

   public String getGameName() {
      return gameName;
   }

   public void setGameName(String gameName) {
      this.gameName = gameName;
   }

   public void setNeedsUpdating(boolean needsUpdating) {
      this.needsUpdating = needsUpdating;
   }

   public String getMostRecentVersion() {
      return mostRecentVersion;
   }

   public void setMostRecentVersion(String mostRecentVersion) {
      this.mostRecentVersion = mostRecentVersion;
   }

   public int getDownloadPercentage() {
      return downloadPercentage;
   }

   public void setDownloadPercentage(int downloadPercentage) {
      this.downloadPercentage = downloadPercentage;
   }

   public int getKbs() {
      return kbs;
   }

   public void setKbs(int kbs) {
      this.kbs = kbs;
   }

   public double getETA() {
      return ETA;
   }

   public void setETA(double eTA) {
      ETA = eTA;
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (39 views)
2014-12-09 22:41:13

BurntPizza (74 views)
2014-12-08 04:46:31

JscottyBieshaar (36 views)
2014-12-05 12:39:02

SHC (49 views)
2014-12-03 16:27:13

CopyableCougar4 (44 views)
2014-11-29 21:32:03

toopeicgaming1999 (112 views)
2014-11-26 15:22:04

toopeicgaming1999 (98 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!