Thread: autocache

Results 1 to 4 of 4
  1. #1 autocache 
    Registered Member
    Join Date
    Aug 2013
    Age
    22
    Posts
    181
    Thanks given
    22
    Thanks received
    12
    Rep Power
    37
    hello
    what i need is someone to create CacherDownloader and will auto download the cache for my project im working on,

    current cachedownloader.java

    Code:
    package com.evo.cache.updater;
    
    import com.evo.*;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    
    /**
    * Handles downloadiong our cache files from the webserver
    * 
    * @author Arithium
    *
    */
    public class CacheDownloader {
    
        /**
         * The size of our buffer
         */
        private static final int BUFFER_SIZE = 1024;
    
        /**
         * The path to save our config files in, we assume the user is still using
         * an older version of java so no need to use path
         */
        private static final File CONFIG_SAVE_PATH = new File(SignLink.getAbsoluteCacheDirectory() + "/config/");
    
        /**
         * If our directory doesn't exist, make it
         */
        static {
            if (!CONFIG_SAVE_PATH.exists()) {
                CONFIG_SAVE_PATH.mkdir();
            }
        }
        //how to use start
        /*
    
    CacheDownloader.start(this, CacheDownloader.FileType.MAINCACHE);
    CacheDownloader.start(this, CacheDownloader.FileType.FILEDATA);
    CacheDownloader.start(this, CacheDownloader.FileType.SPRITES);
    
         */
        //how to use end
    
    
    
        /**
         * Different file types to download archives from
         * 
         * @author Mobster
         *
         */
        public enum FileType {
    MAINCACHE(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/MANCACHE.zip", 1),
    FILEDATA(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/FILEDATA.zip", 1),
    SPRITES(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/SPRITES.zip", 1);
    
            /**
             * The link to the file on the webserver
    **/
            private String url;
    
            /**
             * The current version of the file
             */
            private int version;
    
            /**
             * The directory to extract the file after downloaded
             */
            private String directory;
    
            /**
             * Constructs a new {@link FileType} for a downloadable file
             *
             * @param directory
             * The directory to extract the file
             * @param url
             * The url to the file on the web server
             * @param version
             * The current version of the file
             */
            FileType(String directory, String url, int version) {
                this.directory = directory;
                this.url = url;
                this.version = version;
            }
    
            /**
             * Gets the current version of the file
             * 
             * @return The current version of the file
             */
            public int getVersion() {
                return version;
            }
    
            /**
             * Gets the url of the file on the webserver
             * 
             * @return The url of the file on the webserver
             */
            public String getURL() {
                return url;
            }
    
            /**
             * Gets the directory to extract the file too
             * 
             * @return The directory to extract the file too
             */
            public String getDirectory() {
                return directory;
            }
    
            @Override
            public String toString() {
                return name().toLowerCase();
            }
        }
    
        /**
         * Deletes the zip file from the directory
         * 
         * @param fileName
         * The name of the file to delete
         */
        private static void deleteZIP(String fileName, FileType type) {
            // A File object to represent the filename
            File f = new File(type.getDirectory(), 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");
            }
        }
    
        /**
         * Starts our cache downloader
         * 
         * @param Client
         * The {@link Client} instance
         * @param type
         * The {@link FileType} we are downloading
         */
        public static void start(GameClient Client, FileType type) {
            try {
                File location = new File(type.getDirectory());
                int version = readVersion(type);
                if (!location.exists()) {
                    downloadFile(Client, type, getArchivedName(type));
                    unZip(type);
                    deleteZIP(getArchivedName(type), type);
                    writeVersion(type);
                } else {
                    if (version != type.getVersion()) {
                        downloadFile(Client, type, getArchivedName(type));
                        unZip(type);
                        deleteZIP(getArchivedName(type), type);
                        writeVersion(type);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         * Starts the downloading process
         * 
         * @param Client
         * The {@link Client} instance
         * @param address
         * The address of the file to download
         * @param localFileName
         * The name of our file to store in our cache directory
         */
        private static void downloadFile(GameClient Client, FileType type, String localFileName) {
            try {
                URL url = new URL(type.getURL());
                URLConnection conn = url.openConnection();
                try (OutputStream out = new BufferedOutputStream(new FileOutputStream(type.getDirectory() + "/" + localFileName)); InputStream in = conn.getInputStream()) {
                    byte[] data = new byte[BUFFER_SIZE];
                    int numRead;
                    long numWritten = 0;
                    long start = System.nanoTime();
                    final double NANOS_PER_SECOND = 1000000000.0;
                    final double BYTES_PER_MIB = 1024;
                    int length = conn.getContentLength();
                    while ((numRead = in.read(data)) != -1) {
                        out.write(data, 0, numRead);
                        numWritten += numRead;
                        double speed = Math.round(((NANOS_PER_SECOND / BYTES_PER_MIB) * numWritten) / ((System.nanoTime() - start) + 1));
                        int percentage = (int) (((double) numWritten / (double) length) * 100D);
                        int totalLength = length / 1024;
                        int totalReceived = (int) (numWritten / 1024);
                        drawLoadingText(percentage == 100 ? 101 : percentage,
                                "Downloading " + type.toString() + " @ " + Math.round(speed) + " kb/s" + " " + totalReceived + "/" + totalLength + "kb");
                    }
    
                    drawLoadingText(101, "Downloaded " + type.toString() + ", unzipping.");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        static GameClient client;
    
        private static void drawLoadingText(int amount, String text) {
            client.drawLoadingBar(text, amount);
        }
        /**
         * Gets the archive name
         * 
         * @param type
         * @return
         */
        private static String getArchivedName(FileType type) {
            int lastSlashIndex = type.getURL().lastIndexOf('/');
    
            if ((lastSlashIndex >= 0) && (lastSlashIndex < (type.getURL().length() - 1))) {
                return type.getURL().substring(lastSlashIndex + 1, type.getURL().length() - 5);
            } else {
            }
    
            return "";
        }
    
        /**
         * Starts the unzipping process
         * 
         * @param zin
         * @param s
         * @throws IOException
         */
       &nbsp;private static void unzip(ZipInputStream zin, String s) throws IOException {
            try (FileOutputStream out = new FileOutputStream(s)) {
                byte[] buf = new byte[BUFFER_SIZE];
                int len = 0;
    
                while ((len = zin.read(buf)) != -1) {
                   &nbsp;out.write(buf, 0, len);
                }
            }
        }
    
        /**
         * Attempts to unzip the archive
         * 
         * @param type
         */
       &nbsp;private static void unZip(FileType type) {
            String archive = type.getDirectory() + File.separator + getArchivedName(type);
            try (ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(archive)))) {
                ZipEntry e;
                while ((e = zin.getNextEntry()) != null) {
                    if (e.isDirectory()) {
                        (new File(type.getDirectory() + File.separator + e.getName())).mkdir();
                    } else {
                        if (e.getName().equals(archive)) {
                            unzip(zin, archive);
                            break;
                        }
    
                        unzip(zin, type.getDirectory() + File.separator + e.getName());
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    
    
        /**
         * Gets the current Client version
         *
         * @return
         */
       &nbsp;private static int readVersion(FileType type) {
            try (DataInputStream in = new DataInputStream(new FileInputStream(new File(CONFIG_SAVE_PATH, type.toString() + "_version.dat")))) {
                int version = in.readInt();
                return version;
            } catch (IOException ignored) {
            }
    
            return -1;
        }
    
        /**
         * Writes the current version
         */
       &nbsp;private static void writeVersion(FileType type) {
            try (DataOutputStream out = new DataOutputStream(new FileOutputStream(new File(CONFIG_SAVE_PATH, type.toString() + "_version.dat")))) {
               &nbsp;out.writeInt(type.getVersion());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
    }
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Mar 2019
    Posts
    5
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Code:
    MAINCACHE(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/MANCACHE.zip", 1),
    FILEDATA(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/FILEDATA.zip", 1),
    SPRITES(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/SPRITES.zip", 1);
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Aug 2013
    Age
    22
    Posts
    181
    Thanks given
    22
    Thanks received
    12
    Rep Power
    37
    Quote Originally Posted by Judge Exe View Post
    Code:
    MAINCACHE(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/MANCACHE.zip", 1),
    FILEDATA(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/FILEDATA.zip", 1),
    SPRITES(SignLink.getAbsoluteCacheDirectory(), "https://runeline.com/SPRITES.zip", 1);

    Thanks for pointing the out, but what is the problem because of using 3 URLS not 1?
    Reply With Quote  
     

  4. #4  
    Administrator

    Kevy's Avatar
    Join Date
    Jul 2006
    Posts
    1,108
    Thanks given
    61
    Thanks received
    1,509
    Rep Power
    4436
    Please use the code tags next time Corrected this for you
    Thanks, Kevin.
    Reply With Quote  
     

  5. Thankful user:



Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Autocache Download Problem
    By RuneTrip in forum Help
    Replies: 2
    Last Post: 06-27-2010, 10:51 PM
  2. Can i have a link to james autocacher
    By thescaper in forum Help
    Replies: 0
    Last Post: 07-02-2009, 04:30 PM
  3. [REQUEST] Autocache downloader
    By netzo in forum Requests
    Replies: 0
    Last Post: 09-17-2008, 06:20 PM
  4. Autocache Download - Uppit [377]
    By Ian... in forum Tutorials
    Replies: 5
    Last Post: 04-16-2008, 06:40 PM
  5. Selling AutoCache Downloaders!
    By Ninja Cat in forum RS2 Client
    Replies: 10
    Last Post: 06-24-2007, 02:41 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •