/* ================= Host_InitCommon ================= */ void Host_InitCommon( int argc, const char** argv, const char *progname, qboolean bChangeGame ) { char dev_level[4]; char *baseDir; // some commands may turn engine into infinite loop, // e.g. xash.exe +game xash -game xash // so we clear all cmd_args, but leave dbg states as well Sys_ParseCommandLine( argc, argv ); host.enabledll = !Sys_CheckParm( "-nodll" ); host.shutdown_issued = false; host.crashed = false; #ifdef DLL_LOADER if( host.enabledll ) Setup_LDT_Keeper( ); // Must call before any thread creating #endif #if defined(XASH_SDL) && !(defined(PANDORA) || defined(RPI)) if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_EVENTS )) { Sys_Error( "SDL_Init: %s", SDL_GetError() ); } #endif if( ( baseDir = getenv( "XASH3D_BASEDIR" ) ) ) { Q_strncpy( host.rootdir, baseDir, sizeof(host.rootdir) ); } else { #if defined(PANDORA) || defined(RPI) Q_strncpy( host.rootdir, ".", sizeof( host.rootdir ) ); #else #if defined(XASH_SDL) if( !( baseDir = SDL_GetBasePath() ) ) Sys_Error( "couldn't determine current directory: %s", SDL_GetError() ); Q_strncpy( host.rootdir, baseDir, sizeof( host.rootdir ) ); #else if( !getcwd( host.rootdir, sizeof(host.rootdir) ) ) host.rootdir[0] = 0; #endif #endif } if( host.rootdir[Q_strlen( host.rootdir ) - 1] == '/' ) host.rootdir[Q_strlen( host.rootdir ) - 1] = 0; if( !Sys_CheckParm( "-noch" ) ) { Sys_SetupCrashHandler(); } host.change_game = bChangeGame; host.state = HOST_INIT; // initialization started host.developer = host.old_developer = 0; host.textmode = false; host.mempool = Mem_AllocPool( "Zone Engine" ); if( Sys_CheckParm( "-console" )) host.developer = 1; if( Sys_CheckParm( "-dev" )) { if( Sys_GetParmFromCmdLine( "-dev", dev_level )) { if( Q_isdigit( dev_level )) host.developer = abs( Q_atoi( dev_level )); else host.developer++; // -dev == 1, -dev -console == 2 } else host.developer++; // -dev == 1, -dev -console == 2 } #ifdef XASH_DEDICATED host.type = HOST_DEDICATED; // predict state #else if( Sys_CheckParm("-dedicated") || progname[0] == '#' ) host.type = HOST_DEDICATED; else host.type = HOST_NORMAL; #endif host.con_showalways = true; host.mouse_visible = false; #ifdef XASH_SDL if( SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS )) { SDL_Init( SDL_INIT_TIMER ); host.type = HOST_DEDICATED; } #endif if ( SetCurrentDirectory( host.rootdir ) != 0) MsgDev( D_INFO, "%s is working directory now\n", host.rootdir ); else Sys_Error( "Changing working directory to %s failed.\n", host.rootdir ); // set default gamedir if( progname[0] == '#' ) progname++; Q_strncpy( SI.ModuleName, progname, sizeof( SI.ModuleName )); if( host.type == HOST_DEDICATED ) { Sys_MergeCommandLine( ); if( host.developer < 3 ) host.developer = 3; // otherwise we see empty console } else { // don't show console as default if( host.developer < D_WARN ) host.con_showalways = false; } host.old_developer = host.developer; if( !Sys_CheckParm( "-nowcon" ) ) Con_CreateConsole(); // first text message into console or log MsgDev( D_NOTE, "Sys_LoadLibrary: Loading Engine Library - ok\n" ); // startup cmds and cvars subsystem Cmd_Init(); Cvar_Init(); // share developer level across all dlls Q_snprintf( dev_level, sizeof( dev_level ), "%i", host.developer ); Cvar_Get( "developer", dev_level, CVAR_INIT, "current developer level" ); Cmd_AddCommand( "exec", Host_Exec_f, "execute a script file" ); Cmd_AddCommand( "memlist", Host_MemStats_f, "prints memory pool information" ); Cmd_AddCommand( "userconfigd", Host_Userconfigd_f, "execute all scripts from userconfig.d" ); cmd_scripting = Cvar_Get( "cmd_scripting", "0", CVAR_ARCHIVE, "enable simple condition checking and variable operations" ); FS_Init(); Image_Init(); Sound_Init(); FS_LoadGameInfo( NULL ); Q_strncpy( host.gamefolder, GI->gamefolder, sizeof( host.gamefolder )); #if !(defined(PANDORA) || defined(RPI)) if( GI->secure ) { // clear all developer levels when game is protected Cvar_FullSet( "developer", "0", CVAR_INIT ); host.developer = host.old_developer = 0; host.con_showalways = false; } #endif HPAK_Init(); IN_Init(); Key_Init(); }
/* =============== CL_ChangeGame This is experiment. Use with precaution =============== */ qboolean CL_ChangeGame( const char *gamefolder, qboolean bReset ) { if( Host_IsDedicated() ) return false; if( Q_stricmp( host.gamefolder, gamefolder )) { kbutton_t *mlook, *jlook; qboolean mlook_active = false, jlook_active = false; string mapname, maptitle; int maxEntities; mlook = (kbutton_t *)clgame.dllFuncs.KB_Find( "in_mlook" ); jlook = (kbutton_t *)clgame.dllFuncs.KB_Find( "in_jlook" ); if( mlook && ( mlook->state & 1 )) mlook_active = true; if( jlook && ( jlook->state & 1 )) jlook_active = true; // so reload all images (remote connect) Mod_ClearAll( true ); R_ShutdownImages(); FS_LoadGameInfo( (bReset) ? host.gamefolder : gamefolder ); R_InitImages(); // save parms maxEntities = clgame.maxEntities; Q_strncpy( mapname, clgame.mapname, MAX_STRING ); Q_strncpy( maptitle, clgame.maptitle, MAX_STRING ); Com_ResetLibraryError(); if( !CL_LoadProgs( va( "%s/%s", GI->dll_path, GI->client_lib))) Sys_Warn( "Can't initialize client library\n%s", Com_GetLibraryError() ); // restore parms clgame.maxEntities = maxEntities; Q_strncpy( clgame.mapname, mapname, MAX_STRING ); Q_strncpy( clgame.maptitle, maptitle, MAX_STRING ); // invalidate fonts so we can reloading them again Q_memset( &cls.creditsFont, 0, sizeof( cls.creditsFont )); SCR_InstallParticlePalette(); SCR_LoadCreditsFont(); Con_InvalidateFonts(); SCR_RegisterTextures (); CL_FreeEdicts (); SCR_VidInit (); if( cls.key_dest == key_game ) // restore mouse state clgame.dllFuncs.IN_ActivateMouse(); // restore mlook state if( mlook_active ) Cmd_ExecuteString( "+mlook\n", src_command ); if( jlook_active ) Cmd_ExecuteString( "+jlook\n", src_command ); return true; } return false; }
/* ================= Host_InitCommon ================= */ void Host_InitCommon( const char* moduleName, const char* cmdLine, const char *progname, qboolean bChangeGame ) { char dev_level[4]; char szTemp[MAX_SYSPATH]; string szRootPath; #ifdef _WIN32 MEMORYSTATUS lpBuffer; lpBuffer.dwLength = sizeof( MEMORYSTATUS ); GlobalMemoryStatus( &lpBuffer ); #endif #ifndef __ANDROID__ if( !(SDL_GetBasePath()) ) Sys_Error( "couldn't determine current directory" ); Q_strncpy(host.rootdir, SDL_GetBasePath(), sizeof(host.rootdir)); if( host.rootdir[Q_strlen( host.rootdir ) - 1] == '/' ) host.rootdir[Q_strlen( host.rootdir ) - 1] = 0; #else Q_strncpy(host.rootdir, GAMEPATH, sizeof(host.rootdir)); #endif #ifdef _WIN32 host.oldFilter = SetUnhandledExceptionFilter( Sys_Crash ); host.hInst = GetModuleHandle( NULL ); #endif host.change_game = bChangeGame; host.state = HOST_INIT; // initialzation started host.developer = host.old_developer = 0; CRT_Init(); // init some CRT functions // some commands may turn engine into infinity loop, // e.g. xash.exe +game xash -game xash // so we clearing all cmd_args, but leave dbg states as well if( cmdLine ) Sys_ParseCommandLine( cmdLine ); #ifdef _WIN32 SetErrorMode( SEM_FAILCRITICALERRORS ); // no abort/retry/fail errors #endif host.mempool = Mem_AllocPool( "Zone Engine" ); if( Sys_CheckParm( "-console" )) host.developer = 1; if( Sys_CheckParm( "-dev" )) { if( Sys_GetParmFromCmdLine( "-dev", dev_level )) { if( Q_isdigit( dev_level )) host.developer = abs( Q_atoi( dev_level )); else host.developer++; // -dev == 1, -dev -console == 2 } else host.developer++; // -dev == 1, -dev -console == 2 } host.type = HOST_NORMAL; // predict state host.con_showalways = true; #ifdef PANDORA if( Sys_CheckParm( "-noshouldermb" )) noshouldermb = 1; #endif #ifdef __ANDROID__ if (chdir(host.rootdir) == 0) MsgDev(D_INFO,"%s is working directory now",host.rootdir); else MsgDev(D_ERROR,"%s is not exists",host.rootdir); #else // we can specified custom name, from Sys_NewInstance if( SDL_GetBasePath() && !host.change_game ) { Q_strncpy( szTemp, SDL_GetBasePath(), sizeof(szTemp) ); FS_FileBase( szTemp, SI.ModuleName ); } if(moduleName) Q_strncpy(SI.ModuleName, moduleName, sizeof(SI.ModuleName)); FS_ExtractFilePath( SI.ModuleName, szRootPath ); if( Q_stricmp( host.rootdir, szRootPath )) { Q_strncpy( host.rootdir, szRootPath, sizeof( host.rootdir )); #ifdef _WIN32 SetCurrentDirectory( host.rootdir ); #else chdir( host.rootdir ); #endif } #endif if( SI.ModuleName[0] == '#' ) host.type = HOST_DEDICATED; // determine host type if( progname[0] == '#' ) { Q_strncpy( SI.ModuleName, progname + 1, sizeof( SI.ModuleName )); host.type = HOST_DEDICATED; } else Q_strncpy( SI.ModuleName, progname, sizeof( SI.ModuleName )); if( host.type == HOST_DEDICATED ) { // check for duplicate dedicated server host.hMutex = SDL_CreateMutex( ); if( !host.hMutex ) { MSGBOX( "Dedicated server already running" ); Sys_Quit(); return; } Sys_MergeCommandLine( cmdLine ); SDL_DestroyMutex( host.hMutex ); host.hMutex = SDL_CreateSemaphore( 0 ); if( host.developer < 3 ) host.developer = 3; // otherwise we see empty console } else { // don't show console as default if( host.developer < D_WARN ) host.con_showalways = false; } host.old_developer = host.developer; Con_CreateConsole(); // first text message into console or log MsgDev( D_NOTE, "Sys_LoadLibrary: Loading xash.dll - ok\n" ); // startup cmds and cvars subsystem Cmd_Init(); Cvar_Init(); // share developer level across all dlls Q_snprintf( dev_level, sizeof( dev_level ), "%i", host.developer ); Cvar_Get( "developer", dev_level, CVAR_INIT, "current developer level" ); Cmd_AddCommand( "exec", Host_Exec_f, "execute a script file" ); Cmd_AddCommand( "memlist", Host_MemStats_f, "prints memory pool information" ); FS_Init(); Image_Init(); Sound_Init(); FS_LoadGameInfo( NULL ); Q_strncpy( host.gamefolder, GI->gamefolder, sizeof( host.gamefolder )); if( GI->secure ) { // clear all developer levels when game is protected Cvar_FullSet( "developer", "0", CVAR_INIT ); host.developer = host.old_developer = 0; host.con_showalways = false; } HPAK_Init(); IN_Init(); Key_Init(); }