-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfiguration.java
342 lines (305 loc) · 12.3 KB
/
Configuration.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package org.rsbot;
import org.rsbot.log.LogFormatter;
import org.rsbot.log.SystemConsoleHandler;
import org.rsbot.log.TextAreaLogHandler;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.LogManager;
public class Configuration {
public enum OperatingSystem {
MAC, WINDOWS, LINUX, UNKNOWN
}
public static class Paths {
public static class Resources {
public static final String ROOT = "resources";
public static final String SCRIPTS = Paths.SCRIPTS_NAME_SRC + "/";
public static final String ROOT_IMG = ROOT + "/images";
public static final String ICON = ROOT_IMG + "/icon.png";
public static final String ICON_APPADD = ROOT_IMG + "/application_add.png";
public static final String ICON_APPDELETE = ROOT_IMG + "/application_delete.png";
public static final String ICON_ARROWIN = ROOT_IMG + "/arrow_in.png";
public static final String ICON_REFRESH = ROOT_IMG + "/arrow_refresh.png";
public static final String ICON_DELETE = ROOT_IMG + "/delete.png";
public static final String ICON_GITHUB = ROOT_IMG + "/github.png";
public static final String ICON_PLAY = ROOT_IMG + "/control_play_blue.png";
public static final String ICON_PAUSE = ROOT_IMG + "/control_pause.png";
public static final String DATABASE_ERROR = ROOT_IMG + "/database_error.png";
public static final String ICON_ADD = ROOT_IMG + "/add.png";
public static final String ICON_HOME = ROOT_IMG + "/home.png";
public static final String ICON_BOT = ROOT_IMG + "/bot.png";
public static final String ICON_CLOSE = ROOT_IMG + "/close.png";
public static final String ICON_TICK = ROOT_IMG + "/tick.png";
public static final String ICON_MOUSE = ROOT_IMG + "/mouse.png";
public static final String ICON_PHOTO = ROOT_IMG + "/photo.png";
public static final String ICON_REPORTKEY = ROOT_IMG + "/report_key.png";
public static final String ICON_REPORT_DISK = ROOT_IMG + "/report_disk.png";
public static final String ICON_INFO = ROOT_IMG + "/information.png";
public static final String ICON_KEY = ROOT_IMG + "/key.png";
public static final String ICON_KEYBOARD = ROOT_IMG + "/keyboard.png";
public static final String ICON_CONNECT = ROOT_IMG + "/connect.png";
public static final String ICON_DISCONNECT = ROOT_IMG + "/disconnect.png";
public static final String ICON_START = ROOT_IMG + "/control_play.png";
public static final String ICON_SCRIPT = ROOT_IMG + "/script.png";
public static final String ICON_SCRIPT_ADD = ROOT_IMG + "/script_add.png";
public static final String ICON_SCRIPT_LIVE = ROOT_IMG + "/script_lightning.png";
public static final String ICON_SCRIPT_GEAR = ROOT_IMG + "/script_gear.png";
public static final String ICON_SCRIPT_EDIT = ROOT_IMG + "/script_edit.png";
public static final String ICON_WEBLINK = ROOT_IMG + "/world_link.png";
public static final String ICON_WRENCH = ROOT_IMG + "/wrench.png";
public static final String VERSION = ROOT + "/version.txt";
}
public static class URLs {
public static final String HOST = "powerbot.org";
private static final String BASE = "http://links." + HOST + "/";
public static final String DOWNLOAD = BASE + "download";
public static final String UPDATE = BASE + "modscript";
public static final String VERSION = BASE + "version.txt";
public static final String VERSION_KILL = BASE + "version-kill";
public static final String PROJECT = BASE + "git-project";
public static final String SITE = BASE + "site";
public static final String SDN_CONTROL = BASE + "sdn-control";
public static final String AD_INFO = BASE + "botad-info";
public static final String MONITORING_CONTROL = BASE + "monitoring";
public static final String WEBCOMPILER = BASE + "webcompile";
public static final String SERVICELOGIN = BASE + "servicelogin";
}
public static final String ROOT = new File(".").getAbsolutePath();
public static final String COMPILE_SCRIPTS_BAT = "Compile-Scripts.bat";
public static final String COMPILE_SCRIPTS_SH = "compile-scripts.sh";
public static final String COMPILE_FIND_JDK = "FindJDK.bat";
public static final String SCRIPTS_NAME_SRC = "scripts";
public static final String SCRIPTS_NAME_OUT = "Scripts";
public static String getAccountsFile() {
final String path;
if (Configuration.getCurrentOperatingSystem() == OperatingSystem.WINDOWS) {
path = System.getenv("APPDATA") + File.separator + Configuration.NAME + "_Accounts.ini";
} else {
path = Paths.getUnixHome() + File.separator + "." + Configuration.NAME_LOWERCASE + "acct";
}
return path;
}
public static String getHomeDirectory() {
final String env = System.getenv(Configuration.NAME.toUpperCase() + "_HOME");
if (env == null || env.isEmpty()) {
return (Configuration.getCurrentOperatingSystem() == OperatingSystem.WINDOWS ?
FileSystemView.getFileSystemView().getDefaultDirectory().getAbsolutePath() :
Paths.getUnixHome()) + File.separator + Configuration.NAME;
} else {
return env;
}
}
public static String getLogsDirectory() {
return Paths.getHomeDirectory() + File.separator + "Logs";
}
public static String getPathCache() {
return Paths.getSettingsDirectory() + File.separator + "path.txt";
}
public static String getUIDsFile() {
return Paths.getSettingsDirectory() + File.separator + "uid.txt";
}
public static String getScreenshotsDirectory() {
return Paths.getHomeDirectory() + File.separator + "Screenshots";
}
public static String getScriptsDirectory() {
return Paths.getHomeDirectory() + File.separator + Paths.SCRIPTS_NAME_OUT;
}
public static String getScriptsSourcesDirectory() {
return Paths.getScriptsDirectory() + File.separator + "Sources";
}
public static String getScriptsPrecompiledDirectory() {
return Paths.getScriptsDirectory() + File.separator + "Precompiled";
}
public static String getScriptsNetworkDirectory() {
return Paths.getScriptsDirectory() + File.separator + "Network";
}
public static String getCacheDirectory() {
return Paths.getHomeDirectory() + File.separator + "Cache";
}
public static String getScriptCacheDirectory() {
return getCacheDirectory() + File.separator + "Scripts";
}
public static String getVersionCache() {
return Paths.getCacheDirectory() + File.separator + "info.dat";
}
public static String getWebDatabase() {
return Paths.getSettingsDirectory() + File.separator + "Web.store";
}
public static String getServiceKey() {
return Paths.getSettingsDirectory() + File.separator + "service.key";
}
public static String getSettingsDirectory() {
return Paths.getHomeDirectory() + File.separator + "Settings";
}
public static String getGarbageDirectory() {
final File dir = new File(Configuration.Paths.getScriptCacheDirectory(), ".java");
if (!dir.exists()) {
dir.mkdirs();
}
String path = dir.getAbsolutePath();
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (final UnsupportedEncodingException ignored) {
}
return path;
}
public static String getRunningJarPath() {
if (!RUNNING_FROM_JAR) {
return null;
}
String path = new File(Configuration.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getAbsolutePath();
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (UnsupportedEncodingException ignored) {
}
return path;
}
public static String getUnixHome() {
final String home = System.getProperty("user.home");
return home == null ? "~" : home;
}
}
public static final String NAME = "RSBot";
public static final String NAME_LOWERCASE = NAME.toLowerCase();
private static final OperatingSystem CURRENT_OS;
public static boolean RUNNING_FROM_JAR = false;
public static class Twitter {
public static final boolean ENABLED = true;
public static final String NAME = "rsbotorg";
public static final String HASHTAG = "#" + NAME_LOWERCASE;
public static final int MESSAGES = 3;
}
static {
final URL resource = Configuration.class.getClassLoader().getResource(Paths.Resources.VERSION);
if (resource != null) {
Configuration.RUNNING_FROM_JAR = true;
}
final String os = System.getProperty("os.name");
if (os.contains("Mac")) {
CURRENT_OS = OperatingSystem.MAC;
} else if (os.contains("Windows")) {
CURRENT_OS = OperatingSystem.WINDOWS;
} else if (os.contains("Linux")) {
CURRENT_OS = OperatingSystem.LINUX;
} else {
CURRENT_OS = OperatingSystem.UNKNOWN;
}
final ArrayList<String> dirs = new ArrayList<String>();
dirs.add(Paths.getHomeDirectory());
dirs.add(Paths.getLogsDirectory());
dirs.add(Paths.getCacheDirectory());
dirs.add(Paths.getSettingsDirectory());
dirs.add(Paths.getScriptsDirectory());
dirs.add(Paths.getScriptsSourcesDirectory());
dirs.add(Paths.getScriptsPrecompiledDirectory());
for (final String name : dirs) {
final File dir = new File(name);
if (!dir.exists()) {
dir.mkdirs();
}
}
final Properties logging = new Properties();
final String logFormatter = LogFormatter.class.getCanonicalName();
final String fileHandler = FileHandler.class.getCanonicalName();
logging.setProperty("handlers", TextAreaLogHandler.class.getCanonicalName() + "," + fileHandler);
logging.setProperty(".level", "INFO");
logging.setProperty(SystemConsoleHandler.class.getCanonicalName() + ".formatter", logFormatter);
logging.setProperty(fileHandler + ".formatter", logFormatter);
logging.setProperty(TextAreaLogHandler.class.getCanonicalName() + ".formatter", logFormatter);
logging.setProperty(fileHandler + ".pattern", Paths.getLogsDirectory() + File.separator + "%u.%g.log");
logging.setProperty(fileHandler + ".count", "10");
final ByteArrayOutputStream logout = new ByteArrayOutputStream();
try {
logging.store(logout, "");
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(logout.toByteArray()));
} catch (final Exception ignored) {
}
if (Configuration.RUNNING_FROM_JAR) {
String path = resource.toString();
try {
path = URLDecoder.decode(path, "UTF-8");
} catch (final UnsupportedEncodingException ignored) {
}
final String prefix = "jar:file:/";
if (path.indexOf(prefix) == 0) {
path = path.substring(prefix.length());
path = path.substring(0, path.indexOf('!'));
if (File.separatorChar != '/') {
path = path.replace('/', File.separatorChar);
}
try {
final File pathfile = new File(Paths.getPathCache());
if (pathfile.exists()) {
pathfile.delete();
}
pathfile.createNewFile();
final Writer out = new BufferedWriter(new FileWriter(Paths.getPathCache()));
out.write(path);
out.close();
} catch (final Exception e) {
e.printStackTrace();
}
}
}
}
public static URL getResourceURL(final String path) throws MalformedURLException {
return RUNNING_FROM_JAR ? Configuration.class.getResource("/" + path) : new File(path).toURI().toURL();
}
public static Image getImage(final String resource) {
try {
return Toolkit.getDefaultToolkit().getImage(getResourceURL(resource));
} catch (final Exception e) {
}
return null;
}
public static OperatingSystem getCurrentOperatingSystem() {
return Configuration.CURRENT_OS;
}
public static int getVersion() {
InputStreamReader is = null;
BufferedReader reader = null;
try {
is = new InputStreamReader(RUNNING_FROM_JAR ?
Configuration.class.getClassLoader().getResourceAsStream(
Paths.Resources.VERSION) : new FileInputStream(Paths.Resources.VERSION));
reader = new BufferedReader(is);
final String s = reader.readLine().trim();
return Integer.parseInt(s);
} catch (final Exception e) {
} finally {
try {
if (is != null) {
is.close();
}
if (reader != null) {
reader.close();
}
} catch (final IOException ioe) {
}
}
return -1;
}
public static String getVersionFormatted() {
return getVersionFormatted(getVersion());
}
public static String getVersionFormatted(final int version) {
final float v = (float) version / 100;
String s = Float.toString(v);
final int z = s.indexOf('.');
if (z == -1) {
s += ".00";
} else {
final String exp = s.substring(z + 1);
if (exp.length() == 1) {
s += "0";
}
}
return s;
}
}