game_launch: change game with execve on supported platforms

This commit is contained in:
Alibek Omarov 2022-06-09 13:42:16 +03:00
parent eeb170af22
commit 07a9c4602d
2 changed files with 93 additions and 53 deletions

View File

@ -25,63 +25,82 @@ GNU General Public License for more details.
#include <emscripten.h> #include <emscripten.h>
#endif #endif
#if XASH_WIN32
#define XASH_NOCONHOST 1
#endif
static char szGameDir[128]; // safe place to keep gamedir static char szGameDir[128]; // safe place to keep gamedir
static int g_iArgc; static int g_iArgc;
static char **g_pszArgv; static char **g_pszArgv;
#if XASH_WIN32 || XASH_POSIX
#define USE_EXECVE_FOR_CHANGE_GAME 1
#else
#define USE_EXECVE_FOR_CHANGE_GAME 0
#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 ) void Launcher_ChangeGame( const char *progname )
{ {
#if USE_EXECVE_FOR_CHANGE_GAME
Host_Shutdown();
#if XASH_WIN32
_putenv_s( E_GAME, progname );
_execve( szArgv[0], szArgv, environ );
#else
char envstr[256];
snprintf( envstr, sizeof( envstr ), E_GAME "=%s", progname );
putenv( envstr );
execve( szArgv[0], szArgv, environ );
#endif
#else
Q_strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 ); Q_strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 );
Host_Shutdown( ); Host_Shutdown( );
exit( Host_Main( g_iArgc, g_pszArgv, szGameDir, 1, &Launcher_ChangeGame ) ); exit( Host_Main( g_iArgc, g_pszArgv, szGameDir, 1, &Launcher_ChangeGame ) );
#endif
} }
#if XASH_NOCONHOST #if XASH_WIN32
#include <windows.h> #include <windows.h>
#include <shellapi.h> // CommandLineToArgvW #include <shellapi.h> // CommandLineToArgvW
int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow) int __stdcall WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int nShow)
{ {
int szArgc; LPWSTR* lpArgv;
char **szArgv; int ret, i;
LPWSTR* lpArgv = CommandLineToArgvW(GetCommandLineW(), &szArgc);
int i = 0;
szArgv = (char**)malloc(szArgc*sizeof(char*)); lpArgv = CommandLineToArgvW( GetCommandLineW(), &szArgc );
for (; i < szArgc; ++i) szArgv = ( char** )malloc( (szArgc + 1) * sizeof( char* ));
for( i = 0; i < szArgc; ++i )
{ {
size_t size = wcslen(lpArgv[i]) + 1; size_t size = wcslen(lpArgv[i]) + 1;
szArgv[i] = (char*)malloc(size);
wcstombs(szArgv[i], lpArgv[i], size); // just in case, allocate some more memory
szArgv[i] = ( char * )malloc( size * sizeof( wchar_t ));
wcstombs( szArgv[i], lpArgv[i], size );
} }
szArgv[i] = NULL; szArgv[szArgc] = 0;
LocalFree(lpArgv); LocalFree( lpArgv );
main( szArgc, szArgv ); ret = main( szArgc, szArgv );
for( i = 0; i < szArgc; ++i ) for( i = 0; i < szArgc; ++i )
free( szArgv[i] ); free( szArgv[i] );
free( szArgv ); free( szArgv );
return ret;
} }
#endif #endif // XASH_WIN32
int main( int argc, char** argv ) int main( int argc, char** argv )
{ {
char gamedir_buf[32] = ""; const char *game = getenv( E_GAME );
const char *gamedir = getenv( "XASH3D_GAMEDIR" );
if( !COM_CheckString( gamedir ) ) if( !game )
{ game = GAME_PATH;
gamedir = "valve";
} Q_strncpy( szGameDir, gamedir, sizeof( szGameDir ));
else
{
Q_strncpy( gamedir_buf, gamedir, 32 );
gamedir = gamedir_buf;
}
#if XASH_EMSCRIPTEN #if XASH_EMSCRIPTEN
#ifdef EMSCRIPTEN_LIB_FS #ifdef EMSCRIPTEN_LIB_FS
@ -108,7 +127,7 @@ int main( int argc, char** argv )
IOS_LaunchDialog(); IOS_LaunchDialog();
} }
#endif #endif
return Host_Main( g_iArgc, g_pszArgv, gamedir, 0, &Launcher_ChangeGame ); return Host_Main( g_iArgc, g_pszArgv, szGameDir, 0, &Launcher_ChangeGame );
} }
#endif #endif

