Skip to content
This repository was archived by the owner on Dec 7, 2019. It is now read-only.

Commit 5fb6e7f

Browse files
committed
Added agent tab.
1 parent 812bd26 commit 5fb6e7f

5 files changed

Lines changed: 117 additions & 22 deletions

File tree

Lang.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
// Statistics tab text
2525
300:Statistics
2626
301:Record some data for details to appear here.
27+
// Agent tab text
28+
500:Agent
29+
501:Attatch
2730
// Changelog
2831
401:Initial release
2932
402:Fixed issue where the statistics key-interceptor would never be destroyed.\n\nRefactored code and added documentation to a few more classes.

src/me/coley/clicker/agent/Agent.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.io.File;
44
import java.lang.instrument.Instrumentation;
55

6+
import javax.swing.DefaultListModel;
7+
import javax.swing.ListModel;
8+
69
import com.sun.tools.attach.VirtualMachine;
710
import com.sun.tools.attach.VirtualMachineDescriptor;
811

@@ -29,21 +32,21 @@ public static void agentmain(String agentArgs) {
2932
public static void premain(String agentArgs) {
3033
if (agentArgs.contains("dir:")) {
3134
String dir = agentArgs.substring(agentArgs.indexOf(":") + 1);
32-
MainGUI.main(new String[] { "dir", dir });
35+
MainGUI.main(new String[] { "dir:" + dir, "displayAgentTab:false" });
3336
} else {
34-
MainGUI.main(new String[] {});
37+
MainGUI.main(new String[] {"displayAgentTab:false"});
3538
}
3639
}
3740

3841
/**
39-
* Loads this agent into a given VM.
42+
* Loads this agent into a given VM.
4043
*
4144
* @param agentPath
4245
* The path to the agent jar
4346
*/
4447
public static void loadAgentToTarget(String target, String options) {
4548
// Oddly though using this only works half of the times.
46-
// Tried it 10 times command line.
49+
// Tried it 10 times command line.
4750
// Five times it injected into the target VM
4851
// Five times it ran as if no arguments were given at all
4952
// TODO: Figure out why this behaves oddly
@@ -76,7 +79,16 @@ private static void loadAgent(String agentPath, String vmID, String options) {
7679
* @return
7780
*/
7881
private static String getCurrentLocation() {
79-
// Wrapped in File.getAbsolutePath() because the path it returns it looks nicer.
82+
// Wrapped in File.getAbsolutePath() because the path it returns it
83+
// looks nicer.
8084
return new File(Agent.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath();
8185
}
86+
87+
public static ListModel<String> getJVMS() {
88+
DefaultListModel<String> model = new DefaultListModel<String> ();
89+
for (VirtualMachineDescriptor vm : VirtualMachine.list()) {
90+
model.addElement(vm.displayName());
91+
}
92+
return model;
93+
}
8294
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package me.coley.clicker.agent;
2+
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
import javax.swing.JList;
7+
8+
public class AttatchListener implements ActionListener {
9+
private final JList<String> list;
10+
11+
public AttatchListener(JList<String> list) {
12+
this.list = list;
13+
}
14+
15+
@Override
16+
public void actionPerformed(ActionEvent e) {
17+
int i = list.getSelectedIndex();
18+
if (i != -1) {
19+
String id = list.getModel().getElementAt(i);
20+
try {
21+
Agent.loadAgentToTarget(id, "dir:" + System.getProperty("settings.dir"));
22+
System.exit(0);
23+
} catch (Exception ex) {
24+
ex.printStackTrace();
25+
}
26+
}
27+
}
28+
29+
}

src/me/coley/clicker/ui/MainGUI.java

Lines changed: 65 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.awt.event.WindowEvent;
1010

1111
import javax.swing.BorderFactory;
12+
import javax.swing.JButton;
1213
import javax.swing.JComboBox;
1314
import javax.swing.JFrame;
15+
import javax.swing.JList;
1416
import javax.swing.JTabbedPane;
1517
import javax.swing.JTextArea;
1618
import javax.swing.UIManager;
@@ -27,6 +29,7 @@
2729
import me.coley.clicker.Stats;
2830
import me.coley.clicker.Values;
2931
import me.coley.clicker.agent.Agent;
32+
import me.coley.clicker.agent.AttatchListener;
3033
import me.coley.clicker.jna.KeyHandler;
3134
import me.coley.clicker.ui.controls.LabeledBindButton;
3235
import me.coley.clicker.ui.controls.LabeledCheckbox;
@@ -39,17 +42,19 @@
3942
import me.coley.simplejna.hook.key.KeyHook;
4043

4144
import java.awt.BorderLayout;
42-
import java.awt.Color;
4345
import java.awt.Dimension;
4446

4547
import javax.swing.JPanel;
4648
import javax.swing.JScrollPane;
4749

4850
import java.util.Random;
51+
import java.util.concurrent.atomic.AtomicBoolean;
4952
import java.util.logging.Level;
5053
import java.util.logging.Logger;
5154
import javax.swing.JSplitPane;
5255
import javax.swing.JTree;
56+
import javax.swing.ListSelectionModel;
57+
5358
import java.awt.FlowLayout;
5459

5560
public class MainGUI {
@@ -59,6 +64,7 @@ public class MainGUI {
5964
public final Clicker clicker = new Clicker(this);
6065
public final Stats stats = new Stats(this);
6166
private final KeyHandler handler = new KeyHandler(this);
67+
private final boolean isAttatched;
6268
public GraphingPanel graph;
6369
private JFrame frmClicker;
6470
private JTextArea txtStats;
@@ -71,18 +77,23 @@ public static void main(String[] args) {
7177
String curDir = System.getProperty("user.dir");
7278
System.setProperty("settings.dir", curDir);
7379
// Handle args if there are any
74-
if (args.length >= 2) {
75-
String arg1 = args[0];
76-
String arg2 = args[1];
77-
if (arg1.contains("agent")) {
78-
// Agent that runs the program in another VM.
79-
String target = arg2;
80-
Agent.loadAgentToTarget(target, "dir:" + curDir);
81-
return;
82-
} else if (arg1.contains("dir")) {
83-
// Adjusts the settings.dir property.
84-
String dir = arg2;
85-
System.setProperty("settings.dir", dir);
80+
AtomicBoolean displayAgent = new AtomicBoolean(true);
81+
if (args.length >= 0) {
82+
for (String arg : args) {
83+
if (arg.startsWith("agentTarget:")) {
84+
// Agent that runs the program in another VM.
85+
String target = arg.substring("agentTarget:".length());
86+
Agent.loadAgentToTarget(target, "dir:" + curDir);
87+
return;
88+
} else if (arg.contains("dir:")) {
89+
// Adjusts the settings.dir property.
90+
String dir = arg.substring("dir:".length());
91+
System.setProperty("settings.dir", dir);
92+
} else if (arg.contains("displayAgentTab:")) {
93+
//
94+
String val = arg.substring("displayAgentTab:".length());
95+
displayAgent.set(Boolean.parseBoolean(val));
96+
}
8697
}
8798
}
8899
try {
@@ -96,7 +107,7 @@ public static void main(String[] args) {
96107
EventQueue.invokeLater(new Runnable() {
97108
public void run() {
98109
try {
99-
MainGUI gui = new MainGUI();
110+
MainGUI gui = new MainGUI(!displayAgent.get());
100111
gui.initSettings();
101112
gui.initGui();
102113
gui.loadSettings();
@@ -107,6 +118,10 @@ public void run() {
107118
});
108119
}
109120

121+
public MainGUI(boolean isAttatched) {
122+
this.isAttatched = isAttatched;
123+
}
124+
110125
/**
111126
* Initialize the contents of the frame.
112127
*/
@@ -118,13 +133,24 @@ private void initGui() {
118133
frmClicker.setBounds(100, 100, 422, 398);
119134
frmClicker.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
120135
frmClicker.addWindowListener(new WindowAdapter() {
121-
122136
@Override
123137
public void windowClosing(WindowEvent e) {
124138
log.log(Level.INFO, "Saving settings...");
125139
settings.save();
126140
keybinds.save();
127-
System.exit(0);
141+
if (!isAttatched) {
142+
// Not attached, just suicide the program
143+
System.exit(0);
144+
} else {
145+
// Attached, exiting would kill the parent program.
146+
// Carefully disable / dispose instead.
147+
if (clicker.getStatus())
148+
clicker.toggle();
149+
if (stats.getStatus())
150+
stats.toggle();
151+
KeyHook.unhook(handler);
152+
frmClicker.dispose();
153+
}
128154
}
129155
});
130156
JTabbedPane tabs = new JTabbedPane(JTabbedPane.TOP);
@@ -194,7 +220,6 @@ public void itemStateChanged(ItemEvent e) {
194220
tabSettings.add(combo);
195221

196222
}
197-
198223
JPanel tabStatistics = new JPanel();
199224
tabs.addTab(Lang.get(Lang.STATISTICS_TITLE), null, tabStatistics, null);
200225
tabStatistics.setLayout(new BorderLayout());
@@ -214,6 +239,29 @@ public void itemStateChanged(ItemEvent e) {
214239
sp.setDividerLocation(frmClicker.getHeight() / 2);
215240
tabStatistics.add(sp, BorderLayout.CENTER);
216241
}
242+
if (!isAttatched) {
243+
JPanel tabAgent = new JPanel();
244+
tabs.addTab(Lang.get(Lang.AGENT_TITLE), null, tabAgent, null);
245+
tabAgent.setLayout(new BorderLayout());
246+
JButton btnAction = new JButton(Lang.get(Lang.AGENT_ATTATCH_VM));
247+
JList<String> list = new JList<String>();
248+
list.setModel(Agent.getJVMS());
249+
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
250+
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
251+
btnAction.addActionListener(new AttatchListener(list));
252+
JScrollPane listScroller = new JScrollPane(list);
253+
tabAgent.add(listScroller, BorderLayout.CENTER);
254+
tabAgent.add(btnAction, BorderLayout.NORTH);
255+
/*
256+
* list = new JList(data); //data has type Object[]
257+
* list.setSelectionMode(ListSelectionModel.
258+
* SINGLE_INTERVAL_SELECTION);
259+
* list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
260+
* list.setVisibleRowCount(-1); ... JScrollPane listScroller = new
261+
* JScrollPane(list); listScroller.setPreferredSize(new
262+
* Dimension(250, 80));
263+
*/
264+
}
217265
log.log(Level.INFO, "Displaying gui");
218266
frmClicker.setVisible(true);
219267
}

src/me/coley/clicker/util/Lang.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class Lang {
4444
// Statistics tab
4545
STATISTICS_TITLE = 300,
4646
STATISTICS_RECORD_SOME_DATA = 301,
47+
// Agent tab
48+
AGENT_TITLE = 500,
49+
AGENT_ATTATCH_VM = 501,
4750
// Changelog
4851
CHANGELOG_1_0 = 401,
4952
CHANGELOG_1_1 = 402,

0 commit comments

Comments
 (0)