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. Click Here To Register...] name
* The file version string.
* @[Only registered and activated users can see links. Click Here To Register...] 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. Click Here To Register...] cacheVersion
* The version of the cache.
* @[Only registered and activated users can see links. Click Here To Register...] 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. Click Here To Register...] zin The zip inputstream
* @[Only registered and activated users can see links. Click Here To Register...] s The location of the zip file
* @[Only registered and activated users can see links. Click Here To Register...]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:Quote:
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;
}
