loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat Jul 06 12:27:14 2013 -0400 (2013-07-06)
changeset 848 ac037b3e3dcd
parent 842 118a5744a851
child 850 3d9258ef9a30
permissions -rw-r--r--
Fixed NULL conversion warning in loader.
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2010 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     9  * This software is provided 'as-is', without any express or implied warranty.
    10  * In no event will the authors be held liable for any damages arising from 
    11  * the use of this software.
    12  * 
    13  * Permission is granted to anyone to use this software for any purpose, 
    14  * including commercial applications, and to alter it and redistribute it 
    15  * freely, subject to the following restrictions:
    16  *
    17  * 1. The origin of this software must not be misrepresented; you must not 
    18  * claim that you wrote the original software. If you use this software in a 
    19  * product, an acknowledgment in the product documentation would be 
    20  * appreciated but is not required.
    21  * 2. Altered source versions must be plainly marked as such, and must not be
    22  * misrepresented as being the original software.
    23  * 3. This notice may not be removed or altered from any source distribution.
    24  */
    25 
    26 #include <time.h>
    27 #include <assert.h>
    28 #include <stdarg.h>
    29 #include <stdio.h>
    30 #include <stdlib.h>
    31 #include <string.h>
    32 #include "loader.h"
    33 #include "serverplugin.h"
    34 #include "gamedll.h"
    35 #include "utility.h"
    36 #if defined __APPLE__
    37 #include <crt_externs.h>
    38 #endif
    39 
    40 static HMODULE mm_library = NULL;
    41 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    42 MetamodBackend mm_backend = MMBackend_UNKNOWN;
    43 
    44 extern void
    45 mm_LogFatal(const char *message, ...)
    46 {
    47 	FILE *fp;
    48 	time_t t;
    49 	va_list ap;
    50 	char header[256];
    51 
    52 	fp = fopen(mm_fatal_logfile, "at");
    53 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    54 		return;
    55 
    56 	t = time(NULL);
    57 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    58 	fprintf(fp, "L %s: ", header);
    59 	
    60 	va_start(ap, message);
    61 	vfprintf(fp, message, ap);
    62 	va_end(ap);
    63 
    64 	fprintf(fp, "\n");
    65 
    66 	fclose(fp);	
    67 }
    68 
    69 static const char *backend_names[] =
    70 {
    71 	"1.ep1",
    72 	"2.darkm",
    73 	"2.ep2",
    74 	"2.bgt",
    75 	"2.eye",
    76 	"2.css",
    77 	"2.ep2v",
    78 	"2.l4d",
    79 	"2.l4d2",
    80 	"2.swarm",
    81 	"2.portal2",
    82 	"2.csgo",
    83 	"2.dota",
    84 };
    85 
    86 #if defined _WIN32
    87 #define LIBRARY_EXT		".dll"
    88 #define LIBRARY_MINEXT	".dll"
    89 #elif defined __APPLE__
    90 #define LIBRARY_EXT		".dylib"
    91 #define LIBRARY_MINEXT	".dylib"
    92 #elif defined __linux__
    93 #define LIBRARY_EXT		LIB_SUFFIX
    94 #define LIBRARY_MINEXT	".so"
    95 #endif
    96 
    97 bool
    98 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    99 {
   100 	size_t len, temp_len;
   101 	char mm_path[PLATFORM_MAX_PATH * 2];
   102 
   103 	/* Get our path */
   104 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
   105 		return false;
   106 
   107 	len = strlen(mm_path);
   108 	temp_len = strlen("server" LIBRARY_EXT);
   109 	if (len < temp_len)
   110 		return false;
   111 
   112 	/* Build log file name */
   113 	mm_path[len - temp_len] = '\0';
   114 	mm_Format(mm_fatal_logfile,
   115 			  sizeof(mm_fatal_logfile),
   116 			  "%smetamod-fatal.log",
   117 			  mm_path);
   118 
   119 	/* Replace server.dll with the new binary we want */
   120 	mm_Format(&mm_path[len - temp_len],
   121 			  sizeof(mm_path) - (len - temp_len),
   122 			  "metamod.%s" LIBRARY_MINEXT,
   123 			  backend_names[backend]);
   124 
   125 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   126 
   127 	return (mm_library != NULL);
   128 }
   129 
   130 void
   131 mm_UnloadMetamodLibrary()
   132 {
   133 	mm_UnloadLibrary(mm_library);
   134 	mm_library = NULL;
   135 }
   136 
   137 #if defined _WIN32
   138 #define EXPORT extern "C" __declspec(dllexport)
   139 #elif defined __GNUC__
   140 #if __GNUC__ == 4
   141 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   142 #else
   143 #define EXPORT extern "C"
   144 #endif
   145 #endif
   146 
   147 EXPORT void *
   148 CreateInterface(const char *name, int *ret)
   149 {
   150 	/* If we've got a VSP bridge, do nothing. */
   151 	if (vsp_bridge != NULL)
   152 	{
   153 		if (ret != NULL)
   154 			*ret = 1;
   155 		return NULL;
   156 	}
   157 
   158 	void *ptr;
   159 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   160 	{
   161 		/* Either load as VSP or start VSP listener */
   162 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   163 	}
   164 	else if (gamedll_bridge == NULL)
   165 	{
   166 		/* Load as gamedll */
   167 		ptr = mm_GameDllRequest(name, ret);
   168 	}
   169 	else
   170 	{
   171 		/* If we've got a gamedll bridge, forward the request. */
   172 		return gamedll_bridge->QueryInterface(name, ret);
   173 	}
   174 
   175 	if (ret != NULL)
   176 		*ret = (ptr != NULL) ? 0 : 1;
   177 
   178 	return ptr;
   179 }
   180 
   181 void *
   182 mm_GetProcAddress(const char *name)
   183 {
   184 	return mm_GetLibAddress(mm_library, name);
   185 }
   186 
   187 bool
   188 mm_GetGameName(char *buffer, size_t size)
   189 {
   190 	buffer[0] = '\0';
   191 
   192 #if defined _WIN32
   193 	static char game[128];
   194 
   195 	LPWSTR pCmdLine = GetCommandLineW();
   196 	int argc;
   197 	LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc);
   198 	for (int i = 0; i < argc; ++i)
   199 	{
   200 		if (wcscmp(wargv[i], L"-game") != 0)
   201 			continue;
   202 
   203 		if (++i >= argc)
   204 			break;
   205 
   206 		wcstombs(buffer, wargv[i], size);
   207 		buffer[size-1] = '\0';
   208 		break;
   209 	}
   210 
   211 	LocalFree(wargv);
   212 
   213 	return buffer[0] != 0;
   214 #elif defined __APPLE__
   215 	int argc = *_NSGetArgc();
   216 	char **argv = *_NSGetArgv();
   217 	for (int i = 0; i < argc; ++i)
   218 	{
   219 		if (strcmp(argv[i], "-game") != 0)
   220 			continue;
   221 
   222 		if (++i >= argc)
   223 			break;
   224 
   225 		strncpy(buffer, argv[i], size);
   226 		buffer[size-1] = '\0';
   227 		break;
   228 	}
   229 
   230 	return buffer[0] != 0;
   231 #elif defined __linux__
   232 	FILE *pFile = fopen("/proc/self/cmdline", "rb");
   233 	if (!pFile)
   234 		return false;
   235 
   236 	char *arg = NULL;
   237 	size_t argsize = 0;
   238 	bool bNextIsGame = false;
   239 
   240 	while (getdelim(&arg, &argsize, 0, pFile) != -1)
   241 	{
   242 		if (bNextIsGame)
   243 		{
   244 			strncpy(buffer, arg, size);
   245 			buffer[size-1] = '\0';
   246 			break;
   247 		}
   248 
   249 		if (strcmp(arg, "-game") == 0)
   250 		{
   251 			bNextIsGame = true;
   252 		}
   253 	}
   254 
   255 	free(arg);
   256 	fclose(pFile);
   257 
   258 	return buffer[0] != 0;
   259 #else
   260 #error unsupported platform
   261 #endif
   262 }
   263 
   264 MetamodBackend
   265 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   266 {
   267 	if (engineFactory("VEngineServer024", NULL) != NULL)
   268 	{
   269 		return MMBackend_DOTA;
   270 	}
   271 	else if (engineFactory("VEngineServer023", NULL) != NULL)
   272 	{
   273 		return MMBackend_CSGO;
   274 	}
   275 	else if (engineFactory("VEngineServer022", NULL) != NULL &&
   276 		engineFactory("VEngineCvar007", NULL) != NULL)
   277 	{
   278 		if (strcmp(game_name, "portal2") == 0)
   279 		{
   280 			return MMBackend_Portal2;
   281 		}
   282 		if (engineFactory("EngineTraceServer004", NULL) != NULL)
   283 		{
   284 			return MMBackend_AlienSwarm;
   285 		}
   286 		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   287 		{
   288 			return MMBackend_Left4Dead2;
   289 		}
   290 		return MMBackend_Left4Dead;
   291 	}
   292 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   293 	{
   294 		/* Check for OB */
   295 		if (engineFactory("VEngineCvar004", NULL) != NULL)
   296 		{
   297 			if (engineFactory("VModelInfoServer002", NULL) != NULL)
   298 			{
   299 				/* BGT has same iface version numbers and libs as ep2 */
   300 				if (strcmp(game_name, "pm") == 0)
   301 				{
   302 					return MMBackend_BloodyGoodTime;
   303 				}
   304 				else
   305 				{
   306 					return MMBackend_Episode2;
   307 				}
   308 			}
   309 			else if (engineFactory("VModelInfoServer003", NULL) != NULL)
   310 			{
   311 				if (engineFactory("VFileSystem017", NULL) != NULL)
   312 				{
   313 					return MMBackend_EYE;
   314 				}
   315 				else if (strcmp(game_name, "cstrike") == 0)
   316 				{
   317 					return MMBackend_CSS;
   318 				}
   319 				else
   320 				{
   321 					return MMBackend_Episode2Valve;
   322 				}
   323 			}
   324 		}
   325 		/* Check for Episode One/Old Engine */
   326 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   327 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   328 				  engineFactory("VEngineCvar002", NULL) != NULL))
   329 		{
   330 			/* Check for Dark Messiah which has a weird directory structure */
   331 			if (strcmp(game_name, ".") == 0)
   332 			{
   333 				return MMBackend_DarkMessiah;
   334 			}
   335 			return MMBackend_Episode1;
   336 		}
   337 	}
   338 
   339 	return MMBackend_UNKNOWN;
   340 }
   341