engine: host: refactoring, reduce usage of global variables a bit

This commit is contained in:
Alibek Omarov
2024-06-12 09:50:28 +03:00
parent 393e31e509
commit 739592e50d
8 changed files with 72 additions and 138 deletions

View File

@@ -182,15 +182,6 @@ GAMEINFO stuff
internal shared gameinfo structure (readonly for engine parts)
========================================================================
*/
typedef struct sysinfo_s
{
string exeName; // exe.filename
string rcName; // .rc script name
string basedirName; // name of base directory
string gamedll;
string clientlib;
} sysinfo_t;
typedef enum
{
HOST_INIT = 0, // initalize operations
@@ -323,7 +314,6 @@ typedef struct host_parm_s
qboolean key_overstrike; // key overstrike mode
qboolean stuffcmds_pending; // should execute stuff commands
qboolean allow_cheats; // this host will allow cheating
qboolean con_showalways; // show console always (developer and dedicated)
qboolean change_game; // initialize when game is changed
qboolean mouse_visible; // vgui override cursor control (never change outside Platform_SetCursorType!)
qboolean shutdown_issued; // engine is shutting down
@@ -341,7 +331,6 @@ typedef struct host_parm_s
qboolean movevars_changed;
qboolean renderinfo_changed;
char gamefolder[MAX_QPATH]; // it's a default gamefolder
poolhandle_t imagepool; // imagelib mempool
poolhandle_t soundpool; // soundlib mempool
@@ -357,10 +346,11 @@ typedef struct host_parm_s
// count of sleeps can be inserted between frames
double pureframetime;
string gamedll;
string clientlib;
} host_parm_t;
extern host_parm_t host;
extern sysinfo_t SI;
#define CMD_SERVERDLL BIT( 0 ) // added by server.dll
#define CMD_CLIENTDLL BIT( 1 ) // added by client.dll
@@ -374,7 +364,7 @@ typedef void (*xcommand_t)( void );
//
// filesystem_engine.c
//
void FS_Init( void );
void FS_Init( const char *basedir );
void FS_Shutdown( void );
void *FS_GetNativeObject( const char *obj );

View File

