loader/loader.cpp
changeset 820 a05f33ba5ad4
parent 818 1516c9026fd0
child 842 118a5744a851
     1.1 --- a/loader/loader.cpp	Mon Mar 04 11:07:15 2013 -0500
     1.2 +++ b/loader/loader.cpp	Fri Mar 22 20:47:35 2013 -0400
     1.3 @@ -33,11 +33,9 @@
     1.4  #include "serverplugin.h"
     1.5  #include "gamedll.h"
     1.6  #include "utility.h"
     1.7 -#include "valve_commandline.h"
     1.8 -
     1.9 -#undef GetCommandLine
    1.10 -
    1.11 -typedef ICommandLine *(*GetCommandLine)();
    1.12 +#if defined __APPLE__
    1.13 +#include <crt_externs.h>
    1.14 +#endif
    1.15  
    1.16  static HMODULE mm_library = NULL;
    1.17  static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    1.18 @@ -186,94 +184,81 @@
    1.19  	return mm_GetLibAddress(mm_library, name);
    1.20  }
    1.21  
    1.22 -#if defined _WIN32
    1.23 -#define TIER0_NAME			"bin\\tier0.dll"
    1.24 -#define VSTDLIB_NAME		"bin\\vstdlib.dll"
    1.25 -#elif defined __APPLE__
    1.26 -#define TIER0_NAME			"bin/libtier0.dylib"
    1.27 -#define VSTDLIB_NAME		"bin/libvstdlib.dylib"
    1.28 -#elif defined __linux__
    1.29 -#define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
    1.30 -#define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
    1.31 -#endif
    1.32 -
    1.33 -const char *
    1.34 -mm_GetGameName()
    1.35 +bool
    1.36 +mm_GetGameName(char *buffer, size_t size)
    1.37  {
    1.38 -	void *lib;
    1.39 -	char error[255];
    1.40 -	GetCommandLine valve_cmdline;
    1.41 -	char lib_path[PLATFORM_MAX_PATH];
    1.42 -	const char *game_name;
    1.43 +	buffer[0] = '\0';
    1.44 +
    1.45 +#if defined _WIN32
    1.46 +	static char game[128];
    1.47  
    1.48 -#ifdef __linux__
    1.49 -	if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path))
    1.50 -		&& !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path))
    1.51 -		&& !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path)))
    1.52 -#else
    1.53 -        if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
    1.54 -#endif
    1.55 +	LPWSTR pCmdLine = GetCommandLineW();
    1.56 +	int argc;
    1.57 +	LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc);
    1.58 +	for (int i = 0; i < argc; ++i)
    1.59  	{
    1.60 -		mm_LogFatal("Could not find path for tier0");
    1.61 -		return NULL;
    1.62 -	}
    1.63 +		if (wcscmp(wargv[i], L"-game") != 0)
    1.64 +			continue;
    1.65  
    1.66 -	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
    1.67 -	{
    1.68 -		mm_LogFatal("Could not load %s: %s", lib_path, error);
    1.69 -		return NULL;
    1.70 +		if (++i >= argc)
    1.71 +			break;
    1.72 +
    1.73 +		wcstombs(buffer, wargv[i], size);
    1.74 +		buffer[size-1] = '\0';
    1.75 +		break;
    1.76  	}
    1.77  
    1.78 -	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
    1.79 +	LocalFree(wargv);
    1.80  
    1.81 -	/* '_Tier0' dropped on Alien Swarm version */
    1.82 -	if (valve_cmdline == NULL)
    1.83 +	return buffer[0] != 0;
    1.84 +#elif defined __APPLE__
    1.85 +	int argc = *_NSGetArgc();
    1.86 +	char **argv = *_NSGetArgv();
    1.87 +	for (int i = 0; i < argc; ++i)
    1.88  	{
    1.89 -		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
    1.90 +		if (strcmp(argv[i], "-game") != 0)
    1.91 +			continue;
    1.92 +
    1.93 +		if (++i >= argc)
    1.94 +			break;
    1.95 +
    1.96 +		strncpy(buffer, argv[i], size);
    1.97 +		buffer[size-1] = '\0';
    1.98 +		break;
    1.99  	}
   1.100  
   1.101 -	if (valve_cmdline == NULL)
   1.102 +	return buffer[0] != 0;
   1.103 +#elif defined __linux__
   1.104 +	FILE *pFile = fopen("/proc/self/cmdline", "rb");
   1.105 +	if (!pFile)
   1.106 +		return false;
   1.107 +
   1.108 +	char *arg = NULL;
   1.109 +	size_t argsize = 0;
   1.110 +	bool bNextIsGame = false;
   1.111 +
   1.112 +	while (getdelim(&arg, &argsize, NULL, pFile) != -1)
   1.113  	{
   1.114 -		/* We probably have a Ship engine. */
   1.115 -		mm_UnloadLibrary(lib);
   1.116 -#ifdef __linux__
   1.117 -		if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path))
   1.118 -			&& !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path))
   1.119 -			&& !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path)))
   1.120 -#else
   1.121 -		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   1.122 -#endif
   1.123 +		if (bNextIsGame)
   1.124  		{
   1.125 -			mm_LogFatal("Could not find path for vstdlib");
   1.126 -			return NULL;
   1.127 +			strncpy(buffer, arg, size);
   1.128 +			buffer[size-1] = '\0';
   1.129 +			break;
   1.130  		}
   1.131  
   1.132 -		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   1.133 +		if (strcmp(arg, "-game") == 0)
   1.134  		{
   1.135 -			mm_LogFatal("Could not load %s: %s", lib_path, error);
   1.136 -			return NULL;
   1.137 +			bNextIsGame = true;
   1.138  		}
   1.139 -
   1.140 -		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   1.141  	}
   1.142  
   1.143 -	if (valve_cmdline == NULL)
   1.144 -	{
   1.145 -		mm_LogFatal("Could not locate any command line functionality");
   1.146 -		return NULL;
   1.147 -	}
   1.148 -
   1.149 -	game_name = valve_cmdline()->ParmValue("-game");
   1.150 +	free(arg);
   1.151 +	fclose(pFile);
   1.152  
   1.153 -	mm_UnloadLibrary(lib);
   1.154 -
   1.155 -	/* This probably means that the game directory is actually the current directory */
   1.156 -	if (!game_name)
   1.157 -	{
   1.158 -		game_name = ".";
   1.159 -	}
   1.160 -
   1.161 -	return game_name;
   1.162 +	return buffer[0] != 0;
   1.163 +#else
   1.164 +#error unsupported platform
   1.165 +#endif
   1.166  }
   1.167  
   1.168  MetamodBackend