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