-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdllmain.cpp
More file actions
672 lines (550 loc) · 18 KB
/
dllmain.cpp
File metadata and controls
672 lines (550 loc) · 18 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
// SvenInt (c) Sw1ft
// dllmain.cpp
#ifdef _WIN32
#if !defined(_DEBUG) && defined(_CPPRTTI)
#error "RTTI enabled"
#endif
#else
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <pthread.h>
#endif
#include "stdafx.h"
#include "svenint.h"
#include "modules/config.h"
#include "modules/client.h"
#include "modules/hooks.h"
#include "modules/menu.h"
#include "modules/opengl.h"
#include "modules/server.h"
#include "modules/scripts.h"
#include "features/base_feature.h"
#include "game/dbg.h"
#include "utils/detours.h"
#include "utils/prof.h"
//-----------------------------------------------------------------------------
// Macro Definitions
//-----------------------------------------------------------------------------
#define SHOW_DEBUG_CONSOLE ( 1 )
#define CHECK_FOR_UPDATE ( 1 )
//-----------------------------------------------------------------------------
// Load SvenInt
//-----------------------------------------------------------------------------
bool LoadSvenInt( void )
{
#if DUMP_FILE_OFFSET
InitPrintDumpFileOffset();
#endif
Detours()->Init();
Msg( "Loading SvenInt v" SVENINT_VERSION_STRING "\n" );
PROF_MEASURE( loadTime );
loadTime.Start();
if ( !Globals::gamedata->Init() )
{
Warning( "[SvenInt] Failed to get gamedata\n" );
Warning2( "[SvenInt] Make sure you have %s file \"./" SVENINT_FOLDER_NAME "/gamedata.txt\" (https://github.com/sw1ft747/svenint/blob/main/resource/svenint/gamedata.txt)\n", "downloaded" );
#ifdef WIN32
Warning2( "[SvenInt] Fall back using statically generated patterns and offsets\n" );
#else
return false;
#endif
}
if ( !GameData::GetGameModules() )
{
Warning( "[SvenInt] Failed to get required game libraries\n" );
return false;
}
if ( !GameData::GetGameInterfaceFactories() )
{
Warning( "[SvenInt] Failed to get required game interface factories\n" );
return false;
}
if ( !GameData::GetGameInterfaces() )
{
Warning( "[SvenInt] Failed to get required game interfaces\n" );
return false;
}
if ( !GameData::FindGameSymbols() )
{
Warning( "[SvenInt] Failed to get required game symbols\n" );
Warning2( "[SvenInt] Make sure you have %s file \"./" SVENINT_FOLDER_NAME "/gamedata.txt\" (https://github.com/sw1ft747/svenint/blob/main/resource/svenint/gamedata.txt)\n", "updated" );
#ifdef WIN32
Warning2( "[SvenInt] Try your luck on setting parameter \"PreferRVA\" to 0 in the file \"./" SVENINT_FOLDER_NAME "/gamedata.txt\"\n" );
#endif
return false;
}
Globals::cvar = CVar();
if ( !Globals::cvar->Init() )
{
Warning( "[SvenInt] Failed to initialize CVar system\n" );
return false;
}
// Grab screen size
Globals::gameutils->Init();
// Load features
bool bFeaturesOK = LoadFeatures();
// Auto pause any detour that attaches
Detours()->AutoPauseDetours( true );
// Attach module detours but paused
Modules::hooks->AttachDetours();
// Suspend all threads
Detours()->BeginGlobalAttach();
Modules::config->Init();
Modules::client->Init();
Modules::server->Init();
Modules::hooks->Init();
Modules::menu->Init();
Modules::scripts->Init();
// Post load features & attach their detours but paused
PostLoadFeatures();
// Remove auto pause & unpause all attached detours
Detours()->AutoPauseDetours( false );
Detours()->UnpauseAllDetours();
// Print state
ConColorMsg( { bFeaturesOK ? 0 : 255, 255, 90, 255 }, bFeaturesOK ? "[SvenInt] Successfully loaded\n" : "[SvenInt] Loaded with limited features\n" );
// Allow print to the game console & dump queued messages
AllowMsgPrint( true );
PrintQueudMessages();
// Resume suspended threads
Detours()->EndGlobalAttach();
Msg( "Loaded SvenInt in %.2f s\n", (float)PROF_TIME_SEC( loadTime.Elapsed() ) );
#if DUMP_FILE_OFFSET
ShutdownPrintDumpFileOffset();
#endif
return true;
}
//-----------------------------------------------------------------------------
// Unload SvenInt
//-----------------------------------------------------------------------------
void UnloadSvenInt( void )
{
Msg( "Unloading SvenInt...\n" );
// Suspend all threads
Detours()->BeginGlobalAttach();
// Pause all attached detours
Detours()->PauseAllDetours();
// Deattach all detours
UnloadFeatures();
Modules::hooks->DeattachDetours();
Modules::scripts->Shutdown();
Modules::menu->Shutdown();
Modules::hooks->Shutdown();
Modules::server->Shutdown();
Modules::client->Shutdown();
Modules::config->Shutdown();
Modules::opengl->Shutdown();
Globals::cvar->Shutdown();
Msg( "Successfully unloaded SvenInt\n" );
// Resume suspended threads
Detours()->EndGlobalAttach();
Globals::gamedata->Shutdown();
MemCheckLeaks();
}
//-----------------------------------------------------------------------------
// Debug console
//-----------------------------------------------------------------------------
#ifdef WIN32
void CreateConsole( FILE **pFile )
{
if ( pFile == NULL || *pFile != NULL )
return;
AllocConsole();
freopen_s( pFile, "CONOUT$", "w", stdout );
}
void CloseConsole( FILE **pFile )
{
if ( pFile == NULL || *pFile == NULL )
return;
fclose( *pFile );
FreeConsole();
*pFile = NULL;
}
#endif
//-----------------------------------------------------------------------------
// Main thread
//-----------------------------------------------------------------------------
#ifdef WIN32
DWORD WINAPI MainThread( HMODULE hModule )
#else
void *MainThread( void *arg )
#endif
{
Globals::commandline = CommandLine();
#ifdef WIN32
Globals::commandline->CreateCmdLine( GetCommandLine() );
// Allocate debug console
#if SHOW_DEBUG_CONSOLE
const bool bNoConsole = Globals::commandline->HasParm( "-sint_noconsole" );
const bool bKeepConsole = Globals::commandline->HasParm( "-sint_keepconsole" );
if ( !bNoConsole )
CreateConsole( (FILE **)&gpDbgConsoleFile );
#endif
#else
int argc;
char **argv;
char **get_argv( int *const argcptr );
argv = get_argv( &argc );
if ( argv != NULL )
{
Globals::commandline->CreateCmdLine( argc, argv );
free( argv );
}
gpDbgConsoleFile = (void *)fopen( "svenint.log", "w" );
#endif
// Load SvenInt
if ( LoadSvenInt() )
{
void CheckForUpdate( void );
#ifdef WIN32
Msg( "Hold 'Right CTRL' to unload SvenInt.\n" );
#if SHOW_DEBUG_CONSOLE
if ( !bNoConsole && !bKeepConsole )
{
DevMsg( "Closing the debug console in 3 seconds...\n" );
Sleep( 3000 );
CloseConsole( (FILE **)&gpDbgConsoleFile );
}
#endif
#if CHECK_FOR_UPDATE
CheckForUpdate();
#endif
// Don't unload SvenInt until 'RCtrl' will be pressed
while ( !GetAsyncKeyState( Modules::menu->GetUnloadKey() ) ) { Sleep( 200 ); };
UnloadSvenInt();
#else
#if CHECK_FOR_UPDATE
CheckForUpdate();
#endif
fclose( (FILE *)gpDbgConsoleFile );
gpDbgConsoleFile = NULL;
*(bool *)arg = true;
pthread_exit( NULL );
#endif
}
else
{
// If possible, print all queued messages into the game's console
// HACK: suspend all threads
Detours()->BeginGlobalAttach();
AllowMsgPrint( true );
PrintQueudMessages();
Detours()->EndGlobalAttach();
#ifdef WIN32
#if SHOW_DEBUG_CONSOLE
if ( !bNoConsole )
{
// Sleep( 5000 );
DevMsg( "Press any key to close the console\n" );
getchar();
}
}
if ( !bNoConsole )
CloseConsole( (FILE **)&gpDbgConsoleFile );
#else
}
#endif
FreeLibraryAndExitThread( hModule, EXIT_SUCCESS );
return EXIT_SUCCESS;
#else
}
fclose( (FILE *)gpDbgConsoleFile );
gpDbgConsoleFile = NULL;
*(bool *)arg = false;
pthread_exit( NULL );
#endif
}
//-----------------------------------------------------------------------------
// Check for updates
//-----------------------------------------------------------------------------
#if CHECK_FOR_UPDATE
typedef void CURL;
typedef int CURLcode;
#define CURLE_OK 0
#define CURLOPT_URL 10002
#define CURLOPT_WRITEFUNCTION 20011
#define CURLOPT_WRITEDATA 10001
#define CURLOPT_TIMEOUT 13
typedef CURL *( *curl_easy_init_t )( void );
typedef CURLcode( *curl_easy_setopt_t )( CURL *, int, ... );
typedef CURLcode( *curl_easy_perform_t )( CURL * );
typedef const char *( *curl_easy_strerror_t )( CURLcode );
typedef void ( *curl_easy_cleanup_t )( CURL * );
struct UpdateDownloadBuffer
{
char *memory;
size_t size;
};
static size_t UpdateDownloadBufferWriteCallback( void *contents, size_t size, size_t nmemb, void *userp )
{
size_t realsize = size * nmemb;
UpdateDownloadBuffer *mem = (UpdateDownloadBuffer *)userp;
char *ptr = (char *)MemRealloc( mem->memory, mem->size + realsize + 1 );
if ( ptr == NULL )
return 0;
mem->memory = ptr;
memcpy( &( mem->memory[ mem->size ] ), contents, realsize );
mem->size += realsize;
mem->memory[ mem->size ] = 0;
return realsize;
}
static void Update_CheckVersion( const char *data )
{
if ( data == NULL || data[ 0 ] == '\0' )
return;
int iMajorVer = 0, iMinorVer = 0, iPatchVer = 0;
#if 1
char szDate[ 32 ] = { 0 };
char *pszChangelogDuped = MemStrdup( data );
if ( sscanf( data, "%31s - Version %d.%d.%d", szDate, &iMajorVer, &iMinorVer, &iPatchVer ) < 4 )
{
Warning2( "<Update Check> Failed to parse changelog format\n" );
return;
}
if ( SVENINT_VERSION() < SVENINT_VERSION_CHECK( iMajorVer, iMinorVer, iPatchVer ) )
{
Msg( "\n" );
Msg( "===============================================" );
Msg( "\n" );
Msg( "\n* Changelog from %s:\n\n", szDate );
const char *pszChangelog = strchr( data, '\n' );
if ( pszChangelog != NULL )
{
pszChangelog++;
Msg( "%s\n", pszChangelog );
}
Msg( "\n" );
Msg( "===============================================" );
Msg( "\n" );
Warning2( "\nA newer version of SvenInt is available: %d.%d.%d (Current: %d.%d.%d)\n",
iMajorVer, iMinorVer, iPatchVer, SVENINT_MAJOR_VERSION, SVENINT_MINOR_VERSION, SVENINT_PATCH_VERSION );
Warning2( "Consider updating it: https://github.com/sw1ft747/svenint/releases\n" );
Modules::menu->FeedChangelog( pszChangelogDuped );
}
else
{
MemFree( pszChangelogDuped );
Msg( "SvenInt is up to date.\n" );
}
#else
const char *pszMajorVer = strstr( data, "SVENINT_MAJOR_VERSION" );
const char *pszMinorVer = strstr( data, "SVENINT_MINOR_VERSION" );
const char *pszPatchVer = strstr( data, "SVENINT_PATCH_VERSION" );
#pragma warning( push )
#pragma warning( disable : 6031 )
if ( pszMajorVer != NULL )
sscanf( pszMajorVer, "SVENINT_MAJOR_VERSION %d", &iMajorVer );
if ( pszMinorVer != NULL )
sscanf( pszMinorVer, "SVENINT_MINOR_VERSION %d", &iMinorVer );
if ( pszPatchVer != NULL )
sscanf( pszPatchVer, "SVENINT_PATCH_VERSION %d", &iPatchVer );
#pragma warning( pop )
if ( SVENINT_VERSION() < SVENINT_VERSION_CHECK( iMajorVer, iMinorVer, iPatchVer ) )
{
Warning2( "A newer version of SvenInt is available: %d.%d.%d (Current: %d.%d.%d)\n",
iMajorVer, iMinorVer, iPatchVer, SVENINT_MAJOR_VERSION, SVENINT_MINOR_VERSION, SVENINT_PATCH_VERSION );
Warning2( "Consider updating it: https://github.com/sw1ft747/svenint/releases\n" );
/*
if ( Globals::cls->state == ca_active )
{
Globals::gameutils->PrintChatText( "A newer version of SvenInt is available: %d.%d.%d (Current: %d.%d.%d)\n",
iMajorVer, iMinorVer, iPatchVer, SVENINT_MAJOR_VERSION, SVENINT_MINOR_VERSION, SVENINT_PATCH_VERSION );
Globals::gameutils->PrintChatText( "Consider updating it: https://github.com/sw1ft747/svenint/releases\n" );
}
*/
}
else
{
Msg( "SvenInt is up to date.\n" );
}
#endif
}
void CheckForUpdate( void )
{
if ( Globals::commandline->HasParm( "-sint_nocheckupdates" ) )
return;
#ifdef WIN32
module_t hCurl = MemoryUtils()->GetModule( "libcurl.dll" );
#else
module_t hCurl = MemoryUtils()->GetModule( "libcurl.so.4" );
#endif
if ( hCurl == NULL )
return;
curl_easy_init_t curl_easy_init = (curl_easy_init_t)MemoryUtils()->GetProcAddress( hCurl, "curl_easy_init" );
curl_easy_setopt_t curl_easy_setopt = (curl_easy_setopt_t)MemoryUtils()->GetProcAddress( hCurl, "curl_easy_setopt" );
curl_easy_perform_t curl_easy_perform = (curl_easy_perform_t)MemoryUtils()->GetProcAddress( hCurl, "curl_easy_perform" );
curl_easy_strerror_t curl_easy_strerror = (curl_easy_strerror_t)MemoryUtils()->GetProcAddress( hCurl, "curl_easy_strerror" );
curl_easy_cleanup_t curl_easy_cleanup = (curl_easy_cleanup_t)MemoryUtils()->GetProcAddress( hCurl, "curl_easy_cleanup" );
if ( curl_easy_init == NULL || curl_easy_setopt == NULL ||
curl_easy_perform == NULL || curl_easy_strerror == NULL ||
curl_easy_cleanup == NULL )
{
return;
}
CURL *curl;
CURLcode res;
UpdateDownloadBuffer chunk = { NULL, 0 };
chunk.memory = (char *)MemAlloc( 1 );
curl = curl_easy_init();
if ( curl != NULL )
{
#if 1
curl_easy_setopt( curl, CURLOPT_URL, "https://raw.githubusercontent.com/sw1ft747/svenint/refs/heads/main/changelog_latest.txt" );
#else
curl_easy_setopt( curl, CURLOPT_URL, "https://raw.githubusercontent.com/sw1ft747/svenint/refs/heads/main/svenint.h" );
#endif
curl_easy_setopt( curl, CURLOPT_WRITEFUNCTION, UpdateDownloadBufferWriteCallback );
curl_easy_setopt( curl, CURLOPT_WRITEDATA, (void *)&chunk );
curl_easy_setopt( curl, CURLOPT_TIMEOUT, 10L );
res = curl_easy_perform( curl );
if ( res == CURLE_OK )
{
Update_CheckVersion( chunk.memory );
}
else
{
Warning2( "Failed to check for updates. Reason: %s\n", curl_easy_strerror( res ) );
}
curl_easy_cleanup( curl );
MemFree( chunk.memory );
}
}
#endif
//-----------------------------------------------------------------------------
// Entry point
//-----------------------------------------------------------------------------
#ifdef WIN32
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
HANDLE hThread = CreateThread( NULL, NULL, (LPTHREAD_START_ROUTINE)MainThread, hModule, NULL, NULL );
if ( hThread != NULL )
CloseHandle( hThread );
return TRUE;
}
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return FALSE;
}
#else
static bool loaded = false;
static pthread_t tid;
int __attribute__((constructor)) module_load( void )
{
pthread_create( &tid, NULL, MainThread, &loaded );
//loaded = ( MainThread( NULL ) != NULL );
return 0;
}
void __attribute__((destructor)) module_unload( void )
{
pthread_join( tid, NULL );
if ( loaded )
{
loaded = false;
// UnloadSvenInt();
}
}
//-----------------------------------------------------------------------------
// Get command line arguments
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
// https://stackoverflow.com/questions/28020711/how-to-pass-argument-to-constructor-on-library-load
char **get_argv( int *const argcptr )
{
char **argv;
char *data = NULL;
size_t size = 0; /* Allocated to data */
size_t used = 0;
size_t argc, i;
ssize_t bytes;
int fd;
if ( argcptr )
*argcptr = 0;
do
{
fd = open( "/proc/self/cmdline", O_RDONLY | O_NOCTTY );
} while ( fd == -1 && errno == EINTR );
if ( fd == -1 )
return NULL;
while ( 1 )
{
if ( used >= size )
{
char *old_data = data;
size = ( used | 4095 ) + 4096;
data = (char *)realloc( data, size + 1 );
if ( data == NULL )
{
free( old_data );
close( fd );
errno = ENOMEM;
return NULL;
}
}
do
{
bytes = read( fd, data + used, size - used );
} while ( bytes == (ssize_t)-1 && errno == EINTR );
if ( bytes < (ssize_t)0 )
{
free( data );
close( fd );
errno = EIO;
return NULL;
}
else
if ( bytes == (ssize_t)0 )
break;
else
used += bytes;
}
if ( close( fd ) )
{
free( data );
errno = EIO;
return NULL;
}
/* Let's be safe and overallocate one pointer here. */
argc = 1;
for ( i = 0; i < used; i++ )
if ( data[ i ] == '\0' )
argc++;
/* Reallocate to accommodate both pointers and data. */
argv = (char **)realloc( data, ( argc + 1 ) * sizeof( char * ) + used + 1 );
if ( argv == NULL )
{
free( data );
errno = ENOMEM;
return NULL;
}
data = (char *)( argv + argc + 1 );
memmove( data, argv, used );
/* In case the input lacked a trailing NUL byte. */
data[ used ] = '\0';
/* Assign the pointers. */
argv[ 0 ] = data;
argc = 0;
for ( i = 0; i < used; i++ )
if ( data[ i ] == '\0' )
argv[ ++argc ] = data + i + 1;
/* Final pointer points to past data. Make it end the array. */
argv[ argc ] = NULL;
if ( argcptr )
*argcptr = (int)argc;
return argv;
}
#endif