View File

@ -14,24 +14,25 @@ GNU General Public License for more details.
*/ */
#include "port.h" #include "port.h"
#include "build.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <unistd.h>
#if defined(__APPLE__) || defined(__unix__) || defined(__HAIKU__) #if XASH_POSIX
#define XASHLIB "libxash." OS_LIB_EXT #define XASHLIB "libxash." OS_LIB_EXT
#elif _WIN32 #elif XASH_WIN32
#if !__MINGW32__ && _MSC_VER >= 1200 #define XASHLIB "xash.dll"
#define USE_WINMAIN #define dlerror() GetStringLastError()
#endif #include <shellapi.h> // CommandLineToArgvW
#define XASHLIB "xash.dll" #else
#define dlerror() GetStringLastError() #error // port me!
#include <shellapi.h> // CommandLineToArgvW
#endif #endif
#ifdef WIN32 #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.
@ -42,6 +43,12 @@ __declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1;
} }
#endif #endif
#if XASH_WIN32 || XASH_POSIX
#define USE_EXECVE_FOR_CHANGE_GAME 1
#else
#define USE_EXECVE_FOR_CHANGE_GAME 0
#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
@ -54,7 +61,7 @@ static pfnShutdown Xash_Shutdown = NULL;
static char szGameDir[128]; // safe place to keep gamedir static char szGameDir[128]; // safe place to keep gamedir
static int szArgc; static int szArgc;
static char **szArgv; static char **szArgv;
static HINSTANCE hEngine; static HINSTANCE hEngine;
static void Xash_Error( const char *szFmt, ... ) static void Xash_Error( const char *szFmt, ... )
{ {
@ -70,6 +77,7 @@ static void Xash_Error( const char *szFmt, ... )
#else #else
fprintf( stderr, "Xash Error: %s\n", buffer ); fprintf( stderr, "Xash Error: %s\n", buffer );
#endif #endif
exit( 1 ); exit( 1 );
} }
@ -116,32 +124,44 @@ static void Sys_ChangeGame( const char *progname )
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" );
Sys_UnloadEngine();
strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 ); strncpy( szGameDir, progname, sizeof( szGameDir ) - 1 );
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 )
{ {
int ret; int ret;
pfnChangeGame changeGame = NULL; pfnChangeGame changeGame = NULL;
const char *game = getenv( E_GAME );
if( !game )
game = GAME_PATH;
Sys_LoadEngine(); Sys_LoadEngine();
#ifndef XASH_DISABLE_MENU_CHANGEGAME
if( Xash_Shutdown ) if( Xash_Shutdown )
changeGame = Sys_ChangeGame; changeGame = Sys_ChangeGame;
#endif
const char *game = getenv( E_GAME );
if( !game )
game = GAME_PATH;
ret = Xash_Main( szArgc, szArgv, game, 0, changeGame ); ret = Xash_Main( szArgc, szArgv, game, 0, changeGame );
@ -150,7 +170,7 @@ _inline int Sys_Start( void )
return ret; return ret;
} }
#ifndef USE_WINMAIN #if !XASH_WIN32
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
szArgc = argc; szArgc = argc;
@ -159,7 +179,6 @@ int main( int argc, char **argv )
return Sys_Start(); return Sys_Start();
} }
#else #else
//#pragma comment(lib, "shell32.lib")
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;
@ -170,8 +189,10 @@ int __stdcall WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR cmdLine, int
for( i = 0; i < szArgc; ++i ) for( i = 0; i < szArgc; ++i )
{ {
int size = wcslen(lpArgv[i]) + 1; size_t size = wcslen(lpArgv[i]) + 1;
szArgv[i] = ( char* )malloc( size );
// just in case, allocate some more memory
szArgv[i] = ( char * )malloc( size * sizeof( wchar_t ));
wcstombs( szArgv[i], lpArgv[i], size ); wcstombs( szArgv[i], lpArgv[i], size );
} }
szArgv[szArgc] = 0; szArgv[szArgc] = 0;