-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathhooks.cpp
842 lines (741 loc) · 25 KB
/
hooks.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
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#include "ai.h"
#include "hooks.h"
#include "camera.h"
#include "tinythread.h"
#include "debug.h"
#include "MemAccess.h"
#include "SDL_events.h"
#include "modules/Gui.h"
#include "modules/Screen.h"
#include "df/enabler.h"
#include "df/graphic.h"
#include "df/init.h"
#include "df/interfacest.h"
#include "df/renderer.h"
#include "df/viewscreen_movieplayerst.h"
#include <zlib.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <sys/mman.h>
#include <sys/time.h>
#endif
REQUIRE_GLOBAL(enabler);
REQUIRE_GLOBAL(gps);
REQUIRE_GLOBAL(gview);
REQUIRE_GLOBAL(init);
REQUIRE_GLOBAL(ui);
extern "C" DFHACK_IMPORT uint32_t SDL_GetTicks(void);
extern "C" DFHACK_IMPORT int SDL_SemTryWait(void *sem);
extern "C" DFHACK_IMPORT uint32_t SDL_ThreadID(void);
bool lockstep_tick_count_forced = false;
volatile uint32_t lockstep_tick_count = 0;
volatile bool lockstep_hooked = false;
volatile bool disabling_plugin = false;
volatile bool unloading_plugin = false;
extern bool & enabled;
static volatile bool lockstep_want_shutdown = false;
static volatile bool lockstep_ready_for_shutdown = false;
static volatile bool lockstep_want_shutdown_now = false;
#ifdef _WIN32
static uint8_t Real_GetTickCount[6];
static DWORD WINAPI Fake_GetTickCount(void)
{
return lockstep_tick_count;
}
#else
static uint8_t Real_gettimeofday[6];
static int Fake_gettimeofday(struct timeval *tv, struct timezone *) throw()
{
tv->tv_sec = lockstep_tick_count / 1000;
tv->tv_usec = (lockstep_tick_count % 1000) * 1000;
return 0;
}
#endif
static uint8_t Real_SDL_GetTicks[6];
static uint32_t Fake_SDL_GetTicks()
{
return lockstep_tick_count;
}
static void Add_Hook(void *target, uint8_t(&real)[6], const void *fake)
{
MemoryPatcher patcher;
patcher.verifyAccess(target, 6, true);
int32_t jump_offset((uint8_t *)fake - (uint8_t *)target - 4 - 1);
real[0] = *((uint8_t *)target + 0);
real[1] = *((uint8_t *)target + 1);
real[2] = *((uint8_t *)target + 2);
real[3] = *((uint8_t *)target + 3);
real[4] = *((uint8_t *)target + 4);
real[5] = *((uint8_t *)target + 5);
*((uint8_t *)target + 0) = 0xE9;
*((uint8_t *)target + 1) = *((uint8_t *)&jump_offset + 0);
*((uint8_t *)target + 2) = *((uint8_t *)&jump_offset + 1);
*((uint8_t *)target + 3) = *((uint8_t *)&jump_offset + 2);
*((uint8_t *)target + 4) = *((uint8_t *)&jump_offset + 3);
*((uint8_t *)target + 5) = 0x90;
#if defined(_WIN32)
FlushInstructionCache(GetCurrentProcess(), target, 6);
#endif
}
static void Remove_Hook(void *target, uint8_t(&real)[6], const void *)
{
MemoryPatcher patcher;
patcher.verifyAccess(target, 6, true);
*((uint8_t *)target + 0) = real[0];
*((uint8_t *)target + 1) = real[1];
*((uint8_t *)target + 2) = real[2];
*((uint8_t *)target + 3) = real[3];
*((uint8_t *)target + 4) = real[4];
*((uint8_t *)target + 5) = real[5];
#if defined(_WIN32)
FlushInstructionCache(GetCurrentProcess(), target, 6);
#endif
}
template<typename T>
inline static void swap3(T & a, T & b, T & c)
{
a = b;
b = c;
c = a;
}
static void lockstep_swap_arrays() {
auto & r = enabler->renderer;
swap3(r->screen, r->screen_old, gps->screen);
swap3(r->screentexpos, r->screentexpos_old, gps->screentexpos);
swap3(r->screentexpos_addcolor, r->screentexpos_addcolor_old, gps->screentexpos_addcolor);
swap3(r->screentexpos_grayscale, r->screentexpos_grayscale_old, gps->screentexpos_grayscale);
swap3(r->screentexpos_cf, r->screentexpos_cf_old, gps->screentexpos_cf);
swap3(r->screentexpos_cbr, r->screentexpos_cbr_old, gps->screentexpos_cbr);
gps->screen_limit = gps->screen + gps->dimx * gps->dimy * 4;
}
static void lockstep_render_things()
{
//GRAB CURRENT SCREEN AT THE END OF THE LIST
df::viewscreen *currentscreen = &gview->view;
while (currentscreen->child != nullptr)
{
currentscreen = currentscreen->child;
}
//NO INTERFACE LEFT, LEAVE
if (currentscreen == &gview->view)
{
return;
}
if (currentscreen->breakdown_level == interface_breakdown_types::NONE)
{
currentscreen->render();
}
else
{
memset(gps->screen, 0, gps->dimx * gps->dimy * 4);
memset(gps->screentexpos, 0, gps->dimx * gps->dimy * sizeof(long));
}
// don't render REC/PLAY/FPS indicators
}
static void lockstep_removescreen(df::viewscreen *scr)
{
// df-ai: omitted minimap refreshing code as we don't use the minimap and we don't have the function symbols on Windows
//FIX LINKS
if (scr->parent)
{
scr->parent->child = scr->child;
}
if (scr->child)
{
scr->child->parent = scr->parent;
}
//WASTE SCREEN
delete scr;
}
static void lockstep_remove_to_first()
{
//GRAB LAST SCREEN AT THE END OF THE LIST
df::viewscreen *lastscreen = &gview->view;
while (lastscreen->child)
{
lastscreen = lastscreen->child;
}
//NO INTERFACE LEFT
if (lastscreen == &gview->view)
{
return;
}
//GO AHEAD
while (lastscreen->parent != &gview->view)
{
df::viewscreen *par = lastscreen->parent;
lockstep_removescreen(lastscreen);
lastscreen = par;
}
}
static constexpr int32_t MOVIEBUFFSIZE = sizeof(df::interfacest::supermoviebuffer) / sizeof(df::interfacest::supermoviebuffer[0]);
static constexpr int32_t COMPMOVIEBUFFSIZE = sizeof(df::interfacest::supermoviebuffer_comp) / sizeof(df::interfacest::supermoviebuffer_comp[0]);
static BOOST_NOINLINE bool zlibError(int err, const char* msg)
{
std::cerr << "zlib error (CMV \"" << gview->movie_file << "\" is probably corrupted): " << msg << ": " << zError(err) << std::endl;
return false;
}
#define CHECK_ERR(err, msg) BOOST_UNLIKELY(((err) != Z_OK) && zlibError(err, msg))
static int32_t lockstep_write_movie_chunk()
{
int32_t inputsize = gview->supermovie_pos;
if (BOOST_UNLIKELY(inputsize > MOVIEBUFFSIZE))
{
inputsize = MOVIEBUFFSIZE;
}
//DUMP CURRENT BUFFER INTO A COMPRESSION STREAM
z_stream c_stream;
int err;
c_stream.zalloc = (alloc_func)0;
c_stream.zfree = (free_func)0;
c_stream.opaque = (voidpf)0;
err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
if (CHECK_ERR(err, "deflateInit"))
{
return 5000001;
}
c_stream.next_out = (Bytef*)gview->supermoviebuffer_comp;
c_stream.avail_out = COMPMOVIEBUFFSIZE;
c_stream.next_in = (Bytef*)gview->supermoviebuffer;
c_stream.avail_in = inputsize;
while (int32_t(c_stream.total_in) != inputsize && c_stream.total_out < COMPMOVIEBUFFSIZE)
{
//c_stream.avail_in = c_stream.avail_out = 1; // force small buffers
err = deflate(&c_stream, Z_NO_FLUSH);
if (CHECK_ERR(err, "deflate"))
{
return 5000001;
}
}
// Finish the stream, still forcing small buffers:
for (;;)
{
err = deflate(&c_stream, Z_FINISH);
if (err == Z_STREAM_END)
{
break;
}
if (CHECK_ERR(err, "deflate"))
{
return 5000001;
}
}
err = deflateEnd(&c_stream);
if (CHECK_ERR(err, "deflateEnd"))
{
return 5000001;
}
int length = 0;
if (c_stream.total_out > 0)
{
if (BOOST_UNLIKELY(gview->first_movie_write))
{
//GET RID OF ANY EXISTING MOVIES IF THIS IS THE FIRST TIME THROUGH
unlink(gview->movie_file.c_str());
}
//OPEN UP THE MOVIE FILE AND APPEND
std::ofstream f(gview->movie_file, std::ios::out | std::ios::binary | std::ios::app);
if (BOOST_LIKELY(f.is_open()))
{
//WRITE A HEADER
if (BOOST_UNLIKELY(gview->first_movie_write))
{
const int32_t movie_version = 10000;
f.write((const char *)&movie_version, sizeof(int32_t));
int32_t header[3];
header[0] = init->display.grid_x;
header[1] = init->display.grid_y;
extern std::unique_ptr<AI> dwarfAI;
if (BOOST_LIKELY(dwarfAI->camera.movie_started_in_lockstep))
{
header[1] *= 2;
}
header[2] = gview->supermovie_delayrate;
f.write((const char *)&header, sizeof(header));
}
//WRITE IT
uint32_t compsize = c_stream.total_out;
f.write((const char *)&compsize, sizeof(uint32_t));
f.write((const char *)gview->supermoviebuffer_comp, c_stream.total_out);
f.seekp(0, std::ios::end);
length = f.tellp();
}
else
{
gview->supermovie_on = 0;
}
gview->first_movie_write = 0;
}
return length;
}
static void lockstep_finish_movie()
{
gview->supermovie_on = 0;
gview->currentblocksize = 0;
gview->nextfilepos = 0;
gview->supermovie_pos = 0;
extern std::unique_ptr<AI> dwarfAI;
dwarfAI->camera.check_record_status();
}
static void lockstep_handlemovie(bool flushall)
{
//SAVE A MOVIE FRAME INTO THE CURRENT MOVIE BUFFER
if (gview->supermovie_on == 1)
{
if (BOOST_UNLIKELY(gview->supermovie_delaystep > 0 && !flushall))
{
gview->supermovie_delaystep--;
}
else
{
if (BOOST_LIKELY(!flushall))
{
gview->supermovie_delaystep = gview->supermovie_delayrate;
}
if (!flushall || gview->supermovie_delaystep == 0)
{
extern std::unique_ptr<AI> dwarfAI;
//SAVING CHARACTERS, THEN COLORS
short x2, y2;
for (x2 = 0; x2 < init->display.grid_x; x2++)
{
for (y2 = 0; y2 < init->display.grid_y; y2++)
{
gview->supermoviebuffer[gview->supermovie_pos] = gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 0];
gview->supermovie_pos++;
}
if (BOOST_LIKELY(dwarfAI && dwarfAI->camera.movie_started_in_lockstep))
{
for (y2 = 0; y2 < init->display.grid_y; y2++)
{
gview->supermoviebuffer[gview->supermovie_pos] = x2 < 80 && y2 < 25 ? dwarfAI->lockstep_log_buffer[y2][x2] : ' ';
gview->supermovie_pos++;
}
}
}
char frame_col;
for (x2 = 0; x2 < init->display.grid_x; x2++)
{
for (y2 = 0; y2 < init->display.grid_y; y2++)
{
frame_col = gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 1];
frame_col |= (gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 2] << 3);
if (gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 3])
{
frame_col |= 64;
}
gview->supermoviebuffer[gview->supermovie_pos] = frame_col;
gview->supermovie_pos++;
}
if (BOOST_LIKELY(dwarfAI && dwarfAI->camera.movie_started_in_lockstep))
{
for (y2 = 0; y2 < init->display.grid_y; y2++)
{
gview->supermoviebuffer[gview->supermovie_pos] = y2 < 25 ? dwarfAI->lockstep_log_color[y2] : 7;
gview->supermovie_pos++;
}
}
}
}
int frame_size = init->display.grid_x * init->display.grid_y * 2;
extern std::unique_ptr<AI> dwarfAI;
if (BOOST_LIKELY(dwarfAI->camera.movie_started_in_lockstep))
{
frame_size *= 2;
}
if (BOOST_UNLIKELY(gview->supermovie_pos + frame_size >= MOVIEBUFFSIZE || flushall || !dwarfAI || !dwarfAI->camera.movie_started_in_lockstep))
{
int length = lockstep_write_movie_chunk();
if (length > 5000000 || dwarfAI == nullptr || !dwarfAI->camera.movie_started_in_lockstep)
{
lockstep_finish_movie();
}
else
{
gview->supermovie_pos = 0;
}
}
}
}
}
static bool lockstep_mainloop()
{
//NO INTERFACE LEFT, QUIT
if (BOOST_UNLIKELY(gview->view.child == 0))
{
return true;
}
//GRAB CURRENT SCREEN AT THE END OF THE LIST
df::viewscreen *currentscreen = Gui::getCurViewscreen(false);
//MOVE SCREENS BACK
switch (currentscreen->breakdown_level) {
case interface_breakdown_types::NONE:
{
currentscreen->logic();
if (currentscreen->movies_okay())
{
//HANDLE MOVIES
lockstep_handlemovie(false);
}
// df-ai: don't process input
break;
}
case interface_breakdown_types::QUIT:
{
lockstep_handlemovie(true);
return true;
}
case interface_breakdown_types::STOPSCREEN:
{
if (currentscreen->movies_okay())
{
//HANDLE MOVIES
lockstep_handlemovie(false);
}
lockstep_removescreen(currentscreen);
break;
}
case interface_breakdown_types::TOFIRST:
{
if (currentscreen->movies_okay())
{
//HANDLE MOVIES
lockstep_handlemovie(false);
}
lockstep_remove_to_first();
break;
}
}
return 0;
}
static bool lockstep_drain_sdl()
{
SDL::Event event;
while (SDL_PollEvent(&event))
{
DFAI_DEBUG(lockstep, 1, "event: " << event.type);
// Handle SDL events
switch ((SDL::EventType)event.type) {
case SDL::ET_KEYDOWN:
case SDL::ET_KEYUP:
break;
case SDL::ET_QUIT:
return true;
case SDL::ET_MOUSEBUTTONDOWN:
case SDL::ET_MOUSEBUTTONUP:
case SDL::ET_MOUSEMOTION:
break;
case SDL::ET_ACTIVEEVENT:
case SDL::ET_VIDEOEXPOSE:
break;
case SDL::ET_VIDEORESIZE:
enabler->renderer->resize(event.resize.w, event.resize.h);
break;
default:
break;
}
}
return false;
}
static bool Hook_Want_Disable()
{
if (BOOST_UNLIKELY(!config.lockstep || !enabled || disabling_plugin || unloading_plugin))
{
lockstep_want_shutdown = true;
}
return lockstep_want_shutdown;
}
static void lockstep_loop()
{
CoreSuspendClaimMain claimMain;
while (BOOST_LIKELY(!Hook_Want_Disable()))
{
DFAI_DEBUG(lockstep, 1, "calling mainloop (A)");
if (BOOST_UNLIKELY(lockstep_mainloop()))
{
DFAI_DEBUG(lockstep, 1, "want shutdown (A)");
Hook_Shutdown_Now();
break;
}
DFAI_DEBUG(lockstep, 1, "calling DFHack (A)");
SDL_NumJoysticks();
if (BOOST_UNLIKELY(Hook_Want_Disable()))
{
DFAI_DEBUG(lockstep, 1, "want disable (A)");
break;
}
lockstep_tick_count += 10;
if (BOOST_LIKELY(!enabler->flag.bits.maxfps && !ui->main.autosave_request))
{
DFAI_DEBUG(lockstep, 1, "calling mainloop (B)");
if (BOOST_UNLIKELY(lockstep_mainloop()))
{
DFAI_DEBUG(lockstep, 1, "want shutdown (B)");
Hook_Shutdown_Now();
break;
}
DFAI_DEBUG(lockstep, 1, "calling DFHack (B)");
SDL_NumJoysticks();
if (BOOST_UNLIKELY(Hook_Want_Disable()))
{
DFAI_DEBUG(lockstep, 1, "want disable (B)");
break;
}
}
else
{
DFAI_DEBUG(lockstep, 1, (enabler->flag.bits.maxfps ? "maxfps" : "autosave_request"));
}
lockstep_tick_count += 10;
enabler->last_tick = lockstep_tick_count;
enabler->clock = lockstep_tick_count;
DFAI_DEBUG(lockstep, 1, "draining events");
if (BOOST_UNLIKELY(lockstep_drain_sdl()))
{
DFAI_DEBUG(lockstep, 1, "user requested quit");
break;
}
DFAI_DEBUG(lockstep, 1, "swap_arrays");
lockstep_swap_arrays();
DFAI_DEBUG(lockstep, 1, "render_things");
lockstep_render_things();
DFAI_DEBUG(lockstep, 1, "update_all");
enabler->renderer->update_all();
DFAI_DEBUG(lockstep, 1, "render");
enabler->renderer->render();
}
// make sure we don't end up with a negative number of queued frames
enabler->outstanding_gframes = 1;
extern std::unique_ptr<AI> dwarfAI;
if (dwarfAI && dwarfAI->camera.movie_started_in_lockstep)
{
DFAI_DEBUG(lockstep, 1, "stopping movie started in lockstep to avoid corruption");
// Stop the current CMV so it doesn't get corrupted on the next frame.
lockstep_handlemovie(true);
}
}
static struct df_ai_renderer : public df::renderer
{
df_ai_renderer(df::renderer *real) : real_renderer(real)
{
copy_fields(real_renderer, this);
}
df::renderer *real_renderer;
static void copy_fields(df::renderer *from, df::renderer *to)
{
to->screen = from->screen;
to->screentexpos = from->screentexpos;
to->screentexpos_addcolor = from->screentexpos_addcolor;
to->screentexpos_grayscale = from->screentexpos_grayscale;
to->screentexpos_cf = from->screentexpos_cf;
to->screentexpos_cbr = from->screentexpos_cbr;
to->screen_old = from->screen_old;
to->screentexpos_old = from->screentexpos_old;
to->screentexpos_addcolor_old = from->screentexpos_addcolor_old;
to->screentexpos_grayscale_old = from->screentexpos_grayscale_old;
to->screentexpos_cf_old = from->screentexpos_cf_old;
to->screentexpos_cbr_old = from->screentexpos_cbr_old;
}
virtual void update_tile(int32_t x, int32_t y)
{
copy_fields(this, real_renderer);
real_renderer->update_tile(x, y);
copy_fields(real_renderer, this);
}
virtual void update_all()
{
copy_fields(this, real_renderer);
real_renderer->update_all();
copy_fields(real_renderer, this);
}
virtual void render()
{
copy_fields(this, real_renderer);
real_renderer->render();
copy_fields(real_renderer, this);
}
virtual void set_fullscreen()
{
copy_fields(this, real_renderer);
real_renderer->set_fullscreen();
copy_fields(real_renderer, this);
}
virtual void zoom(df::zoom_commands)
{
copy_fields(this, real_renderer);
enabler->renderer = real_renderer;
DFAI_DEBUG(lockstep, 1, "lockstep_loop started");
lockstep_loop();
DFAI_DEBUG(lockstep, 1, "lockstep_loop ended");
lockstep_ready_for_shutdown = true;
}
virtual void resize(int32_t w, int32_t h)
{
copy_fields(this, real_renderer);
real_renderer->resize(w, h);
copy_fields(real_renderer, this);
}
virtual void grid_resize(int32_t w, int32_t h)
{
copy_fields(this, real_renderer);
real_renderer->grid_resize(w, h);
copy_fields(real_renderer, this);
}
virtual bool get_mouse_coords(int32_t *x, int32_t *y)
{
copy_fields(this, real_renderer);
bool res = real_renderer->get_mouse_coords(x, y);
copy_fields(real_renderer, this);
return res;
};
virtual bool uses_opengl()
{
copy_fields(this, real_renderer);
bool res = real_renderer->uses_opengl();
copy_fields(real_renderer, this);
return res;
}
} *lockstep_renderer = nullptr;
void Hook_Update()
{
if (BOOST_LIKELY(SDL_ThreadID() == enabler->renderer_threadid))
{
return;
}
if (lockstep_want_shutdown)
{
while (!lockstep_ready_for_shutdown)
{
tthread::this_thread::yield();
}
DFAI_DEBUG(lockstep, 1, "trying to unhook");
lockstep_want_shutdown = false;
lockstep_ready_for_shutdown = false;
Hook_Shutdown();
}
else if (!lockstep_hooked && config.lockstep && !Hook_Want_Disable() && !lockstep_want_shutdown_now)
{
DFAI_DEBUG(lockstep, 1, "trying to hook");
if (init->display.flag.is_set(init_display_flags::TEXT))
{
auto & out = Core::getInstance().getConsole();
out << COLOR_LIGHTRED << "[df-ai] lockstep mode does not work with PRINT_MODE:TEXT. Disabling lockstep in the df-ai config.";
out << COLOR_RESET << std::endl;
config.set(out, config.lockstep, false);
return;
}
if (strict_virtual_cast<df::viewscreen_movieplayerst>(Gui::getCurViewscreen(false)))
{
DFAI_DEBUG(lockstep, 1, "not hooking while movie player is present");
return;
}
lockstep_hooked = true;
#ifdef _WIN32
if (!lockstep_tick_count_forced)
{
lockstep_tick_count = GetTickCount();
}
DFAI_DEBUG(lockstep, 1, "initial lockstep_tick_count is " << lockstep_tick_count);
Add_Hook((void *)GetTickCount, Real_GetTickCount, (void *)Fake_GetTickCount);
#else
struct timeval tv;
gettimeofday(&tv, nullptr);
if (!lockstep_tick_count_forced)
{
lockstep_tick_count = tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
DFAI_DEBUG(lockstep, 1, "initial lockstep_tick_count is " << lockstep_tick_count);
Add_Hook((void *)gettimeofday, Real_gettimeofday, (void *)Fake_gettimeofday);
#endif
DFAI_DEBUG(lockstep, 1, "hooked time");
Add_Hook((void *)SDL_GetTicks, Real_SDL_GetTicks, (void *)Fake_SDL_GetTicks);
DFAI_DEBUG(lockstep, 1, "hooked ticks");
enabler->outstanding_gframes = 0;
enabler->outstanding_frames = 0;
lockstep_renderer = new df_ai_renderer(enabler->renderer);
enabler->renderer = lockstep_renderer;
CoreSuspendReleaseMain releaser;
SDL_SemWait(enabler->async_zoom.sem);
enabler->async_zoom.queue.push_back(zoom_commands::zoom_reset);
SDL_SemPost(enabler->async_zoom.sem);
SDL_SemPost(enabler->async_zoom.sem_fill);
while (lockstep_hooked && !lockstep_want_shutdown)
{
while (SDL_SemTryWait(enabler->async_tobox.sem_fill) == 0)
{
SDL_SemWait(enabler->async_tobox.sem);
auto msg = enabler->async_tobox.queue.front();
enabler->async_tobox.queue.pop_front();
switch (msg.cmd)
{
case df::enabler::T_async_tobox::T_queue::pause:
case df::enabler::T_async_tobox::T_queue::render:
{
df::enabler::T_async_frombox::T_queue complete;
complete.msg = df::enabler::T_async_frombox::T_queue::complete;
SDL_SemWait(enabler->async_frombox.sem);
enabler->async_frombox.queue.push_back(complete);
SDL_SemPost(enabler->async_frombox.sem);
SDL_SemPost(enabler->async_frombox.sem_fill);
break;
}
case df::enabler::T_async_tobox::T_queue::start:
case df::enabler::T_async_tobox::T_queue::inc:
case df::enabler::T_async_tobox::T_queue::set_fps:
break;
}
SDL_SemPost(enabler->async_tobox.sem);
}
tthread::this_thread::sleep_for(tthread::chrono::seconds(1));
}
}
}
extern bool check_enabled(color_ostream & out);
void Hook_Shutdown()
{
if (!lockstep_hooked)
{
return;
}
#ifdef _WIN32
Remove_Hook((void *)GetTickCount, Real_GetTickCount, (void *)Fake_GetTickCount);
#else
Remove_Hook((void *)gettimeofday, Real_gettimeofday, (void *)Fake_gettimeofday);
#endif
Remove_Hook((void *)SDL_GetTicks, Real_SDL_GetTicks, (void *)Fake_SDL_GetTicks);
DFAI_DEBUG(lockstep, 1, "unhooked ticks");
if (BOOST_LIKELY(lockstep_renderer == enabler->renderer))
{
enabler->renderer = lockstep_renderer->real_renderer;
}
DFAI_DEBUG(lockstep, 1, "deleting df_ai_renderer");
delete lockstep_renderer;
lockstep_renderer = nullptr;
DFAI_DEBUG(lockstep, 1, "resetting calculated fps");
enabler->calculated_fps = int(enabler->fps);
enabler->calculated_gfps = int(enabler->gfps);
enabler->frame_timings.clear();
enabler->gframe_timings.clear();
enabler->frame_sum = enabler->gframe_sum = 0;
enabler->frame_last = enabler->gframe_last = SDL_GetTicks();
lockstep_hooked = false;
if (lockstep_want_shutdown_now)
{
if (df::viewscreen *screen = Gui::getCurViewscreen(true))
{
screen->breakdown_level = interface_breakdown_types::QUIT;
}
}
else if (disabling_plugin)
{
enabled = false;
disabling_plugin = false;
check_enabled(Core::getInstance().getConsole());
}
}
void Hook_Shutdown_Now()
{
lockstep_want_shutdown = true;
lockstep_want_shutdown_now = true;
}