| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <signal.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <limits.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <stdarg.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
#include <string.h> |
| 31 |
#include <ctype.h> |
| 32 |
#include <errno.h> |
| 33 |
|
| 34 |
#ifdef __EMSCRIPTEN__ |
| 35 |
#include <emscripten/emscripten.h> |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifndef DEDICATED |
| 39 |
#ifdef USE_INTERNAL_SDL_HEADERS |
| 40 |
# include "SDL.h" |
| 41 |
# include "SDL_cpuinfo.h" |
| 42 |
#else |
| 43 |
# include <SDL.h> |
| 44 |
# include <SDL_cpuinfo.h> |
| 45 |
#endif |
| 46 |
#endif |
| 47 |
|
| 48 |
#include "sys_local.h" |
| 49 |
#include "sys_loadlib.h" |
| 50 |
|
| 51 |
#include "../qcommon/q_shared.h" |
| 52 |
#include "../qcommon/qcommon.h" |
| 53 |
|
| 54 |
static char binaryPath[ MAX_OSPATH ] = { 0 }; |
| 55 |
static char installPath[ MAX_OSPATH ] = { 0 }; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
void Sys_SetBinaryPath(const char *path) |
| 63 |
{ |
| 64 |
Q_strncpyz(binaryPath, path, sizeof(binaryPath)); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
char *Sys_BinaryPath(void) |
| 73 |
{ |
| 74 |
return binaryPath; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void Sys_SetDefaultInstallPath(const char *path) |
| 83 |
{ |
| 84 |
Q_strncpyz(installPath, path, sizeof(installPath)); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
char *Sys_DefaultInstallPath(void) |
| 93 |
{ |
| 94 |
if (*installPath) |
| 95 |
return installPath; |
| 96 |
else |
| 97 |
return Sys_Cwd(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
char *Sys_DefaultAppPath(void) |
| 106 |
{ |
| 107 |
return Sys_BinaryPath(); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
void Sys_In_Restart_f( void ) |
| 118 |
{ |
| 119 |
#ifndef DEDICATED |
| 120 |
if( !SDL_WasInit( SDL_INIT_VIDEO ) ) |
| 121 |
{ |
| 122 |
Com_Printf( "in_restart: Cannot restart input while video is shutdown\n" ); |
| 123 |
return; |
| 124 |
} |
| 125 |
#endif |
| 126 |
|
| 127 |
IN_Restart( ); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
char *Sys_ConsoleInput(void) |
| 138 |
{ |
| 139 |
return CON_Input( ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
char *Sys_GetClipboardData(void) |
| 148 |
{ |
| 149 |
#ifdef DEDICATED |
| 150 |
return NULL; |
| 151 |
#else |
| 152 |
char *data = NULL; |
| 153 |
char *cliptext; |
| 154 |
|
| 155 |
if ( ( cliptext = SDL_GetClipboardText() ) != NULL ) { |
| 156 |
if ( cliptext[0] != '\0' ) { |
| 157 |
size_t bufsize = strlen( cliptext ) + 1; |
| 158 |
|
| 159 |
data = Z_Malloc( bufsize ); |
| 160 |
Q_strncpyz( data, cliptext, bufsize ); |
| 161 |
|
| 162 |
|
| 163 |
strtok( data, "\n\r\b" ); |
| 164 |
} |
| 165 |
SDL_free( cliptext ); |
| 166 |
} |
| 167 |
return data; |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
#ifdef DEDICATED |
| 172 |
# define PID_FILENAME PRODUCT_NAME "_server.pid" |
| 173 |
#else |
| 174 |
# define PID_FILENAME PRODUCT_NAME ".pid" |
| 175 |
#endif |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static char *Sys_PIDFileName( const char *gamedir ) |
| 183 |
{ |
| 184 |
const char *homeStatePath = Cvar_VariableString( "fs_homestatepath" ); |
| 185 |
|
| 186 |
if( *homeStatePath != '\0' ) |
| 187 |
return va( "%s/%s/%s", homeStatePath, gamedir, PID_FILENAME ); |
| 188 |
|
| 189 |
return NULL; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
void Sys_RemovePIDFile( const char *gamedir ) |
| 198 |
{ |
| 199 |
char *pidFile = Sys_PIDFileName( gamedir ); |
| 200 |
|
| 201 |
if( pidFile != NULL ) |
| 202 |
remove( pidFile ); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static qboolean Sys_WritePIDFile( const char *gamedir ) |
| 213 |
{ |
| 214 |
char *pidFile = Sys_PIDFileName( gamedir ); |
| 215 |
FILE *f; |
| 216 |
qboolean stale = qfalse; |
| 217 |
|
| 218 |
if( pidFile == NULL ) |
| 219 |
return qfalse; |
| 220 |
|
| 221 |
|
| 222 |
if( ( f = fopen( pidFile, "r" ) ) != NULL ) |
| 223 |
{ |
| 224 |
char pidBuffer[ 64 ] = { 0 }; |
| 225 |
int pid; |
| 226 |
|
| 227 |
pid = fread( pidBuffer, sizeof( char ), sizeof( pidBuffer ) - 1, f ); |
| 228 |
fclose( f ); |
| 229 |
|
| 230 |
if(pid > 0) |
| 231 |
{ |
| 232 |
pid = atoi( pidBuffer ); |
| 233 |
if( !Sys_PIDIsRunning( pid ) ) |
| 234 |
stale = qtrue; |
| 235 |
} |
| 236 |
else |
| 237 |
stale = qtrue; |
| 238 |
} |
| 239 |
|
| 240 |
if( FS_CreatePath( pidFile ) ) { |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
if( ( f = fopen( pidFile, "w" ) ) != NULL ) |
| 245 |
{ |
| 246 |
fprintf( f, "%d", Sys_PID( ) ); |
| 247 |
fclose( f ); |
| 248 |
} |
| 249 |
else |
| 250 |
Com_Printf( S_COLOR_YELLOW "Couldn't write %s.\n", pidFile ); |
| 251 |
|
| 252 |
return stale; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
void Sys_InitPIDFile( const char *gamedir ) { |
| 261 |
if( Sys_WritePIDFile( gamedir ) ) { |
| 262 |
#ifndef DEDICATED |
| 263 |
char message[1024]; |
| 264 |
char modName[MAX_OSPATH]; |
| 265 |
|
| 266 |
FS_GetModDescription( gamedir, modName, sizeof ( modName ) ); |
| 267 |
Q_CleanStr( modName ); |
| 268 |
|
| 269 |
Com_sprintf( message, sizeof (message), "The last time %s ran, " |
| 270 |
"it didn't exit properly. This may be due to inappropriate video " |
| 271 |
"settings. Would you like to start with \"safe\" video settings?", modName ); |
| 272 |
|
| 273 |
if( Sys_Dialog( DT_YES_NO, message, "Abnormal Exit" ) == DR_YES ) { |
| 274 |
Cvar_Set( "com_abnormalExit", "1" ); |
| 275 |
} |
| 276 |
#endif |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
qboolean Sys_OpenFolderInFileManager( const char *path, qboolean create ) |
| 286 |
{ |
| 287 |
if( create ) |
| 288 |
{ |
| 289 |
if( FS_CreatePath( path ) ) |
| 290 |
return qfalse; |
| 291 |
} |
| 292 |
|
| 293 |
return Sys_OpenFolderInPlatformFileManager( path ); |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
static Q_NO_RETURN void Sys_Exit( int exitCode ) |
| 304 |
{ |
| 305 |
CON_Shutdown( ); |
| 306 |
|
| 307 |
#ifndef DEDICATED |
| 308 |
SDL_Quit( ); |
| 309 |
#endif |
| 310 |
|
| 311 |
if( exitCode < 2 && com_fullyInitialized ) |
| 312 |
{ |
| 313 |
|
| 314 |
Sys_RemovePIDFile( FS_GetCurrentGameDir() ); |
| 315 |
} |
| 316 |
|
| 317 |
NET_Shutdown( ); |
| 318 |
|
| 319 |
Sys_PlatformExit( ); |
| 320 |
|
| 321 |
exit( exitCode ); |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
void Sys_Quit( void ) |
| 330 |
{ |
| 331 |
Sys_Exit( 0 ); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
cpuFeatures_t Sys_GetProcessorFeatures( void ) |
| 340 |
{ |
| 341 |
cpuFeatures_t features = 0; |
| 342 |
|
| 343 |
#ifndef DEDICATED |
| 344 |
if( SDL_HasRDTSC( ) ) features |= CF_RDTSC; |
| 345 |
if( SDL_Has3DNow( ) ) features |= CF_3DNOW; |
| 346 |
if( SDL_HasMMX( ) ) features |= CF_MMX; |
| 347 |
if( SDL_HasSSE( ) ) features |= CF_SSE; |
| 348 |
if( SDL_HasSSE2( ) ) features |= CF_SSE2; |
| 349 |
if( SDL_HasAltiVec( ) ) features |= CF_ALTIVEC; |
| 350 |
#endif |
| 351 |
|
| 352 |
return features; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
void Sys_Init(void) |
| 361 |
{ |
| 362 |
Cmd_AddCommand( "in_restart", Sys_In_Restart_f ); |
| 363 |
Cvar_Set( "arch", OS_STRING " " ARCH_STRING ); |
| 364 |
Cvar_Set( "username", Sys_GetCurrentUser( ) ); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
void Sys_AnsiColorPrint( const char *msg ) |
| 375 |
{ |
| 376 |
static char buffer[ MAXPRINTMSG ]; |
| 377 |
int length = 0; |
| 378 |
static int q3ToAnsi[ 8 ] = |
| 379 |
{ |
| 380 |
7, |
| 381 |
31, |
| 382 |
32, |
| 383 |
33, |
| 384 |
34, |
| 385 |
36, |
| 386 |
35, |
| 387 |
0 |
| 388 |
}; |
| 389 |
|
| 390 |
while( *msg ) |
| 391 |
{ |
| 392 |
if( Q_IsColorString( msg ) || *msg == '\n' ) |
| 393 |
{ |
| 394 |
|
| 395 |
if( length > 0 ) |
| 396 |
{ |
| 397 |
buffer[ length ] = '\0'; |
| 398 |
fputs( buffer, stderr ); |
| 399 |
length = 0; |
| 400 |
} |
| 401 |
|
| 402 |
if( *msg == '\n' ) |
| 403 |
{ |
| 404 |
|
| 405 |
fputs( "\033[0m\n", stderr ); |
| 406 |
msg++; |
| 407 |
} |
| 408 |
else |
| 409 |
{ |
| 410 |
|
| 411 |
Com_sprintf( buffer, sizeof( buffer ), "\033[0m\033[%dm", |
| 412 |
q3ToAnsi[ ColorIndex( *( msg + 1 ) ) ] ); |
| 413 |
fputs( buffer, stderr ); |
| 414 |
msg += 2; |
| 415 |
} |
| 416 |
} |
| 417 |
else |
| 418 |
{ |
| 419 |
if( length >= MAXPRINTMSG - 1 ) |
| 420 |
break; |
| 421 |
|
| 422 |
buffer[ length ] = *msg; |
| 423 |
length++; |
| 424 |
msg++; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
if( length > 0 ) |
| 430 |
{ |
| 431 |
buffer[ length ] = '\0'; |
| 432 |
fputs( buffer, stderr ); |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
void Sys_Print( const char *msg ) |
| 442 |
{ |
| 443 |
CON_LogWrite( msg ); |
| 444 |
CON_Print( msg ); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
void Sys_Error( const char *error, ... ) |
| 453 |
{ |
| 454 |
va_list argptr; |
| 455 |
char string[1024]; |
| 456 |
|
| 457 |
va_start (argptr,error); |
| 458 |
Q_vsnprintf (string, sizeof(string), error, argptr); |
| 459 |
va_end (argptr); |
| 460 |
|
| 461 |
Sys_ErrorDialog( string ); |
| 462 |
|
| 463 |
Sys_Exit( 3 ); |
| 464 |
} |
| 465 |
|
| 466 |
#if 0 |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
static Q_PRINTF_FUNC(1, 2) void Sys_Warn( char *warning, ... ) |
| 473 |
{ |
| 474 |
va_list argptr; |
| 475 |
char string[1024]; |
| 476 |
|
| 477 |
va_start (argptr,warning); |
| 478 |
Q_vsnprintf (string, sizeof(string), warning, argptr); |
| 479 |
va_end (argptr); |
| 480 |
|
| 481 |
CON_Print( va( "Warning: %s", string ) ); |
| 482 |
} |
| 483 |
#endif |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
int Sys_FileTime( char *path ) |
| 493 |
{ |
| 494 |
struct stat buf; |
| 495 |
|
| 496 |
if (stat (path,&buf) == -1) |
| 497 |
return -1; |
| 498 |
|
| 499 |
return buf.st_mtime; |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
void Sys_UnloadDll( void *dllHandle ) |
| 508 |
{ |
| 509 |
if( !dllHandle ) |
| 510 |
{ |
| 511 |
Com_Printf("Sys_UnloadDll(NULL)\n"); |
| 512 |
return; |
| 513 |
} |
| 514 |
|
| 515 |
Sys_UnloadLibrary(dllHandle); |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
void *Sys_LoadDll(const char *name, qboolean useSystemLib) |
| 528 |
{ |
| 529 |
void *dllhandle = NULL; |
| 530 |
|
| 531 |
if(!Sys_DllExtension(name)) |
| 532 |
{ |
| 533 |
Com_Printf("Refusing to attempt to load library \"%s\": Extension not allowed.\n", name); |
| 534 |
return NULL; |
| 535 |
} |
| 536 |
|
| 537 |
if(useSystemLib) |
| 538 |
{ |
| 539 |
Com_Printf("Trying to load \"%s\"...\n", name); |
| 540 |
dllhandle = Sys_LoadLibrary(name); |
| 541 |
} |
| 542 |
|
| 543 |
if(!dllhandle) |
| 544 |
{ |
| 545 |
const char *topDir; |
| 546 |
char libPath[MAX_OSPATH]; |
| 547 |
int len; |
| 548 |
|
| 549 |
topDir = Sys_BinaryPath(); |
| 550 |
|
| 551 |
if(!*topDir) |
| 552 |
topDir = "."; |
| 553 |
|
| 554 |
len = Com_sprintf(libPath, sizeof(libPath), "%s%c%s", topDir, PATH_SEP, name); |
| 555 |
if(len < sizeof(libPath)) |
| 556 |
{ |
| 557 |
Com_Printf("Trying to load \"%s\" from \"%s\"...\n", name, topDir); |
| 558 |
dllhandle = Sys_LoadLibrary(libPath); |
| 559 |
} |
| 560 |
else |
| 561 |
{ |
| 562 |
Com_Printf("Skipping trying to load \"%s\" from \"%s\", file name is too long.\n", name, topDir); |
| 563 |
} |
| 564 |
|
| 565 |
if(!dllhandle) |
| 566 |
{ |
| 567 |
const char *basePath = Cvar_VariableString("fs_basepath"); |
| 568 |
|
| 569 |
if(!basePath || !*basePath) |
| 570 |
basePath = "."; |
| 571 |
|
| 572 |
if(FS_FilenameCompare(topDir, basePath)) |
| 573 |
{ |
| 574 |
len = Com_sprintf(libPath, sizeof(libPath), "%s%c%s", basePath, PATH_SEP, name); |
| 575 |
if(len < sizeof(libPath)) |
| 576 |
{ |
| 577 |
Com_Printf("Trying to load \"%s\" from \"%s\"...\n", name, basePath); |
| 578 |
dllhandle = Sys_LoadLibrary(libPath); |
| 579 |
} |
| 580 |
else |
| 581 |
{ |
| 582 |
Com_Printf("Skipping trying to load \"%s\" from \"%s\", file name is too long.\n", name, basePath); |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
if(!dllhandle) |
| 587 |
Com_Printf("Loading \"%s\" failed\n", name); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
return dllhandle; |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
void *Sys_LoadGameDll(const char *name, |
| 602 |
vmMainProc *entryPoint, |
| 603 |
intptr_t (*systemcalls)(intptr_t, ...)) |
| 604 |
{ |
| 605 |
void *libHandle; |
| 606 |
void (*dllEntry)(intptr_t (*syscallptr)(intptr_t, ...)); |
| 607 |
|
| 608 |
assert(name); |
| 609 |
|
| 610 |
if(!Sys_DllExtension(name)) |
| 611 |
{ |
| 612 |
Com_Printf("Refusing to attempt to load library \"%s\": Extension not allowed.\n", name); |
| 613 |
return NULL; |
| 614 |
} |
| 615 |
|
| 616 |
Com_Printf( "Loading DLL file: %s\n", name); |
| 617 |
libHandle = Sys_LoadLibrary(name); |
| 618 |
|
| 619 |
if(!libHandle) |
| 620 |
{ |
| 621 |
Com_Printf("Sys_LoadGameDll(%s) failed:\n\"%s\"\n", name, Sys_LibraryError()); |
| 622 |
return NULL; |
| 623 |
} |
| 624 |
|
| 625 |
dllEntry = Sys_LoadFunction( libHandle, "dllEntry" ); |
| 626 |
*entryPoint = Sys_LoadFunction( libHandle, "vmMain" ); |
| 627 |
|
| 628 |
if ( !*entryPoint || !dllEntry ) |
| 629 |
{ |
| 630 |
Com_Printf ( "Sys_LoadGameDll(%s) failed to find vmMain function:\n\"%s\" !\n", name, Sys_LibraryError( ) ); |
| 631 |
Sys_UnloadLibrary(libHandle); |
| 632 |
|
| 633 |
return NULL; |
| 634 |
} |
| 635 |
|
| 636 |
Com_Printf ( "Sys_LoadGameDll(%s) found vmMain function at %p\n", name, *entryPoint ); |
| 637 |
dllEntry( systemcalls ); |
| 638 |
|
| 639 |
return libHandle; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
void Sys_ParseArgs( int argc, char **argv ) |
| 648 |
{ |
| 649 |
if( argc == 2 ) |
| 650 |
{ |
| 651 |
if( !strcmp( argv[1], "--version" ) || |
| 652 |
!strcmp( argv[1], "-v" ) ) |
| 653 |
{ |
| 654 |
const char* date = PRODUCT_DATE; |
| 655 |
#ifdef DEDICATED |
| 656 |
fprintf( stdout, Q3_VERSION " dedicated server (%s)\n", date ); |
| 657 |
#else |
| 658 |
fprintf( stdout, Q3_VERSION " client (%s)\n", date ); |
| 659 |
#endif |
| 660 |
Sys_Exit( 0 ); |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
#ifdef PROTOCOL_HANDLER |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
char *Sys_ParseProtocolUri( const char *uri ) |
| 677 |
{ |
| 678 |
|
| 679 |
if ( Q_strncmp( uri, PROTOCOL_HANDLER ":", strlen( PROTOCOL_HANDLER ":" ) ) ) |
| 680 |
{ |
| 681 |
Com_Printf( "Sys_ParseProtocolUri: unsupported protocol.\n" ); |
| 682 |
return NULL; |
| 683 |
} |
| 684 |
uri += strlen( PROTOCOL_HANDLER ":" ); |
| 685 |
if ( !Q_strncmp( uri, "//", strlen( "//" ) ) ) |
| 686 |
{ |
| 687 |
uri += strlen( "//" ); |
| 688 |
} |
| 689 |
Com_Printf( "Sys_ParseProtocolUri: %s\n", uri ); |
| 690 |
|
| 691 |
|
| 692 |
if ( !Q_strncmp( uri, "connect/", strlen( "connect/" ) ) ) |
| 693 |
{ |
| 694 |
int i, bufsize; |
| 695 |
char *out; |
| 696 |
|
| 697 |
uri += strlen( "connect/" ); |
| 698 |
if ( *uri == '\0' || *uri == '?' ) |
| 699 |
{ |
| 700 |
Com_Printf( "Sys_ParseProtocolUri: missing argument.\n" ); |
| 701 |
return NULL; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
for ( i=0; uri[i] != '\0'; i++ ) |
| 708 |
{ |
| 709 |
if ( uri[i] == '?' ) |
| 710 |
{ |
| 711 |
|
| 712 |
|
| 713 |
break; |
| 714 |
} |
| 715 |
|
| 716 |
if ( isalpha( uri[i] ) == 0 && isdigit( uri[i] ) == 0 |
| 717 |
&& uri[i] != '.' && uri[i] != ':' && uri[i] != '-' |
| 718 |
&& uri[i] != '[' && uri[i] != ']' ) |
| 719 |
{ |
| 720 |
Com_Printf( "Sys_ParseProtocolUri: hostname contains unsupported character.\n" ); |
| 721 |
return NULL; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
bufsize = strlen( "connect " ) + i + 1; |
| 726 |
out = malloc( bufsize ); |
| 727 |
strcpy( out, "connect " ); |
| 728 |
strncat( out, uri, i ); |
| 729 |
return out; |
| 730 |
} |
| 731 |
else |
| 732 |
{ |
| 733 |
Com_Printf( "Sys_ParseProtocolUri: unsupported command.\n" ); |
| 734 |
return NULL; |
| 735 |
} |
| 736 |
} |
| 737 |
#endif |
| 738 |
|
| 739 |
#ifndef DEFAULT_BASEDIR |
| 740 |
# if defined(DEFAULT_RELATIVE_BASEDIR) |
| 741 |
# define DEFAULT_BASEDIR Sys_BinaryPathRelative(DEFAULT_RELATIVE_BASEDIR) |
| 742 |
# elif defined(__APPLE__) |
| 743 |
# define DEFAULT_BASEDIR Sys_StripAppBundle(Sys_BinaryPath()) |
| 744 |
# else |
| 745 |
# define DEFAULT_BASEDIR Sys_BinaryPath() |
| 746 |
# endif |
| 747 |
#endif |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
void Sys_SigHandler( int signal ) |
| 755 |
{ |
| 756 |
static qboolean signalcaught = qfalse; |
| 757 |
|
| 758 |
if( signalcaught ) |
| 759 |
{ |
| 760 |
fprintf( stderr, "DOUBLE SIGNAL FAULT: Received signal %d, exiting...\n", |
| 761 |
signal ); |
| 762 |
} |
| 763 |
else |
| 764 |
{ |
| 765 |
signalcaught = qtrue; |
| 766 |
VM_Forced_Unload_Start(); |
| 767 |
#ifndef DEDICATED |
| 768 |
CL_Shutdown(va("Received signal %d", signal), qtrue, qtrue); |
| 769 |
#endif |
| 770 |
SV_Shutdown(va("Received signal %d", signal) ); |
| 771 |
VM_Forced_Unload_Done(); |
| 772 |
} |
| 773 |
|
| 774 |
if( signal == SIGTERM || signal == SIGINT ) |
| 775 |
Sys_Exit( 1 ); |
| 776 |
else |
| 777 |
Sys_Exit( 2 ); |
| 778 |
} |
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
int main( int argc, char **argv ) |
| 786 |
{ |
| 787 |
int i; |
| 788 |
char commandLine[ MAX_STRING_CHARS ] = { 0 }; |
| 789 |
#ifdef PROTOCOL_HANDLER |
| 790 |
char *protocolCommand = NULL; |
| 791 |
#endif |
| 792 |
|
| 793 |
#ifdef USE_AUTOUPDATER |
| 794 |
Sys_LaunchAutoupdater(argc, argv); |
| 795 |
#endif |
| 796 |
|
| 797 |
#ifndef DEDICATED |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
# if !SDL_VERSION_ATLEAST(MINSDL_MAJOR,MINSDL_MINOR,MINSDL_PATCH) |
| 802 |
# error A more recent version of SDL is required |
| 803 |
# endif |
| 804 |
|
| 805 |
|
| 806 |
SDL_version ver; |
| 807 |
SDL_GetVersion( &ver ); |
| 808 |
|
| 809 |
#define MINSDL_VERSION \ |
| 810 |
XSTRING(MINSDL_MAJOR) "." \ |
| 811 |
XSTRING(MINSDL_MINOR) "." \ |
| 812 |
XSTRING(MINSDL_PATCH) |
| 813 |
|
| 814 |
if( SDL_VERSIONNUM( ver.major, ver.minor, ver.patch ) < |
| 815 |
SDL_VERSIONNUM( MINSDL_MAJOR, MINSDL_MINOR, MINSDL_PATCH ) ) |
| 816 |
{ |
| 817 |
Sys_Dialog( DT_ERROR, va( "SDL version " MINSDL_VERSION " or greater is required, " |
| 818 |
"but only version %d.%d.%d was found. You may be able to obtain a more recent copy " |
| 819 |
"from https://www.libsdl.org/.", ver.major, ver.minor, ver.patch ), "SDL Library Too Old" ); |
| 820 |
|
| 821 |
Sys_Exit( 1 ); |
| 822 |
} |
| 823 |
#endif |
| 824 |
|
| 825 |
Sys_PlatformInit( ); |
| 826 |
Sys_SetMaxFileLimit( ); |
| 827 |
|
| 828 |
|
| 829 |
Sys_Milliseconds( ); |
| 830 |
|
| 831 |
#ifdef __APPLE__ |
| 832 |
|
| 833 |
if ( argc >= 2 && Q_strncmp ( argv[1], "-psn", 4 ) == 0 ) |
| 834 |
argc = 1; |
| 835 |
#endif |
| 836 |
|
| 837 |
Sys_ParseArgs( argc, argv ); |
| 838 |
Sys_SetBinaryPath( Sys_Dirname( argv[ 0 ] ) ); |
| 839 |
Sys_SetDefaultInstallPath( DEFAULT_BASEDIR ); |
| 840 |
|
| 841 |
|
| 842 |
for( i = 1; i < argc; i++ ) |
| 843 |
{ |
| 844 |
qboolean containsSpaces; |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
if ( !strcmp( argv[i], "--uri" ) ) |
| 849 |
{ |
| 850 |
#ifdef PROTOCOL_HANDLER |
| 851 |
if ( argc > i+1 ) |
| 852 |
{ |
| 853 |
protocolCommand = Sys_ParseProtocolUri( argv[i+1] ); |
| 854 |
} |
| 855 |
#endif |
| 856 |
break; |
| 857 |
} |
| 858 |
|
| 859 |
containsSpaces = strchr(argv[i], ' ') != NULL; |
| 860 |
if (containsSpaces) |
| 861 |
Q_strcat( commandLine, sizeof( commandLine ), "\"" ); |
| 862 |
|
| 863 |
Q_strcat( commandLine, sizeof( commandLine ), argv[ i ] ); |
| 864 |
|
| 865 |
if (containsSpaces) |
| 866 |
Q_strcat( commandLine, sizeof( commandLine ), "\"" ); |
| 867 |
|
| 868 |
Q_strcat( commandLine, sizeof( commandLine ), " " ); |
| 869 |
} |
| 870 |
|
| 871 |
#ifdef PROTOCOL_HANDLER |
| 872 |
if ( protocolCommand != NULL ) |
| 873 |
{ |
| 874 |
Q_strcat( commandLine, sizeof( commandLine ), "+" ); |
| 875 |
Q_strcat( commandLine, sizeof( commandLine ), protocolCommand ); |
| 876 |
free( protocolCommand ); |
| 877 |
} |
| 878 |
#endif |
| 879 |
|
| 880 |
CON_Init( ); |
| 881 |
Com_Init( commandLine ); |
| 882 |
NET_Init( ); |
| 883 |
|
| 884 |
signal( SIGILL, Sys_SigHandler ); |
| 885 |
signal( SIGFPE, Sys_SigHandler ); |
| 886 |
signal( SIGSEGV, Sys_SigHandler ); |
| 887 |
signal( SIGTERM, Sys_SigHandler ); |
| 888 |
signal( SIGINT, Sys_SigHandler ); |
| 889 |
|
| 890 |
#ifdef __EMSCRIPTEN__ |
| 891 |
emscripten_set_main_loop( Com_Frame, 0, 1 ); |
| 892 |
#else |
| 893 |
while( 1 ) |
| 894 |
{ |
| 895 |
Com_Frame( ); |
| 896 |
} |
| 897 |
#endif |
| 898 |
|
| 899 |
return 0; |
| 900 |
} |