Thread: Can anyone work this out? Would really appreciate the help! [Cache Downloader]

Results 1 to 8 of 8
  1. #1 Can anyone work this out? Would really appreciate the help! [Cache Downloader] 
    Registered Member
    Join Date
    Jun 2010
    Posts
    13
    Thanks given
    3
    Thanks received
    3
    Rep Power
    29
    Alright lads, I've been trying to get my head around this for a day or two now, and I cant work out why my cache isn't auto downloading!

    I have zipped the files inside my cache correctly. I have not zipped the cache file containing the cache. Every time I load the client I just get error connecting to server.

    If anyone could work this out, that would be awesome and much appreciated!

    If you need any more info, let me know!

    Spoiler for CacheDownloader:
    Code:
    package com.asgardia.client.cache;
    
    import com.asgardia.Configuration;
    import com.asgardia.client.Client;
    import com.asgardia.client.signlink;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    
    /**
     * Enchanced cache downloader
     * Handles cache downloading & unzipping
     * @author Gabriel Hannason
     */
    public class CacheDownloader {
    
    	private static final String CACHE_PATH = signlink.findcachedir();
    
    	private static final String VERSION_FILE = CACHE_PATH + "versions.dat";
    
    	public static void init() {
    
    		if(Configuration.LOCALHOST) {
    			return;
    		}
    
    		try {
    			//DebuggingRunnables.getMessageThread("cachestart").start();;
    
    			for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {
    
    				boolean exists = getLocalVersion(cacheFile.identifier) != -1;
    				//boolean exists = new File(signlink.findcachedir() + cacheFile.identifier).exists();
    
    				long CacheLocal = getLocalVersion(CACHE_DOWNLOAD_FILES.CACHE.identifier);
    
    				long CacheRemote = getRemoteVersion(CACHE_DOWNLOAD_FILES.CACHE.link);
    
    				if(CacheLocal != CacheRemote && CacheRemote != -1){
    					updateFiles(CACHE_DOWNLOAD_FILES.CACHE);
    					writeVersions(CacheRemote);
    				}
    
    				if (!exists) {
    					updateFiles(cacheFile);
    				}
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
    		int total = cacheFile.file.length;
    
    		int current = 1;
    
    		for (String file : cacheFile.file) {
    			downloadFile(cacheFile, file, current, total);
    			if (file.endsWith(".zip")) {
    				unzip(new File(signlink.findcachedir() + file));
    			}
    			current++;
    		}
    
    
    	}
    
    	public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
    		String fileURL = cacheFile.link;
    		String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCase());
    		URL url = new URL(fileURL);
    		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    		httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    		int responseCode = httpConn.getResponseCode();
    
    		// always check HTTP response code first
    		if (responseCode == HttpURLConnection.HTTP_OK) {
    			// opens input stream from the HTTP connection
    			InputStream inputStream = httpConn.getInputStream();
    			String saveFilePath = signlink.findcachedir() + file;
    			// opens an output stream to save into file
    			FileOutputStream outputStream = new FileOutputStream(saveFilePath);
    
    			int bytesRead = -1;
    			byte[] buffer = new byte[4096];
    			long startTime = System.currentTimeMillis();
    			int downloaded = 0;
    			long numWritten = 0;
    			int length = httpConn.getContentLength();
    			while ((bytesRead = inputStream.read(buffer)) != -1) {
    				outputStream.write(buffer, 0, bytesRead);
    				numWritten += bytesRead;
    				downloaded += bytesRead;
    				int percentage = (int)(((double)numWritten / (double)length) * 100D);
    				int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
    				String s = total > 1 ? "("+current+"/"+total+")" : "";
    				drawLoadingText(percentage, (new StringBuilder()).append("Downloading "+downloadingText+""+s+": "+percentage+"%").toString());
    			}
    
    			//DebuggingRunnables.getMessageThread("cacheend").start();
    			outputStream.close();
    			inputStream.close();
    
    		} else {
    			System.out.println("download link replied HTTP code: " + responseCode);
    		}
    		httpConn.disconnect();
    	}
    
    	/**
    	 * Gets the current version for the file.
    	 *
    	 *  @[Only registered and activated users can see links. ] name
    	 *            The file version string.
    	 *  @[Only registered and activated users can see links. ] The version.
    	 */
    	private static long getLocalVersion(String name) {
    		BufferedReader br = null;
    
    		try {
    			File versionDir = new File(VERSION_FILE);
    
    			if (!versionDir.exists()) {
    				versionDir.createNewFile();
    				return -1;
    			}
    
    			if (name.equals("SPRITE_VER")) {
    				if (!new File(CACHE_PATH + "sprites.dat").exists() || !new File(CACHE_PATH + "sprites.idx").exists()) {
    					return -1;
    				}
    			}
    
    			br = new BufferedReader(new FileReader(VERSION_FILE));
    
    			String line;
    
    			while ((line = br.readLine()) != null) {
    				if (line.startsWith(name + " = ")) {
    					long size = Long.parseLong(line.replace(name + " = ", ""));
    					br.close();
    					return size;
    				}
    			}
    
    			br.close();
    			return -1;
    		} catch (Exception e) {
    			return -1;
    		}
    	}
    
    	/**
    	 * Writes the versions into the version file.
    	 *
    	 *  @[Only registered and activated users can see links. ] cacheVersion
    	 *            The version of the cache.
    	 *  @[Only registered and activated users can see links. ] spritesVersion
    	 *            The version of the sprites.
    	 */
    	private static void writeVersions(long cacheVersion) {
    		BufferedWriter writer = null;
    
    		try {
    			writer = new BufferedWriter(new FileWriter(VERSION_FILE));
    			writer.write("CACHE_VER = " + cacheVersion);
    			writer.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	private static long getRemoteVersion(String url) {
    		try {
    			return ((HttpURLConnection) new URL(url).openConnection()).getContentLength();
    		} catch (Exception e) {
    			return -1;
    		}
    	}
    
    	private static void drawLoadingText(int amount, String text) {
    		Client.getClient().drawLoadingText(amount, text);
    	}
    
    	private static void unzip(final File file) {
    		//DebuggingRunnables.getMessageThread("zipstart").start();
    		try {
    			InputStream in =  new BufferedInputStream(new FileInputStream(file));
    			ZipInputStream zin = new ZipInputStream(in);
    			ZipEntry e;
    			while((e=zin.getNextEntry()) != null) {
    				if(e.isDirectory()) {
    					(new File(signlink.findcachedir() + e.getName())).mkdir();
    				} else {
    					if (e.getName().equals(file.getName())) {
    						unzipPartlyArchive(zin, file.getName());
    						break;
    					}
    					unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
    				}
    			}
    			zin.close();
    			file.delete();
    			//DebuggingRunnables.getMessageThread("zipend").start();
    		} catch(Exception e) {}
    	}
    
    	/**
    	 * Unzips a partly archive
    	 *  @[Only registered and activated users can see links. ] zin	The zip inputstream
    	 *  @[Only registered and activated users can see links. ] s		The location of the zip file
    	 *  @[Only registered and activated users can see links. ]s IOException	The method can throw an IOException.
    	 */
    	private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
    		FileOutputStream out = new FileOutputStream(s);
    		drawLoadingText(100, "Unpacking data..");
    		byte [] b = new byte[1024];
    		int len = 0;
    
    		while ((len = zin.read(b)) != -1) {
    			out.write(b,0,len);
    		}
    		out.close();
    	}
    
    	public static String getDownloadLink(String name) {
    		try {
    			String webLink = "mydropboxlink.zip" + name;
    			StringBuilder result = new StringBuilder();
    			URL url = new URL(webLink);
    			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    			conn.setRequestMethod("GET");
    			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			String line;
    			while ((line = rd.readLine()) != null) {
    				if (line.contains("url:")) {
    					return line.replace("url:", "").trim();
    				}
    			}
    			rd.close();
    			return result.toString();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return "ERROR_IN_URL";
    	}
    
    	enum CACHE_DOWNLOAD_FILES {
    
    		CACHE(new String[]{"cache.zip"}, "CACHE_VER", getDownloadLink("cache")),
    		;
    
    		CACHE_DOWNLOAD_FILES(String[] file, String identifier, String link) {
    			this.file = file;
    			this.identifier = identifier;
    			this.link = link;
    		}
    
    		private String[] file;
    		private String identifier;
    		private String link;
    	}
    }



    Spoiler for Configuation File:
    package com.asgardia;

    public class Configuration<BETA> {

    /**
    * Connection information
    */
    public final static boolean LOCALHOST = false;
    public final static boolean BETA = true;
    public static String HOST = LOCALHOST ? "127.0.0.1" : BETA ? "127.0.0.1" : "127.0.0.1"; // TEST
    public static int PORT = 43594;

    /**
    * Client information
    */
    public final static String CLIENT_NAME = "Asgardia";
    public static final boolean SEND_HASH = true;
    public static boolean upscaling = true;
    public static final boolean DISCO_ITEMS = false;

    /**
    * Misc.
    */
    public final static String JAGGRAB_HOST = "127.0.0.1";
    public static final boolean JAGCACHED_ENABLED = false;

    public static final boolean debuggingModels = false;

    /**
    * The NPC bits. 12 = 317/377 14 = 474+ 16 = 600+
    */
    public final static int NPC_BITS = 18;

    public static final int[] packetSizes = { 0, 0, 0, 1, 6, 0, 0, 0, 4, 0, // 0
    0, 2, -1, 1, 1, -1, 1, 0, 0, 0, // 10
    0, 0, 0, 0, 1, 0, 0, -1, 1, 1, // 20
    0, 0, 0, 0, -2, 4, 3, 0, 2, 0, // 30
    0, 0, 0, 0, 5, 8, 0, 6, 0, 0, // 40
    9, 0, 0, -2, 6, 0, 0, 0, 0, 0, // 50
    -2, 1, 0, 0, 2, -2, 0, 0, 0, 0, // 60
    6, 5, 2, 4, 2, 4, 0, 0, 0, 4, // 70
    0, -2, 0, 0, 7, 2, 1, 6, 6, 0, // 80
    2, 2, 0, 0, 0, 0, 0, 4, 0, 1, // 90
    2, 2, 0, 1, -1, 4, 1, 0, 1, 0, // 100
    1, 1, 1, 1, 2, 1, 0, 15, 0, 0, // 110
    0, 4, 4, -1, 9, 0, -2, 1, 0, 0, // 120 // 9
    -1, 0, 0, 0, 9, 0, 0, 0, 0, 0, // 130
    3, 10, 2, 0, 0, 0, 0, 14, 0, 0, // 140
    0, 6, 5, 3, 0, 0, 3, 0, 0, 0, // 150
    4, 5, 0, 0, 2, 0, 6, 0, 0, 0, // 160
    // 0, 3, /*0*/ -1, 0, 5, 7, 10, 6, 5, 1, //170
    0, 3, -2, -2, 5, 5, 10, 6, 5, -2, // 170
    0, 0, 0, 0, 0, 2, 0, -1, 0, 0, // 180
    0, 0, 0, 0, 0, 2, -1, 0, -1, 0, // 190
    4, 0, 0, 0, 0, -1, 3, 10, 4, 4, // 200
    0, 0, 0, 0, -1, 7, 0, -2, 2, 0, // 210
    0, 1, -2, -2, 0, 0, 0, 0, 0, 0, // 220
    8, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 230
    2, -2, 0, 0, -1, 0, 6, 0, 4, 3, // 240
    -1, 0, 0, -1, 6, 0, 0// 250

    };

    /**
    * Exp counter
    */
    public static boolean xpCounterOpen = true;
    public static int xpDropsPosition = 0;
    public static int xpCounterSize = 0;
    public static int xpDropsSpeed = 0;
    public static int xpCounterType = 0;
    public static int xpCounterProgress = 1;
    public static int xpDropsColour = 0xffffff;
    public static boolean xpDropsGroup = true;

    }
    Fuck COVID

    Reply With Quote  
     

  2. #2  
    Nalore - Coming soon...

    Tutus Frutus's Avatar
    Join Date
    Feb 2018
    Posts
    417
    Thanks given
    129
    Thanks received
    200
    Rep Power
    638
    Quote Originally Posted by Josh125 View Post
    Alright lads, I've been trying to get my head around this for a day or two now, and I cant work out why my cache isn't auto downloading!

    I have zipped the files inside my cache correctly. I have not zipped the cache file containing the cache. Every time I load the client I just get error connecting to server.

    If anyone could work this out, that would be awesome and much appreciated!

    If you need any more info, let me know!

    Spoiler for CacheDownloader:
    Code:
    package com.asgardia.client.cache;
    
    import com.asgardia.Configuration;
    import com.asgardia.client.Client;
    import com.asgardia.client.signlink;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    
    
    /**
     * Enchanced cache downloader
     * Handles cache downloading & unzipping
     * @author Gabriel Hannason
     */
    public class CacheDownloader {
    
    	private static final String CACHE_PATH = signlink.findcachedir();
    
    	private static final String VERSION_FILE = CACHE_PATH + "versions.dat";
    
    	public static void init() {
    
    		if(Configuration.LOCALHOST) {
    			return;
    		}
    
    		try {
    			//DebuggingRunnables.getMessageThread("cachestart").start();;
    
    			for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {
    
    				boolean exists = getLocalVersion(cacheFile.identifier) != -1;
    				//boolean exists = new File(signlink.findcachedir() + cacheFile.identifier).exists();
    
    				long CacheLocal = getLocalVersion(CACHE_DOWNLOAD_FILES.CACHE.identifier);
    
    				long CacheRemote = getRemoteVersion(CACHE_DOWNLOAD_FILES.CACHE.link);
    
    				if(CacheLocal != CacheRemote && CacheRemote != -1){
    					updateFiles(CACHE_DOWNLOAD_FILES.CACHE);
    					writeVersions(CacheRemote);
    				}
    
    				if (!exists) {
    					updateFiles(cacheFile);
    				}
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
    		int total = cacheFile.file.length;
    
    		int current = 1;
    
    		for (String file : cacheFile.file) {
    			downloadFile(cacheFile, file, current, total);
    			if (file.endsWith(".zip")) {
    				unzip(new File(signlink.findcachedir() + file));
    			}
    			current++;
    		}
    
    
    	}
    
    	public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
    		String fileURL = cacheFile.link;
    		String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCase());
    		URL url = new URL(fileURL);
    		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    		httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    		int responseCode = httpConn.getResponseCode();
    
    		// always check HTTP response code first
    		if (responseCode == HttpURLConnection.HTTP_OK) {
    			// opens input stream from the HTTP connection
    			InputStream inputStream = httpConn.getInputStream();
    			String saveFilePath = signlink.findcachedir() + file;
    			// opens an output stream to save into file
    			FileOutputStream outputStream = new FileOutputStream(saveFilePath);
    
    			int bytesRead = -1;
    			byte[] buffer = new byte[4096];
    			long startTime = System.currentTimeMillis();
    			int downloaded = 0;
    			long numWritten = 0;
    			int length = httpConn.getContentLength();
    			while ((bytesRead = inputStream.read(buffer)) != -1) {
    				outputStream.write(buffer, 0, bytesRead);
    				numWritten += bytesRead;
    				downloaded += bytesRead;
    				int percentage = (int)(((double)numWritten / (double)length) * 100D);
    				int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
    				String s = total > 1 ? "("+current+"/"+total+")" : "";
    				drawLoadingText(percentage, (new StringBuilder()).append("Downloading "+downloadingText+""+s+": "+percentage+"%").toString());
    			}
    
    			//DebuggingRunnables.getMessageThread("cacheend").start();
    			outputStream.close();
    			inputStream.close();
    
    		} else {
    			System.out.println("download link replied HTTP code: " + responseCode);
    		}
    		httpConn.disconnect();
    	}
    
    	/**
    	 * Gets the current version for the file.
    	 *
    	 *  @[Only registered and activated users can see links. ] name
    	 *            The file version string.
    	 *  @[Only registered and activated users can see links. ] The version.
    	 */
    	private static long getLocalVersion(String name) {
    		BufferedReader br = null;
    
    		try {
    			File versionDir = new File(VERSION_FILE);
    
    			if (!versionDir.exists()) {
    				versionDir.createNewFile();
    				return -1;
    			}
    
    			if (name.equals("SPRITE_VER")) {
    				if (!new File(CACHE_PATH + "sprites.dat").exists() || !new File(CACHE_PATH + "sprites.idx").exists()) {
    					return -1;
    				}
    			}
    
    			br = new BufferedReader(new FileReader(VERSION_FILE));
    
    			String line;
    
    			while ((line = br.readLine()) != null) {
    				if (line.startsWith(name + " = ")) {
    					long size = Long.parseLong(line.replace(name + " = ", ""));
    					br.close();
    					return size;
    				}
    			}
    
    			br.close();
    			return -1;
    		} catch (Exception e) {
    			return -1;
    		}
    	}
    
    	/**
    	 * Writes the versions into the version file.
    	 *
    	 *  @[Only registered and activated users can see links. ] cacheVersion
    	 *            The version of the cache.
    	 *  @[Only registered and activated users can see links. ] spritesVersion
    	 *            The version of the sprites.
    	 */
    	private static void writeVersions(long cacheVersion) {
    		BufferedWriter writer = null;
    
    		try {
    			writer = new BufferedWriter(new FileWriter(VERSION_FILE));
    			writer.write("CACHE_VER = " + cacheVersion);
    			writer.close();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    
    	private static long getRemoteVersion(String url) {
    		try {
    			return ((HttpURLConnection) new URL(url).openConnection()).getContentLength();
    		} catch (Exception e) {
    			return -1;
    		}
    	}
    
    	private static void drawLoadingText(int amount, String text) {
    		Client.getClient().drawLoadingText(amount, text);
    	}
    
    	private static void unzip(final File file) {
    		//DebuggingRunnables.getMessageThread("zipstart").start();
    		try {
    			InputStream in =  new BufferedInputStream(new FileInputStream(file));
    			ZipInputStream zin = new ZipInputStream(in);
    			ZipEntry e;
    			while((e=zin.getNextEntry()) != null) {
    				if(e.isDirectory()) {
    					(new File(signlink.findcachedir() + e.getName())).mkdir();
    				} else {
    					if (e.getName().equals(file.getName())) {
    						unzipPartlyArchive(zin, file.getName());
    						break;
    					}
    					unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
    				}
    			}
    			zin.close();
    			file.delete();
    			//DebuggingRunnables.getMessageThread("zipend").start();
    		} catch(Exception e) {}
    	}
    
    	/**
    	 * Unzips a partly archive
    	 *  @[Only registered and activated users can see links. ] zin	The zip inputstream
    	 *  @[Only registered and activated users can see links. ] s		The location of the zip file
    	 *  @[Only registered and activated users can see links. ]s IOException	The method can throw an IOException.
    	 */
    	private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
    		FileOutputStream out = new FileOutputStream(s);
    		drawLoadingText(100, "Unpacking data..");
    		byte [] b = new byte[1024];
    		int len = 0;
    
    		while ((len = zin.read(b)) != -1) {
    			out.write(b,0,len);
    		}
    		out.close();
    	}
    
    	public static String getDownloadLink(String name) {
    		try {
    			String webLink = "mydropboxlink.zip" + name;
    			StringBuilder result = new StringBuilder();
    			URL url = new URL(webLink);
    			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    			conn.setRequestMethod("GET");
    			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    			String line;
    			while ((line = rd.readLine()) != null) {
    				if (line.contains("url:")) {
    					return line.replace("url:", "").trim();
    				}
    			}
    			rd.close();
    			return result.toString();
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		return "ERROR_IN_URL";
    	}
    
    	enum CACHE_DOWNLOAD_FILES {
    
    		CACHE(new String[]{"cache.zip"}, "CACHE_VER", getDownloadLink("cache")),
    		;
    
    		CACHE_DOWNLOAD_FILES(String[] file, String identifier, String link) {
    			this.file = file;
    			this.identifier = identifier;
    			this.link = link;
    		}
    
    		private String[] file;
    		private String identifier;
    		private String link;
    	}
    }


    1. What is your cache url, is it a direct link? (Couldnt spot it quickly in your snippets on phone) and the gedownloadlink method gives a wrong url for sure.

    2. Please show a screenshot of excactly what is packed inside of the zip file, just to make sure.

    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. #3  
    Misthalin where it all began.
    Manatee's Avatar
    Join Date
    Dec 2019
    Posts
    50
    Thanks given
    1
    Thanks received
    11
    Rep Power
    12
    this link is your current link where is downloading the cache from.
    Code:
    "mydropboxlink.zip"
    upload your cache files and replace the link and you should be good to go i gues..
    Reply With Quote  
     

  4. #4  
    Registered Member
    Join Date
    Jun 2010
    Posts
    13
    Thanks given
    3
    Thanks received
    3
    Rep Power
    29
    Quote Originally Posted by Tutus Frutus View Post
    1. What is your cache url, is it a direct link? (Couldnt spot it quickly in your snippets on phone) and the gedownloadlink method gives a wrong url for sure.

    2. Please show a screenshot of excactly what is packed inside of the zip file, just to make sure.
    Sure man, The cache:

    Quote Originally Posted by Mantee View Post
    this link is your current link where is downloading the cache from.
    Code:
    "mydropboxlink.zip"
    upload your cache files and replace the link and you should be good to go i gues..
    Spoiler for CacheDownloader.java:
    package com.asgardia.client.cache;

    import com.asgardia.Configuration;
    import com.asgardia.client.Client;
    import com.asgardia.client.signlink;

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;


    /**
    * Enchanced cache downloader
    * Handles cache downloading & unzipping
    * @author Gabriel Hannason
    */
    public class CacheDownloader {

    private static final String CACHE_PATH = signlink.findcachedir();

    private static final String VERSION_FILE = CACHE_PATH + "versions.dat";

    public static void init() {

    if(Configuration.LOCALHOST) {
    return;
    }

    try {
    //DebuggingRunnables.getMessageThread("cachestart"). start();;

    for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {

    boolean exists = getLocalVersion(cacheFile.identifier) != -1;
    //boolean exists = new File(signlink.findcachedir() + cacheFile.identifier).exists();

    long CacheLocal = getLocalVersion(CACHE_DOWNLOAD_FILES.CACHE.identif ier);

    long CacheRemote = getRemoteVersion(CACHE_DOWNLOAD_FILES.CACHE.link);

    if(CacheLocal != CacheRemote && CacheRemote != -1){
    updateFiles(CACHE_DOWNLOAD_FILES.CACHE);
    writeVersions(CacheRemote);
    }

    if (!exists) {
    updateFiles(cacheFile);
    }
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
    int total = cacheFile.file.length;

    int current = 1;

    for (String file : cacheFile.file) {
    downloadFile(cacheFile, file, current, total);
    if (file.endsWith(".zip")) {
    unzip(new File(signlink.findcachedir() + file));
    }
    current++;
    }


    }

    public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
    String fileURL = cacheFile.link;
    String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCa se());
    URL url = new URL(fileURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    int responseCode = httpConn.getResponseCode();

    // always check HTTP response code first
    if (responseCode == HttpURLConnection.HTTP_OK) {
    // opens input stream from the HTTP connection
    InputStream inputStream = httpConn.getInputStream();
    String saveFilePath = signlink.findcachedir() + file;
    // opens an output stream to save into file
    FileOutputStream outputStream = new FileOutputStream(saveFilePath);

    int bytesRead = -1;
    byte[] buffer = new byte[4096];
    long startTime = System.currentTimeMillis();
    int downloaded = 0;
    long numWritten = 0;
    int length = httpConn.getContentLength();
    while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
    numWritten += bytesRead;
    downloaded += bytesRead;
    int percentage = (int)(((double)numWritten / (double)length) * 100D);
    int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
    String s = total > 1 ? "("+current+"/"+total+")" : "";
    drawLoadingText(percentage, (new StringBuilder()).append("Downloading "+downloadingText+""+s+": "+percentage+"%").toString());
    }

    //DebuggingRunnables.getMessageThread("cacheend").st art();
    outputStream.close();
    inputStream.close();

    } else {
    System.out.println("download link replied HTTP code: " + responseCode);
    }
    httpConn.disconnect();
    }

    /**
    * Gets the current version for the file.
    *
    * @param name
    * The file version string.
    * @return The version.
    */
    private static long getLocalVersion(String name) {
    BufferedReader br = null;

    try {
    File versionDir = new File(VERSION_FILE);

    if (!versionDir.exists()) {
    versionDir.createNewFile();
    return -1;
    }

    if (name.equals("SPRITE_VER")) {
    if (!new File(CACHE_PATH + "sprites.dat").exists() || !new File(CACHE_PATH + "sprites.idx").exists()) {
    return -1;
    }
    }

    br = new BufferedReader(new FileReader(VERSION_FILE));

    String line;

    while ((line = br.readLine()) != null) {
    if (line.startsWith(name + " = ")) {
    long size = Long.parseLong(line.replace(name + " = ", ""));
    br.close();
    return size;
    }
    }

    br.close();
    return -1;
    } catch (Exception e) {
    return -1;
    }
    }

    /**
    * Writes the versions into the version file.
    *
    * @param cacheVersion
    * The version of the cache.
    * @param spritesVersion
    * The version of the sprites.
    */
    private static void writeVersions(long cacheVersion) {
    BufferedWriter writer = null;

    try {
    writer = new BufferedWriter(new FileWriter(VERSION_FILE));
    writer.write("CACHE_VER = " + cacheVersion);
    writer.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private static long getRemoteVersion(String url) {
    try {
    return ((HttpURLConnection) new URL(url).openConnection()).getContentLength();
    } catch (Exception e) {
    return -1;
    }
    }

    private static void drawLoadingText(int amount, String text) {
    Client.getClient().drawLoadingText(amount, text);
    }

    private static void unzip(final File file) {
    //DebuggingRunnables.getMessageThread("zipstart").st art();
    try {
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry e;
    while((e=zin.getNextEntry()) != null) {
    if(e.isDirectory()) {
    (new File(signlink.findcachedir() + e.getName())).mkdir();
    } else {
    if (e.getName().equals(file.getName())) {
    unzipPartlyArchive(zin, file.getName());
    break;
    }
    unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
    }
    }
    zin.close();
    file.delete();
    //DebuggingRunnables.getMessageThread("zipend").star t();
    } catch(Exception e) {}
    }

    /**
    * Unzips a partly archive
    * @param zin The zip inputstream
    * @param s The location of the zip file
    * @throws IOException The method can throw an IOException.
    */
    private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
    FileOutputStream out = new FileOutputStream(s);
    drawLoadingText(100, "Unpacking data..");
    byte [] b = new byte[1024];
    int len = 0;

    while ((len = zin.read(b)) != -1) {
    out.write(b,0,len);
    }
    out.close();
    }

    public static String getDownloadLink(String name) {
    try {
    String webLink = "[Only registered and activated users can see links. ]" + name;
    StringBuilder result = new StringBuilder();
    URL url = new URL(webLink);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
    if (line.contains("url:")) {
    return line.replace("url:", "").trim();
    }
    }
    rd.close();
    return result.toString();
    } catch(Exception e) {
    e.printStackTrace();
    }
    return "ERROR_IN_URL";
    }

    enum CACHE_DOWNLOAD_FILES {

    CACHE(new String[]{"cache.zip"}, "CACHE_VER", getDownloadLink("cache")),
    ;

    CACHE_DOWNLOAD_FILES(String[] file, String identifier, String link) {
    this.file = file;
    this.identifier = identifier;
    this.link = link;
    }

    private String[] file;
    private String identifier;
    private String link;
    }
    }


    The cache file is made in the C:/User drive with the .dat/idx & sprites files made but with no data within them.

    All i get is connecting to Update Server, so it's either the link or something wrong within the code.
    Reply With Quote  
     

  5. #5  
    Nalore - Coming soon...

    Tutus Frutus's Avatar
    Join Date
    Feb 2018
    Posts
    417
    Thanks given
    129
    Thanks received
    200
    Rep Power
    638
    Quote Originally Posted by Josh125 View Post
    Sure man, The cache:





    The cache file is made in the C:/User drive with the .dat/idx & sprites files made but with no data within them.

    All i get is connecting to Update Server, so it's either the link or something wrong within the code.
    The link is wrong, dl=0 is NOT a direct link.

    Change it to dl=1 and test, or change it to this [Only registered and activated users can see links. ]

    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  6. #6  
    Registered Member
    Join Date
    Jun 2010
    Posts
    13
    Thanks given
    3
    Thanks received
    3
    Rep Power
    29
    Quote Originally Posted by Tutus Frutus View Post
    The link is wrong, dl=0 is NOT a direct link.

    Change it to dl=1 and test, or change it to this [Only registered and activated users can see links. ]
    Thank's for your help man. I'll give it a shot later! Much appreciated mate.

    EDIT: I have tried all of the folowing link's and it still doesn't auto download
    When typing the highlighted one below in the search bar, it directly download's in the browser rather than views the files, so the link works fine, must be the cache downloader.

    Spoiler for CacheDownloader.java:
    package com.asgardia.client.cache;

    import com.asgardia.Configuration;
    import com.asgardia.client.Client;
    import com.asgardia.client.signlink;

    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;


    /**
    * Enchanced cache downloader
    * Handles cache downloading & unzipping
    * @author Gabriel Hannason
    */
    public class CacheDownloader {

    private static final String CACHE_PATH = signlink.findcachedir();

    private static final String VERSION_FILE = CACHE_PATH + "versions.dat";

    public static void init() {

    if(Configuration.LOCALHOST) {
    return;
    }

    try {
    //DebuggingRunnables.getMessageThread("cachestart"). start();;

    for(CACHE_DOWNLOAD_FILES cacheFile : CACHE_DOWNLOAD_FILES.values()) {

    boolean exists = getLocalVersion(cacheFile.identifier) != -1;
    //boolean exists = new File(signlink.findcachedir() + cacheFile.identifier).exists();

    long CacheLocal = getLocalVersion(CACHE_DOWNLOAD_FILES.CACHE.identif ier);

    long CacheRemote = getRemoteVersion(CACHE_DOWNLOAD_FILES.CACHE.link);

    if(CacheLocal != CacheRemote && CacheRemote != -1){
    updateFiles(CACHE_DOWNLOAD_FILES.CACHE);
    writeVersions(CacheRemote);
    }

    if (!exists) {
    updateFiles(cacheFile);
    }
    }
    } catch(Exception e) {
    e.printStackTrace();
    }
    }

    public static void updateFiles(CACHE_DOWNLOAD_FILES cacheFile) throws Exception {
    int total = cacheFile.file.length;

    int current = 1;

    for (String file : cacheFile.file) {
    downloadFile(cacheFile, file, current, total);
    if (file.endsWith(".zip")) {
    unzip(new File(signlink.findcachedir() + file));
    }
    current++;
    }


    }

    public static void downloadFile(CACHE_DOWNLOAD_FILES cacheFile, String file, int current, int total) throws IOException {
    String fileURL = cacheFile.link;
    String downloadingText = Client.optimizeText(cacheFile.toString().toLowerCa se());
    URL url = new URL(fileURL);
    HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
    httpConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    int responseCode = httpConn.getResponseCode();

    // always check HTTP response code first
    if (responseCode == HttpURLConnection.HTTP_OK) {
    // opens input stream from the HTTP connection
    InputStream inputStream = httpConn.getInputStream();
    String saveFilePath = signlink.findcachedir() + file;
    // opens an output stream to save into file
    FileOutputStream outputStream = new FileOutputStream(saveFilePath);

    int bytesRead = -1;
    byte[] buffer = new byte[4096];
    long startTime = System.currentTimeMillis();
    int downloaded = 0;
    long numWritten = 0;
    int length = httpConn.getContentLength();
    while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
    numWritten += bytesRead;
    downloaded += bytesRead;
    int percentage = (int)(((double)numWritten / (double)length) * 100D);
    int downloadSpeed = (int) ((downloaded / 1024) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
    String s = total > 1 ? "("+current+"/"+total+")" : "";
    drawLoadingText(percentage, (new StringBuilder()).append("Downloading "+downloadingText+""+s+": "+percentage+"%").toString());
    }

    //DebuggingRunnables.getMessageThread("cacheend").st art();
    outputStream.close();
    inputStream.close();

    } else {
    System.out.println("download link replied HTTP code: " + responseCode);
    }
    httpConn.disconnect();
    }

    /**
    * Gets the current version for the file.
    *
    * @param name
    * The file version string.
    * @return The version.
    */
    private static long getLocalVersion(String name) {
    BufferedReader br = null;

    try {
    File versionDir = new File(VERSION_FILE);

    if (!versionDir.exists()) {
    versionDir.createNewFile();
    return -1;
    }

    if (name.equals("SPRITE_VER")) {
    if (!new File(CACHE_PATH + "sprites.dat").exists() || !new File(CACHE_PATH + "sprites.idx").exists()) {
    return -1;
    }
    }

    br = new BufferedReader(new FileReader(VERSION_FILE));

    String line;

    while ((line = br.readLine()) != null) {
    if (line.startsWith(name + " = ")) {
    long size = Long.parseLong(line.replace(name + " = ", ""));
    br.close();
    return size;
    }
    }

    br.close();
    return -1;
    } catch (Exception e) {
    return -1;
    }
    }

    /**
    * Writes the versions into the version file.
    *
    * @param cacheVersion
    * The version of the cache.
    * @param
    * // The version of the sprites.
    */
    private static void writeVersions(long cacheVersion) {
    BufferedWriter writer = null;

    try {
    writer = new BufferedWriter(new FileWriter(VERSION_FILE));
    writer.write("CACHE_VER = " + cacheVersion);
    writer.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    private static long getRemoteVersion(String url) {
    try {
    return ((HttpURLConnection) new URL(url).openConnection()).getContentLength();
    } catch (Exception e) {
    return -1;
    }
    }

    private static void drawLoadingText(int amount, String text) {
    Client.getClient().drawLoadingText(amount, text);
    }

    private static void unzip(final File file) {
    //DebuggingRunnables.getMessageThread("zipstart").st art();
    try {
    InputStream in = new BufferedInputStream(new FileInputStream(file));
    ZipInputStream zin = new ZipInputStream(in);
    ZipEntry e;
    while((e=zin.getNextEntry()) != null) {
    if(e.isDirectory()) {
    (new File(signlink.findcachedir() + e.getName())).mkdir();
    } else {
    if (e.getName().equals(file.getName())) {
    unzipPartlyArchive(zin, file.getName());
    break;
    }
    unzipPartlyArchive(zin, signlink.findcachedir() + e.getName());
    }
    }
    zin.close();
    file.delete();
    //DebuggingRunnables.getMessageThread("zipend").star t();
    } catch(Exception e) {}
    }

    /**
    * Unzips a partly archive
    * @param zin The zip inputstream
    * @param s The location of the zip file
    * @throws IOException The method can throw an IOException.
    */
    private static void unzipPartlyArchive(ZipInputStream zin, String s) throws Exception {
    FileOutputStream out = new FileOutputStream(s);
    drawLoadingText(100, "Unpacking data..");
    byte [] b = new byte[1024];
    int len = 0;

    while ((len = zin.read(b)) != -1) {
    out.write(b,0,len);
    }
    out.close();
    }

    public static String getDownloadLink(String name) {
    try {
    String webLink = "[Only registered and activated users can see links. ]" + name;
    StringBuilder result = new StringBuilder();
    URL url = new URL(webLink);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
    if (line.contains("url:")) {
    return line.replace("url:", "").trim();
    }
    }
    rd.close();
    return result.toString();
    } catch(Exception e) {
    e.printStackTrace();
    }
    return "ERROR_IN_URL";
    }

    enum CACHE_DOWNLOAD_FILES {

    CACHE(new String[]{"cache.zip"}, "CACHE_VER", getDownloadLink("cache")),
    ;

    CACHE_DOWNLOAD_FILES(String[] file, String identifier, String link) {
    this.file = file;
    this.identifier = identifier;
    this.link = link;
    }

    private String[] file;
    private String identifier;
    private String link;
    }
    }


    Spoiler for All the link's i have tried::
    http://dl.dropbox.com/s/v6i3tcw187y0f2b/.appletasg.zip
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]


    Quote Originally Posted by Tutus Frutus View Post
    The link is wrong, dl=0 is NOT a direct link.

    Change it to dl=1 and test, or change it to this [Only registered and activated users can see links. ]
    I Wonder.. Could there be something in there that read's to Not auto-download. Like, to disable the auto-cachedownload and just use the local cache that's there already? Honestly cannot get my head around it.
    Fuck COVID

    Reply With Quote  
     

  7. #7  
    Nalore - Coming soon...

    Tutus Frutus's Avatar
    Join Date
    Feb 2018
    Posts
    417
    Thanks given
    129
    Thanks received
    200
    Rep Power
    638
    Quote Originally Posted by Josh125 View Post
    Thank's for your help man. I'll give it a shot later! Much appreciated mate.

    EDIT: I have tried all of the folowing link's and it still doesn't auto download
    When typing the highlighted one below in the search bar, it directly download's in the browser rather than views the files, so the link works fine, must be the cache downloader.



    Spoiler for All the link's i have tried::
    http://dl.dropbox.com/s/v6i3tcw187y0f2b/.appletasg.zip
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]




    I Wonder.. Could there be something in there that read's to Not auto-download. Like, to disable the auto-cachedownload and just use the local cache that's there already? Honestly cannot get my head around it.
    `String webLink = "https://www.dropbox.com/s/v6i3tcw187...etasg.zip?dl=1" + name;` remove "+ name" here...

    [Only registered and activated users can see links. ]
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  8. #8  
    Registered Member
    Join Date
    Jun 2010
    Posts
    13
    Thanks given
    3
    Thanks received
    3
    Rep Power
    29
    Quote Originally Posted by Tutus Frutus View Post
    `String webLink = "https://www.dropbox.com/s/v6i3tcw187...etasg.zip?dl=1" + name;` remove "+ name" here...
    Tried that too! Didn't work either

    Quote Originally Posted by Tutus Frutus View Post
    `String webLink = "https://www.dropbox.com/s/v6i3tcw187...etasg.zip?dl=1" + name;` remove "+ name" here...


    Thanks for your help mate, I had a helping hand from Hzn, who got it working!
    Fuck COVID

    Reply With Quote  
     


Thread Information
Users Browsing this Thread

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


User Tag List

Similar Threads

  1. Replies: 4
    Last Post: 08-25-2017, 04:54 PM
  2. Can anyone comment this MYSQL stuff out?
    By Projecteco in forum Help
    Replies: 2
    Last Post: 05-28-2015, 04:56 AM
  3. Can anyone figure this out?
    By dren1 in forum Help
    Replies: 0
    Last Post: 08-19-2014, 10:00 AM
  4. Replies: 6
    Last Post: 01-08-2011, 08:07 PM
  5. can anyone make this into a web client :/
    By Penor in forum RS2 Client
    Replies: 0
    Last Post: 07-16-2008, 12:31 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •