/
Main.java
518 lines (487 loc) · 17.8 KB
/
Main.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package cz.autoclient;
import cz.autoclient.updates.Updater;
import cz.autoclient.GUI.Dialogs;
import cz.autoclient.main_automation.Automat;
import cz.autoclient.GUI.Gui;
import cz.autoclient.GUI.summoner_spells.ButtonSummonerSpellMaster;
import cz.autoclient.GUI.summoner_spells.InputSummonerSpell;
import cz.autoclient.GUI.updates.UpdateProgressWindow;
import cz.autoclient.PVP_net.Setnames;
import cz.autoclient.robots.Robot;
import cz.autoclient.robots.exceptions.NoSuchRobotException;
import cz.autoclient.settings.InputHandlers;
import cz.autoclient.settings.Settings;
import cz.autoclient.settings.input_handlers.*;
import cz.autoclient.settings.secure.PasswordFailedException;
import cz.autoclient.settings.secure.SecureSettings;
import cz.autoclient.settings.secure.UniqueID;
import cz.autoclient.updates.VersionId;
import java.awt.Frame;
import java.beans.Expression;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import org.apache.logging.log4j.LogManager;
import sirius.constants.IMKConsts;
import sirius.constants.IWMConsts;
// Call templates
// S>s,top,3,400;d,2000;s,Hello everyone\, I go top :)
// S>s,top,3,400;d,2000;s,Hello everyone\, I go top :)
// S>s,top,3,400;d,2000;s,Hello everyone\, I go top :)
public class Main
implements IWMConsts, IMKConsts
{
//Some application constants
public static final String SETTINGS_FILE = "data/settings.bin";
private VersionId VERSION = new VersionId("0.0-error");
public static final File BACKUP_DIR = new File("data/backup/");
public static boolean debug = true;
public Automat ac;
public Gui gui;
public Settings settings;
public Updater updater;
public final String[] runAsAdminPaths = new String[]{"run-as-admin.vbs", "release_aditional_files/run-as-admin.vbs", "admin.vbs"};
public final ArrayList<Robot> robots = new ArrayList<>();
public Main()
{
updater = new Updater("Darker/auto-client", getVersion(), new File("./updates/"));
System.out.println("Running auto client "+getVersion());
//Normal program
startGUI();
}
public final VersionId getVersion() {
if(VERSION.affix.equals("error")) {
InputStream in = Main.class.getResourceAsStream("/version");
Scanner sc = new Scanner(in, "UTF-8");
VERSION = new VersionId(sc.useDelimiter("\\A").next());
}
return VERSION;
}
public boolean ToolRunning() {
return ac != null && ac.isAlive() && !ac.isInterrupted();
}
public void StopTool()
{
if ((ac != null) && (ac.isAlive()) && (!ac.isInterrupted()))
{
System.out.println("Stopping tool..");
ac.interrupt();
}
else {
System.out.println("Tried to stop tool while it wasn't running!");
}
}
public void StartTool()
{
if ((ac == null) || (!ac.isAlive()))
{
System.out.println("Starting tool..");
ac = new Automat(gui, settings);
ac.start();
}
}
private void startGUI()
{
settings = new Settings();
try {
settings.loadFromFile(SETTINGS_FILE);
}
catch(FileNotFoundException e) {
//Do nothing, this is expected for first run, before the settings file is created
System.out.println("No settings loaded, they will be re-created. Error:"+e);
}
catch(IOException e) {
//This means the settings probably exist but are corrupted
System.err.println("Settings corrupted. Error:"+e);
e.printStackTrace();
File setFile = new File(SETTINGS_FILE);
if(setFile.isFile()) {
File newFile = null;
boolean copySuccess = false;
Exception copyError = null;
try {
BACKUP_DIR.mkdirs();
String filename = BACKUP_DIR.getAbsolutePath()+"/backup_";
int iterator = 1;
while((newFile = new File(filename+(iterator++)+".bin")).exists()) {
//Sometimes you're so pro you do everything in the while condition
}
Files.copy(setFile.toPath(), newFile.toPath());
copySuccess = true;
}
catch(Exception ex) {
copyError = ex;
newFile = null;
}
final String path = copySuccess?newFile.getAbsolutePath():copyError.toString();
final boolean success = copySuccess;
new Thread() {
@Override
public void run() {
StringBuilder message = new StringBuilder("Your settings file was corrupted.\n");
if(success) {
message.append("Corrupted file was moved into ");
message.append(path);
message.append(" for backup and default settings will now be used.");
}
else {
message.append("Additionally, a following error has occured when attempting to backup corrupted file:\n ");
message.append(path);
message.append("\nWe're sorry, all your settings are lost :(");
}
JOptionPane.showMessageDialog(null,
message.toString()
, "Settings corrupted",JOptionPane.ERROR_MESSAGE);
}
}.start();
}
}
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
gui = new Gui(Main.this, settings);
gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gui.setVisible(true);
if(updater!=null) {
gui.setUpdateManager(updater);
updater.setSettings(settings);
if(settings.getBoolean(Setnames.UPDATES_AUTOCHECK.name, (Boolean)Setnames.UPDATES_AUTOCHECK.default_val))
updater.checkForUpdates();
}
//Dialogs.dialogInfoOnceAsync("Hello :)", "First fun", gui, "DIALOG_FIRST_RUN", settings, true);
}
});
// If the application is configured to allways start as admin it will restart now
// It's slow but it works without creating some other kind of settings
if(settings.getBoolean(Setnames.START_AS_ADMIN.name, (boolean)Setnames.START_AS_ADMIN.default_val)) {
System.out.println("Starting as admin because of settings.");
tryRestartAsAdmin(false);
return;
}
//System.out.println("PW: "+settings.getSetting(Setnames.REMEMBER_PASSWORD.name));
//Fill empty fields with default values
Setnames.setDefaults(settings);
//Initialise encryption
SecureSettings encryptor = settings.getEncryptor();
//Constant password.S-1-5-21-3630785732-580163861-2202204989-1004
//Constant password.S-1-5-21-3630785732-580163861-2202204989-1004
/*EncryptedSetting test = new EncryptedSetting(encryptor);
settings.setSetting(Setnames.REMEMBER_PASSWORD.name, test);
test.setEncryptedValue(new byte[] {0,56,64,32,44,55,66,99,88,77,88,55,66,33});*/
encryptor.addPassword("Constant password.");
encryptor.addPassword(UniqueID.WINDOWS_USER_SID);
try {
encryptor.getMergedPassword();
//System.out.println("Password key: "+encryptor.getMergedPassword());
//System.out.println("Encryption password: "+encryptor.getMergedPassword());
} catch (PasswordFailedException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
private <T extends Robot> T createRobot(Class<T> type) throws NoSuchRobotException {
try {
T robot = (T)new Expression(type, "new", new Object[]{}).getValue();
robot.setLogger(LogManager.getLogger(type.getName()));
return robot;
}
catch(Exception e) {
throw new NoSuchRobotException("Constructor has failed.");
}
}
public <T extends Robot> T findRobot(Class<T> type) throws NoSuchRobotException {
T result = null;
for(Robot r : robots) {
if(type.isInstance(r)) {
result = (T)r;
break;
}
}
if(result==null) {
result = createRobot(type);
robots.add(result);
}
return result;
}
public static void main(String[] args)
{
//Register GUI settings handlers
InputHandlers.register(InputJTextField.class, javax.swing.JPasswordField.class);
InputHandlers.register(InputJTextField.class, JTextField.class);
InputHandlers.register(InputJCheckBox.class, JCheckBox.class);
InputHandlers.register(InputJCheckBoxMenuItem.class, JCheckBoxMenuItem.class);
InputHandlers.register(InputJSpinner.class, JSpinner.class);
InputHandlers.register(InputJComboBox.class, JComboBox.class);
//My GUI handler for the Summoner Spell buttons
InputHandlers.register(InputSummonerSpell.class, ButtonSummonerSpellMaster.class);
//Start program
Main ac = new Main();
}
public Thread TerminateAsync(final boolean force) {
Thread t = new Thread("Terminate AutoClient") {
@Override
public void run() {
Main.this.Terminate(force);
}
};
t.start();
return t;
}
public Thread TerminateAsync() {
return TerminateAsync(false);
}
/**
* Terminates the whole program while saving settings.
* @param force if true, terminates the program using System.exit(0)
*/
public void Terminate(final boolean force, final boolean saveSettings) {
destroyGui();
//Stop tool thread if running
if(ToolRunning())
StopTool();
//Save settings
if(saveSettings)
saveSettings();
//Here, no non-deamon threads should be running (daemon thread does not prolong the applicatione execution).
if(force)
System.exit(0);
}
public void saveSettings() {
if(settings!=null) {
try {
settings.loadSettingsFromBoundFields();
settings.saveToFile(SETTINGS_FILE, false);
}
catch(IOException e) {
System.err.println("Problem saving settings:");
e.printStackTrace(System.err);
}
}
else {
System.out.println("Settings is null!");
}
if(updater!=null)
updater.saveAll();
}
public void destroyGui() {
//gui is JFrame representing the application window
SwingUtilities.invokeLater(
()->{
if(gui!=null) {
gui.setVisible(false);
gui.teardown();
gui = null;
}
for(Frame frame : JFrame.getFrames()) {
//System.out.println("Frame " + frame.getTitle());
if(frame.isDisplayable()) {
//System.out.println(" Destroying frame " + frame.getTitle());
frame.dispose();
}
}
}
);
}
public void InstallUpdate() {
if(updater!=null) {
if(updater.getUpdates().installStepIs(Updater.InstallStep.CAN_COPY_FILES, Updater.InstallStep.CAN_UNPACK)) {
updater.installUpdate();
updater.terminateAfterAction();
System.out.println("Installing updates, will terminate after that.");
}
else
throw new IllegalStateException("Tried to install update but this is no time to install.");
}
else
throw new IllegalStateException("Tried to install update but updates are disabled.");
}
public void Terminate() {
Terminate(false, true);
}
public void Terminate(boolean force) {
Terminate(force, true);
}
public void Restart(boolean force, boolean saveSettings) {
if(saveSettings)
saveSettings();
//Stop tool thread if running
if(ToolRunning())
StopTool();
try {
Runtime.getRuntime().exec("javaw -jar \""+getSelfPath()+"\" >restart.log");
} catch (FileNotFoundException ex) {
System.out.println("Cannot find the jar file.");
} catch (IOException ex) {
System.out.println("Cannot execute the command.");
}
Terminate(force);
}
public void Restart(boolean force) {
Restart(force, true);
}
public void Restart() {
Restart(false, true);
}
public Thread RestartAsync(final boolean force, final boolean saveSettings) {
Thread t = new Thread("Restart AutoClient") {
@Override
public void run() {
Main.this.Restart(force, saveSettings);
}
};
t.start();
return t;
}
public Thread RestartAsync(boolean force) {
return RestartAsync(force, true);
}
public Thread RestartAsync() {
return RestartAsync(false, true);
}
public void UpdateAndRestart() {
Thread restartThread = new Thread("UpdateAndRestart") {
@Override
public void run() {
destroyGui();
saveSettings();
UpdateProgressWindow wnd = new UpdateProgressWindow(updater, ()->{
destroyGui();
//Stop tool thread if running
if(ToolRunning())
StopTool();
try {
Runtime.getRuntime().exec("javaw -jar \""+getSelfPath()+"\" >restart.log");
} catch (FileNotFoundException ex) {
System.out.println("Cannot find the jar file.");
} catch (IOException ex) {
System.out.println("Cannot execute the command.");
}
});
//updater.setUpdateListener(wnd);
InstallUpdate();
}
};
restartThread.start();
}
public void RestartWithAdminRightsAsync() throws FileNotFoundException, IOException {
RestartWithAdminRightsAsync(false);
}
public void RestartWithAdminRightsAsync(final boolean force) throws FileNotFoundException, IOException {
if(!force)
Terminate(force);
StartWithAdminRights();
if(force)
Terminate(force);
}
private static volatile Boolean isAdmin = null;
private static final Object isAdmin_mutex = new Object();
public static boolean isAdmin(){
Boolean is = isAdmin;
if(is!=null)
return is;
synchronized(isAdmin_mutex) {
if((is = isAdmin)!=null)
return is;
PrintStream systemErr = System.err;
Preferences prefs = Preferences.systemRoot();
synchronized(systemErr) { // better synchroize to avoid problems with other threads that access System.err
System.setErr(null);
try{
prefs.put("foo", "bar"); // SecurityException on Windows
prefs.remove("foo");
prefs.flush(); // BackingStoreException on Linux
return isAdmin = true;
}
catch(Exception e) {
return isAdmin = false;
}
finally {
System.setErr(systemErr);
}
}
}
}
public void tryRestartAsAdmin(final boolean force) {
try {
RestartWithAdminRightsAsync(force);
}
catch (FileNotFoundException ex) {
Dialogs.dialogErrorAsync(ex.getMessage());
}
catch (IOException ex) {
Dialogs.dialogErrorAsync("Cannot read or use the helper VBS file.\n "+ex);
}
}
/**
* Start this very jar file elevated on Windows. It is strongly recommended to close any existing IO
* before calling this method and avoid writing anything more to files. The new instance of this same
* program will be started and simultaneous write/write or read/write would cause errors.
* @throws FileNotFoundException if the helper vbs script was not found
* @throws IOException if there was another failure inboking VBS script
*/
public void StartWithAdminRights() throws FileNotFoundException, IOException {
String jarPath = getSelfPath();
//The path to the helper script. This scripts takes 1 argument which is a Jar file full path
File runAsAdmin = null;
// Temporary file to check
File fileTmp = null;
// All possible paths will be checked until existing vbs file is found
for(int i=0; i<runAsAdminPaths.length && runAsAdmin==null; i++) {
fileTmp = new File(runAsAdminPaths[i]);
if(fileTmp.exists()) {
runAsAdmin = fileTmp;
}
}
if(runAsAdmin==null) {
throw new FileNotFoundException(
"The VBSScript used for elevation not found at "
+ new File(runAsAdminPaths[0]).getAbsolutePath()
);
}
// If exception was not thrown the program can be launched
String command = "cscript \""+runAsAdmin.getAbsolutePath()+"\" "+jarPath;
System.out.println("Executing '"+command+"'");
//Note that .exec is asynchronous
//After it starts, you must terminate your program ASAP, or you'll have 2 instances running
Runtime.getRuntime().exec(command);
}
public static String getSelfPath() throws FileNotFoundException {
//Our
String jarPath;
//System.out.println("Current relative path is: " + s);
try {
jarPath = "\""+new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getAbsolutePath()+"\"";
} catch (URISyntaxException ex) {
throw new FileNotFoundException("Could not fetch the path to the current jar file. Got this URISyntax exception:"+ex);
}
//If the jar path was created but doesn't contain .jar, we're (most likely) not running from jar
//typically this happens when running the program from IDE
//These 4 lines just serve as a fallback in testing, should be deleted in production
//code and replaced with another FileNotFoundException
if(!jarPath.contains(".jar")) {
Path currentRelativePath = Paths.get("");
jarPath = "\""+currentRelativePath.toAbsolutePath().toString()+"\\AutoClient.jar\"";
}
return jarPath;
}
}