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