First commit

This commit is contained in:
Clisprail
2013-04-03 20:21:15 +02:00
commit c230a353b1
44 changed files with 2104 additions and 0 deletions
@@ -0,0 +1,59 @@
package org.parabot.environment;
import java.lang.reflect.Constructor;
import java.net.URL;
import org.parabot.core.Context;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.ui.BotUI;
import org.parabot.core.ui.ServerSelector;
import org.parabot.core.ui.components.BotToolbar;
import org.parabot.environment.servers.ServerProvider;
import org.parabot.environment.servers.loader.ServerLoader;
/**
*
* @author Clisprail
*
*/
public class Environment {
/**
* Loads a new environment
* @param url
*/
public static void load(final URL url, final String serverName) {
ServerSelector.getInstance().dispose();
if(!BotUI.getInstance().isVisible()) {
BotUI.getInstance().setVisible(true);
}
final ClassPath classPath = new ClassPath();
classPath.addJar(url.toString());
final ServerLoader serverLoader = new ServerLoader(classPath);
final String[] 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 Class<?> serverProviderClass = serverLoader.loadClass(serverProviders[0]);
final Constructor<?> con = serverProviderClass.getConstructor();
ServerProvider server = (ServerProvider) con.newInstance();
server.context.setEnvironment(serverLoader);
BotToolbar.getInstance().addTab(server.context, serverName);
server.context.load();
} catch (Throwable t) {
throw new RuntimeException("Error while loading server. " + t.getMessage());
}
}
}).start();
}
}
@@ -0,0 +1,14 @@
package org.parabot.environment.api.utils;
/**
* A simple class to filter things
*
* @author Clisprail
*
* @param <F>
*/
public interface Filter<F>
{
public boolean accept(F f);
}
@@ -0,0 +1,22 @@
package org.parabot.environment.servers;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* A server manifest
* @author Clisprail
*
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface ServerManifest {
String author();
String name();
double version();
Type type();
}
@@ -0,0 +1,53 @@
package org.parabot.environment.servers;
import java.applet.Applet;
import java.applet.AppletStub;
import java.net.URL;
import javax.swing.JMenuBar;
import org.parabot.core.Context;
/**
* Provides a server to the bot
*
* @author Clisprail
*
*/
public abstract class ServerProvider {
public Context context = new Context(this);
/**
* Hooks to parse
* @return URL to hooks file
*/
public URL getHooks() {
return null;
}
/**
* Jar to parse
* @return URL to client jar
*/
public abstract String getJar();
public abstract Applet fetchApplet();
public String getAccessorsPackage() {
return null;
}
/**
* Add custom items to the bot menu bar
* @param menu bar to add items on
*/
public void addMenuItems(JMenuBar bar) {
}
public AppletStub getStub() {
return null;
}
public void parseJar() {
context.getClassPath().addJar(getJar());
}
}
@@ -0,0 +1,14 @@
package org.parabot.environment.servers;
/**
*
* Bot type
*
* @author Clisprail
*
*/
public enum Type {
INJECTION, REFLECTION, COLOR, OTHER
}
@@ -0,0 +1,40 @@
package org.parabot.environment.servers.loader;
import java.util.ArrayList;
import java.util.List;
import org.objectweb.asm.tree.ClassNode;
import org.parabot.core.asm.ASMClassLoader;
import org.parabot.core.classpath.ClassPath;
import org.parabot.environment.servers.ServerProvider;
/**
*
* @author Clisprail
*
*
*/
public class ServerLoader extends ASMClassLoader {
private ClassPath classPath = null;
public ServerLoader(ClassPath classPath) {
super(classPath);
this.classPath = classPath;
}
/**
* Gets all classes that extends ServerProvider
* @return string array of class names that extends ServerProvider
*/
public final String[] getServerClassNames() {
final List<String> classNames = new ArrayList<String>();
for (ClassNode c : classPath.classes.values())
if (c.superName.replace('/', '.').equals(
ServerProvider.class.getName())) {
classNames.add(c.name.replace('/', '.'));
}
return classNames.toArray(new String[classNames.size()]);
}
}