-
Notifications
You must be signed in to change notification settings - Fork 1
/
LaunchGame.java
149 lines (131 loc) · 5.47 KB
/
LaunchGame.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
/*
* This file is part of CustomLauncherRewrite.
*
* CustomLauncherRewrite is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CustomLauncherRewrite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CustomLauncherRewrite. If not, see <https://www.gnu.org/licenses/>.
*/
package lol.hyper.customlauncher.login;
import lol.hyper.customlauncher.ConfigHandler;
import lol.hyper.customlauncher.tools.ExceptionWindow;
import lol.hyper.customlauncher.tools.OSDetection;
import lol.hyper.customlauncher.tools.PopUpWindow;
import lol.hyper.customlauncher.ttrupdater.TTRUpdater;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.util.Map;
public final class LaunchGame extends Thread {
/**
* The LaunchGame logger.
*/
public final Logger logger = LogManager.getLogger(this);
/**
* The account's login cookie.
*/
private final String cookie;
/**
* The game server to use.
*/
private final String gameServer;
/**
* The file to check for TTR updates.
*/
private final String manifest;
/**
* Creates a LaunchGame instance.
*
* @param cookie The login cookie to use.
* @param gameServer The game server to use.
*/
public LaunchGame(String cookie, String gameServer, String manifest) {
this.cookie = cookie;
this.gameServer = gameServer;
this.manifest = manifest;
}
/**
* Launch the game.
*/
public void run() {
ConfigHandler configHandler = new ConfigHandler();
File installPath = configHandler.getInstallPath();
if (!installPath.exists()) {
JOptionPane.showMessageDialog(null, "Unable to launch the game. The install location cannot be found.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
ProcessBuilder pb = new ProcessBuilder();
String[] launchCommand = null;
// Check for TTR updates after login
// We do this after login since TTR sends back a patch manifest to check game files after login
// This used to not be the case, but we follow what the real launcher does
TTRUpdater ttrUpdater = new TTRUpdater();
ttrUpdater.setVisible(true);
ttrUpdater.checkUpdates(manifest);
switch (OSDetection.osType) {
case "linux" -> {
launchCommand = new String[]{"./TTREngine"};
// Make sure it's executable before running
File fullPath = new File(installPath, "TTREngine");
if (!fullPath.canExecute()) {
logger.info(fullPath.getAbsolutePath() + " is not executable. Attempting to set it.");
boolean result;
try {
result = fullPath.setExecutable(true);
} catch (SecurityException exception) {
logger.error("Unable to set " + fullPath.getAbsolutePath() + " as an executable!", exception);
new ExceptionWindow(exception);
return;
}
if (!result) {
logger.error("Unable to set " + fullPath.getAbsolutePath() + " as an executable!");
new PopUpWindow(null, "Unable to set " + fullPath.getAbsolutePath() + " as an executable!\nMake sure this file is executable!");
return;
} else {
logger.info(fullPath.getAbsolutePath() + " was set executable successfully!");
}
}
}
case "win32" -> launchCommand = new String[]{"cmd", "/c", "TTREngine.exe"};
case "win64" -> launchCommand = new String[]{"cmd", "/c", "TTREngine64.exe"};
}
if (launchCommand == null) {
logger.error("Unable to determine operating system!");
new PopUpWindow(null, "Unable to determine operating system!");
return;
}
logger.info("Launching game from " + installPath.getAbsolutePath());
// dirty little trick to redirect the output
// the game freezes if you don't do this
// https://stackoverflow.com/a/58922302
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectErrorStream(true);
// make sure we set the working directory and command
pb.directory(installPath);
pb.command(launchCommand);
Map<String, String> env = pb.environment();
env.put("TTR_GAMESERVER", this.gameServer);
env.put("TTR_PLAYCOOKIE", this.cookie);
Thread t1 = new Thread(() -> {
try {
Process process = pb.start();
process.getInputStream().close();
process.waitFor();
} catch (IOException | InterruptedException exception) {
logger.error("Unable to launch game!", exception);
new ExceptionWindow(exception);
}
});
t1.start();
}
}