diff --git a/loader/gamedll.cpp b/loader/gamedll.cpp index 15c9460..49832bf 100644 --- a/loader/gamedll.cpp +++ b/loader/gamedll.cpp @@ -41,7 +41,7 @@ class IServerGameDLL; IGameDllBridge* gamedll_bridge = NULL; static int game_info_detected = 0; -static const char *game_name = NULL; +static char game_name[128]; static char gamedll_paths[MAX_GAMEDLL_PATHS][PLATFORM_MAX_PATH]; static void *gamedll_libs[MAX_GAMEDLL_PATHS]; static unsigned int gamedll_path_count = 0; @@ -70,7 +70,7 @@ mm_DetectGameInformation() game_info_detected = -1; - if ((game_name = mm_GetGameName()) == NULL) + if (!mm_GetGameName(game_name, sizeof(game_name))) { return false; } diff --git a/loader/loader.cpp b/loader/loader.cpp index 7f3cc71..653bef0 100644 --- a/loader/loader.cpp +++ b/loader/loader.cpp @@ -33,11 +33,9 @@ #include "serverplugin.h" #include "gamedll.h" #include "utility.h" -#include "valve_commandline.h" - -#undef GetCommandLine - -typedef ICommandLine *(*GetCommandLine)(); +#if defined __APPLE__ +#include +#endif static HMODULE mm_library = NULL; static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log"; @@ -186,94 +184,81 @@ mm_GetProcAddress(const char *name) return mm_GetLibAddress(mm_library, name); } -#if defined _WIN32 -#define TIER0_NAME "bin\\tier0.dll" -#define VSTDLIB_NAME "bin\\vstdlib.dll" -#elif defined __APPLE__ -#define TIER0_NAME "bin/libtier0.dylib" -#define VSTDLIB_NAME "bin/libvstdlib.dylib" -#elif defined __linux__ -#define TIER0_NAME "bin/" LIB_PREFIX "tier0" LIB_SUFFIX -#define VSTDLIB_NAME "bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX -#endif - -const char * -mm_GetGameName() +bool +mm_GetGameName(char *buffer, size_t size) { - void *lib; - char error[255]; - GetCommandLine valve_cmdline; - char lib_path[PLATFORM_MAX_PATH]; - const char *game_name; + buffer[0] = '\0'; -#ifdef __linux__ - if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path)) - && !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path)) - && !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path))) -#else - if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path))) -#endif +#if defined _WIN32 + static char game[128]; + + LPWSTR pCmdLine = GetCommandLineW(); + int argc; + LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc); + for (int i = 0; i < argc; ++i) { - mm_LogFatal("Could not find path for tier0"); - return NULL; + if (wcscmp(wargv[i], L"-game") != 0) + continue; + + if (++i >= argc) + break; + + wcstombs(buffer, wargv[i], size); + buffer[size-1] = '\0'; + break; } - if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL) + LocalFree(wargv); + + return buffer[0] != 0; +#elif defined __APPLE__ + int argc = *_NSGetArgc(); + char **argv = *_NSGetArgv(); + for (int i = 0; i < argc; ++i) { - mm_LogFatal("Could not load %s: %s", lib_path, error); - return NULL; + if (strcmp(argv[i], "-game") != 0) + continue; + + if (++i >= argc) + break; + + strncpy(buffer, argv[i], size); + buffer[size-1] = '\0'; + break; } - valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0"); + return buffer[0] != 0; +#elif defined __linux__ + FILE *pFile = fopen("/proc/self/cmdline", "rb"); + if (!pFile) + return false; - /* '_Tier0' dropped on Alien Swarm version */ - if (valve_cmdline == NULL) - { - valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine"); - } + char *arg = NULL; + size_t argsize = 0; + bool bNextIsGame = false; - if (valve_cmdline == NULL) + while (getdelim(&arg, &argsize, NULL, pFile) != -1) { - /* We probably have a Ship engine. */ - mm_UnloadLibrary(lib); -#ifdef __linux__ - if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path)) - && !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path)) - && !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path))) -#else - if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path))) -#endif + if (bNextIsGame) { - mm_LogFatal("Could not find path for vstdlib"); - return NULL; + strncpy(buffer, arg, size); + buffer[size-1] = '\0'; + break; } - if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL) + if (strcmp(arg, "-game") == 0) { - mm_LogFatal("Could not load %s: %s", lib_path, error); - return NULL; + bNextIsGame = true; } - - valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine"); } - if (valve_cmdline == NULL) - { - mm_LogFatal("Could not locate any command line functionality"); - return NULL; - } + free(arg); + fclose(pFile); - game_name = valve_cmdline()->ParmValue("-game"); - - mm_UnloadLibrary(lib); - - /* This probably means that the game directory is actually the current directory */ - if (!game_name) - { - game_name = "."; - } - - return game_name; + return buffer[0] != 0; +#else +#error unsupported platform +#endif } MetamodBackend diff --git a/loader/loader.h b/loader/loader.h index 3cdadaa..0a21686 100644 --- a/loader/loader.h +++ b/loader/loader.h @@ -108,8 +108,8 @@ mm_UnloadMetamodLibrary(); extern void mm_LogFatal(const char *message, ...); -extern const char * -mm_GetGameName(); +extern bool +mm_GetGameName(char *buffer, size_t size); extern MetamodBackend mm_DetermineBackend(QueryValveInterface qvi, const char *game_name); diff --git a/loader/serverplugin.cpp b/loader/serverplugin.cpp index 0ae2e1f..be4f17c 100644 --- a/loader/serverplugin.cpp +++ b/loader/serverplugin.cpp @@ -70,7 +70,7 @@ IVspBridge *vsp_bridge = NULL; */ class ServerPlugin { - const char *game_name; + char game_name[128]; unsigned int vsp_version; bool load_allowed; public: @@ -88,7 +88,7 @@ public: /* Backend should already filled in if loaded as gamedll */ if (gamedll_bridge == NULL) { - if ((game_name = mm_GetGameName()) == NULL) + if (!mm_GetGameName(game_name, sizeof(game_name))) { return false; } diff --git a/loader/valve_commandline.h b/loader/valve_commandline.h deleted file mode 100644 index e4e8319..0000000 --- a/loader/valve_commandline.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _INCLUDE_VALVE_COMMAND_LINE_H_ -#define _INCLUDE_VALVE_COMMAND_LINE_H_ - -class ICommandLine -{ -public: - virtual void CreateCmdLine( const char *commandline ) = 0; - virtual void CreateCmdLine( int argc, char **argv ) = 0; - virtual const char *GetCmdLine( void ) const = 0; - - // Check whether a particular parameter exists - virtual const char *CheckParm( const char *psz, const char **ppszValue = 0 ) const = 0; - virtual void RemoveParm( const char *parm ) = 0; - virtual void AppendParm( const char *pszParm, const char *pszValues ) = 0; - - // Returns the argument after the one specified, or the default if not found - virtual const char *ParmValue( const char *psz, const char *pDefaultVal = 0 ) const = 0; - virtual int ParmValue( const char *psz, int nDefaultVal ) const = 0; - virtual float ParmValue( const char *psz, float flDefaultVal ) const = 0; - - // Gets at particular parameters - virtual int ParmCount() const = 0; - virtual int FindParm( const char *psz ) const = 0; // Returns 0 if not found. - virtual const char* GetParm( int nIndex ) const = 0; -}; - -#endif /* _INCLUDE_VALVE_COMMAND_LINE_H_ */ -