Browse Source

game_launch: rip out execv code

pull/2/head
Alibek Omarov 2 years ago
parent
commit
df83b155a1
  1. 134
      engine/common/launcher.c
  2. 42
      game_launch/game.cpp

134
engine/common/launcher.c

@ -23,54 +23,78 @@ GNU General Public License for more details.
#if XASH_EMSCRIPTEN #if XASH_EMSCRIPTEN
#include <emscripten.h> #include <emscripten.h>
#endif #elif XASH_WIN32
extern "C"
#if XASH_WIN32 {
#include <process.h> // _execve // Enable NVIDIA High Performance Graphics while using Integrated Graphics.
#else __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
#include <unistd.h> // execve
#endif
extern char **environ;
static char szGameDir[128]; // safe place to keep gamedir
static int g_iArgc;
static char **g_pszArgv;
#if XASH_WIN32 || XASH_POSIX // Enable AMD High Performance Graphics while using Integrated Graphics.
#define USE_EXECVE_FOR_CHANGE_GAME 1 __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
#else }
#define USE_EXECVE_FOR_CHANGE_GAME 0
#endif #endif
#define E_GAME "XASH3D_GAME" // default env dir to start from #define E_GAME "XASH3D_GAME" // default env dir to start from
#define GAME_PATH "valve" // default dir to start from #define GAME_PATH "valve" // default dir to start from
void Launcher_ChangeGame( const char *progname ) static char szGameDir[128]; // safe place to keep gamedir
static int szArgc;
static char **szArgv;
static void Sys_ChangeGame( const char *progname )
{ {
char envstr[256]; // a1ba: may never be called within engine
// if platform supports execv() function
Q_strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 );
Host_Shutdown( );
exit( Host_Main( szArgc, szArgv, szGameDir, 1, &Launcher_ChangeGame ) );
}
#if USE_EXECVE_FOR_CHANGE_GAME _inline int Sys_Start( void )
Host_Shutdown(); {
int ret;
const char *game = getenv( E_GAME );
#if XASH_WIN32 if( !game )
_putenv_s( E_GAME, progname ); game = GAME_PATH;
_execve( g_pszArgv[0], g_pszArgv, _environ );
#else
snprintf( envstr, sizeof( envstr ), E_GAME "=%s", progname );
putenv( envstr );
execve( g_pszArgv[0], g_pszArgv, environ );
#endif
#else Q_strncpy( szGameDir, game, sizeof( szGameDir ));
Q_strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 ); #if XASH_EMSCRIPTEN
Host_Shutdown( ); #ifdef EMSCRIPTEN_LIB_FS
exit( Host_Main( g_iArgc, g_pszArgv, szGameDir, 1, &Launcher_ChangeGame ) ); // For some unknown reason emscripten refusing to load libraries later
COM_LoadLibrary("menu", 0 );
COM_LoadLibrary("server", 0 );
COM_LoadLibrary("client", 0 );
#endif
#if XASH_DEDICATED
// NodeJS support for debug
EM_ASM(try{
FS.mkdir('/xash');
FS.mount(NODEFS, { root: '.'}, '/xash' );
FS.chdir('/xash');
}catch(e){};);
#endif
#elif XASH_IOS
{
void IOS_LaunchDialog( void );
IOS_LaunchDialog();
}
#endif #endif
ret = Host_Main( szArgc, szArgv, game, 0, Launcher_ChangeGame );
return ret;
} }
#if XASH_WIN32 #if !XASH_WIN32
#include <windows.h> int main( int argc, char **argv )
#include <shellapi.h> // CommandLineToArgvW {
szArgc = argc;
szArgv = argv;
return Sys_Start();
}
#else
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow) int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow)
{ {
LPWSTR* lpArgv; LPWSTR* lpArgv;
@ -91,9 +115,9 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n
LocalFree( lpArgv ); LocalFree( lpArgv );
ret = main( szArgc, szArgv ); ret = Sys_Start();
for( i = 0; i < szArgc; ++i ) for( ; i < szArgc; ++i )
free( szArgv[i] ); free( szArgv[i] );
free( szArgv ); free( szArgv );
@ -101,41 +125,5 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n
} }
#endif // XASH_WIN32 #endif // XASH_WIN32
int main( int argc, char** argv )
{
const char *game = getenv( E_GAME );
if( !game )
game = GAME_PATH;
Q_strncpy( szGameDir, game, sizeof( szGameDir ));
#if XASH_EMSCRIPTEN
#ifdef EMSCRIPTEN_LIB_FS
// For some unknown reason emscripten refusing to load libraries later
COM_LoadLibrary("menu", 0 );
COM_LoadLibrary("server", 0 );
COM_LoadLibrary("client", 0 );
#endif
#if XASH_DEDICATED
// NodeJS support for debug
EM_ASM(try{
FS.mkdir('/xash');
FS.mount(NODEFS, { root: '.'}, '/xash' );
FS.chdir('/xash');
}catch(e){};);
#endif
#endif
g_iArgc = argc;
g_pszArgv = argv;
#if XASH_IOS
{
void IOS_LaunchDialog( void );
IOS_LaunchDialog();
}
#endif
return Host_Main( g_iArgc, g_pszArgv, szGameDir, 0, &Launcher_ChangeGame );
}
#endif #endif

42
game_launch/game.cpp

