mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 16:49:10 +00:00
[TASK] Improved the Server loader
This commit is contained in:
@@ -15,6 +15,7 @@ public class Configuration {
|
||||
public static final String GET_SERVER_PROVIDERS = "http://bdn.parabot.org/api/get.php?action=server_providers";
|
||||
public static final String GET_SERVER_PROVIDER = "http://bdn.parabot.org/api/get.php?action=server_provider&name=";
|
||||
public static final String GET_SERVER_PROVIDER_INFO = "http://bdn.parabot.org/api/get.php?action=server_information&name=";
|
||||
public static final String GET_SERVER_SETTINGS = "http://bdn.parabot.org/api/get.php?action=get_settings";
|
||||
public static final String GET_BOT_VERSION = "http://bdn.parabot.org/api/v2/bot/version";
|
||||
public static final String API_DOWNLOAD_BOT = "http://bdn.parabot.org/api/v2/bot/download/client/";
|
||||
public static final String DOWNLOAD_BOT = "http://bdn.parabot.org/versions/";
|
||||
|
||||
@@ -312,7 +312,7 @@ public class Directories {
|
||||
* @param extension The extension to be searched for, including the dot (like .json)
|
||||
* @return An array of of files that match the request
|
||||
*/
|
||||
private File[] listFilesWithExtension(File directory, final String extension){
|
||||
public static File[] listFilesWithExtension(File directory, final String extension){
|
||||
return directory.listFiles(new FilenameFilter() {
|
||||
public boolean accept(File dir, String filename) {
|
||||
return filename.endsWith(extension);
|
||||
@@ -320,7 +320,7 @@ public class Directories {
|
||||
});
|
||||
}
|
||||
|
||||
private File[] listJSONFiles(File directory) {
|
||||
public static File[] listJSONFiles(File directory) {
|
||||
return listFilesWithExtension(directory, ".json");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,15 @@ package org.parabot.core.desc;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.parabot.core.Configuration;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.ui.utils.UILog;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
@@ -29,40 +32,44 @@ public class ServerProviderInfo {
|
||||
this.properties = new Properties();
|
||||
this.settings = new HashMap<>();
|
||||
try {
|
||||
String line;
|
||||
Core.verbose("Reading info: " + providerInfo);
|
||||
BufferedReader br = WebUtil.getReader(new URL(providerInfo.toString()), username, password);
|
||||
|
||||
//TODO Make this one line (web sided)
|
||||
JSONParser parser = new JSONParser();
|
||||
if ((line = br.readLine()) != null) {
|
||||
JSONObject jsonObject = (JSONObject) parser.parse(line);
|
||||
for (Object o : jsonObject.entrySet()) {
|
||||
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) o;
|
||||
if (String.valueOf(pairs.getKey()).equalsIgnoreCase("settings")){
|
||||
JSONObject object = (JSONObject) pairs.getValue();
|
||||
for (Object settingObject : object.entrySet()){
|
||||
Map.Entry<?, ?> settingValue = (Map.Entry<?, ?>) settingObject;
|
||||
String key = (String) settingValue.getKey();
|
||||
long value = (Long) settingValue.getValue();
|
||||
settings.put(key, (int) value);
|
||||
}
|
||||
}else {
|
||||
properties.put(String.valueOf(pairs.getKey()), String.valueOf(pairs.getValue()));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
UILog.log(
|
||||
"Error",
|
||||
"Failed to load server provider, error: [No information about the provider found.]",
|
||||
JOptionPane.ERROR_MESSAGE);
|
||||
return;
|
||||
}
|
||||
br.close();
|
||||
} catch (Exception e) {
|
||||
JSONObject jsonObject = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||
for (Object o : jsonObject.entrySet()) {
|
||||
Map.Entry<?, ?> pairs = (Map.Entry<?, ?>) o;
|
||||
if (String.valueOf(pairs.getKey()).equalsIgnoreCase("settings")){
|
||||
JSONObject object = (JSONObject) pairs.getValue();
|
||||
parseSettings(object);
|
||||
}else {
|
||||
properties.put(String.valueOf(pairs.getKey()), String.valueOf(pairs.getValue()));
|
||||
}
|
||||
}
|
||||
if (br != null) {
|
||||
br.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public ServerProviderInfo(){
|
||||
try {
|
||||
BufferedReader br = WebUtil.getReader(new URL(Configuration.GET_SERVER_SETTINGS));
|
||||
JSONObject settings = (JSONObject) WebUtil.getJsonParser().parse(br);
|
||||
} catch (ParseException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private void parseSettings(JSONObject object){
|
||||
for (Object settingObject : object.entrySet()){
|
||||
Map.Entry<?, ?> settingValue = (Map.Entry<?, ?>) settingObject;
|
||||
String key = (String) settingValue.getKey();
|
||||
long value = (Long) settingValue.getValue();
|
||||
settings.put(key, (int) value);
|
||||
}
|
||||
}
|
||||
|
||||
public URL getClient() {
|
||||
try {
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
package org.parabot.core.parsers.servers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FilenameFilter;
|
||||
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.Directories;
|
||||
import org.parabot.core.classpath.ClassPath;
|
||||
import org.parabot.core.desc.ServerDescription;
|
||||
import org.parabot.environment.api.utils.WebUtil;
|
||||
import org.parabot.environment.servers.ServerManifest;
|
||||
import org.parabot.environment.servers.executers.LocalServerExecuter;
|
||||
import org.parabot.environment.servers.loader.ServerLoader;
|
||||
@@ -66,7 +71,20 @@ public class LocalServers extends ServerParser {
|
||||
}
|
||||
}
|
||||
|
||||
for (File file : Directories.listJSONFiles(Directories.getServerPath())){
|
||||
try {
|
||||
JSONObject object = (JSONObject) WebUtil.getJsonParser().parse(new FileReader(file));
|
||||
String name = (String) object.get("name");
|
||||
String clientClass = (String) object.get("client-class");
|
||||
|
||||
JSONObject locations = (JSONObject) object.get("locations");
|
||||
String server = (String) locations.get("server");
|
||||
String hooks = (String) locations.get("hooks");
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -56,10 +56,8 @@ public class PublicServerExecuter extends ServerExecuter {
|
||||
serverProviderInfo.getCRC32() + ".jar");
|
||||
final String jarUrl = Configuration.GET_SERVER_PROVIDER
|
||||
+ this.serverName;
|
||||
System.out.println(jarUrl);
|
||||
|
||||
Core.verbose("Downloading: " + jarUrl + " ...");
|
||||
|
||||
|
||||
if(destination.exists()) {
|
||||
Core.verbose("Found cached server provider [CRC32: " + serverProviderInfo.getCRC32() + "]");
|
||||
|
||||
Reference in New Issue
Block a user