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