@ -26,18 +26,11 @@ GNU General Public License for more details.
#define LoadLibrary( x ) dlopen( x, RTLD_NOW ) #define LoadLibrary( x ) dlopen( x, RTLD_NOW )
#define GetProcAddress( x, y ) dlsym( x, y ) #define GetProcAddress( x, y ) dlsym( x, y )
#define FreeLibrary( x ) dlclose( x ) #define FreeLibrary( x ) dlclose( x )
#include <unistd.h> // execve
#elif XASH_WIN32 #elif XASH_WIN32
#define XASHLIB "xash.dll" #define XASHLIB "xash.dll"
#define SDL2LIB "SDL2.dll" #define SDL2LIB "SDL2.dll"
#define dlerror() GetStringLastError() #define dlerror() GetStringLastError()
#include <shellapi.h> // CommandLineToArgvW
#include <process.h> // _execve
#else
#error // port me!
#endif
#ifdef XASH_WIN32
extern "C" extern "C"
{ {
// Enable NVIDIA High Performance Graphics while using Integrated Graphics. // Enable NVIDIA High Performance Graphics while using Integrated Graphics.
@ -46,12 +39,8 @@ __declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
// Enable AMD High Performance Graphics while using Integrated Graphics. // Enable AMD High Performance Graphics while using Integrated Graphics.
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
} }
#endif
#if XASH_WIN32 || XASH_POSIX
#define USE_EXECVE_FOR_CHANGE_GAME 0
#else #else
#define USE_EXECVE_FOR_CHANGE_GAME 0 #error // port me!
#endif #endif
#define E_GAME "XASH3D_GAME" // default env dir to start from #define E_GAME "XASH3D_GAME" // default env dir to start from
@ -61,7 +50,6 @@ typedef void (*pfnChangeGame)( const char *progname );
typedef int (*pfnInit)( int argc, char **argv, const char *progname, int bChangeGame, pfnChangeGame func ); typedef int (*pfnInit)( int argc, char **argv, const char *progname, int bChangeGame, pfnChangeGame func );
typedef void (*pfnShutdown)( void ); typedef void (*pfnShutdown)( void );
extern char **environ;
static pfnInit Xash_Main; static pfnInit Xash_Main;
static pfnShutdown Xash_Shutdown = NULL; static pfnShutdown Xash_Shutdown = NULL;
static char szGameDir[128]; // safe place to keep gamedir static char szGameDir[128]; // safe place to keep gamedir
@ -105,10 +93,14 @@ static void Sys_LoadEngine( void )
#if XASH_WIN32 #if XASH_WIN32
HMODULE hSdl; HMODULE hSdl;
if ( ( hSdl = LoadLibraryEx( SDL2LIB, NULL, LOAD_LIBRARY_AS_DATAFILE ) ) == NULL ) if (( hSdl = LoadLibraryEx( SDL2LIB, NULL, LOAD_LIBRARY_AS_DATAFILE )) == NULL )
{
Xash_Error("Unable to load the " SDL2LIB ": %s", dlerror() ); Xash_Error("Unable to load the " SDL2LIB ": %s", dlerror() );
}
else else
{
FreeLibrary( hSdl ); FreeLibrary( hSdl );
}
#endif #endif
if(( hEngine = LoadLibrary( XASHLIB )) == NULL ) if(( hEngine = LoadLibrary( XASHLIB )) == NULL )
@ -130,29 +122,18 @@ static void Sys_UnloadEngine( void )
if( Xash_Shutdown ) Xash_Shutdown( ); if( Xash_Shutdown ) Xash_Shutdown( );
if( hEngine ) FreeLibrary( hEngine ); if( hEngine ) FreeLibrary( hEngine );
hEngine = NULL;
Xash_Main = NULL; Xash_Main = NULL;
Xash_Shutdown = NULL; Xash_Shutdown = NULL;
} }
static void Sys_ChangeGame( const char *progname ) static void Sys_ChangeGame( const char *progname )
{ {
// a1ba: may never be called within engine
// if platform supports execv() function
if( !progname || !progname[0] ) if( !progname || !progname[0] )
Xash_Error( "Sys_ChangeGame: NULL gamedir" ); Xash_Error( "Sys_ChangeGame: NULL gamedir" );
#if USE_EXECVE_FOR_CHANGE_GAME
#if XASH_WIN32
_putenv_s( E_GAME, progname );
Sys_UnloadEngine();
_execve( szArgv[0], szArgv, _environ );
#else
char envstr[256];
snprintf( envstr, sizeof( envstr ), E_GAME "=%s", progname );
putenv( envstr );
Sys_UnloadEngine();
execve( szArgv[0], szArgv, environ );
#endif
#else
if( Xash_Shutdown == NULL ) if( Xash_Shutdown == NULL )
Xash_Error( "Sys_ChangeGame: missed 'Host_Shutdown' export\n" ); Xash_Error( "Sys_ChangeGame: missed 'Host_Shutdown' export\n" );
@ -161,7 +142,6 @@ static void Sys_ChangeGame( const char *progname )
Sys_UnloadEngine(); Sys_UnloadEngine();
Sys_LoadEngine (); Sys_LoadEngine ();
Xash_Main( szArgc, szArgv, szGameDir, 1, Sys_ChangeGame ); Xash_Main( szArgc, szArgv, szGameDir, 1, Sys_ChangeGame );
#endif
} }
_inline int Sys_Start( void ) _inline int Sys_Start( void )
@ -173,12 +153,14 @@ _inline int Sys_Start( void )
if( !game ) if( !game )
game = GAME_PATH; game = GAME_PATH;
strncpy( szGameDir, game, sizeof( szGameDir ) - 1 );
Sys_LoadEngine(); Sys_LoadEngine();
if( Xash_Shutdown ) if( Xash_Shutdown )
changeGame = Sys_ChangeGame; changeGame = Sys_ChangeGame;
ret = Xash_Main( szArgc, szArgv, game, 0, changeGame ); ret = Xash_Main( szArgc, szArgv, szGameDir, 0, changeGame );
Sys_UnloadEngine(); Sys_UnloadEngine();

Loading…
Cancel
Save