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
+1 -1
View File
@@ -73,7 +73,7 @@
<dependency> <dependency>
<groupId>org.parabot</groupId> <groupId>org.parabot</groupId>
<artifactId>internal-api</artifactId> <artifactId>internal-api</artifactId>
<version>1.4.46</version> <version>1.4.5</version>
</dependency> </dependency>
</dependencies> </dependencies>
+4 -23
View File
@@ -20,6 +20,7 @@ import java.net.URISyntaxException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.Date;
/** /**
* The core of parabot * 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() { private static void validateCache() {
File[] cache = Directories.getCachePath().listFiles(); // Already handled by Directories initiating
Integer lowest = null; // Method will be used once BDN V3 has a functionality for this
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();
}
} }
public static void downloadNewVersion() { public static void downloadNewVersion() {
@@ -2,4 +2,7 @@ package org.parabot.core.asm.redirect;
public class ProcessBuilderRedirect { 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) { public static String getProperty(String s) {
String value; String value;
switch (s) { switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path": case "java.class.path":
value = "."; value = ".";
break; break;
default: default:
value = System.getProperty(s); value = System.getProperty(s);
break;
} }
System.out.printf("GetSystemProp %s = %s\n", s, value); System.out.printf("GetSystemProp %s = %s\n", s, value);
return value; return value;
@@ -42,9 +40,6 @@ public class SystemRedirect {
public static String getProperty(String s, String s2) { public static String getProperty(String s, String s2) {
String value = null; String value = null;
switch (s2) { switch (s2) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path": case "java.class.path":
value = "."; value = ".";
break; break;
@@ -52,14 +47,12 @@ public class SystemRedirect {
if (value == null) { if (value == null) {
switch (s) { switch (s) {
case "user.home":
value = Directories.getCachePath().getAbsolutePath();
break;
case "java.class.path": case "java.class.path":
value = "."; value = ".";
break; break;
default: default:
value = System.getProperty(s); value = System.getProperty(s);
break;
} }
} }
System.out.printf("GetSystemProp %s = %s\n", s, value); System.out.printf("GetSystemProp %s = %s\n", s, value);
@@ -12,40 +12,49 @@ import java.util.LinkedList;
/** /**
* Initializes the bot environment
* *
* Initiliazes the bot environment * @author Everel, JKetelaar
*
* @author Everel
*
*/ */
public class Environment { public class Environment {
/** /**
* Loads a new environment * Loads a new environment
* *
* @param desc * @param desc
*/ */
public static void load(final ServerDescription desc) { public static void load(final ServerDescription desc) {
LinkedList<Library> libs = new LinkedList<>(); LinkedList<Library> libs = new LinkedList<>();
libs.add(new JavaFX()); libs.add(new JavaFX());
for(Library lib : libs) { for (Library lib : libs) {
if (lib.requiresJar()) { loadLibrary(lib, true);
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() + "..."); Core.verbose("Loading server: " + desc.toString() + "...");
ServerParser.SERVER_CACHE.get(desc).run(); 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; package org.parabot.environment.api.utils;
import org.parabot.api.misc.StringUtil;
/** /**
* @author mkyong, JKetelaar * @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 { public enum RandomType {
SCRIPT(0, "Script"), SCRIPT(0, "Script"),
ON_SCRIPT_START(0, "On script start"), ON_SCRIPT_START(1, "On script start"),
ON_SERVER_START(0, "On server start"), ON_SERVER_START(2, "On server start"),
ON_SCRIPT_FINISH(0, "On script finish"); ON_SCRIPT_FINISH(3, "On script finish");
private int id; private int id;
private String name; private String name;
@@ -0,0 +1,32 @@
package org.parabot;
import org.junit.Assert;
import org.junit.Test;
import org.parabot.core.Directories;
import java.io.File;
import java.io.IOException;
/**
* @author JKetelaar
*/
public class CacheValidationTest {
@Test
public void test() throws IOException {
Directories.validate();
File fileOne = new File(Directories.getCachePath(), "should-exist.tmp");
File fileTwo = new File(Directories.getCachePath(), "should-not-exist.tmp");
fileOne.createNewFile();
fileTwo.createNewFile();
fileTwo.setLastModified(System.currentTimeMillis() / 1000 - 350000);
Directories.clearCache(259200, false);
Assert.assertTrue(fileOne.exists());
Assert.assertTrue(!fileTwo.exists());
}
}