loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sun Mar 23 23:37:46 2014 -0400 (2014-03-23)
changeset 909 57797db1d513
parent 896 0f4ca0bedc67
child 910 09101853db35
permissions -rw-r--r--
Add support for Contagion (bug 6006).
     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 	"2.hl2dm",
    85 	"2.dods",	
    86 	"2.tf2",
    87 	"2.nd",
    88 	"2.sdk2013",
    89 	"2.blade",
    90 	"2.insurgency",
    91 	"2.contagion",
    92 };
    93 
    94 #if defined _WIN32
    95 #define LIBRARY_EXT		".dll"
    96 #define LIBRARY_MINEXT	".dll"
    97 #elif defined __APPLE__
    98 #define LIBRARY_EXT		".dylib"
    99 #define LIBRARY_MINEXT	".dylib"
   100 #elif defined __linux__
   101 #define LIBRARY_EXT		LIB_SUFFIX
   102 #define LIBRARY_MINEXT	".so"
   103 #endif
   104 
   105 bool
   106 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
   107 {
   108 	size_t len, temp_len;
   109 	char mm_path[PLATFORM_MAX_PATH * 2];
   110 
   111 	/* Get our path */
   112 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
   113 		return false;
   114 
   115 	len = strlen(mm_path);
   116 	temp_len = strlen("server" LIBRARY_EXT);
   117 	if (len < temp_len)
   118 		return false;
   119 
   120 	/* Build log file name */
   121 	mm_path[len - temp_len] = '\0';
   122 	mm_Format(mm_fatal_logfile,
   123 			  sizeof(mm_fatal_logfile),
   124 			  "%smetamod-fatal.log",
   125 			  mm_path);
   126 
   127 	/* Replace server.dll with the new binary we want */
   128 	mm_Format(&mm_path[len - temp_len],
   129 			  sizeof(mm_path) - (len - temp_len),
   130 			  "metamod.%s" LIBRARY_MINEXT,
   131 			  backend_names[backend]);
   132 
   133 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   134 
   135 	return (mm_library != NULL);
   136 }
   137 
   138 void
   139 mm_UnloadMetamodLibrary()
   140 {
   141 	mm_UnloadLibrary(mm_library);
   142 	mm_library = NULL;
   143 }
   144 
   145 #if defined _WIN32
   146 #define EXPORT extern "C" __declspec(dllexport)
   147 #elif defined __GNUC__
   148 #if __GNUC__ == 4
   149 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   150 #else
   151 #define EXPORT extern "C"
   152 #endif
   153 #endif
   154 
   155 EXPORT void *
   156 CreateInterface(const char *name, int *ret)
   157 {
   158 	/* If we've got a VSP bridge, do nothing. */
   159 	if (vsp_bridge != NULL)
   160 	{
   161 		if (ret != NULL)
   162 			*ret = 1;
   163 		return NULL;
   164 	}
   165 
   166 	void *ptr;
   167 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   168 	{
   169 		/* Either load as VSP or start VSP listener */
   170 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   171 	}
   172 	else if (gamedll_bridge == NULL)
   173 	{
   174 		/* Load as gamedll */
   175 		ptr = mm_GameDllRequest(name, ret);
   176 	}
   177 	else
   178 	{
   179 		/* If we've got a gamedll bridge, forward the request. */
   180 		return gamedll_bridge->QueryInterface(name, ret);
   181 	}
   182 
   183 	if (ret != NULL)
   184 		*ret = (ptr != NULL) ? 0 : 1;
   185 
   186 	return ptr;
   187 }
   188 
   189 void *
   190 mm_GetProcAddress(const char *name)
   191 {
   192 	return mm_GetLibAddress(mm_library, name);
   193 }
   194 
   195 void
   196 mm_GetGameName(char *buffer, size_t size)
   197 {
   198 	buffer[0] = '\0';
   199 
   200 #if defined _WIN32
   201 	static char game[128];
   202 
   203 	LPWSTR pCmdLine = GetCommandLineW();
   204 	int argc;
   205 	LPWSTR *wargv = CommandLineToArgvW(pCmdLine, &argc);
   206 	for (int i = 0; i < argc; ++i)
   207 	{
   208 		if (wcscmp(wargv[i], L"-game") != 0)
   209 			continue;
   210 
   211 		if (++i >= argc)
   212 			break;
   213 
   214 		wcstombs(buffer, wargv[i], size);
   215 		buffer[size-1] = '\0';
   216 		break;
   217 	}
   218 
   219 	LocalFree(wargv);
   220 
   221 #elif defined __APPLE__
   222 	int argc = *_NSGetArgc();
   223 	char **argv = *_NSGetArgv();
   224 	for (int i = 0; i < argc; ++i)
   225 	{
   226 		if (strcmp(argv[i], "-game") != 0)
   227 			continue;
   228 
   229 		if (++i >= argc)
   230 			break;
   231 
   232 		strncpy(buffer, argv[i], size);
   233 		buffer[size-1] = '\0';
   234 		break;
   235 	}
   236 
   237 #elif defined __linux__
   238 	FILE *pFile = fopen("/proc/self/cmdline", "rb");
   239 	if (pFile)
   240 	{
   241 		char *arg = NULL;
   242 		size_t argsize = 0;
   243 		bool bNextIsGame = false;
   244 
   245 		while (getdelim(&arg, &argsize, 0, pFile) != -1)
   246 		{
   247 			if (bNextIsGame)
   248 			{
   249 				strncpy(buffer, arg, size);
   250 				buffer[size-1] = '\0';
   251 				break;
   252 			}
   253 
   254 			if (strcmp(arg, "-game") == 0)
   255 			{
   256 				bNextIsGame = true;
   257 			}
   258 		}
   259 
   260 		free(arg);
   261 		fclose(pFile);
   262 	}
   263 #else
   264 #error unsupported platform
   265 #endif
   266 
   267 	if (buffer[0] == 0)
   268 	{
   269 		strncpy(buffer, ".", size);
   270 	}
   271 }
   272 
   273 MetamodBackend
   274 mm_DetermineBackend(QueryValveInterface engineFactory, QueryValveInterface serverFactory, const char *game_name)
   275 {
   276 	if (engineFactory("VEngineServer024", NULL) != NULL)
   277 	{
   278 		return MMBackend_DOTA;
   279 	}
   280 	else if (engineFactory("VEngineServer023", NULL) != NULL)
   281 	{
   282 		if (engineFactory("IEngineSoundServer004", NULL) != NULL)
   283 		{
   284 			return MMBackend_Insurgency;
   285 		}
   286 		return MMBackend_CSGO;
   287 	}
   288 	else if (engineFactory("VEngineServer022", NULL) != NULL &&
   289 		engineFactory("VEngineCvar007", NULL) != NULL)
   290 	{
   291 		if (serverFactory("ServerGameClients004", NULL))
   292 		{
   293 			if (strcmp(game_name, "portal2") == 0)
   294 			{
   295 				return MMBackend_Portal2;
   296 			}
   297 
   298 			return MMBackend_Blade;
   299 		}
   300 
   301 		if (engineFactory("EngineTraceServer004", NULL) != NULL)
   302 		{
   303 			return MMBackend_AlienSwarm;
   304 		}
   305 		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   306 		{
   307 			if (strcmp(game_name, "nucleardawn") == 0)
   308 			{
   309 				return MMBackend_NuclearDawn;
   310 			}
   311 			else if (strcmp(game_name, "contagion") == 0)
   312 			{
   313 				return MMBackend_Contagion;
   314 			}
   315 			else
   316 			{
   317 				return MMBackend_Left4Dead2;
   318 			}
   319 		}
   320 
   321 		return MMBackend_Left4Dead;
   322 	}
   323 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   324 	{
   325 		/* Check for OB */
   326 		if (engineFactory("VEngineCvar004", NULL) != NULL)
   327 		{
   328 			if (engineFactory("VModelInfoServer002", NULL) != NULL)
   329 			{
   330 				/* BGT has same iface version numbers and libs as ep2 */
   331 				if (strcmp(game_name, "pm") == 0)
   332 				{
   333 					return MMBackend_BloodyGoodTime;
   334 				}
   335 				else
   336 				{
   337 					return MMBackend_Episode2;
   338 				}
   339 			}
   340 			else if (engineFactory("VModelInfoServer003", NULL) != NULL)
   341 			{
   342 				if (engineFactory("VFileSystem017", NULL) != NULL)
   343 				{
   344 					return MMBackend_EYE;
   345 				}
   346 				else if (strcmp(game_name, "cstrike") == 0)
   347 				{
   348 					return MMBackend_CSS;
   349 				}
   350 				else if (strcmp(game_name, "tf") == 0)
   351 				{
   352 					return MMBackend_TF2;
   353 				}
   354 				else if (strcmp(game_name, "dod") == 0)
   355 				{
   356 					return MMBackend_DODS;
   357 				}
   358 				else if (strcmp(game_name, "hl2mp") == 0)
   359 				{
   360 					return MMBackend_HL2DM;
   361 				}
   362 				else
   363 				{
   364 					return MMBackend_SDK2013;
   365 				}
   366 			}
   367 		}
   368 		/* Check for Episode One/Old Engine */
   369 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   370 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   371 				  engineFactory("VEngineCvar002", NULL) != NULL))
   372 		{
   373 			/* Check for Dark Messiah which has a weird directory structure */
   374 			if (strcmp(game_name, ".") == 0)
   375 			{
   376 				return MMBackend_DarkMessiah;
   377 			}
   378 			return MMBackend_Episode1;
   379 		}
   380 	}
   381 
   382 	return MMBackend_UNKNOWN;
   383 }
   384