-
Notifications
You must be signed in to change notification settings - Fork 13
/
gamelauncher.cpp
288 lines (272 loc) · 9.71 KB
/
gamelauncher.cpp
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
#include "gamelauncher.h"
#include "profilemanager.h"
#include "EnvPathUtil.h"
#include <QFile>
#include <QDir>
#include "supportedandroidabis.h"
#include <sstream>
GameLauncher::GameLauncher(QObject *parent) : QObject(parent) {
}
std::string GameLauncher::findLauncher(std::string name) {
std::string path;
#ifdef GAME_LAUNCHER_PATH
if (EnvPathUtil::findInPath(name, path, GAME_LAUNCHER_PATH, EnvPathUtil::getAppDir().c_str()))
return path;
#endif
if (EnvPathUtil::findInPath(name, path))
return path;
return std::string();
}
void GameLauncher::start(bool disableGameLog, QString arch, bool hasVerifiedLicense, QString filepath) {
if (running()) {
return;
}
m_disableGameLog = disableGameLog;
process.reset(new QProcess);
QStringList args;
QStringList cargs;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
if (m_gameDir.length() > 0) {
args.append("-dg");
args.append(m_gameDir);
}
if (filepath.length() > 0) {
args.append("--import-file-path");
args.append(filepath);
}
if (!hasVerifiedLicense) {
args.append("--free-only");
}
if (m_profile != nullptr) {
if (m_profile->dataDirCustom) {
args.append("-dd");
args.append(m_profile->dataDir);
}
if (m_profile->windowCustomSize) {
args.append("-ww");
args.append(QString::number(m_profile->windowWidth));
args.append("-wh");
args.append(QString::number(m_profile->windowHeight));
}
if (m_profile->texturePatch != 0) {
args.append("-tp");
args.append(QString::number(m_profile->texturePatch == 1));
}
#ifdef __APPLE__
if (m_profile->graphicsAPI == 1) {
env.insert("ANGLE_DEFAULT_PLATFORM", "metal");
}
if (m_profile->graphicsAPI == 2) {
env.insert("ANGLE_DEFAULT_PLATFORM", "gl");
}
#endif
std::string commandline = m_profile->commandline.toStdString();
if(!commandline.empty()) {
char quote = '\0';
std::string arg;
for(size_t i = 0, length = commandline.length(); i < length; i++) {
auto&& cur = commandline[i];
switch (cur) {
case ' ':
case '\t':
case '\r':
case '\n':
if(quote == '\0') {
if(!arg.empty()) {
cargs.append(QString::fromStdString(arg));
}
arg = "";
} else {
arg += cur;
}
break;
case '"':
case '\'':
if(quote == '\0') {
quote = cur;
} else if(cur == quote) {
quote = '\0';
} else {
arg += cur;
}
break;
case '\\':
i++;
if(i < length) {
cur = commandline[i];
switch (cur) {
case 'n':
arg += '\n';
break;
case 'r':
arg += '\r';
break;
case 't':
arg += '\t';
break;
case '0':
arg += '0';
break;
default:
arg += cur;
break;
}
}
break;
default:
arg += cur;
break;
}
}
if(!arg.empty()) {
cargs.append(QString::fromStdString(arg));
}
}
auto keys = m_profile->env->keys();
for (auto it = keys.constBegin(); it != keys.constEnd(); it++) {
env.insert(*it, m_profile->env->value(*it).toString());
}
}
process->setProcessEnvironment(env);
process->setProcessChannelMode(QProcess::MergedChannels);
if (m_disableGameLog) {
#ifdef _WIN32
process->setStandardOutputFile("nul");
#else
process->setStandardOutputFile("/dev/null");
#endif
}
emit logCleared();
logAttached();
connect(process.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &GameLauncher::handleFinished);
connect(process.data(), &QProcess::errorOccurred, this, &GameLauncher::handleError);
m_crashed = false;
std::stringstream errormsg;
auto abis = SupportedAndroidAbis::getAbis();
std::string launcherpath;
auto _arch = arch.toStdString();
int64_t hasCustomInterpreter = cargs.indexOf("%command%");
QString customExecutable = "";
if(hasCustomInterpreter != -1) {
customExecutable = cargs.front();
cargs.pop_front();
cargs.append(args);
args.clear();
hasCustomInterpreter--;
}
args.append(cargs);
for (auto&& abi : abis) {
if((_arch.empty() || _arch == abi.first) && QFile(m_gameDir + "/lib/" + QString::fromStdString(abi.first) + "/libminecraftpe.so").exists()) {
if(!(launcherpath = findLauncher(abi.second.launchername)).empty()) {
auto executable = QString::fromStdString(launcherpath);
if(hasCustomInterpreter != -1) {
args[hasCustomInterpreter] = executable;
executable = customExecutable;
}
process->start(executable, args);
emit stateChanged();
return;
} else {
errormsg << tr("Could not find the gamelauncher for Minecraft (%1)\nPlease add the launcher '%2' to your 'PATH' (environmentvariable) and restart the launcher\n").arg(QString::fromStdString(abi.first)).arg(QString::fromStdString(abi.second.launchername)).toStdString();
}
}
}
if(errormsg.width() == 0) {
errormsg << "Game not found\n";
}
process.reset();
m_crashed = true;
logAttached();
emit stateChanged();
emit logAppended(QString::fromStdString(errormsg.str()));
emit launchFailed();
}
void GameLauncher::startFile(QString file) {
fileprocess.reset(new QProcess);
QStringList args;
args.append(file);
fileprocess->setProcessChannelMode(QProcess::MergedChannels);
logAttached();
connect(fileprocess.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), [this](int exitCode, QProcess::ExitStatus exitStatus) {
emit fileStarted(exitCode == 0);
});
connect(fileprocess.data(), &QProcess::errorOccurred, [this](QProcess::ProcessError error) {
emit fileStarted(false);
});
connect(fileprocess.data(), &QProcess::readyReadStandardOutput, [this]() {
emit logAppended(QString::fromUtf8(fileprocess->readAllStandardOutput()));
});
std::string launcherpath;
if(!(launcherpath = findLauncher("mcpelauncher-client")).empty()) {
fileprocess->start(QString::fromStdString(launcherpath), args);
return;
} else {
emit fileStarted(false);
}
}
void GameLauncher::handleStdOutAvailable() {
emit logAppended(QString::fromUtf8(process->readAllStandardOutput()));
}
void GameLauncher::handleFinished(int exitCode, QProcess::ExitStatus exitStatus) {
if(!m_disableGameLog && process->bytesAvailable()) {
handleStdOutAvailable();
}
QString msg;
switch (exitCode)
{
case 51: // Failed to load Minecraft lib
msg = tr("Incompatible Minecraft installation, please select a different or older Version\nThis Launcher is a free Open Source Software which usually fell behind official updates from Google Play\nIn some cases there are missing game files,\nmissing Symbols expected to be provided by this Launcher via updates\n or otherwise broke the Launcher");
emit corruptedInstall();
break;
case 127: // Failed to load launcher dependencies (GNU/Linux)
msg = tr("Missing launcher dependencies, please install all missing libraries in their right version");
emit launchFailed();
break;
default:
m_crashed = exitCode != 0;
if (m_crashed) {
msg = tr("Process exited with unexpected exit code: %1\n").arg(exitCode);
logAttached();
} else {
msg = tr("Process exited normally\n");
}
break;
}
process.reset();
if (!m_disableGameLog)
emit logAppended("\n" + msg);
emit stateChanged();
}
void GameLauncher::handleError(QProcess::ProcessError error) {
if (error == QProcess::FailedToStart) {
m_crashed = true;
logAttached();
emit logAppended(tr("Your system is unable to execute the launcher"));
emit stateChanged();
launchFailed();
}
}
void GameLauncher::kill() {
if (running()) {
process->kill();
process->waitForFinished();
process.reset();
emit stateChanged();
}
}
void GameLauncher::logAttached() {
if(!m_disableGameLog) {
m_gamelogopen = true;
if (process) {
connect(process.data(), &QProcess::readyReadStandardOutput, this, &GameLauncher::handleStdOutAvailable);
}
}
}
void GameLauncher::logDetached() {
if(!m_disableGameLog) {
m_gamelogopen = false;
if (process) {
disconnect(process.data(), &QProcess::readyReadStandardOutput, this, &GameLauncher::handleStdOutAvailable);
}
}
}