Updated whole environment

This commit is contained in:
Clisprail
2013-04-06 21:02:29 +02:00
parent d5409d4138
commit e682387dfd
11 changed files with 244 additions and 62 deletions
+3
View File
@@ -0,0 +1,3 @@
/id_rsa
/id_rsa.pub
/known_hosts
+3
View File
@@ -0,0 +1,3 @@
/id_rsa
/id_rsa.pub
/known_hosts
+4
View File
@@ -2,6 +2,8 @@ package org.parabot;
import javax.swing.UIManager; import javax.swing.UIManager;
import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.ui.ServerSelector; import org.parabot.core.ui.ServerSelector;
/** /**
@@ -19,6 +21,8 @@ public class Landing {
} catch (Throwable t) { } catch (Throwable t) {
t.printStackTrace(); t.printStackTrace();
} }
Directories.validate();
Core.enableDevMode();
ServerSelector.getInstance().setVisible(true); ServerSelector.getInstance().setVisible(true);
} }
} }
@@ -0,0 +1,97 @@
package org.parabot.core;
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
import org.parabot.environment.OperatingSystem;
/**
*
* @author Clisprail
* @author Matt
*
*/
public class Directories {
/**
* Gets default user directory
* @return default user director
*/
public static File getDefaultDirectory() {
switch (OperatingSystem.getOS()) {
case WINDOWS:
JFileChooser fr = new JFileChooser();
FileSystemView fw = fr.getFileSystemView();
return fw.getDefaultDirectory();
default:
return new File(System.getProperty("user.home"));
}
}
/**
* Gets bot workspace
* @return workspace of bot
*/
public static File getWorkspace() {
return new File(getDefaultDirectory(), "/Parabot/");
}
/**
* Get script sources path
* @return script sources path
*/
public static File getScriptSourcesPath() {
return new File(getDefaultDirectory(), "/Parabot/scripts/sources/");
}
/**
* Get script compiled path
* @return script compiled path
*/
public static File getScriptCompiledPath() {
return new File(getDefaultDirectory(), "/Parabot/scripts/compiled/");
}
/**
* Gets settings directory
* @return settings directory
*/
public static File getSettingsPath() {
return new File(getDefaultDirectory(), "/Parabot/settings/");
}
/**
* Gets servers directory
* @return servers directory
*/
public static File getServerPath() {
return new File(getDefaultDirectory(), "/Parabot/servers/");
}
/**
* Validates all directories and makes them if necessary
*/
public static void validate() {
final File defaultPath = getDefaultDirectory();
if(defaultPath == null || !defaultPath.exists()) {
throw new RuntimeException("Default path not found");
}
final Queue<File> files = new LinkedList<File>();
files.add(getWorkspace());
files.add(getServerPath());
files.add(getSettingsPath());
files.add(getScriptSourcesPath());
files.add(getScriptCompiledPath());
while(files.size() > 0) {
final File file = files.poll();
if(!file.exists()) {
file.mkdirs();
}
}
}
}
@@ -2,5 +2,6 @@ package org.parabot.core;
public class WebConstants { public class WebConstants {
public static final String HOME = "http://parnassian.host56.com/"; public static final String HOME = "http://parnassian.host56.com/";
public static final String SERVER_MANIFEST = HOME + "servers/manifest.dat";
} }
@@ -1,5 +1,16 @@
package org.parabot.core.desc; package org.parabot.core.desc;
public class ServerDescription { public class ServerDescription {
public String serverName = null;
public String author = null;
public int revision = 0;
public int providerIndex = -1;
public ServerDescription(final String serverName, final String author, final int revision, final int providerIndex) {
this.serverName = serverName;
this.author = author;
this.revision = revision;
this.providerIndex = providerIndex;
}
} }
@@ -1,9 +1,18 @@
package org.parabot.core.parsers; package org.parabot.core.parsers;
import java.net.URL; import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.parabot.core.Core; import org.parabot.core.Core;
import org.parabot.core.Directories;
import org.parabot.core.classpath.ClassPath;
import org.parabot.core.desc.ServerDescription; import org.parabot.core.desc.ServerDescription;
import org.parabot.environment.servers.ServerManifest;
import org.parabot.environment.servers.ServerProvider;
import org.parabot.environment.servers.loader.ServerLoader;
/** /**
* *
@@ -11,11 +20,7 @@ import org.parabot.core.desc.ServerDescription;
* *
*/ */
public class ServerManifestParser { public class ServerManifestParser {
private URL url = null; public static Map<ServerDescription, ServerCache> cache = new HashMap<ServerDescription, ServerCache>();
public ServerManifestParser(final URL url) {
this.url = url;
}
/** /**
* Gets server descriptions * Gets server descriptions
@@ -33,8 +38,53 @@ public class ServerManifestParser {
} }
private ServerDescription[] localDesc() { private ServerDescription[] localDesc() {
return null; final ClassPath path = new ClassPath();
path.loadClasses(Directories.getServerPath(), null);
final ServerLoader loader = new ServerLoader(path);
final List<ServerProvider> providers = new ArrayList<ServerProvider>();
final List<ServerDescription> descs = new ArrayList<ServerDescription>();
for(final String className : loader.getServerClassNames()) {
try {
final Class<?> serverProviderClass = loader.loadClass(className);
final Object annotation = serverProviderClass.getAnnotation(ServerManifest.class);
if(annotation == null) {
throw new RuntimeException("Missing manifest at " + className);
}
final ServerManifest manifest = (ServerManifest) annotation;
final Constructor<?> con = serverProviderClass.getConstructor();
final ServerProvider server = (ServerProvider) con.newInstance();
providers.add(server);
descs.add(new ServerDescription(manifest.name(), manifest.author(), 0, providers.size() - 1));
} catch (Throwable t) {
t.printStackTrace();
}
}
if(providers.isEmpty()) {
return null;
}
final ServerCache cachedServer = new ServerCache(loader, providers.toArray(new ServerProvider[providers.size()]));
for(final ServerDescription desc : descs) {
cache.put(desc, cachedServer);
}
return descs.toArray(new ServerDescription[descs.size()]);
} }
public class ServerCache {
private ServerLoader serverLoader = null;
private ServerProvider[] serverProviders = null;
private ServerCache(final ServerLoader serverLoader, final ServerProvider[] serverProviders) {
this.serverLoader = serverLoader;
this.serverProviders = serverProviders;
}
public ServerLoader getLoader() {
return serverLoader;
}
public ServerProvider[] getProviders() {
return serverProviders;
}
}
} }
@@ -1,42 +1,44 @@
package org.parabot.core.ui; package org.parabot.core.ui;
import java.awt.Dimension; import java.awt.Dimension;
import java.util.ArrayList; import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import org.parabot.core.desc.ServerDescription;
import org.parabot.core.parsers.ServerManifestParser;
import org.parabot.core.ui.utils.Center; import org.parabot.core.ui.utils.Center;
import org.parabot.core.ui.widgets.ServerWidget; import org.parabot.core.ui.widgets.ServerWidget;
public class ServerSelector extends JFrame {
public class ServerSelector extends JFrame
{
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static ServerSelector instance = null; private static ServerSelector instance = null;
public static ServerSelector getInstance() { public static ServerSelector getInstance() {
if(instance != null) { if (instance != null) {
instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); instance.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
return instance; return instance;
} }
return instance = new ServerSelector(); return instance = new ServerSelector();
} }
public ServerSelector() { public ServerSelector() {
setLayout(null); setLayout(null);
ServerWidget[] widgets = getServers(); Queue<ServerWidget> widgets = getServers();
JPanel p = new JPanel(); JPanel p = new JPanel();
p.setBounds(0, 0, 400, 800); p.setBounds(0, 0, 400, 800);
p.setLayout(null); p.setLayout(null);
p.setPreferredSize(new Dimension(400, widgets.length * 100)); p.setPreferredSize(new Dimension(400, widgets.size() * 100));
JScrollPane pane = new JScrollPane(p); JScrollPane pane = new JScrollPane(p);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
for (int i = 0; i < widgets.length; i++) { final int count = widgets.size() - 1;
widgets[i].setBounds(0, i * 100, 400, 100); while (widgets.size() > 0) {
p.add(widgets[i]); final ServerWidget widget = widgets.poll();
widget.setBounds(0, (count - widgets.size()) * 100, 400, 100);
p.add(widget);
} }
pane.setBounds(0, 0, 400, 200); pane.setBounds(0, 0, 400, 200);
pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
@@ -47,12 +49,13 @@ public class ServerSelector extends JFrame
Center.centerFramea(this, 406, 228); Center.centerFramea(this, 406, 228);
} }
public ServerWidget[] getServers() { public Queue<ServerWidget> getServers() {
ArrayList<ServerWidget> widgets = new ArrayList<ServerWidget>(); final Queue<ServerWidget> widgets = new LinkedList<ServerWidget>();
widgets.add(new ServerWidget("RecklessPk", "Clisprail", "317")); for (ServerDescription desc : new ServerManifestParser()
widgets.add(new ServerWidget("Soulsplit", "Clisprail", "317")); .getDescriptions()) {
return widgets.toArray(new ServerWidget[widgets.size()]); widgets.add(new ServerWidget(desc));
}
return widgets;
} }
} }
@@ -7,13 +7,11 @@ import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import org.parabot.core.WebConstants; import org.parabot.core.desc.ServerDescription;
import org.parabot.environment.Environment; import org.parabot.environment.Environment;
/** /**
@@ -25,27 +23,23 @@ public class ServerWidget extends JPanel {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String name = null; private String name = null;
public ServerDescription desc = null;
final ActionListener play = new ActionListener() { final ActionListener play = new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
try { load(desc, name);
load(new URL(WebConstants.HOME + name
+ ".jar"), name);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
} }
}; };
public ServerWidget(final String serverName, final String author, public ServerWidget(final ServerDescription desc) {
final String revision) { this.desc = desc;
setLayout(null); setLayout(null);
this.name = serverName.replaceAll(" ", ""); this.name = desc.serverName.replaceAll(" ", "");
JLabel l = new JLabel(); JLabel l = new JLabel();
l.setFont(new Font("Arial", Font.BOLD, 16)); l.setFont(new Font("Arial", Font.BOLD, 16));
l.setForeground(Color.white); l.setForeground(Color.white);
l.setText(serverName); l.setText(desc.serverName);
l.setBounds(10, 10, 100, 20); l.setBounds(10, 10, 100, 20);
add(l); add(l);
final Font f = new Font("Arial", Font.PLAIN, 12); final Font f = new Font("Arial", Font.PLAIN, 12);
@@ -54,13 +48,13 @@ public class ServerWidget extends JPanel {
l.setFont(f); l.setFont(f);
l.setForeground(Color.white); l.setForeground(Color.white);
l.setBounds(10, 45, 100, 20); l.setBounds(10, 45, 100, 20);
l.setText("Author: " + author); l.setText("Author: " + desc.author);
add(l); add(l);
l = new JLabel(); l = new JLabel();
l.setFont(f); l.setFont(f);
l.setForeground(Color.white); l.setForeground(Color.white);
l.setBounds(10, 60, 100, 20); l.setBounds(10, 60, 100, 20);
l.setText("Revision: " + revision); l.setText("Revision: " + desc.revision);
add(l); add(l);
final JButton b = new JButton("Start"); final JButton b = new JButton("Start");
b.setFocusable(false); b.setFocusable(false);
@@ -90,7 +84,7 @@ public class ServerWidget extends JPanel {
g2d.fillRect(0, 0, w, h); g2d.fillRect(0, 0, w, h);
} }
public void load(final URL url, final String serverName) { public void load(final ServerDescription desc, final String serverName) {
Environment.load(url, serverName); Environment.load(desc, serverName);
} }
} }
@@ -1,10 +1,12 @@
package org.parabot.environment; package org.parabot.environment;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.net.URL;
import org.parabot.core.Context; import org.parabot.core.Context;
import org.parabot.core.Core;
import org.parabot.core.classpath.ClassPath; 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.BotUI;
import org.parabot.core.ui.ServerSelector; import org.parabot.core.ui.ServerSelector;
import org.parabot.core.ui.components.BotToolbar; import org.parabot.core.ui.components.BotToolbar;
@@ -22,38 +24,53 @@ public class Environment {
* Loads a new environment * Loads a new environment
* @param url * @param url
*/ */
public static void load(final URL url, final String serverName) { public static void load(final ServerDescription desc, final String serverName) {
ServerSelector.getInstance().dispose(); ServerSelector.getInstance().dispose();
if(!BotUI.getInstance().isVisible()) { if(!BotUI.getInstance().isVisible()) {
BotUI.getInstance().setVisible(true); BotUI.getInstance().setVisible(true);
} }
final ClassPath classPath = new ClassPath(); final ClassPath classPath = Core.isDevMode() ? null : new ClassPath();
classPath.addJar(url.toString()); final ServerCache cache = Core.isDevMode() ? ServerManifestParser.cache.get(desc) : null;
final ServerLoader serverLoader = new ServerLoader(classPath); final ServerLoader serverLoader = Core.isDevMode() ? cache.getLoader() : new ServerLoader(classPath);
final String[] serverProviders = serverLoader.getServerClassNames(); String[] serverProviders = null;
if (serverProviders == null) { if(!Core.isDevMode()) {
throw new RuntimeException("No server provided."); serverProviders = serverLoader.getServerClassNames();
if (serverProviders == null) {
throw new RuntimeException("No server provided.");
}
} }
final String id = "tab" + Context.getID(); final String id = "tab" + Context.getID();
final ThreadGroup bot = new ThreadGroup(id); final ThreadGroup bot = new ThreadGroup(id);
new Thread(bot, new Runnable() { new Thread(bot, new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
final Class<?> serverProviderClass = serverLoader.loadClass(serverProviders[0]); final ServerProvider server = !Core.isDevMode() ? fetchServerProvider(serverLoader) : cache.getProviders()[desc.providerIndex];
final Constructor<?> con = serverProviderClass.getConstructor(); final Context context = new Context(server);
ServerProvider server = (ServerProvider) con.newInstance(); context.setEnvironment(serverLoader);
server.context.setEnvironment(serverLoader); BotToolbar.getInstance().addTab(context, serverName);
BotToolbar.getInstance().addTab(server.context, serverName); context.load();
server.context.load();
} catch (Throwable t) { } catch (Throwable t) {
throw new RuntimeException("Error while loading server. " + t.getMessage()); t.printStackTrace();
} }
} }
}).start(); }).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());
}
}
} }
@@ -13,7 +13,6 @@ import org.parabot.core.Context;
* *
*/ */
public abstract class ServerProvider { public abstract class ServerProvider {
public Context context = new Context(this);
/** /**
* Hooks to parse * Hooks to parse
@@ -47,7 +46,7 @@ public abstract class ServerProvider {
} }
public void parseJar() { public void parseJar() {
context.getClassPath().addJar(getJar()); Context.resolve().getClassPath().addJar(getJar());
} }
} }