Merge branch 'development' into feature/PushBulletNotifications

This commit is contained in:
Jeroen Ketelaar
2017-02-05 18:58:20 +01:00
committed by GitHub
8 changed files with 89 additions and 113 deletions
+4 -23
View File
@@ -20,6 +20,7 @@ import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
/**
* The core of parabot
@@ -206,31 +207,11 @@ public class Core {
}
/**
* Validates the cache and removes the cache contents if required
* Method that removes the cache contents after 3 days
*/
private static void validateCache() {
File[] cache = Directories.getCachePath().listFiles();
Integer lowest = null;
if (cache != null) {
for (File f : cache) {
int date = (int) (f.lastModified() / 1000);
if (lowest == null || date < lowest) {
lowest = date;
}
}
}
try {
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(WebUtil.getContents("http://bdn.parabot.org/api/v2/bot/cache", "date=" + lowest));
if ((boolean) object.get("result")) {
Core.verbose("Making space for the latest cache files");
Directories.clearCache();
} else {
Core.verbose("Cache is up to date");
}
} catch (MalformedURLException | ParseException e) {
e.printStackTrace();
}
// Already handled by Directories initiating
// Method will be used once BDN V3 has a functionality for this
}
public static void downloadNewVersion() {
@@ -2,4 +2,7 @@ package org.parabot.core.asm.redirect;
public class ProcessBuilderRedirect {
public static ProcessBuilder redirectErrorStream(ProcessBuilder pb, boolean redirectErrorStream) {
return pb;
}
}
@@ -26,14 +26,12 @@ public class SystemRedirect {
public static String getProperty(String s) {
String value;
switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
default:
value = System.getProperty(s);
break;
}
System.out.printf("GetSystemProp %s = %s\n", s, value);
return value;
@@ -42,9 +40,6 @@ public class SystemRedirect {
public static String getProperty(String s, String s2) {
String value = null;
switch (s2) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
@@ -52,14 +47,12 @@ public class SystemRedirect {
if (value == null) {
switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path":
value = ".";
break;
default:
value = System.getProperty(s);
break;
}
}
System.out.printf("GetSystemProp %s = %s\n", s, value);
@@ -12,40 +12,49 @@ import java.util.LinkedList;
/**
*
* Initiliazes the bot environment
*
* @author Everel
*
* Initializes the bot environment
*
* @author Everel, JKetelaar
*/
public class Environment {
/**
* Loads a new environment
*
* @param desc
*/
public static void load(final ServerDescription desc) {
LinkedList<Library> libs = new LinkedList<>();
libs.add(new JavaFX());
for(Library lib : libs) {
if (lib.requiresJar()) {
if (!lib.hasJar()) {
Core.verbose("Downloading " + lib.getLibraryName() + "...");
VerboseLoader.setState("Downloading " + lib.getLibraryName() + "...");
WebUtil.downloadFile(lib.getDownloadLink(), lib.getJarFile(), VerboseLoader.get());
Core.verbose("Downloaded " + lib.getLibraryName() + ".");
}
Core.verbose("Initializing " + lib.getLibraryName());
lib.init();
}
}
Core.verbose("Loading server: " + desc.toString() + "...");
/**
* Loads a new environment
*
* @param desc
*/
public static void load(final ServerDescription desc) {
ServerParser.SERVER_CACHE.get(desc).run();
}
LinkedList<Library> libs = new LinkedList<>();
libs.add(new JavaFX());
for (Library lib : libs) {
loadLibrary(lib, true);
}
Core.verbose("Loading server: " + desc.toString() + "...");
ServerParser.SERVER_CACHE.get(desc).run();
}
/**
* Loads library into environment
*
* @param library
* @param verboseLoader defines if verboseLoader should be enabled
*/
public static void loadLibrary(Library library, boolean verboseLoader) {
if (library.requiresJar()) {
if (!library.hasJar()) {
Core.verbose("Downloading " + library.getLibraryName() + "...");
if (verboseLoader) {
VerboseLoader.setState("Downloading " + library.getLibraryName() + "...");
}
WebUtil.downloadFile(library.getDownloadLink(), library.getJarFile(), VerboseLoader.get());
Core.verbose("Downloaded " + library.getLibraryName() + ".");
}
Core.verbose("Initializing " + library.getLibraryName());
library.init();
}
}
}
@@ -1,52 +1,10 @@
package org.parabot.environment.api.utils;
import org.parabot.api.misc.StringUtil;
/**
* @author mkyong, JKetelaar
*/
public class StringUtils {
public class StringUtils extends StringUtil {
private static java.util.Random random = new java.util.Random();
private static char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".toCharArray();
public static String convertHexToString(String hex) {
StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();
for (int i = 0; i < hex.length() - 1; i += 2) {
// grab the hex in pairs
String output = hex.substring(i, (i + 2));
// convert hex to decimal
int decimal = Integer.parseInt(output, 16);
// convert the decimal to character
sb.append((char) decimal);
temp.append(decimal);
}
return sb.toString();
}
public static String implode(String separator, String... data) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length - 1; i++) {
//data.length - 1 => to not add separator at the end
if (!data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
sb.append(data[i]);
sb.append(separator);
}
}
sb.append(data[data.length - 1].trim());
return sb.toString();
}
public static String randomString(final int length) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
return sb.toString();
}
}
@@ -6,9 +6,9 @@ package org.parabot.environment.randoms;
public enum RandomType {
SCRIPT(0, "Script"),
ON_SCRIPT_START(0, "On script start"),
ON_SERVER_START(0, "On server start"),
ON_SCRIPT_FINISH(0, "On script finish");
ON_SCRIPT_START(1, "On script start"),
ON_SERVER_START(2, "On server start"),
ON_SCRIPT_FINISH(3, "On script finish");
private int id;
private String name;