You are viewing our Forum Archives. To view or take place in current topics click here.
Finished Coding RSPS | Need help with Auto Cache Downloader
Posted:

Finished Coding RSPS | Need help with Auto Cache DownloaderPosted:

MousaMsh
  • Challenger
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
I can't get the auto cache down loader working properly, If you can get it to work, Ill be more than happy to give you Mod/Admin on my RSPS when its released

Skype: tonebawsjinx
#2. Posted:
ThatOnePigMan
  • Junior Member
Status: Offline
Joined: Jun 04, 201310Year Member
Posts: 78
Reputation Power: 3
Status: Offline
Joined: Jun 04, 201310Year Member
Posts: 78
Reputation Power: 3
Put a download page on your website for you cache
#3. Posted:
MousaMsh
  • Challenger
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
I would do that but its unprofessional I would say. I dont want people having to put the cache in there C Drive manually.
#4. Posted:
-3Dz-
  • Powerhouse
Status: Offline
Joined: Feb 24, 201212Year Member
Posts: 414
Reputation Power: 16
Status: Offline
Joined: Feb 24, 201212Year Member
Posts: 414
Reputation Power: 16
ThatOnePigMan wrote Put a download page on your website for you cache


That's a good way to not get players... Sad,


Also to help, You would need a file called "CacheDownloader" Also need to set it up in some other files. but here is the CacheDownloader Code to help abit.



import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLConnection;
import java.net.URL;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.Enumeration;

import sign.signlink;

public class CacheDownloader {

   private client client;

   private final int BUFFER = 1024;

   private final int VERSION = 0; // Version of cache
   //private String cacheLink = "LINK HERE"; // Link to cache
   private String cacheLink = "LINK HERE"; // Link to cache
   
   private String fileToExtract = getCacheDir() + getArchivedName();

   public CacheDownloader(client client) {
      this.client = client;
   }

   private void drawLoadingText(String text) {
      client.drawLoadingText(35, text);
      //System.out.println(text);
   }


   private void drawLoadingText(int amount, String text) {
      client.drawLoadingText(amount, text);
      //System.out.println(text);
   }

   private String getCacheDir() {
      return signlink.findcachedir();
   }

   private String getCacheLink() {
      return cacheLink;
   }

   private int getCacheVersion() {
      return VERSION;
   }

   public CacheDownloader downloadCache() {
      try {
      File location = new File(getCacheDir());
      File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
      
      if(!location.exists()) {
         //drawLoadingText("Loading new Updates....");
         downloadFile(getCacheLink(), getArchivedName());

         unZip();
         //System.out.println("UNZIP");

         BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
         versionFile.close();
         deleteZIP(getArchivedName());
      } else {
         if(!version.exists()) {
            //drawLoadingText("~ First Time Installation, Only Once! ~");
            downloadFile(getCacheLink(), getArchivedName());

            unZip();
            //System.out.println("UNZIP");

            BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
            versionFile.close();
            deleteZIP(getArchivedName());

         } else {
            return null;
         }
      }
      } catch(Exception e) {

      }
      return null;
   }
   
   private void downloadFile(String adress, String localFileName) {
      OutputStream out = null;
      URLConnection conn;
      InputStream in = null;
      
      try {

         URL url = new URL(adress);
         out = new BufferedOutputStream(
            new FileOutputStream(getCacheDir() + "/" +localFileName));

         conn = url.openConnection();
         in = conn.getInputStream();
      
         byte[] data = new byte[BUFFER];
   
         int numRead;
         long numWritten = 0;
         int length = conn.getContentLength();

   
         while((numRead = in.read(data)) != -1) {
            out.write(data, 0, numRead);
            numWritten += numRead;

                     int percentage = (int)(((double)numWritten / (double)length) * 100D);
            drawLoadingText(percentage, "Downloading Cache " + percentage + "%...");

         }

         System.out.println(localFileName + "\t" + numWritten);
         drawLoadingText("Unpacking..");

      } catch (Exception exception) {
         exception.printStackTrace();
      } finally {
         try {
            if (in != null) {
               in.close();
            }
            if (out != null) {
               out.close();
            }
         } catch (IOException ioe) {
         }
      }

   }

   private String getArchivedName() {
      int lastSlashIndex = getCacheLink().lastIndexOf('/');
      if (lastSlashIndex >= 0
         && lastSlashIndex < getCacheLink().length() -1) {
         return getCacheLink().substring(lastSlashIndex + 1);
      } else {
         //System.err.println("error retreiving archivaed name.");
      }
      return "";
   }




   private void unZip() {

      try {
             InputStream in =
            new BufferedInputStream(new FileInputStream(fileToExtract));
         ZipInputStream zin = new ZipInputStream(in);
         ZipEntry e;

         while((e=zin.getNextEntry()) != null) {

                        if(e.isDirectory()) {
               (new File(getCacheDir() + e.getName())).mkdir();
                        } else {

            if (e.getName().equals(fileToExtract)) {
               unzip(zin, fileToExtract);
               break;
            }
                      unzip(zin, getCacheDir() + e.getName());
            }
            //System.out.println("unzipping2 " + e.getName());
         }
         zin.close();

      } catch(Exception e) {
         e.printStackTrace();
      }
   }

private void deleteZIP(String fileName){
    // A File object to represent the filename
    File f = new File(getCacheDir() + fileName);

    // Make sure the file or directory exists and isn't write protected
    if (!f.exists())
      throw new IllegalArgumentException(
          "Delete: no such file or directory: " + fileName);

    if (!f.canWrite())
      throw new IllegalArgumentException("Delete: write protected: "
          + fileName);

    // If it is a directory, make sure it is empty
    if (f.isDirectory()) {
      String[] files = f.list();
      if (files.length > 0)
        throw new IllegalArgumentException(
            "Delete: directory not empty: " + fileName);
    }

    // Attempt to delete it
    boolean success = f.delete();

    if (!success)
      throw new IllegalArgumentException("Delete: deletion failed");

   }

   private void unzip(ZipInputStream zin, String s)
      throws IOException {

      FileOutputStream out = new FileOutputStream(s);
      //System.out.println("unzipping " + s);
      byte [] b = new byte[BUFFER];
      int len = 0;

      while ((len = zin.read(b)) != -1) {
         out.write(b,0,len);
      }
      out.close();
   }
}
#5. Posted:
MousaMsh
  • Challenger
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
I did all that shit, still doesn't work
Don't know why
Followed like 3 tutorial's and it doesn't work
#6. Posted:
-3Dz-
  • Powerhouse
Status: Offline
Joined: Feb 24, 201212Year Member
Posts: 414
Reputation Power: 16
Status: Offline
Joined: Feb 24, 201212Year Member
Posts: 414
Reputation Power: 16
MousaMsh wrote I did all that ****, still doesn't work
Don't know why
Followed like 3 tutorial's and it doesn't work


Are you using Dropbox public Download link share when putting the link in? Also have you edited the other files and not just The CacheDownloader File" Also make sure the Cache is packed right.
#7. Posted:
MousaMsh
  • Challenger
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
Status: Offline
Joined: Apr 18, 201311Year Member
Posts: 103
Reputation Power: 3
Message me on skype
Itll be easier

Skype: tonebawsjinx
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.