forked from clementgallet/libTAS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLoop.cpp
More file actions
796 lines (658 loc) · 26.9 KB
/
Copy pathGameLoop.cpp
File metadata and controls
796 lines (658 loc) · 26.9 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
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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
/*
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/>.
*/
#ifdef __unix__
#include "config.h"
#endif
#include "GameLoop.h"
#include "GameThread.h"
#include "GameEvents.h"
#ifdef __unix__
#include "GameEventsXcb.h"
#elif defined(__APPLE__) && defined(__MACH__)
#include "GameEventsQuartz.h"
#endif
#include "utils.h"
#include "AutoSave.h"
// #include "SaveState.h"
#include "SaveStateList.h"
#include "lua/Input.h"
#include "lua/Main.h"
#include "ramsearch/MemAccess.h"
#include "../shared/sockethelpers.h"
#include "../shared/SharedConfig.h"
#include "../shared/messages.h"
#include <string>
#include <iostream>
#include <sstream>
#include <cerrno>
#include <unistd.h> // fork()
#include <future>
#include <csignal> // kill
#include <sys/stat.h> // stat
#include <sys/wait.h> // waitpid
// #include <X11/X.h>
#include <stdint.h>
GameLoop::GameLoop(Context* c) : movie(MovieFile(c)), context(c)
{
#ifdef __unix__
gameEvents = new GameEventsXcb(c, &movie);
#elif defined(__APPLE__) && defined(__MACH__)
gameEvents = new GameEventsQuartz(c, &movie);
#endif
}
void GameLoop::start()
{
init();
initProcessMessages();
Lua::Main::callLua(context, "onStartup");
while (1)
{
bool exitMsg = startFrameMessages();
if (exitMsg) {
loopExit();
return;
}
emit uiChanged();
/* We are at a frame boundary */
/* If we did not yet receive the game window id, just make the game running */
bool endInnerLoop = false;
if (context->game_window ) do {
/* Check if game is still running */
int ret = waitpid(context->fork_pid, nullptr, WNOHANG);
if (ret == context->fork_pid) {
emit alertToShow(QString("Game was closed"));
loopExit();
return;
}
int eventFlag = gameEvents->handleEvent();
if (eventFlag & GameEvents::RETURN_FLAG_UPDATE)
emit uiChanged();
endInnerLoop = context->config.sc.running ||
(eventFlag & GameEvents::RETURN_FLAG_ADVANCE) ||
(context->status == Context::QUITTING);
if (!endInnerLoop) {
sleepSendPreview();
}
} while (!endInnerLoop);
AllInputs ai;
processInputs(ai);
prev_ai = ai;
/* Set the status to restart */
if (ai.flags & (1 << SingleInput::FLAG_RESTART)) {
context->status = Context::RESTARTING;
}
bool shouldQuit = false;
/* Pause if needed */
if ((context->pause_frame == (context->framecount + 1)) ||
((context->config.sc.recording != SharedConfig::NO_RECORDING) &&
((context->config.sc.movie_framecount + context->pause_frame) == (context->framecount + 1)))) {
if (!context->interactive) {
/* Quit at the end of the movie if non-interactive */
shouldQuit = true;
} else {
/* Disable pause */
context->pause_frame = 0;
/* Pause and disable fast-forward */
context->config.sc.running = false;
context->config.sc.fastforward = false;
context->config.sc_modified = true;
emit sharedConfigChanged();
}
}
Lua::Main::callLua(context, "onFrame");
endFrameMessages(ai);
if (shouldQuit) {
context->status = Context::QUITTING;
}
}
}
void GameLoop::init()
{
/* Unvalidate the game pid */
context->game_pid = 0;
/* Unvalidate the game window id */
context->game_window = 0;
/* Reset savestate flag */
context->didASavestate = false;
/* Reset the frame count if not restarting */
if (context->status != Context::RESTARTING)
context->framecount = 0;
/* Set the initial frame count for the game */
context->config.sc.initial_framecount = context->framecount;
/* Reset the rerecord count if not restarting */
if (context->status != Context::RESTARTING)
context->rerecord_count = 0;
/* Reset the encoding segment if not restarting */
if (context->status != Context::RESTARTING)
context->encoding_segment = 0;
/* Extract the game executable name from the game executable path */
context->gamename = fileFromPath(context->gamepath);
/* Clear the event queue and parameters */
gameEvents->init();
/* Remove savestates again in case we did not exist cleanly the previous time */
remove_savestates(context);
/* Remove the file socket */
int err = removeSocket(context->socket_filename);
if (err != 0)
emit alertToShow(QString("Could not remove socket file %1: %2").arg(context->socket_filename.c_str(), strerror(err)));
/* Init savestate list */
SaveStateList::init(context);
/* We fork here so that the child process calls the game */
context->fork_pid = fork();
if (context->fork_pid == 0) {
GameThread::launch(context);
}
/* Compute the MD5 hash of the game binary */
context->md5_game.clear();
std::ostringstream cmd;
cmd << "md5sum -b \"" << context->gamepath << "\" 2> /dev/null";
FILE *md5str = popen(cmd.str().c_str(), "r");
if (md5str != NULL) {
std::array<char,33> buf;
if (fgets(buf.data(), buf.size(), md5str) != nullptr) {
context->md5_game.assign(buf.data());
}
pclose(md5str);
}
/* Only open the movie if we did not restart */
if (context->status != Context::RESTARTING) {
/* Opening a movie, which imports the inputs and parameters if in read mode,
* or prepare a movie if in write mode.
*/
if (context->config.sc.recording == SharedConfig::RECORDING_READ) {
int ret = movie.loadMovie();
if (ret < 0) {
emit alertToShow(MovieFile::errorString(ret));
context->config.sc.recording = SharedConfig::NO_RECORDING;
}
else {
/* Update the UI accordingly */
emit configChanged();
}
/* Check md5 match */
if ((!context->md5_movie.empty()) && (context->md5_game.compare(context->md5_movie) != 0))
emit alertToShow(QString("Game executable hash does not match with the hash stored in the movie!"));
}
else {
movie.clear();
}
}
/* We must add a blank frame in most cases */
if (context->config.sc.recording == SharedConfig::RECORDING_WRITE) {
/* Add one blank frame in every movie corresponding to the input
* between the game startup and the first screen draw, which is for now
* impossible to set. Exception is if we restarted.
*/
if (context->framecount == movie.inputs->nbFrames()) {
AllInputs ai;
ai.emptyInputs();
movie.inputs->setInputs(ai, false);
}
}
/* Set the current time to the initial time, except when restarting */
if (context->status != Context::RESTARTING) {
context->current_time_sec = context->config.sc.initial_time_sec;
context->current_time_nsec = context->config.sc.initial_time_nsec;
}
/* If auto-restart is set, write back savefiles on game exit */
context->config.sc.write_savefiles_on_exit =
(context->config.sc.recording != SharedConfig::NO_RECORDING) &&
(context->config.auto_restart);
context->status = Context::ACTIVE;
emit statusChanged();
}
void GameLoop::initProcessMessages()
{
/* Connect to the socket between the program and the game */
initSocketProgram(context->socket_filename);
/* Receive informations from the game */
int message = receiveMessage();
while (message != MSGB_END_INIT) {
switch (message) {
/* Get the game process pid */
case MSGB_PID:
receiveData(&context->game_pid, sizeof(pid_t));
MemAccess::init(context->game_pid);
break;
case MSGB_GIT_COMMIT:
{
std::string lib_commit = receiveString();
#ifdef LIBTAS_INTERIM_COMMIT
std::string gui_commit = LIBTAS_INTERIM_COMMIT;
if (lib_commit.compare(gui_commit) != 0) {
std::cerr << "Interim commit of GUI (" << gui_commit << ") does not match commit of library (" << lib_commit << ")!" << std::endl;
}
#else
std::cerr << "Library has interim commit (" << lib_commit << ") but not GUI!" << std::endl;
#endif
}
break;
default:
// ui_print("Message init: unknown message\n");
loopExit();
return;
}
message = receiveMessage();
}
/* Send informations to the game */
/* Send shared config size */
sendMessage(MSGN_CONFIG_SIZE);
int config_size = sizeof(SharedConfig);
sendData(&config_size, sizeof(int));
/* Send shared config */
/* This is a bit hackish, change the initial time to the current time before
* sending so that the game gets the correct time after restarting. */
struct timespec it = {context->config.sc.initial_time_sec, context->config.sc.initial_time_nsec};
context->config.sc.initial_time_sec = context->current_time_sec;
context->config.sc.initial_time_nsec = context->current_time_nsec;
sendMessage(MSGN_CONFIG);
sendData(&context->config.sc, sizeof(SharedConfig));
context->config.sc.initial_time_sec = it.tv_sec;
context->config.sc.initial_time_nsec = it.tv_nsec;
/* Send dump file if dumping from the beginning */
if (context->config.sc.av_dumping) {
sendMessage(MSGN_DUMP_FILE);
sendString(context->config.dumpfile);
sendString(context->config.ffmpegoptions);
}
/* Build and send the base savestate path/index */
if (context->config.sc.savestate_settings & SharedConfig::SS_INCREMENTAL) {
sendMessage(MSGN_BASE_SAVESTATE_INDEX);
int index = 0;
sendData(&index, sizeof(int));
if (!(context->config.sc.savestate_settings & SharedConfig::SS_RAM)) {
std::string basesavestatepath = context->config.savestatedir + '/';
basesavestatepath += context->gamename;
basesavestatepath += ".state0";
sendMessage(MSGN_BASE_SAVESTATE_PATH);
sendString(basesavestatepath);
}
}
/* Send the Steam user data path and remote storage */
if (context->config.sc.virtual_steam) {
sendMessage(MSGN_STEAM_USER_DATA_PATH);
sendString(context->config.steamuserdir);
std::string remotestorage = context->config.steamuserdir + "/";
remotestorage += context->gamename;
if (create_dir(remotestorage) < 0) {
std::cerr << "Cannot create dir " << remotestorage << std::endl;
}
sendMessage(MSGN_STEAM_REMOTE_STORAGE);
sendString(remotestorage);
}
sendMessage(MSGN_ENCODING_SEGMENT);
sendData(&context->encoding_segment, sizeof(int));
/* End message */
sendMessage(MSGN_END_INIT);
}
bool GameLoop::startFrameMessages()
{
bool draw_frame = true;
/* Wait for frame boundary */
int message = receiveMessage();
while (message != MSGB_START_FRAMEBOUNDARY) {
switch (message) {
case MSGB_WINDOW_ID:
{
uint32_t int_window;
receiveData(&int_window, sizeof(uint32_t));
gameEvents->registerGameWindow(int_window);
break;
}
case MSGB_ALERT_MSG:
/* Ask the UI thread to display the alert. He is in charge of
* freeing the string.
*/
emit alertToShow(QString(receiveString().c_str()));
break;
case MSGB_ENCODE_FAILED:
context->config.sc.av_dumping = false;
context->config.sc_modified = true;
emit sharedConfigChanged();
break;
case MSGB_FRAMECOUNT_TIME:
receiveData(&context->framecount, sizeof(uint64_t));
receiveData(&context->current_time_sec, sizeof(uint64_t));
receiveData(&context->current_time_nsec, sizeof(uint64_t));
if (context->config.sc.recording == SharedConfig::RECORDING_WRITE) {
/* If the input editor is opened, recording does not truncate inputs */
bool notTruncInputs = false;
emit isInputEditorVisible(notTruncInputs);
if (!notTruncInputs || (context->framecount > context->config.sc.movie_framecount)) {
context->config.sc.movie_framecount = context->framecount;
context->movie_time_sec = context->current_time_sec - context->config.sc.initial_time_sec;
context->movie_time_nsec = context->current_time_nsec - context->config.sc.initial_time_nsec;
if (context->movie_time_nsec < 0) {
context->movie_time_nsec += 1000000000;
context->movie_time_sec--;
}
}
}
break;
case MSGB_GAMEINFO:
receiveData(&context->game_info, sizeof(context->game_info));
emit gameInfoChanged(context->game_info);
break;
case MSGB_FPS:
receiveData(&context->fps, sizeof(float));
receiveData(&context->lfps, sizeof(float));
break;
case MSGB_ENCODING_SEGMENT:
receiveData(&context->encoding_segment, sizeof(int));
break;
case MSGB_INVALIDATE_SAVESTATES:
/* Only save a backtrack savestate if we did at least one savestate.
* This prevent incremental savestating from being inefficient if a
* backtrack savestate is performed at the very beginning of the game.
*/
if ((context->config.sc.savestate_settings & SharedConfig::SS_BACKTRACK) && context->didASavestate)
context->hotkey_pressed_queue.push(HOTKEY_SAVESTATE_BACKTRACK);
/* Invalidate all savestates */
SaveStateList::invalidate();
/* Notify the input editor so it can show it to users */
emit invalidateSavestates();
break;
case MSGB_GETTIME_BACKTRACE:
{
int type;
receiveData(&type, sizeof(int));
uint64_t hash;
receiveData(&hash, sizeof(uint64_t));
std::string trace = receiveString();
emit getTimeTrace(type, static_cast<unsigned long long>(hash), trace);
}
break;
case MSGB_NONDRAW_FRAME:
draw_frame = false;
break;
case MSGB_QUIT:
if (!context->interactive) {
/* Exit the program when game has exit */
exit(0);
}
return true;
case -1:
std::cerr << "The connection to the game was lost. Exiting" << std::endl;
return true;
case -2:
std::cerr << "The connection to the game was closed. Exiting" << std::endl;
return true;
default:
std::cerr << "Got unknown message!!!" << std::endl;
return true;
}
message = receiveMessage();
}
/* Store in movie and indicate the input editor if the current frame
* is a draw frame or not */
movie.editor->setDraw(draw_frame);
/* Send ram watches */
if (context->config.sc.osd & SharedConfig::OSD_RAMWATCHES) {
std::string ramwatch;
emit getRamWatch(ramwatch);
while(!ramwatch.empty()) {
sendMessage(MSGN_RAMWATCH);
sendString(ramwatch);
emit getRamWatch(ramwatch);
}
}
/* Execute the lua callback onPaint here */
Lua::Main::callLua(context, "onPaint");
sendMessage(MSGN_START_FRAMEBOUNDARY);
return false;
}
void GameLoop::sleepSendPreview()
{
/* Sleep a bit to not surcharge the processor */
struct timespec tim = {0, 17L*1000L*1000L};
nanosleep(&tim, NULL);
/* Send a preview of inputs so that the game can display them
* on the HUD */
#ifdef LIBTAS_ENABLE_HUD
/* Don't preview when reading inputs */
if (context->config.sc.recording == SharedConfig::RECORDING_READ)
return;
/* Only preview if we actually print inputs */
if (!(context->config.sc.osd & SharedConfig::OSD_INPUTS))
return;
static AllInputs preview_ai, last_preview_ai;
if (gameEvents->haveFocus()) {
/* Format the keyboard and mouse state and save it in the AllInputs struct */
context->config.km->buildAllInputs(preview_ai, context->game_window, context->config.sc, false);
}
/* Fill controller inputs from the controller input window. */
emit fillControllerInputs(preview_ai);
/* Send inputs if changed */
if (!(preview_ai == last_preview_ai)) {
sendMessage(MSGN_PREVIEW_INPUTS);
sendData(&preview_ai, sizeof(AllInputs));
last_preview_ai = preview_ai;
}
#endif
}
void GameLoop::processInputs(AllInputs &ai)
{
ai.emptyInputs();
/* Set framerate numbers here to prevent potential errors */
if (context->config.sc.variable_framerate) {
ai.framerate_num = context->config.sc.framerate_num;
ai.framerate_den = context->config.sc.framerate_den;
}
/* Don't record inputs if we are quitting */
if (context->status == Context::QUITTING)
return;
/* Record inputs or get inputs from movie file */
switch (context->config.sc.recording) {
case SharedConfig::NO_RECORDING:
case SharedConfig::RECORDING_WRITE:
/* Get inputs if we have input focus */
if (gameEvents->haveFocus()) {
/* Format the keyboard and mouse state and save it in the AllInputs struct */
context->config.km->buildAllInputs(ai, context->game_window, context->config.sc, context->config.mouse_warp);
}
/* Fill controller inputs from the controller input window. */
emit fillControllerInputs(ai);
/* Add framerate if necessary */
if (context->config.sc.variable_framerate) {
ai.framerate_num = context->config.sc.framerate_num;
ai.framerate_den = context->config.sc.framerate_den;
}
/* Call lua onInput() here so that a script can modify inputs */
Lua::Input::registerInputs(&ai);
Lua::Main::callLua(context, "onInput");
if (context->config.sc.recording == SharedConfig::RECORDING_WRITE) {
/* If the input editor is visible, we should keep future inputs.
* If not, we truncate inputs if necessary.
*/
bool keep_inputs = false;
bool past_inputs = context->framecount < movie.inputs->nbFrames();
emit isInputEditorVisible(keep_inputs);
/* Send signal before saving the input */
if (past_inputs) {
if (keep_inputs) {
emit inputsToBeEdited();
}
else {
emit inputsToBeChanged();
}
}
else {
emit inputsToBeAdded();
}
/* If some inputs are locked, copy the inputs from the movie.
* Only do this if the input editor is opened, and if there are
* inputs to copy. */
if (keep_inputs && past_inputs)
movie.setLockedInputs(ai);
/* Save inputs to moviefile */
movie.inputs->setInputs(ai, keep_inputs);
/* Send signal after saving the input */
if (past_inputs) {
if (keep_inputs) {
emit inputsEdited();
}
else {
emit inputsChanged();
}
}
else {
emit inputsAdded();
}
AutoSave::update(context, movie);
}
break;
case SharedConfig::RECORDING_READ:
/* Read inputs from file */
int ret = movie.inputs->getInputs(ai);
if (ret == 1) {
/* We are reading the last frame of the movie */
switch(context->config.on_movie_end) {
case Config::MOVIEEND_READ:
break;
case Config::MOVIEEND_WRITE:
context->config.sc.recording = SharedConfig::RECORDING_WRITE;
context->config.sc_modified = true;
emit sharedConfigChanged();
break;
default:
break;
}
}
if (ret >= 0) { // read succeeded
/* Update framerate */
if (context->config.sc.variable_framerate &&
((context->config.sc.framerate_num != ai.framerate_num) ||
(context->config.sc.framerate_den != ai.framerate_den))) {
context->config.sc.framerate_num = ai.framerate_num;
context->config.sc.framerate_den = ai.framerate_den;
emit updateFramerate();
}
}
else {
/* ai is empty, fill the framerate values */
ai.framerate_num = context->config.sc.framerate_num;
ai.framerate_den = context->config.sc.framerate_den;
/* First frame after movie end */
if (ret == -2) {
/* Check for the moviefile length */
int64_t cur_sec, cur_nsec;
cur_sec = context->current_time_sec - context->config.sc.initial_time_sec;
cur_nsec = context->current_time_nsec - context->config.sc.initial_time_nsec;
if ((context->movie_time_sec != -1) &&
((context->movie_time_sec != cur_sec) ||
(context->movie_time_nsec != cur_nsec))) {
emit alertToShow(QString("Movie length mismatch. Metadata stores %1.%2 seconds but end time is %3.%4 seconds.").arg(context->movie_time_sec).arg(context->movie_time_nsec, 9, 10, QChar('0')).arg(cur_sec).arg(cur_nsec, 9, 10, QChar('0')));
}
context->movie_time_sec = cur_sec;
context->movie_time_nsec = cur_nsec;
}
}
/* Call lua onInput() here so that a script can modify inputs */
Lua::Input::registerInputs(&ai);
Lua::Main::callLua(context, "onInput");
/* Update controller inputs if controller window is shown */
emit showControllerInputs(ai);
AutoSave::update(context, movie);
break;
}
}
void GameLoop::endFrameMessages(AllInputs &ai)
{
/* If the user stopped the game with the Stop button, don't write back
* savefiles.*/
if (context->status == Context::QUITTING) {
context->config.sc.write_savefiles_on_exit = false;
context->config.sc_modified = true;
}
/* If the game was restarted, write back savefiles.*/
if (context->status == Context::RESTARTING) {
context->config.sc.write_savefiles_on_exit = true;
context->config.sc_modified = true;
}
/* Send shared config if modified */
if (context->config.sc_modified) {
/* Send config */
sendMessage(MSGN_CONFIG);
sendData(&context->config.sc, sizeof(SharedConfig));
context->config.sc_modified = false;
}
/* Send dump file if modified */
if (context->config.dumpfile_modified) {
sendMessage(MSGN_DUMP_FILE);
sendString(context->config.dumpfile);
sendString(context->config.ffmpegoptions);
context->config.dumpfile_modified = false;
}
/* Send inputs and end of frame */
sendMessage(MSGN_ALL_INPUTS);
sendData(&ai, sizeof(AllInputs));
if ((context->status == Context::QUITTING) || (context->status == Context::RESTARTING)) {
sendMessage(MSGN_USERQUIT);
}
sendMessage(MSGN_END_FRAMEBOUNDARY);
}
void GameLoop::loopExit()
{
/* Unvalidate the game pid */
context->game_pid = 0;
/* We need to restart the game if we got a restart input, or if:
* - auto-restart is set
* - we are playing or recording a movie
* - the user didn't use the Stop button to stop the game
*/
if ((context->status == Context::RESTARTING) ||
(context->config.auto_restart &&
(context->config.sc.recording != SharedConfig::NO_RECORDING) &&
(context->status != Context::QUITTING))) {
/* We keep the movie opened and indicate the main thread to restart the game */
closeSocket();
/* Remove savestates because they are invalid on future instances of the game */
remove_savestates(context);
/* Backup savestate movies on disk */
SaveStateList::backupMovies();
/* wait on the game process to terminate */
wait(nullptr);
context->status = Context::RESTARTING;
emit statusChanged();
return;
}
if (movie.inputs->modifiedSinceLastSave) {
/* Ask the user if he wants to save the movie, and get the answer.
* Prompting a alert window must be done by the UI thread, so we are
* using std::future/std::promise mechanism.
*/
std::promise<bool> saveAnswer;
std::future<bool> futureSave = saveAnswer.get_future();
emit askToShow(QString("Do you want to save the movie file?"), &saveAnswer);
if (futureSave.get()) {
/* User answered yes */
movie.saveMovie();
}
}
movie.close();
closeSocket();
/* Remove savestates because they are invalid on future instances of the game */
remove_savestates(context);
/* Backup savestate movies on disk */
SaveStateList::backupMovies();
/* Unvalidate game pid */
MemAccess::init(0);
/* Reset the frame count */
context->framecount = 0;
/* wait on the game process to terminate */
wait(nullptr);
context->status = Context::INACTIVE;
emit statusChanged();
}