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. @@ -23,54 +23,78 @@ GNU General Public License for more details.
#if XASH_EMSCRIPTEN
#include <emscripten.h>
#endif
#if XASH_WIN32
#include <process.h> // _execve
#else
#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;
#elif XASH_WIN32
extern "C"
{
// Enable NVIDIA High Performance Graphics while using Integrated Graphics.
__declspec(dllexport) DWORD NvOptimusEnablement = 0x00000001;
#if XASH_WIN32 || XASH_POSIX
#define USE_EXECVE_FOR_CHANGE_GAME 1
#else
#define USE_EXECVE_FOR_CHANGE_GAME 0
// Enable AMD High Performance Graphics while using Integrated Graphics.
__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
}
#endif
#define E_GAME "XASH3D_GAME" // default env 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
Host_Shutdown();
_inline int Sys_Start( void )
{
int ret;
const char *game = getenv( E_GAME );
#if XASH_WIN32
_putenv_s( E_GAME, progname );
_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
if( !game )
game = GAME_PATH;
#else
Q_strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 );
Host_Shutdown( );
exit( Host_Main( g_iArgc, g_pszArgv, szGameDir, 1, &Launcher_ChangeGame ) );
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
#elif XASH_IOS
{
void IOS_LaunchDialog( void );
IOS_LaunchDialog();
}
#endif
ret = Host_Main( szArgc, szArgv, game, 0, Launcher_ChangeGame );
return ret;
}
#if XASH_WIN32
#include <windows.h>
#include <shellapi.h> // CommandLineToArgvW
#if !XASH_WIN32
int main( int argc, char **argv )
{
szArgc = argc;
szArgv = argv;
return Sys_Start();
}
#else
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow)
{
LPWSTR* lpArgv;
@ -91,9 +115,9 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n @@ -91,9 +115,9 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n
LocalFree( lpArgv );
ret = main( szArgc, szArgv );
ret = Sys_Start();
for( i = 0; i < szArgc; ++i )
for( ; i < szArgc; ++i )
free( szArgv[i] );
free( szArgv );
@ -101,41 +125,5 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n @@ -101,41 +125,5 @@ int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int n
}
#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

42
game_launch/game.cpp

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

Loading…
Cancel
Save