1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Hey Guest, If you'd like to instant message with many other users, then join our
    Official Rune-Status Discord Server!
    Dismiss Notice
  3. Got your own Server? Looking for a Server to play right now??
    Make sure you check out the Public Server Directory!
    Dismiss Notice
  4. If you're new to the Rsps Scene and want to jump straight into learning how to Set-up and Customise your own RuneScape Private Sever then check out the Guides and Tutorials for RS2 here.
    Dismiss Notice
Dismiss Notice
Hey Guest,
Are you sure your posting in the Right Sections?
If your unsure which RuneTek version you are working with visit this page to find out.

Automatic Client Cache Downloading

Discussion in 'Guides & Tutorials' started by Ian, Nov 2, 2016.

  1. In this tutorial I will guide you through how to add Automatic Cache Downloading for your RuneScape​
    Private Server Client. Having Automatic Cache Downloading makes it so your users dont need to manually add your cache to the right location and make it alot easier to download, install and play on your Server.

    The code in this Cache downloading system is specific to .ZIP archives and will not work with .RAR, I have looked into .RAR extraction techniques in Java but they require extra dependencies and a re-worked Cache Downloading Java class. I feel asif its alot easier for you to just click the ZIP radiobutton when adding cache to an archive. :emoji_confused:

    The first thing you need to do is to Archive your cache folder, now you dont want to pack the folder containing the cache, just everything inside it in an Archive named after the folder.
    [​IMG]

    As I mentioned above it is essential for this code to work that the file is a ZIP archive:
    [​IMG]

    Add the folowing code into a new file called Cache.Java:
    
    
    import java.io.*;
    import java.net.URLConnection;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    public class Cache {
        private final int CACHE_VER = 1;
        private String CACHE_URL = "ABSOLUTE DOWNLOAD PATH HERE";
        private String ARCHIVE = Signlink.findcachedir() + getArchivedName();
        private Client client;
        Client frame;
    
        public Cache(Client client) {
            this.client = client;
        }
    
        private String localCacheFile() {
            return Signlink.findcachedir() + getArchivedName();
        }
    
        public Cache download() {
            try {
                File location = new File(Signlink.findcachedir());
                File version = new File(Signlink.findcachedir() + "/cacheVersion" + CACHE_VER + ".dat");
                if (!location.exists()) {
                    download(CACHE_URL, getArchivedName());
                    BufferedWriter versionFile = new BufferedWriter(
                            new FileWriter(Signlink.findcachedir() + "/cacheVersion" + CACHE_VER + ".dat"));
                    versionFile.close();
                } else {
                    if (!version.exists()) {
                        download(CACHE_URL, getArchivedName());
                        BufferedWriter versionFile = new BufferedWriter(
                                new FileWriter(Signlink.findcachedir() + "/cacheVersion" + CACHE_VER + ".dat"));
                        versionFile.close();
                    } else {
                        return null;
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return null;
        }
    
        private void download(String adress, String localFileName) {
            OutputStream out = null;
            URLConnection connection;
            InputStream in = null;
            try {
                URL url = new URL(adress);
                out = new BufferedOutputStream(new FileOutputStream(Signlink.findcachedir() + "/" + localFileName));
                connection = url.openConnection();
                in = connection.getInputStream();
                byte[] buffer = new byte[1024];
                int numRead, numWritten = 0, length = connection.getContentLength();
                while ((numRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, numRead);
                    numWritten += numRead;
                    int percentage = (int) (((double) numWritten / (double) length) * 100D);
                    client.drawLoadingText(percentage, "Downloading Cache " + percentage + "%");
                }
                System.out.println(localFileName + "\t" + numWritten);
                client.drawLoadingText(35, "Finished downloading " + getArchivedName() + "!");
                in.close();
                out.close();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            File file = new File(localCacheFile());
            boolean exists = file.exists();
            if (!exists) {
                System.out.println("The cache was not downloaded correctly.");
                System.out.println("Please try downloading it manually at: " + CACHE_URL);
                System.out.println("Extract the archive at : " + Signlink.findcachedir());
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }
                try {
                    client.drawLoadingText(35, "Cache Download Error!");
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            } else {
                client.drawLoadingText(35, "Preparing to extract Cache");
                try {
                    Thread.currentThread().sleep(1000);
                } catch (InterruptedException exception) {
                    exception.printStackTrace();
                }
                unZip();
            }
        }
    
        private String getArchivedName() {
            int lIO = CACHE_URL.lastIndexOf('/');
            if (lIO >= 0 && lIO < CACHE_URL.length() - 1) {
                return CACHE_URL.substring(lIO + 1);
            } else {
                System.err.println("Error retreiving archived file name!");
            }
            return null;
        }
    
        private void unZip() {
            try {
                InputStream in = new BufferedInputStream(new FileInputStream(ARCHIVE));
                ZipInputStream zin = new ZipInputStream(in);
                ZipEntry ze;
                while ((ze = zin.getNextEntry()) != null) {
                    if (ze.isDirectory()) {
                        new File(Signlink.findcachedir() + ze.getName()).mkdir();
                    } else {
                        if (ze.getName().equals(ARCHIVE)) {
                            unzip(zin, ARCHIVE);
                            break;
                        }
                        unzip(zin, Signlink.findcachedir() + ze.getName());
                    }
                    client.drawLoadingText(35, "Unzipping Cache: " + ze.getName());
                }
                zin.closeEntry();
                zin.close();
                in.close();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            try {
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException exception) {
                exception.printStackTrace();
            }
            File file = new File(localCacheFile());
            boolean exists = file.exists();
            if (!exists) {
                System.err.println("There was a problem downloading the cache!");
            } else {
                System.err.println("Attempting to remove corrupted files.");
                try {
                    if (file.delete()) {
                        client.drawLoadingText(35, file.getName() + " was removed");
                    } else {
                        client.drawLoadingText(35, "Error removing: " + file.getName());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        private void unzip(ZipInputStream in, String fileName) throws IOException {
            FileOutputStream out = new FileOutputStream(fileName);
            byte[] buffer = new byte[1024];
            int length = 0, offset = 0;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, offset, length);
            }
            out.close();
        }
    }
    
    Thanks to [email protected]

    Then in the 'startup()' method in Client.Java add this line of code below underneath 'drawLoadingText(20, "Starting up");':


    Now that is all you need to do -Done.

    [​IMG]

    If you have any issues, please read on:
    • Make sure the CACHE_URL is an absolute path to the .ZIP archive, meaning it should instantly start downloading on visiting the link.
    • If you have issues with extracting extra custom content you should consider packing the cache before-hand.
    • Google
    - Ian
     
    #1 Ian, Nov 2, 2016
    Last edited: Jun 23, 2017
  2. Good guide buddy. ;)
     
  3. Thanks for the guide! Will definitely be needing this is the near future.
     
  4. thanks for this ian very useful could you add an update server tutorial?
     
Loading...