v2.01 update

This commit is contained in:
Clisprail
2013-09-04 14:45:23 +02:00
parent 36f7e0407e
commit edb17e188b
89 changed files with 2866 additions and 847 deletions
@@ -1,24 +1,16 @@
package org.parabot.environment;
import java.lang.reflect.Constructor;
import java.net.MalformedURLException;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.build.BuildPath;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.desc.ServerDescription;
import org.parabot.core.parsers.ServerManifestParser;
import org.parabot.core.parsers.ServerManifestParser.ServerCache;
import org.parabot.core.ui.BotUI;
import org.parabot.core.ui.components.BotToolbar;
import org.parabot.environment.servers.ServerProvider;
import org.parabot.environment.servers.loader.ServerLoader;
import org.parabot.core.jython.Jython;
import org.parabot.core.parsers.servers.ServerParser;
import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.environment.api.utils.WebUtil;
/**
*
* @author Clisprail
* @author Everel
*
*/
public class Environment {
@@ -28,65 +20,23 @@ public class Environment {
*
* @param url
*/
public static void load(final ServerDescription desc, final String serverName) {
if (!BotUI.getInstance().isVisible()) {
BotUI.getInstance().setVisible(true);
public static void load(final ServerDescription desc) {
if (!Jython.hasJar()) {
Core.verbose("Downloading jython...");
VerboseLoader.setState("Downloading jython...");
WebUtil.downloadFile(Jython.getDownloadLink(), Jython.getJarFile(),
VerboseLoader.get());
Core.verbose("Downloaded jython.");
}
Core.verbose("Initializing jython...");
Jython.init();
Core.verbose("Loading server: " + desc.toString());
final ClassPath classPath = Core.inDebugMode() ? null : new ClassPath();
final ServerCache cache = Core.inDebugMode() ? ServerManifestParser.cache.get(desc) : null;
// buildpath
if(cache != null) {
if(cache.getLoader().getClassPath().isJar()) {
cache.getLoader().getClassPath().addToBuildPath();
} else {
try {
BuildPath.add(Directories.getServerPath().toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
final ServerLoader serverLoader = Core.inDebugMode() ? cache.getLoader() : new ServerLoader(classPath);
String[] serverProviders = null;
if (!Core.inDebugMode()) {
serverProviders = serverLoader.getServerClassNames();
if (serverProviders == null) {
throw new RuntimeException("No server provided.");
}
}
final String id = "tab" + Context.getID();
final ThreadGroup bot = new ThreadGroup(id);
new Thread(bot, new Runnable() {
@Override
public void run() {
try {
final ServerProvider server = !Core.inDebugMode() ? fetchServerProvider(serverLoader) : cache.getProviders()[desc.providerIndex];
final Context context = new Context(server);
context.setEnvironment(serverLoader);
BotToolbar.getInstance().addTab(context, serverName);
context.load();
} catch (Throwable t) {
t.printStackTrace();
}
}
}).start();
}
private static ServerProvider fetchServerProvider(ServerLoader loader) {
try {
final String[] serverProviders = loader.getServerClassNames();
if (serverProviders == null) {
throw new RuntimeException("No server provided.");
}
final Class<?> serverProviderClass = loader.loadClass(serverProviders[0]);
final Constructor<?> con = serverProviderClass.getConstructor();
return (ServerProvider) con.newInstance();
} catch (Throwable t) {
throw new RuntimeException("Error while loading server. " + t.getMessage());
}
ServerParser.SERVER_CACHE.get(desc).run(bot);
}
}
@@ -1,7 +1,6 @@
package org.parabot.environment;
public enum OperatingSystem
{
public enum OperatingSystem {
WINDOWS, LINUX, MAC, OTHER;
@@ -17,4 +16,3 @@ public enum OperatingSystem
}
}
@@ -4,7 +4,7 @@ import java.awt.Graphics;
/**
*
* @author Clisprail
* @author Everel
*
*/
public interface Paintable {
@@ -3,7 +3,7 @@ package org.parabot.environment.api.utils;
/**
* A simple class to filter things
*
* @author Clisprail
* @author Everel
*
* @param <F>
*/
@@ -2,7 +2,7 @@ package org.parabot.environment.api.utils;
/**
*
* @author Clisprail
* @author Everel
*
*/
public class Random {
@@ -2,7 +2,7 @@ package org.parabot.environment.api.utils;
/**
*
* @author Clisprail
* @author Everel
*
*/
public class Time {
@@ -4,7 +4,7 @@ package org.parabot.environment.api.utils;
*
* A simple timer class
*
* @author Clisprail, Parameter
* @author Everel, Parameter
*
*/
public class Timer
@@ -1,6 +1,9 @@
package org.parabot.environment.api.utils;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -8,24 +11,29 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.parabot.core.io.ProgressListener;
import org.parabot.core.io.SizeInputStream;
/**
*
* @author Clisprail
*
* @author Everel
*
*/
public class WebUtil {
private static String agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.112 Safari/535.1";
/**
* Agent to set at a URL connection
*
* @param userAgent
*/
public static void setUserAgent(final String userAgent) {
agent = userAgent;
}
/**
* Gets useragent
*
* @return useragent
*/
public static String getUserAgent() {
@@ -34,6 +42,7 @@ public class WebUtil {
/**
* Fetches content of a page
*
* @param location
* @return contents of page
* @throws MalformedURLException
@@ -45,12 +54,23 @@ public class WebUtil {
/**
* Get contents from URL
*
* @param url
* @return page contents
*/
public static String getContents(final URL url) {
return getContents(getConnection(url));
}
/**
* Gets contents from URLConnection
*
* @param urlConnection
* @return page contents
*/
public static String getContents(URLConnection urlConnection) {
try {
final BufferedReader in = getReader(url);
final BufferedReader in = getReader(urlConnection);
final StringBuilder builder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
@@ -64,22 +84,44 @@ public class WebUtil {
return null;
}
/**
* Gets buffered reader from string url
*
* @param url
* @return bufferedreader
*/
public static BufferedReader getReader(final String url) {
try {
return getReader(new URL(url));
} catch (MalformedURLException e) {
e.printStackTrace();
}
return null;
}
/**
* Gets BufferedReader from URL
*
* @param url
* @return BufferedReader from URL
*/
public static BufferedReader getReader(final URL url) {
return getReader(getConnection(url));
}
public static BufferedReader getReader(final URLConnection urlConnection) {
try {
return new BufferedReader(new InputStreamReader(getInputStream(url)));
return new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
/**
* Gets inputstream from url
*
* @param url
* @return inputstream from url
*/
@@ -92,9 +134,10 @@ public class WebUtil {
}
return null;
}
/**
* Opens a connection
*
* @param url
* @return URLConnection to URL
*/
@@ -109,4 +152,36 @@ public class WebUtil {
return null;
}
/**
* Downloads a file on the internet
* @param url
* @param destination
* @param listener
*/
public static void downloadFile(final URL url, final File destination,
final ProgressListener listener) {
try {
final URLConnection connection = getConnection(url);
int size = connection.getContentLength();
SizeInputStream sizeInputStream = new SizeInputStream(
connection.getInputStream(), size, listener);
BufferedInputStream in = new BufferedInputStream(sizeInputStream);
FileOutputStream fileOut = new FileOutputStream(destination);
try {
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fileOut.write(data, 0, count);
}
} finally {
if (in != null)
in.close();
if (fileOut != null)
fileOut.close();
}
} catch (Throwable t) {
t.printStackTrace();
}
}
}
@@ -10,7 +10,7 @@ import org.parabot.core.Context;
/**
*
* @author Clisprail, Matt, Dane
* @author Everel, Matt, Dane
*
*/
public class Keyboard implements KeyListener {
@@ -11,7 +11,7 @@ import org.parabot.environment.api.utils.Time;
/**
*
* @author Clisprail
* @author Everel
*
*/
public class Mouse implements MouseListener, MouseMotionListener {
@@ -6,6 +6,11 @@ import org.parabot.environment.scripts.framework.AbstractFramework;
import org.parabot.environment.scripts.framework.LoopTask;
import org.parabot.environment.scripts.framework.Strategy;
/**
*
* @author Everel
*
*/
public class Frameworks {
public static Looper getLooper(LoopTask loopTask) {
@@ -0,0 +1,20 @@
package org.parabot.environment.scripts;
/**
*
* @author Everel
*
*/
public class LocalScriptExecuter extends ScriptExecuter {
private Script script = null;
public LocalScriptExecuter(final Script script) {
this.script = script;
}
@Override
public void run(ThreadGroup tg) {
super.finalize(tg, this.script);
}
}
@@ -0,0 +1,94 @@
package org.parabot.environment.scripts;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JOptionPane;
import org.parabot.core.Configuration;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.forum.AccountManager;
import org.parabot.core.forum.AccountManagerAccess;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.scripts.loader.JavaScriptLoader;
/**
*
* @author Everel
*
*/
public class SDNScriptExecuter extends ScriptExecuter {
private int id = -1;
private static AccountManager manager = null;
public static final AccountManagerAccess MANAGER_FETCHER = new AccountManagerAccess() {
@Override
public final void setManager(AccountManager manager) {
SDNScriptExecuter.manager = manager;
}
};
public SDNScriptExecuter(final int id) {
this.id = id;
}
@Override
public void run(ThreadGroup tg) {
try {
final URLConnection urlConnection = WebUtil.getConnection(new URL(String.format(Configuration.GET_SDN_SCRIPT, manager.getAccount().getUsername(), manager.getAccount().getPassword(), this.id)));
final String contentType = urlConnection.getHeaderField("Content-type");
if(contentType.equals("text/html")) {
// failed to fetch script
UILog.log("Error", new StringBuilder("Failed to load SDN script, error: [Page returned: ").append(WebUtil.getContents(urlConnection)).append("]").toString(), JOptionPane.ERROR_MESSAGE);
} else if(contentType.equals("application/jar")) {
//// JAR LOADING PART ////////
// succesfull request, jar returned
final ClassPath classPath = new ClassPath();
classPath.addJar(urlConnection);
final JavaScriptLoader loader = new JavaScriptLoader(classPath);
final String[] scriptClasses = loader.getScriptClassNames();
if(scriptClasses == null || scriptClasses.length == 0) {
UILog.log("Error", "Failed to load SDN script, error: [No script found in jar file.]", JOptionPane.ERROR_MESSAGE);
return;
} else if(scriptClasses.length > 1) {
UILog.log("Error", "Failed to load SDN script, error: [Multiple scripts found in jar file.]");
return;
}
final String className = scriptClasses[0];
try {
final Class<?> scriptClass = loader.loadClass(className);
final Constructor<?> con = scriptClass.getConstructor();
final Script script = (Script) con.newInstance();
super.finalize(tg, script);
} catch (NoClassDefFoundError ignored) {
UILog.log("Error", "Failed to load SDN script, error: [This server provider does not support this script]", JOptionPane.ERROR_MESSAGE);
} catch(ClassNotFoundException ignored) {
UILog.log("Error", "Failed to load SDN script, error: [This server provider does not support this script]", JOptionPane.ERROR_MESSAGE);
} catch (Throwable t) {
t.printStackTrace();
UILog.log("Error", "Failed to load SDN script, post the stacktrace/error on the parabot forums.", JOptionPane.ERROR_MESSAGE);
}
//// END JAR LOADING ////
} else {
UILog.log("Error", new StringBuilder("Failed to load SDN script, error: [Unknown content type: ").append(contentType).append("]").toString(), JOptionPane.ERROR_MESSAGE);
}
} catch (Throwable t) {
t.printStackTrace();
UILog.log("Error", "Failed to load SDN script, post the stacktrace/error on the parabot forums.", JOptionPane.ERROR_MESSAGE);
}
}
}
@@ -3,7 +3,9 @@ package org.parabot.environment.scripts;
import java.util.Collection;
import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.ui.components.BotToolbar;
import org.parabot.core.ui.components.LogArea;
import org.parabot.environment.api.utils.Time;
import org.parabot.environment.scripts.framework.AbstractFramework;
import org.parabot.environment.scripts.framework.LoopTask;
@@ -12,7 +14,7 @@ import org.parabot.environment.scripts.framework.Strategy;
/**
*
* @author Clisprail
* @author Everel
*
*/
public class Script implements Runnable {
@@ -29,7 +31,7 @@ public class Script implements Runnable {
public static final int STATE_PAUSE = 1;
public static final int STATE_STOPPED = 2;
public boolean onExecute() {
public boolean onExecute() {
return true;
}
@@ -58,24 +60,35 @@ public class Script implements Runnable {
@Override
public final void run() {
Core.verbose("Initializing script...");
Context.resolve().getServerProvider().initScript(this);
Core.verbose("Done.");
if(!onExecute()) {
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
Context.resolve().getServerProvider().unloadScript(this);
this.state = STATE_STOPPED;
Core.verbose("Done.");
return;
}
Core.verbose("Detecting script framework...");
Context.resolve().setRunningScript(this);
BotToolbar.getInstance().toggleRun();
if(this instanceof LoopTask) {
Core.verbose("Script framework detected: LoopTask");
frameWorkType = TYPE_LOOP;
frameWork = Frameworks.getLooper((LoopTask) this);
} else if(strategies != null && !strategies.isEmpty()) {
Core.verbose("Script framework detected: Strategies");
frameWorkType = TYPE_STRATEGY;
frameWork = Frameworks.getStrategyWorker(strategies);
} else {
Core.verbose("Unknown script framework: Other");
frameWorkType = TYPE_OTHER;
}
Core.verbose("Running script...");
LogArea.log("Script started.");
try {
while(this.state != STATE_STOPPED) {
if(this.state == STATE_PAUSE) {
@@ -89,12 +102,14 @@ public class Script implements Runnable {
} catch (Throwable t) {
t.printStackTrace();
}
Core.verbose("Script stopped/finished, unloading and stopping...");
onFinish();
LogArea.log("Script stopped.");
Context.resolve().getServerProvider().unloadScript(this);
this.state = STATE_STOPPED;
Context.resolve().setRunningScript(null);
BotToolbar.getInstance().toggleRun();
Core.verbose("Done.");
}
/**
@@ -0,0 +1,12 @@
package org.parabot.environment.scripts;
public abstract class ScriptExecuter {
public abstract void run(final ThreadGroup tg);
public final void finalize(final ThreadGroup tg, final Script script) {
new Thread(tg, script).start();
}
}
@@ -5,7 +5,7 @@ import java.lang.annotation.RetentionPolicy;
/**
* A script manifest
* @author Clisprail
* @author Everel
*
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -22,5 +22,9 @@ public @interface ScriptManifest {
String description();
String[] servers();
boolean vip() default false;
boolean premium() default false;
}
@@ -0,0 +1,9 @@
package org.parabot.environment.scripts.framework;
import org.parabot.environment.scripts.Script;
public abstract class PythonScript extends Script {
public abstract int loop();
}
@@ -12,14 +12,14 @@ import org.parabot.environment.scripts.Script;
*
* An environment to load a server
*
* @author Clisprail
* @author Everel
*
*
*/
public class ScriptLoader extends ASMClassLoader {
public class JavaScriptLoader extends ASMClassLoader {
private ClassPath classPath = null;
public ScriptLoader(ClassPath classPath) {
public JavaScriptLoader(ClassPath classPath) {
super(classPath);
this.classPath = classPath;
}
@@ -0,0 +1,49 @@
package org.parabot.environment.servers;
import java.net.MalformedURLException;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.build.BuildPath;
import org.parabot.core.classpath.ClassPath;
/**
*
* Loads locally stored server providers
*
* @author Everel
*
*/
public class LocalServerExecuter extends ServerExecuter {
private final ServerProvider serverProvider;
private ClassPath classPath = null;
private String serverName = null;
public LocalServerExecuter(ServerProvider serverProvider,
ClassPath classPath, final String serverName) {
this.serverProvider = serverProvider;
this.classPath = classPath;
this.serverName = serverName;
}
@Override
public void run(ThreadGroup tg) {
// add jar or directory to buildpath.
if (this.classPath.isJar()) {
Core.verbose("Adding server provider jar to buildpath: "
+ this.classPath.lastParsed.toString());
this.classPath.addToBuildPath();
} else {
Core.verbose("Adding server providers directory to buildpath: "
+ Directories.getServerPath().getPath());
try {
BuildPath.add(Directories.getServerPath().toURI().toURL());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
// finalize
super.finalize(tg, this.serverProvider, this.serverName);
}
}
@@ -0,0 +1,105 @@
package org.parabot.environment.servers;
import java.io.File;
import java.lang.reflect.Constructor;
import java.net.URL;
import java.net.URLEncoder;
import javax.swing.JOptionPane;
import org.parabot.core.Configuration;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.build.BuildPath;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.ui.components.VerboseLoader;
import org.parabot.core.ui.utils.UILog;
import org.parabot.environment.api.utils.WebUtil;
import org.parabot.environment.servers.loader.ServerLoader;
/**
*
* @author Everel
*
*/
public class PublicServerExecuter extends ServerExecuter {
private String serverName = null;
private String jarName = null;
public PublicServerExecuter(final String serverName, final String jarName) {
this.serverName = serverName;
this.jarName = jarName;
}
@Override
public void run(ThreadGroup tg) {
try {
final File destination = new File(Directories.getCachePath(),
this.jarName);
final String jarUrl = String.format(
Configuration.GET_SERVER_PROVIDER,
URLEncoder.encode(this.jarName, "UTF-8"));
Core.verbose("Downloading: " + jarUrl + " ...");
WebUtil.downloadFile(new URL(jarUrl), destination,
VerboseLoader.get());
Core.verbose("Server provider downloaded...");
final ClassPath classPath = new ClassPath();
classPath.addJar(destination);
BuildPath.add(destination.toURI().toURL());
ServerLoader serverLoader = new ServerLoader(classPath);
final String[] classNames = serverLoader.getServerClassNames();
if (classNames == null || classNames.length == 0) {
UILog.log(
"Error",
"Failed to load server provider, error: [No provider found in jar file.]",
JOptionPane.ERROR_MESSAGE);
return;
} else if (classNames.length > 1) {
UILog.log(
"Error",
"Failed to load server provider, error: [Multiple providers found in jar file.]");
return;
}
final String className = classNames[0];
try {
final Class<?> providerClass = serverLoader
.loadClass(className);
final Constructor<?> con = providerClass.getConstructor();
final ServerProvider serverProvider = (ServerProvider) con
.newInstance();
super.finalize(tg, serverProvider, this.serverName);
} catch (NoClassDefFoundError ignored) {
UILog.log(
"Error",
"Failed to load server provider, error: [This server provider is not compitable with this version of parabot]",
JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException ignored) {
UILog.log(
"Error",
"Failed to load server provider, error: [This server provider is not compitable with this version of parabot]",
JOptionPane.ERROR_MESSAGE);
} catch (Throwable t) {
t.printStackTrace();
UILog.log(
"Error",
"Failed to load server provider, post the stacktrace/error on the parabot forums.",
JOptionPane.ERROR_MESSAGE);
}
} catch (Exception e) {
e.printStackTrace();
UILog.log(
"Error",
"Failed to load server provider, post the stacktrace/error on the parabot forums.",
JOptionPane.ERROR_MESSAGE);
}
}
}
@@ -0,0 +1,31 @@
package org.parabot.environment.servers;
import org.parabot.core.Context;
import org.parabot.core.ui.components.BotToolbar;
/**
*
* @author Everel
*
*/
public abstract class ServerExecuter {
public abstract void run(final ThreadGroup tg);
public void finalize(final ThreadGroup tg, final ServerProvider provider, final String serverName) {
// loads the server and its it to the gui
new Thread(tg, new Runnable() {
@Override
public void run() {
try {
final Context context = new Context(provider);
BotToolbar.getInstance().addTab(context, serverName);
context.load();
} catch (Throwable t) {
t.printStackTrace();
}
}
}).start();
}
}
@@ -5,7 +5,7 @@ import java.lang.annotation.RetentionPolicy;
/**
* A server manifest
* @author Clisprail
* @author Everel
*
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -16,7 +16,7 @@ import org.parabot.environment.scripts.Script;
/**
* Provides a server to the bot
*
* @author Clisprail
* @author Everel
*
*/
public abstract class ServerProvider implements Opcodes {
@@ -4,7 +4,7 @@ package org.parabot.environment.servers;
*
* Bot type
*
* @author Clisprail
* @author Everel
*
*/
public enum Type {
@@ -12,7 +12,7 @@ import org.parabot.environment.servers.ServerProvider;
*
* An environment to load a server
*
* @author Clisprail
* @author Everel
*
*
*/