@@ -202,7 +202,7 @@ static qboolean FS_DetermineReadOnlyRootDirectory( char *out, size_t size )
FS_Init
================
*/
void FS_Init( void )
void FS_Init( const char *basedir )
{
string gamedir;
char rodir[MAX_OSPATH], rootdir[MAX_OSPATH];
@@ -221,10 +221,10 @@ void FS_Init( void )
COM_StripDirectorySlash( rodir );
if( !Sys_GetParmFromCmdLine( "-game", gamedir ))
Q_strncpy( gamedir, SI.basedirName, sizeof( gamedir )); // gamedir == basedir
Q_strncpy( gamedir, basedir, sizeof( gamedir )); // gamedir == basedir
FS_LoadProgs();
if( !FS_InitStdio( true, rootdir, SI.basedirName, gamedir, rodir ))
if( !g_fsapi.InitStdio( true, rootdir, basedir, gamedir, rodir ))
{
Sys_Error( "Can't init filesystem_stdio!\n" );
return;
@@ -238,11 +238,11 @@ void FS_Init( void )
Cmd_AddRestrictedCommand( "fs_path", FS_Path_f_, "show filesystem search pathes" );
Cmd_AddRestrictedCommand( "fs_clearpaths", FS_ClearPaths_f, "clear filesystem search pathes" );
if( !Sys_GetParmFromCmdLine( "-dll", SI.gamedll ))
SI.gamedll[0] = 0;
if( !Sys_GetParmFromCmdLine( "-dll", host.gamedll ))
host.gamedll[0] = 0;
if( !Sys_GetParmFromCmdLine( "-clientlib", SI.clientlib ))
SI.clientlib[0] = 0;
if( !Sys_GetParmFromCmdLine( "-clientlib", host.clientlib ))
host.clientlib[0] = 0;
}
/*
@@ -255,8 +255,6 @@ void FS_Shutdown( void )
if( g_fsapi.ShutdownStdio )
g_fsapi.ShutdownStdio();
memset( &SI, 0, sizeof( sysinfo_t ));
FS_UnloadProgs();
}

View File

@@ -42,7 +42,6 @@ GNU General Public License for more details.
pfnChangeGame pChangeGame = NULL;
host_parm_t host; // host parms
sysinfo_t SI;
#ifdef XASH_ENGINE_TESTS
struct tests_stats_s tests_stats;
@@ -90,7 +89,7 @@ static feature_message_t engine_features[] =
{ ENGINE_STEP_POSHISTORY_LERP, "MOVETYPE_STEP Position History Based Lerping" },
};
static void Sys_PrintUsage( void )
static void Sys_PrintUsage( const char *exename )
{
string version_str;
const char *usage_str;
@@ -98,10 +97,14 @@ static void Sys_PrintUsage( void )
Q_snprintf( version_str, sizeof( version_str ),
XASH_ENGINE_NAME " %i/" XASH_VERSION " (%s-%s build %i)", PROTOCOL_VERSION, Q_buildos(), Q_buildarch(), Q_buildnum( ));
#if XASH_WIN32
#define XASH_EXE "(xash).exe"
#if XASH_MESSAGEBOX != MSGBOX_STDERR
#if XASH_WIN32
#define XASH_EXE "(xash).exe"
#else
#define XASH_EXE "(xash)"
#endif
#else
#define XASH_EXE "(xash)"
#define XASH_EXE "%s"
#endif
#define O( x, y ) " "x" "y"\n"
@@ -189,7 +192,8 @@ static void Sys_PrintUsage( void )
#if XASH_MESSAGEBOX != MSGBOX_STDERR
Platform_MessageBox( version_str, usage_str, false );
#else
fprintf( stderr, "%s\n%s", version_str, usage_str );
fprintf( stderr, "%s\n", version_str );
fprintf( stderr, usage_str, exename );
#endif
Sys_Quit();
@@ -1002,27 +1006,42 @@ static uint32_t Host_CheckBugcomp( void )
return flags;
}
static void Host_DetermineExecutableName( char *out, size_t size )
{
#if XASH_WIN32
char temp[MAX_SYSPATH];
if( GetModuleFileName( NULL, temp, sizeof( temp )))
COM_FileBase( temp, out, size );
#else
if( host.argc > 0 )
COM_FileBase( host.argv[0], out, size );
else
Q_strncpy( out, "xash", size );
#endif
}
/*
=================
Host_InitCommon
=================
*/
static void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bChangeGame )
static void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bChangeGame, char *exename, size_t exename_size )
{
char dev_level[4];
int developer = DEFAULT_DEV;
char ticrate[16];
int i;
const char *basedir = progname[0] == '#' ? progname + 1 : progname;
char dev_level[4], ticrate[16];
int developer = DEFAULT_DEV;
// 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_DetermineExecutableName( exename, exename_size );
if( !Sys_CheckParm( "-disablehelp" ))
{
if( Sys_CheckParm( "-help" ) || Sys_CheckParm( "-h" ) || Sys_CheckParm( "--help" ))
Sys_PrintUsage();
Sys_PrintUsage( exename );
}
if( !Sys_CheckParm( "-noch" ))
@@ -1033,6 +1052,11 @@ static void Host_InitCommon( int argc, char **argv, const char *progname, qboole
host.change_game = bChangeGame || Sys_CheckParm( "-changegame" );
host.config_executed = false;
host.status = HOST_INIT; // initialzation started
host.type = HOST_DEDICATED; // predict state
#ifndef XASH_DEDICATED
if( !Sys_CheckParm( "-dedicated" ))
host.type = HOST_NORMAL;
#endif
Memory_Init(); // init memory subsystem
@@ -1040,11 +1064,6 @@ static void Host_InitCommon( int argc, char **argv, const char *progname, qboole
host.allow_console = DEFAULT_ALLOWCONSOLE;
// HACKHACK: Quake console is always allowed
// TODO: determine if we are running QWrap more reliable
if( !host.allow_console && ( Sys_CheckParm( "-console" ) || !Q_stricmp( SI.exeName, "quake" )))
host.allow_console = true;
if( Sys_CheckParm( "-dev" ))
{
host.allow_console = true;
@@ -1065,40 +1084,9 @@ static void Host_InitCommon( int argc, char **argv, const char *progname, qboole
}
#endif
host.con_showalways = true;
#if 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
// set default gamedir
if( progname[0] == '#' )
progname++;
Q_strncpy( SI.exeName, progname, sizeof( SI.exeName ));
Q_strncpy( SI.basedirName, progname, sizeof( SI.basedirName ));
if( Host_IsDedicated() )
{
Sys_MergeCommandLine( );
// always enable console for Quake
if( !host.allow_console && ( Sys_CheckParm( "-console" ) || !Q_strnicmp( exename, "quake", 5 )))
host.allow_console = true;
}
else
{
// don't show console as default
if( developer <= DEV_NORMAL )
host.con_showalways = false;
}
// member console allowing
host.allow_console_init = host.allow_console;
@@ -1139,8 +1127,8 @@ static void Host_InitCommon( int argc, char **argv, const char *progname, qboole
Host_RunTests( 0 );
#endif
Platform_Init();
FS_Init();
Platform_Init( Host_IsDedicated( ) || developer >= DEV_EXTENDED );
FS_Init( basedir );
Sys_InitLog();
@@ -1165,39 +1153,9 @@ static void Host_InitCommon( int argc, char **argv, const char *progname, qboole
FS_LoadGameInfo( NULL );
Cvar_PostFSInit();
Q_strncpy( host.gamefolder, GI->gamefolder, sizeof( host.gamefolder ));
for( i = 0; i < 3; i++ )
{
const char *rcName;
switch( i )
{
case 0: rcName = SI.basedirName; break; // e.g. valve.rc
case 1: rcName = SI.exeName; break; // e.g. quake.rc
case 2: rcName = host.gamefolder; break; // e.g. game.rc (ran from default launcher)
}
if( FS_FileExists( va( "%s.rc", rcName ), false ))
{
Q_strncpy( SI.rcName, rcName, sizeof( SI.rcName ));
break;
}
}
Image_CheckPaletteQ1 ();
Host_InitDecals (); // reload decals
// DEPRECATED: by FWGS fork
#if 0
if( GI->secure )
{
// clear all developer levels when game is protected
Cvar_DirectSet( &host_developer, "0" );
host.allow_console_init = false;
host.con_showalways = false;
host.allow_console = false;
}
#endif
HPAK_Init();
IN_Init();
@@ -1221,13 +1179,13 @@ Host_Main
int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGame, pfnChangeGame func )
{
static double oldtime, newtime;
string demoname;
string demoname, exename;
host.starttime = Sys_DoubleTime();
pChangeGame = func; // may be NULL
Host_InitCommon( argc, argv, progname, bChangeGame );
Host_InitCommon( argc, argv, progname, bChangeGame, exename, sizeof( exename ));
// init commands and vars
if( host_developer.value >= DEV_EXTENDED )
@@ -1293,8 +1251,12 @@ int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGa
HPAK_CheckIntegrity( CUSTOM_RES_PATH );
host.errorframe = 0;
if( progname[0] == '#' )
progname++;
// post initializations
switch( host.type )
{
@@ -1303,8 +1265,14 @@ int EXPORT Host_Main( int argc, char **argv, const char *progname, int bChangeGa
Wcon_ShowConsole( false ); // hide console
#endif
// execute startup config and cmdline
Cbuf_AddTextf( "exec %s.rc\n", SI.rcName );
if( FS_FileExists( va( "%s.rc", progname ), false )) // e.g. valve.rc
Cbuf_AddTextf( "exec %s.rc", progname );
else if( FS_FileExists( va( "%s.rc", exename ), false )) // e.g. quake.rc
Cbuf_AddTextf( "exec %s.rc", exename );
else if( FS_FileExists( va( "%s.rc", GI->gamefolder ), false )) // e.g. game.rc (ran from default launcher)
Cbuf_AddTextf( "exec %s.rc", GI->gamefolder );
Cbuf_Execute();
if( !host.config_executed )
{
Cbuf_AddText( "exec config.cfg\n" );

View File

@@ -231,9 +231,9 @@ void COM_GetCommonLibraryPath( ECommonLibraryType eLibType, char *out, size_t si
COM_GenerateClientLibraryPath( "menu", out, size );
break;
case LIBRARY_CLIENT:
if( SI.clientlib[0] )
if( COM_CheckStringEmpty( host.clientlib ))
{
Q_strncpy( out, SI.clientlib, size );
Q_strncpy( out, host.clientlib, size );
}
else
{
@@ -241,9 +241,9 @@ void COM_GetCommonLibraryPath( ECommonLibraryType eLibType, char *out, size_t si
}
break;
case LIBRARY_SERVER:
if( SI.gamedll[0] )
if( COM_CheckStringEmpty( host.gamedll ))
{
Q_strncpy( out, SI.gamedll, size );
Q_strncpy( out, host.gamedll, size );
}
else
{

View File

@@ -181,27 +181,6 @@ void Sys_ParseCommandLine( int argc, char** argv )
}
}
/*
==================
Sys_MergeCommandLine
==================
*/
void Sys_MergeCommandLine( void )
{
const char *blank = "censored";
int i;
if( !host.change_game ) return;
for( i = 0; i < host.argc; i++ )
{
// second call
if( Host_IsDedicated() && !Q_strnicmp( "+menu_", host.argv[i], 6 ))
host.argv[i] = (char *)blank;
}
}
/*
================
Sys_CheckParm

View File

@@ -53,7 +53,6 @@ qboolean Sys_LoadLibrary( dll_info_t *dll );
void* Sys_GetProcAddress( dll_info_t *dll, const char* name );
qboolean Sys_FreeLibrary( dll_info_t *dll );
void Sys_ParseCommandLine( int argc, char **argv );
void Sys_MergeCommandLine( void );
void Sys_SetupCrashHandler( void );
void Sys_RestoreCrashHandler( void );
void Sys_DebugBreak( void );

View File

@@ -69,7 +69,7 @@ void Android_Shutdown( void );
#endif
#if XASH_WIN32
void Wcon_CreateConsole( void );
void Wcon_CreateConsole( qboolean con_showalways );
void Wcon_DestroyConsole( void );
#endif
@@ -97,7 +97,7 @@ void Linux_Shutdown( void );
void Linux_SetTimer( float time );
#endif
static inline void Platform_Init( void )
static inline void Platform_Init( qboolean con_showalways )
{
#if XASH_POSIX
// daemonize as early as possible, because we need to close our file descriptors
@@ -117,7 +117,7 @@ static inline void Platform_Init( void )
#elif XASH_DOS
DOS_Init( );
#elif XASH_WIN32
Wcon_CreateConsole( );
Wcon_CreateConsole( con_showalways );
#elif XASH_LINUX
Linux_Init( );
#endif

View File

@@ -497,7 +497,7 @@ Con_CreateConsole
create win32 console
================
*/
void Wcon_CreateConsole( void )
void Wcon_CreateConsole( qboolean con_showalways )
{
if( Sys_CheckParm( "-log" ))
s_wcd.log_active = true;
@@ -544,7 +544,7 @@ void Wcon_CreateConsole( void )
SetWindowPos( s_wcd.hWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOREPOSITION | SWP_SHOWWINDOW );
// show console if needed
if( host.con_showalways )
if( con_showalways )
{
// make console visible
ShowWindow( s_wcd.hWnd, SW_SHOWDEFAULT );