Merge pull request #254 from Shadowrs/feature/override-randoms

Feature/override randoms
This commit is contained in:
Jeroen Ketelaar
2018-09-12 10:53:12 -05:00
committed by GitHub
3 changed files with 80 additions and 29 deletions
@@ -50,7 +50,28 @@ public class ServerProviderInfo {
}
}
/**
* Initialize configuration with data provided by {@link org.parabot.core.parsers.servers.LocalServers} from a {@code /parabot/servers/config.json} file. Also loads the default Settings map from the BDN.
* @param clientJar Name of the client jar file
* @param hooks Name of the hooks file
* @param name Server name
* @param clientClass Entry class within the client jar
* @param bankTabs Bank tabs - only relevant for certain servers. Default 0
*/
public ServerProviderInfo(String clientJar, String hooks, String name, String clientClass, int bankTabs) {
this(clientJar, hooks, name, clientClass, bankTabs, null);
}
/**
* Initialize configuration with data provided by {@link org.parabot.core.parsers.servers.LocalServers} from a {@code /parabot/servers/config.json} file. Also loads the default Settings map from the BDN.
* @param clientJar Name of the client jar file
* @param hooks Name of the hooks file
* @param name Server name
* @param clientClass Entry class within the client jar
* @param bankTabs Bank tabs - only relevant for certain servers. Default 0
* @param randoms A URL to an endpoint where the Randoms are located. Can be Null, in which case getRandoms() will fallback to the default BDN Randoms URL.
*/
public ServerProviderInfo(String clientJar, String hooks, String name, String clientClass, int bankTabs, String randoms) {
this.properties = new Properties();
this.settings = new HashMap<>();
@@ -69,6 +90,7 @@ public class ServerProviderInfo {
this.properties.setProperty("provider_crc32", String.valueOf(getCRC32(name, "provider")));
this.properties.setProperty("client_crc32", String.valueOf(getCRC32(name, "client")));
this.properties.setProperty("bank_tabs", String.valueOf(bankTabs));
this.properties.setProperty("randoms_jar", randoms);
}
private long getCRC32(String name, String type) {
@@ -141,4 +163,23 @@ public class ServerProviderInfo {
public HashMap<String, Integer> getSettings() {
return settings;
}
/**
* Gets the URL to download the Randoms JAR from.
* @return The provided URL in the server config JSON (denoted by 'randoms:') or, fallback to the default BDN URL.
*/
public URL getRandoms() {
try {
String randomsUrl = properties.getProperty("randoms_jar");
if (randomsUrl == null || randomsUrl.length() == 0) {
// Fallback to default BDN URL if there is no 'randoms' specified in the server JSON configuration.
randomsUrl = Configuration.GET_RANDOMS + (Configuration.BOT_VERSION.isNightly() ? Configuration.NIGHTLY_APPEND : "");
}
return new URL(randomsUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
// Will never return null, unless the BDN URL is changed. It shouldn't be.
return null;
}
}
@@ -1,18 +1,18 @@
package org.parabot.core.parsers.randoms;
import org.parabot.api.io.WebUtil;
import org.parabot.core.Configuration;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.io.NoProgressListener;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.parabot.api.io.WebUtil;
import org.parabot.api.output.Logger;
import org.parabot.core.Configuration;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.io.NoProgressListener;
/**
* @author JKetelaar
@@ -23,12 +23,21 @@ public class PublicRandoms extends RandomParser {
@Override
public void parse() {
File myJar = new File(Directories.getCachePath() + File.separator + fileName);
if (!myJar.exists() || !myJar.canRead()) {
download();
final File destination = new File(Directories.getCachePath() + File.separator + fileName);
final URL overrideDownload = Context.getInstance().getServerProviderInfo().getRandoms();
if (overrideDownload == null) {
throw new NullPointerException("Unable to grab URL for Randoms jar. Default URL for BDN randoms must have changed!");
}
Core.verbose(String.format("[%s] Destination: %s | dl: %s", getClass().getSimpleName(), destination, overrideDownload));
if (!destination.exists() || !destination.canRead()) {
Core.verbose(String.format("[%s] Missing %s - downloading from %s...", getClass().getSimpleName(), destination.getAbsolutePath(), overrideDownload));
download(destination, overrideDownload);
}
try {
URL url = myJar.toURI().toURL();
URL url = destination.toURI().toURL();
URL[] urls = new URL[]{ url };
String server = Context.getInstance().getServerProviderInfo().getServerName();
@@ -36,7 +45,7 @@ public class PublicRandoms extends RandomParser {
Class<?> classToLoad = Class.forName("org.parabot.randoms.Core", true, child);
Method method = classToLoad.getDeclaredMethod("init", String.class);
Object instance = classToLoad.newInstance();
System.out.println(server);
Core.verbose(String.format("[%s] %s %s", getClass().getSimpleName(), "Initing core Randoms for", server));
method.invoke(instance, server);
Core.verbose("Successfully parsed public random!");
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | ClassNotFoundException | MalformedURLException e) {
@@ -45,17 +54,14 @@ public class PublicRandoms extends RandomParser {
}
}
private void download() {
private void download(final File destination, URL downloadLink) {
try {
File random = new File(Directories.getCachePath() + File.separator + fileName);
if (random.exists()) {
if (destination.exists()) {
Core.verbose("Public random dependency already exists, no need to download it...");
return;
}
String downloadLink = ((Configuration.BOT_VERSION.isNightly()) ? Configuration.GET_RANDOMS + Configuration.NIGHTLY_APPEND : Configuration.GET_RANDOMS);
WebUtil.downloadFile(new URL(downloadLink), random, new NoProgressListener());
WebUtil.downloadFile(downloadLink, destination, new NoProgressListener());
} catch (Exception e) {
e.printStackTrace();
}
@@ -1,7 +1,13 @@
package org.parabot.core.parsers.servers;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import org.parabot.core.Configuration;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.classpath.ClassPath;
@@ -13,12 +19,6 @@ import org.parabot.environment.servers.executers.LocalPublicServerExecuter;
import org.parabot.environment.servers.executers.LocalServerExecuter;
import org.parabot.environment.servers.loader.ServerLoader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
/**
* Parses local server providers located in the servers directory
*
@@ -93,11 +93,15 @@ public class LocalServers extends ServerParser {
String server = (String) locations.get("server");
String provider = (String) locations.get("provider");
String hooks = (String) locations.get("hooks");
Core.verbose("[Local server]: " + name);
ServerProviderInfo serverProviderInfo = new ServerProviderInfo(server, hooks, name, clientClass, bankTabs);
String randoms = (String) locations.get("randoms");
if (randoms == null) {
randoms = Configuration.GET_RANDOMS + (Configuration.BOT_VERSION.isNightly() ? Configuration.NIGHTLY_APPEND : "");
}
Core.verbose("[LocalServers]: Parsed server: " + name);
ServerProviderInfo serverProviderInfo = new ServerProviderInfo(server, hooks, name, clientClass, bankTabs, randoms);
ServerDescription desc = new ServerDescription(name, author, version);
if (uuidStr != null && uuidStr.length() > 0) {