-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApplication.java
141 lines (130 loc) · 4.13 KB
/
Application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.rsbot;
import org.rsbot.bot.Bot;
import org.rsbot.gui.BotGUI;
import org.rsbot.log.LogOutputStream;
import org.rsbot.log.SystemConsoleHandler;
import org.rsbot.security.RestrictedSecurityManager;
import org.rsbot.util.ApplicationException;
import org.rsbot.util.io.IOHelper;
import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Application {
private static BotGUI gui;
public static void main(final String[] args) {
try {
bootstrap();
RestrictedSecurityManager.fixHosts();
extractResources();
commands(args);
System.setSecurityManager(new RestrictedSecurityManager());
System.setProperty("java.io.tmpdir", Configuration.Paths.getGarbageDirectory());
gui = new BotGUI();
gui.setVisible(true);
} catch (final Exception e) {
e.printStackTrace();
System.out.print(e.getMessage());
try {
if (gui != null) {
gui.setVisible(false);
}
} catch (final Exception ignored) {
}
final String msg = (e.getClass().isAssignableFrom(ApplicationException.class)) ? e.getMessage() : "Error: " + e.toString() + "\nUnable to start.";
try {
final JFrame frame = new JFrame();
frame.setIconImage(Configuration.getImage(Configuration.Paths.Resources.ICON));
frame.setLocationRelativeTo(frame.getOwner());
frame.setVisible(true);
new Thread() {
@Override
public void run() {
final Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
try {
Thread.sleep(64); // race condition :)
} catch (final InterruptedException ignored) {
}
frame.setLocation(s.width + 0xff, s.height + 0xff);
}
}.start();
JOptionPane.showMessageDialog(frame, msg, Configuration.NAME, JOptionPane.ERROR_MESSAGE);
} catch (final HeadlessException ignored) {
}
try {
if (gui != null) {
gui.cleanExit(false);
}
} catch (final Exception ignored) {
}
System.exit(1);
}
}
private static void commands(final String[] args) {
if (args.length > 1) {
if (args[0].toLowerCase().endsWith("delete")) {
final File jarOld = new File(args[1]);
if (jarOld.exists()) {
if (!jarOld.delete()) {
jarOld.deleteOnExit();
}
}
}
}
}
public static void extractResources() {
final ArrayList<String> extract = new ArrayList<String>(2);
if (Configuration.getCurrentOperatingSystem() == Configuration.OperatingSystem.WINDOWS) {
extract.add(Configuration.Paths.COMPILE_SCRIPTS_BAT);
extract.add(Configuration.Paths.COMPILE_FIND_JDK);
} else {
extract.add(Configuration.Paths.COMPILE_SCRIPTS_SH);
}
for (final String item : extract) {
final String path = Configuration.Paths.Resources.ROOT + "/" + item;
final InputStream in;
try {
in = Configuration.getResourceURL(path).openStream();
} catch (IOException ignored) {
continue;
}
final File output = new File(Configuration.Paths.getHomeDirectory(), item);
IOHelper.write(in, output);
}
}
/**
* Returns the Bot for any object loaded in its client. For internal use
* only (not useful for script writers).
*
* @param o Any object from within the client.
* @return The Bot for the client.
*/
public static Bot getBot(final Object o) {
return gui.getBot(o);
}
/**
* Returns the size of the panel that clients should be drawn into. For
* internal use.
*
* @return The client panel size.
*/
public static Dimension getPanelSize() {
return gui.getPanel().getSize();
}
private static void bootstrap() {
Logger.getLogger("").setLevel(Level.ALL);
Logger.getLogger("").addHandler(new SystemConsoleHandler());
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
private final Logger log = Logger.getLogger("EXCEPTION");
public void uncaughtException(final Thread t, final Throwable e) {
log.logp(Level.SEVERE, "EXCEPTION", "", "Unhandled exception in thread " + t.getName() + ": ", e);
}
});
System.setErr(new PrintStream(new LogOutputStream(Logger.getLogger("STDERR"), Level.SEVERE), true));
}
}