Finished base for randoms

This commit is contained in:
JKetelaar
2014-09-11 23:31:41 +02:00
parent b66c8f970c
commit cd8943d079
4 changed files with 68 additions and 47 deletions
+26 -10
View File
@@ -1,6 +1,6 @@
package org.rev317.min.randoms;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.io.ProgressListener;
import org.parabot.environment.api.utils.WebUtil;
@@ -18,28 +18,43 @@ import java.net.URLClassLoader;
public class Downloader {
public Downloader(){
downloadRandoms();
parseRandoms();
if (downloadRandoms()) {
Core.verbose("Parsing random(s)...");
parseRandoms();
}else{
Core.verbose("There do not seem to be any randoms for this server...");
}
}
private void parseRandoms(){
File myJar = new File(Directories.getCachePath() + "/" + /* Context.getInstance().getServerProviderInfo().getCRC32() + "-randoms.jar"*/ "randoms.jar");
File myJar = new File(Directories.getCachePath() + "/randoms.jar");
if (!myJar.exists() || !myJar.canRead()){
return;
}
try {
URL url = myJar.toURI().toURL();
URL[] urls = new URL[]{url};
String server = "ikov";
URLClassLoader child = new URLClassLoader(urls, this.getClass().getClassLoader());
Class<?> classToLoad = Class.forName("org.parabot.randoms.Core", true, child);
Method method = classToLoad.getDeclaredMethod("init");
Method method = classToLoad.getDeclaredMethod("init", String.class);
Object instance = classToLoad.newInstance();
Object result = method.invoke(instance);
Object result = method.invoke(instance, server);
Core.verbose("Parsed random(s)!");
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | ClassNotFoundException | MalformedURLException e) {
e.printStackTrace();
}
}
private void downloadRandoms(){
private boolean downloadRandoms(){
try {
WebUtil.downloadFile(new URL("http://sdn.parabot.org/providers/getRandom.php?id=" + Context.getInstance().getServerProviderInfo().getCRC32()), new File(Directories.getCachePath() + "/" + Context.getInstance().getServerProviderInfo().getCRC32() + "-randoms.jar"), new ProgressListener() {
File random = new File(Directories.getCachePath() + "/randoms.jar");
if (random.exists()){
Core.verbose("Random already exists, no need to download it.");
return true;
}
String downloadLink = "http://sdn.parabot.org/randoms.php";
WebUtil.downloadFile(new URL(downloadLink), random, new ProgressListener() {
@Override
public void onProgressUpdate(double v) {
@@ -50,8 +65,9 @@ public class Downloader {
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
return random.exists();
} catch (Exception e) {
return false;
}
}
}