-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathGameThread.cpp
More file actions
319 lines (271 loc) · 11.6 KB
/
Copy pathGameThread.cpp
File metadata and controls
319 lines (271 loc) · 11.6 KB
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
/*
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org>
This file is part of libTAS.
libTAS 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.
libTAS 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 libTAS. If not, see <http://www.gnu.org/licenses/>.
*/
// #include <QtWidgets/QApplication>
// #include "ui/MainWindow.h"
//#include "config.h"
#include "GameThread.h"
#include "utils.h"
#include "../shared/SharedConfig.h"
#include <string>
#include <sstream>
#include <iostream>
#include <unistd.h> // chdir()
#include <fcntl.h> // O_RDWR, O_CREAT
void GameThread::launch(Context *context, int sock)
{
#ifdef __unix__
/* Update the LD_LIBRARY_PATH environment variable if the user set one */
if (!context->config.libdir.empty()) {
char* oldlibpath = getenv("LD_LIBRARY_PATH");
std::string libpath = context->config.libdir;
if (oldlibpath) {
libpath.append(":");
libpath.append(oldlibpath);
}
setenv("LD_LIBRARY_PATH", libpath.c_str(), 1);
}
#endif
/* Change the working directory to the user-defined one or game directory */
std::string newdir = context->config.rundir;
if (newdir.empty()) {
/* Get the game directory from path */
size_t sep = context->gamepath.find_last_of("/");
if (sep != std::string::npos)
newdir = context->gamepath.substr(0, sep);
else
newdir = ""; // Should not happen
}
if (0 != chdir(newdir.c_str())) {
std::cerr << "Could not change the working directory to " << newdir << std::endl;
}
/* Set PWD environment variable because games may use it and chdir
* does not update it.
*/
setenv("PWD", newdir.c_str(), 1);
/* Set where stderr of the game is redirected */
int fd;
std::string logfile = context->gamepath + ".log";
switch(context->config.sc.logging_status) {
case SharedConfig::NO_LOGGING:
fd = open("/dev/null", O_RDWR, S_IRUSR | S_IWUSR);
dup2(fd, 2);
close(fd);
break;
case SharedConfig::LOGGING_TO_FILE:
fd = open(logfile.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
dup2(fd, 2);
close(fd);
break;
case SharedConfig::LOGGING_TO_CONSOLE:
default:
break;
}
/* Change settings based on game arch */
int gameArch = extractBinaryType(context->gamepath);
bool macappflag = gameArch & BT_MACOSAPP;
gameArch &= BT_TYPEMASK;
int libtasArch = extractBinaryType(context->libtaspath);
/* Switch to libtas32.so if required */
if (((gameArch == BT_ELF32) || (gameArch == BT_PE32)) && (libtasArch == BT_ELF64)) {
context->libtaspath = context->libtas32path;
/* libtas32.so presence was already checked in ui/ErrorChecking.cpp */
libtasArch = extractBinaryType(context->libtaspath);
}
/* Set additional environment variables regarding Mesa and VDPAU configurations */
if (context->config.sc.opengl_soft) {
setenv("LIBGL_ALWAYS_SOFTWARE", "1", 1);
setenv("VDPAU_DRIVER", "va_gl", 1);
setenv("VDPAU_QUIRKS", "AvoidVA", 1);
}
else {
unsetenv("LIBGL_ALWAYS_SOFTWARE");
unsetenv("VDPAU_DRIVER");
unsetenv("VDPAU_QUIRKS");
}
/* Pass libtas library path to the game */
setenv("LIBTAS_LIBRARY_PATH", context->libtaspath.c_str(), 1);
setenv("LIBTAS_START_FRAME", std::to_string(context->framecount).c_str(), 1);
/* Override timezone for determinism */
setenv("TZ", "UTC0", 1);
/* Pass socket file descriptor to the game */
setenv("LIBTAS_SOCKET_FD", std::to_string(sock).c_str(), 1);
/* Build the argument list to be fed to execv */
std::list<std::string> arg_list;
/* Detect Windows executables and launch wine */
if ((gameArch == BT_PE32) || (gameArch == BT_PE32P)) {
if (context->config.use_proton && !context->config.proton_path.empty()) {
/* Change the executable to proton */
std::string winepath = context->config.proton_path;
winepath += "/dist/bin/wine";
if (gameArch == BT_PE32P)
winepath += "64";
arg_list.push_back(winepath);
/* Set the env variables needed by Proton */
std::string winedllpath = context->config.proton_path;
winedllpath += "/dist/lib64/wine:";
winedllpath += context->config.proton_path;
winedllpath += "/dist/lib/wine";
setenv("WINEDLLPATH", winedllpath.c_str(), 1);
char* oldlibpath = getenv("LD_LIBRARY_PATH");
std::string libpath = context->config.proton_path;
libpath += "/dist/lib64/:";
libpath += context->config.proton_path;
libpath += "/dist/lib/";
if (oldlibpath) {
libpath.append(":");
libpath.append(oldlibpath);
}
setenv("LD_LIBRARY_PATH", libpath.c_str(), 1);
std::string wineprefix = context->config.proton_path;
wineprefix += "/dist/share/default_pfx/";
setenv("WINEPREFIX", wineprefix.c_str(), 1);
}
else {
/* Change the executable to wine */
std::string winename = "wine";
if (gameArch == BT_PE32P)
winename += "64";
/* wine[64] presence was already checked in ui/ErrorChecking.cpp */
std::string cmd = "which ";
cmd += winename;
FILE *output = popen(cmd.c_str(), "r");
if (output != NULL) {
std::array<char,256> buf;
if (fgets(buf.data(), buf.size(), output) != 0) {
std::string winepath = std::string(buf.data());
winepath.pop_back(); // remove trailing newline
arg_list.push_back(winepath);
}
pclose(output);
}
}
/* We need to delay libtas hooking for wine process. */
setenv("LIBTAS_DELAY_INIT", "1", 1);
/* Push the game executable as the first command-line argument */
/* Wine can fail if not specifying a Windows path */
context->gamepath.insert(0, "Z:");
arg_list.push_back(context->gamepath);
}
else {
if (context->attach_gdb) {
#ifdef __unix__
std::string cmd = "which gdb";
#elif defined(__APPLE__) && defined(__MACH__)
std::string cmd = "which lldb";
#endif
FILE *output = popen(cmd.c_str(), "r");
std::array<char,256> buf;
fgets(buf.data(), buf.size(), output);
std::string dbgpath = std::string(buf.data());
dbgpath.pop_back(); // remove trailing newline
pclose(output);
arg_list.push_back(dbgpath);
/* Push debugger arguments */
#ifdef __unix__
arg_list.push_back("-q");
arg_list.push_back("-ex");
/* LD_PRELOAD must be set inside a gdb
* command to be effective */
std::string ldpreloadstr = "set exec-wrapper env 'LD_PRELOAD=";
ldpreloadstr += context->libtaspath;
if (!context->old_ld_preload.empty()) {
ldpreloadstr += ":";
ldpreloadstr += context->old_ld_preload;
}
ldpreloadstr += "'";
arg_list.push_back(ldpreloadstr);
/* We are using SIGSYS and SIGXFSZ for savestates, so don't
* print and pause when one signal is sent *
* Signals SIGPWR SIGXCPU SIG35 and SIG36 are used a lot in some games */
arg_list.push_back("-ex");
arg_list.push_back("handle SIGSYS SIGXFSZ SIGUSR1 SIGUSR2 SIGPWR SIGXCPU SIG35 SIG36 nostop noprint");
arg_list.push_back("-ex");
arg_list.push_back("run");
arg_list.push_back("--args");
#elif defined(__APPLE__) && defined(__MACH__)
arg_list.push_back("-o");
/* DYLD_INSERT_LIBRARIES must be set inside a lldb
* command to be effective */
std::string ldpreloadstr = "set se target.env-vars 'DYLD_INSERT_LIBRARIES=";
ldpreloadstr += context->libtaspath;
if (!context->old_ld_preload.empty()) {
ldpreloadstr += ":";
ldpreloadstr += context->old_ld_preload;
}
ldpreloadstr += "'";
arg_list.push_back(ldpreloadstr);
arg_list.push_back("-o");
arg_list.push_back("set se target.env-vars 'DYLD_FORCE_FLAT_NAMESPACE=1'");
/* We are using SIGSYS and SIGXFSZ for savestates, so don't
* print and pause when one signal is sent */
arg_list.push_back("-o");
arg_list.push_back("run");
/* Signal handling cannot be performed in llvm before the process has started */
// arg_list.push_back("-o");
// arg_list.push_back("process handle -n false -p false -s false SIGSYS SIGXFSZ SIGUSR1 SIGUSR2 SIGXCPU");
arg_list.push_back("--");
#endif
}
/* Tell SDL >= 2.0.2 to let us override functions even if it is statically linked.
* Does not work for wine games, because our custom SDL functions don't
* have the correct calling convention. */
setenv("SDL_DYNAMIC_API", context->libtaspath.c_str(), 1);
/* If MacOS app, insert the real executable */
if (macappflag) {
arg_list.push_back(extractMacOSExecutable(context->gamepath));
}
else {
arg_list.push_back(context->gamepath);
}
}
/* Argument string for sh */
std::ostringstream sharg;
/* Prepend LD_PRELOAD/DYLD_INSERT_LIBRARIES */
if (!(context->attach_gdb && (!((gameArch == BT_PE32) || (gameArch == BT_PE32P))))) {
/* Set the LD_PRELOAD/DYLD_INSERT_LIBRARIES environment variable to
* inject our lib to the game */
#ifdef __unix__
sharg << "LD_PRELOAD=";
#elif defined(__APPLE__) && defined(__MACH__)
sharg << "DYLD_INSERT_LIBRARIES=";
#endif
if (!context->old_ld_preload.empty()) {
sharg << context->libtaspath << ":" << context->old_ld_preload << " ";
}
else {
sharg << context->libtaspath << " ";
}
/* We need to set DYLD_FORCE_FLAT_NAMESPACE so that we can hook into the game */
#if defined(__APPLE__) && defined(__MACH__)
sharg << "DYLD_FORCE_FLAT_NAMESPACE=1 ";
#endif
}
/* Escape and concatenate arguments */
for (std::string arg : arg_list) {
/* Replace all occurrences of `'` with `'\'''` */
const std::string escape_string = "'\\''";
size_t pos = arg.find("'");
while(pos != std::string::npos) {
arg.replace(pos, 1, escape_string);
pos = arg.find("'", pos + escape_string.size());
}
/* Add to the argument string with enclosed `'` and space */
sharg << "'" << arg << "' ";
}
/* Append the game command-line arguments */
sharg << context->config.gameargs;
/* Run the actual game with sh, taking care of splitting arguments */
execlp("sh", "sh", "-c", sharg.str().c_str(), nullptr);
}