mirror of
https://github.com/2006-Scape/Parabot.git
synced 2026-07-03 16:49:10 +00:00
Get rid of threadgroups
This commit is contained in:
@@ -26,13 +26,13 @@ import java.util.TimerTask;
|
||||
public class Context {
|
||||
public static final HashMap<ThreadGroup, Context> threadGroups = new HashMap<ThreadGroup, Context>();
|
||||
private static ArrayList<Paintable> paintables = new ArrayList<Paintable>();
|
||||
private static int id = 1;
|
||||
|
||||
private static Context instance;
|
||||
|
||||
public boolean added;
|
||||
private ASMClassLoader classLoader;
|
||||
private ClassPath classPath;
|
||||
private ServerProvider serverProvider;
|
||||
private int tab;
|
||||
private Applet gameApplet;
|
||||
private HookParser hookParser;
|
||||
private Script runningScript;
|
||||
@@ -42,7 +42,7 @@ public class Context {
|
||||
private Mouse mouse;
|
||||
private Keyboard keyboard;
|
||||
|
||||
public Context(final ServerProvider serverProvider) {
|
||||
private Context(final ServerProvider serverProvider) {
|
||||
threadGroups.put(Thread.currentThread().getThreadGroup(), this);
|
||||
|
||||
this.serverProvider = serverProvider;
|
||||
@@ -50,23 +50,15 @@ public class Context {
|
||||
this.classPath = new ClassPath();
|
||||
this.classLoader = new ASMClassLoader(classPath);
|
||||
this.randomHandler = new RandomHandler();
|
||||
this.tab = id;
|
||||
|
||||
id++;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the context from threadgroup
|
||||
*
|
||||
* @return context
|
||||
*/
|
||||
public static Context resolve() {
|
||||
return threadGroups.get(Thread.currentThread().getThreadGroup());
|
||||
public static Context getInstance(ServerProvider serverProvider) {
|
||||
return instance == null ? instance = new Context(serverProvider) : instance;
|
||||
}
|
||||
|
||||
public static Context currentTab() {
|
||||
// TODO
|
||||
return threadGroups.values().iterator().next();
|
||||
|
||||
public static Context getInstance() {
|
||||
return getInstance(null);
|
||||
}
|
||||
|
||||
public void setEnvironment() {
|
||||
@@ -209,24 +201,6 @@ public class Context {
|
||||
return classLoader;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the id of this context
|
||||
*
|
||||
* @return id context
|
||||
*/
|
||||
public static int getID() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tab id of this context
|
||||
*
|
||||
* @return tab id of this context
|
||||
*/
|
||||
public int getTab() {
|
||||
return tab;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a paintable instance to the paintables
|
||||
*
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ASMUtils implements Opcodes {
|
||||
}
|
||||
|
||||
public static ClassNode getClass(String className) {
|
||||
Context context = Context.resolve();
|
||||
Context context = Context.getInstance();
|
||||
for (ClassNode node : context.getClassPath().classes.values()) {
|
||||
if (node.name.equals(className)) {
|
||||
return node;
|
||||
|
||||
@@ -43,7 +43,7 @@ public class PaintDebugger {
|
||||
}
|
||||
|
||||
public static final PaintDebugger getInstance() {
|
||||
return Context.resolve().getPaintDebugger();
|
||||
return Context.getInstance().getPaintDebugger();
|
||||
}
|
||||
|
||||
public final void addLine(final String debugLine) {
|
||||
|
||||
@@ -21,7 +21,7 @@ public class BotDialog extends JDialog {
|
||||
setLayout(new BorderLayout());
|
||||
setUndecorated(true);
|
||||
setBackground(new Color(0, 0, 0, 0));
|
||||
setContentPane(new PaintComponent(botUI.getSize()));
|
||||
setContentPane(PaintComponent.getInstance(botUI.getSize()));
|
||||
setPreferredSize(botUI.getSize());
|
||||
setSize(botUI.getSize());
|
||||
setVisible(true);
|
||||
|
||||
@@ -7,9 +7,6 @@ import java.awt.event.ActionListener;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseMotionAdapter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.Box;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JButton;
|
||||
@@ -31,7 +28,6 @@ import org.parabot.environment.scripts.Script;
|
||||
public class BotToolbar extends JToolBar {
|
||||
private static final long serialVersionUID = 5373484845104212180L;
|
||||
private static BotToolbar instance;
|
||||
private static Map<TabButton, Context> environments = new HashMap<TabButton, Context>();
|
||||
|
||||
private JButton tab;
|
||||
private final JButton run;
|
||||
@@ -110,7 +106,7 @@ public class BotToolbar extends JToolBar {
|
||||
}
|
||||
|
||||
private void setScriptState(int state) {
|
||||
Context.currentTab().getRunningScript().setState(state);
|
||||
Context.getInstance().getRunningScript().setState(state);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,26 +136,7 @@ public class BotToolbar extends JToolBar {
|
||||
public void addTab(final Context context, final String name) {
|
||||
TabButton b = new TabButton(name);
|
||||
b.setActive(true);
|
||||
b.addActionListener(new ActionListener() {
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
final TabButton tabButton = (TabButton) e.getSource();
|
||||
final Context context = environments.get(tabButton);
|
||||
for(final TabButton button : environments.keySet()) {
|
||||
button.setActive(false);
|
||||
}
|
||||
tabButton.setActive(true);
|
||||
if(!context.appletSet()) {
|
||||
return;
|
||||
}
|
||||
GamePanel.getInstance().setContext(context);
|
||||
}
|
||||
|
||||
});
|
||||
//add(b, getComponentIndex(tab));
|
||||
add(b, 0);
|
||||
environments.put(b, context);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package org.parabot.core.ui.components;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.AlphaComposite;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JComponent;
|
||||
import org.parabot.core.paint.PaintDebugger;
|
||||
import org.parabot.environment.api.utils.Time;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -13,20 +17,58 @@ import javax.swing.JPanel;
|
||||
* @author Everel
|
||||
*
|
||||
*/
|
||||
public class PaintComponent extends JPanel {
|
||||
public class PaintComponent extends JComponent implements Runnable {
|
||||
private static final long serialVersionUID = 4653612412080038193L;
|
||||
private static PaintComponent instance;
|
||||
|
||||
public PaintComponent(Dimension size) {
|
||||
setPreferredSize(size);
|
||||
setSize(size);
|
||||
private BufferedImage buffer;
|
||||
private Graphics2D g2;
|
||||
private Dimension dimensions;
|
||||
private PaintDebugger paintDebugger;
|
||||
|
||||
private PaintComponent(Dimension dimensions) {
|
||||
this.dimensions = dimensions;
|
||||
this.buffer = new BufferedImage(dimensions.width, dimensions.height, BufferedImage.TYPE_INT_ARGB);
|
||||
this.g2 = buffer.createGraphics();
|
||||
|
||||
setPreferredSize(dimensions);
|
||||
setSize(dimensions);
|
||||
setOpaque(false);
|
||||
setDoubleBuffered(true);
|
||||
}
|
||||
|
||||
public static PaintComponent getInstance(Dimension dimensions) {
|
||||
return instance == null ? instance = new PaintComponent(dimensions) : instance;
|
||||
}
|
||||
|
||||
public static PaintComponent getInstance() {
|
||||
return getInstance(null);
|
||||
}
|
||||
|
||||
public void startPainting(PaintDebugger paintDebugger) {
|
||||
this.paintDebugger = paintDebugger;
|
||||
new Thread(this).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g) {
|
||||
System.out.println("paint");
|
||||
g.setColor(Color.red);
|
||||
g.drawString("hi", 10, 10);
|
||||
super.paintComponent(g);
|
||||
g2.setComposite(AlphaComposite.Clear);
|
||||
g2.fillRect(0, 0, dimensions.width, dimensions.height);
|
||||
g2.setComposite(AlphaComposite.SrcOver);
|
||||
|
||||
if(paintDebugger != null) {
|
||||
paintDebugger.debug(g2);
|
||||
}
|
||||
g.drawImage(buffer, 0, 0, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while(true) {
|
||||
Time.sleep(15);
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package org.parabot.environment;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.Core;
|
||||
import org.parabot.core.desc.ServerDescription;
|
||||
import org.parabot.core.jython.Jython;
|
||||
@@ -35,10 +34,7 @@ public class Environment {
|
||||
|
||||
Core.verbose("Loading server: " + desc.toString());
|
||||
|
||||
final String id = "tab" + Context.getID();
|
||||
final ThreadGroup bot = new ThreadGroup(id);
|
||||
|
||||
ServerParser.SERVER_CACHE.get(desc).run(bot);
|
||||
ServerParser.SERVER_CACHE.get(desc).run();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public class Keyboard implements KeyListener {
|
||||
}
|
||||
|
||||
public static Keyboard getInstance() {
|
||||
return Context.resolve().getKeyboard();
|
||||
return Context.getInstance().getKeyboard();
|
||||
}
|
||||
|
||||
static {
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Mouse implements MouseListener, MouseMotionListener {
|
||||
}
|
||||
|
||||
public static Mouse getInstance() {
|
||||
return Context.resolve().getMouse();
|
||||
return Context.getInstance().getMouse();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -62,7 +62,7 @@ public class Script implements Runnable {
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
Context context = Context.resolve();
|
||||
Context context = Context.getInstance();
|
||||
|
||||
Core.verbose("Initializing script...");
|
||||
context.getServerProvider().initScript(this);
|
||||
@@ -70,7 +70,7 @@ public class Script implements Runnable {
|
||||
|
||||
if(!onExecute()) {
|
||||
Core.verbose("Script#onExecute returned false, unloading and stopping script...");
|
||||
Context.resolve().getServerProvider().unloadScript(this);
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
Core.verbose("Done.");
|
||||
return;
|
||||
@@ -113,9 +113,9 @@ public class Script implements Runnable {
|
||||
Core.verbose("Script stopped/finished, unloading and stopping...");
|
||||
onFinish();
|
||||
LogArea.log("Script stopped.");
|
||||
Context.resolve().getServerProvider().unloadScript(this);
|
||||
context.getServerProvider().unloadScript(this);
|
||||
this.state = STATE_STOPPED;
|
||||
Context.resolve().setRunningScript(null);
|
||||
context.setRunningScript(null);
|
||||
BotToolbar.getInstance().toggleRun();
|
||||
Core.verbose("Done.");
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class LocalServerExecuter extends ServerExecuter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ThreadGroup tg) {
|
||||
public void run() {
|
||||
// add jar or directory to buildpath.
|
||||
if (this.classPath.isJar()) {
|
||||
Core.verbose("Adding server provider jar to buildpath: "
|
||||
@@ -43,7 +43,7 @@ public class LocalServerExecuter extends ServerExecuter {
|
||||
}
|
||||
}
|
||||
// finalize
|
||||
super.finalize(tg, this.serverProvider, this.serverName);
|
||||
super.finalize(this.serverProvider, this.serverName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ public class PublicServerExecuter extends ServerExecuter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ThreadGroup tg) {
|
||||
public void run() {
|
||||
try {
|
||||
final File destination = new File(Directories.getCachePath(),
|
||||
this.serverID);
|
||||
@@ -86,7 +86,7 @@ public class PublicServerExecuter extends ServerExecuter {
|
||||
final Constructor<?> con = providerClass.getConstructor();
|
||||
final ServerProvider serverProvider = (ServerProvider) con
|
||||
.newInstance();
|
||||
super.finalize(tg, serverProvider, this.serverName);
|
||||
super.finalize(serverProvider, this.serverName);
|
||||
} catch (NoClassDefFoundError ignored) {
|
||||
UILog.log(
|
||||
"Error",
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.parabot.environment.servers;
|
||||
|
||||
import org.parabot.core.Context;
|
||||
import org.parabot.core.ui.components.BotToolbar;
|
||||
import org.parabot.core.ui.components.PaintComponent;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -12,17 +13,17 @@ import org.parabot.core.ui.components.BotToolbar;
|
||||
*/
|
||||
public abstract class ServerExecuter {
|
||||
|
||||
public abstract void run(final ThreadGroup tg);
|
||||
public abstract void run();
|
||||
|
||||
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() {
|
||||
public void finalize(final ServerProvider provider, final String serverName) {
|
||||
new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final Context context = new Context(provider);
|
||||
Context context = Context.getInstance(provider);
|
||||
BotToolbar.getInstance().addTab(context, serverName);
|
||||
context.load();
|
||||
PaintComponent.getInstance().startPainting(context.getPaintDebugger());
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@ public abstract class ServerProvider implements Opcodes {
|
||||
for (Injectable inj : injectables) {
|
||||
inj.inject();
|
||||
}
|
||||
Context.resolve().setHookParser(parser);
|
||||
Context.getInstance().setHookParser(parser);
|
||||
}
|
||||
|
||||
private HookFile fetchHookFile() {
|
||||
@@ -106,11 +106,11 @@ public abstract class ServerProvider implements Opcodes {
|
||||
}
|
||||
|
||||
public void setClientInstance(Object client) {
|
||||
Context.resolve().setClientInstance(client);
|
||||
Context.getInstance().setClientInstance(client);
|
||||
}
|
||||
|
||||
public void parseJar() {
|
||||
Context.resolve().getClassPath().addJar(getJar());
|
||||
Context.getInstance().getClassPath().addJar(getJar());
|
||||
}
|
||||
|
||||
public void initScript(Script script) {
|
||||
@@ -122,7 +122,7 @@ public abstract class ServerProvider implements Opcodes {
|
||||
}
|
||||
|
||||
public void initMouse() {
|
||||
final Context context = Context.resolve();
|
||||
final Context context = Context.getInstance();
|
||||
final Applet applet = context.getApplet();
|
||||
final Mouse mouse = new Mouse(applet);
|
||||
applet.addMouseListener(mouse);
|
||||
@@ -131,7 +131,7 @@ public abstract class ServerProvider implements Opcodes {
|
||||
}
|
||||
|
||||
public void initKeyboard() {
|
||||
final Context context = Context.resolve();
|
||||
final Context context = Context.getInstance();
|
||||
final Applet applet = context.getApplet();
|
||||
final Keyboard keyboard = new Keyboard(applet);
|
||||
applet.addKeyListener(keyboard);
|
||||
|
||||
Reference in New Issue
Block a user