loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Mar 04 10:38:17 2013 -0500 (2013-03-04)
changeset 818 1516c9026fd0
parent 809 6375d8da3383
child 820 a05f33ba5ad4
permissions -rw-r--r--
Added support for Dota 2 (bug 5640).
     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 #include "valve_commandline.h"
    37 
    38 #undef GetCommandLine
    39 
    40 typedef ICommandLine *(*GetCommandLine)();
    41 
    42 static HMODULE mm_library = NULL;
    43 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    44 MetamodBackend mm_backend = MMBackend_UNKNOWN;
    45 
    46 extern void
    47 mm_LogFatal(const char *message, ...)
    48 {
    49 	FILE *fp;
    50 	time_t t;
    51 	va_list ap;
    52 	char header[256];
    53 
    54 	fp = fopen(mm_fatal_logfile, "at");
    55 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    56 		return;
    57 
    58 	t = time(NULL);
    59 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    60 	fprintf(fp, "L %s: ", header);
    61 	
    62 	va_start(ap, message);
    63 	vfprintf(fp, message, ap);
    64 	va_end(ap);
    65 
    66 	fprintf(fp, "\n");
    67 
    68 	fclose(fp);	
    69 }
    70 
    71 static const char *backend_names[] =
    72 {
    73 	"1.ep1",
    74 	"2.darkm",
    75 	"2.ep2",
    76 	"2.bgt",
    77 	"2.eye",
    78 	"2.css",
    79 	"2.ep2v",
    80 	"2.l4d",
    81 	"2.l4d2",
    82 	"2.swarm",
    83 	"2.portal2",
    84 	"2.csgo",
    85 	"2.dota",
    86 };
    87 
    88 #if defined _WIN32
    89 #define LIBRARY_EXT		".dll"
    90 #define LIBRARY_MINEXT	".dll"
    91 #elif defined __APPLE__
    92 #define LIBRARY_EXT		".dylib"
    93 #define LIBRARY_MINEXT	".dylib"
    94 #elif defined __linux__
    95 #define LIBRARY_EXT		LIB_SUFFIX
    96 #define LIBRARY_MINEXT	".so"
    97 #endif
    98 
    99 bool
   100 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
   101 {
   102 	size_t len, temp_len;
   103 	char mm_path[PLATFORM_MAX_PATH * 2];
   104 
   105 	/* Get our path */
   106 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
   107 		return false;
   108 
   109 	len = strlen(mm_path);
   110 	temp_len = strlen("server" LIBRARY_EXT);
   111 	if (len < temp_len)
   112 		return false;
   113 
   114 	/* Build log file name */
   115 	mm_path[len - temp_len] = '\0';
   116 	mm_Format(mm_fatal_logfile,
   117 			  sizeof(mm_fatal_logfile),
   118 			  "%smetamod-fatal.log",
   119 			  mm_path);
   120 
   121 	/* Replace server.dll with the new binary we want */
   122 	mm_Format(&mm_path[len - temp_len],
   123 			  sizeof(mm_path) - (len - temp_len),
   124 			  "metamod.%s" LIBRARY_MINEXT,
   125 			  backend_names[backend]);
   126 
   127 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   128 
   129 	return (mm_library != NULL);
   130 }
   131 
   132 void
   133 mm_UnloadMetamodLibrary()
   134 {
   135 	mm_UnloadLibrary(mm_library);
   136 	mm_library = NULL;
   137 }
   138 
   139 #if defined _WIN32
   140 #define EXPORT extern "C" __declspec(dllexport)
   141 #elif defined __GNUC__
   142 #if __GNUC__ == 4
   143 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   144 #else
   145 #define EXPORT extern "C"
   146 #endif
   147 #endif
   148 
   149 EXPORT void *
   150 CreateInterface(const char *name, int *ret)
   151 {
   152 	/* If we've got a VSP bridge, do nothing. */
   153 	if (vsp_bridge != NULL)
   154 	{
   155 		if (ret != NULL)
   156 			*ret = 1;
   157 		return NULL;
   158 	}
   159 
   160 	void *ptr;
   161 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   162 	{
   163 		/* Either load as VSP or start VSP listener */
   164 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   165 	}
   166 	else if (gamedll_bridge == NULL)
   167 	{
   168 		/* Load as gamedll */
   169 		ptr = mm_GameDllRequest(name, ret);
   170 	}
   171 	else
   172 	{
   173 		/* If we've got a gamedll bridge, forward the request. */
   174 		return gamedll_bridge->QueryInterface(name, ret);
   175 	}
   176 
   177 	if (ret != NULL)
   178 		*ret = (ptr != NULL) ? 0 : 1;
   179 
   180 	return ptr;
   181 }
   182 
   183 void *
   184 mm_GetProcAddress(const char *name)
   185 {
   186 	return mm_GetLibAddress(mm_library, name);
   187 }
   188 
   189 #if defined _WIN32
   190 #define TIER0_NAME			"bin\\tier0.dll"
   191 #define VSTDLIB_NAME		"bin\\vstdlib.dll"
   192 #elif defined __APPLE__
   193 #define TIER0_NAME			"bin/libtier0.dylib"
   194 #define VSTDLIB_NAME		"bin/libvstdlib.dylib"
   195 #elif defined __linux__
   196 #define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
   197 #define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
   198 #endif
   199 
   200 const char *
   201 mm_GetGameName()
   202 {
   203 	void *lib;
   204 	char error[255];
   205 	GetCommandLine valve_cmdline;
   206 	char lib_path[PLATFORM_MAX_PATH];
   207 	const char *game_name;
   208 
   209 #ifdef __linux__
   210 	if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path))
   211 		&& !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path))
   212 		&& !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path)))
   213 #else
   214         if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
   215 #endif
   216 	{
   217 		mm_LogFatal("Could not find path for tier0");
   218 		return NULL;
   219 	}
   220 
   221 	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   222 	{
   223 		mm_LogFatal("Could not load %s: %s", lib_path, error);
   224 		return NULL;
   225 	}
   226 
   227 	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
   228 
   229 	/* '_Tier0' dropped on Alien Swarm version */
   230 	if (valve_cmdline == NULL)
   231 	{
   232 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   233 	}
   234 
   235 	if (valve_cmdline == NULL)
   236 	{
   237 		/* We probably have a Ship engine. */
   238 		mm_UnloadLibrary(lib);
   239 #ifdef __linux__
   240 		if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path))
   241 			&& !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path))
   242 			&& !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path)))
   243 #else
   244 		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   245 #endif
   246 		{
   247 			mm_LogFatal("Could not find path for vstdlib");
   248 			return NULL;
   249 		}
   250 
   251 		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   252 		{
   253 			mm_LogFatal("Could not load %s: %s", lib_path, error);
   254 			return NULL;
   255 		}
   256 
   257 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   258 	}
   259 
   260 	if (valve_cmdline == NULL)
   261 	{
   262 		mm_LogFatal("Could not locate any command line functionality");
   263 		return NULL;
   264 	}
   265 
   266 	game_name = valve_cmdline()->ParmValue("-game");
   267 
   268 	mm_UnloadLibrary(lib);
   269 
   270 	/* This probably means that the game directory is actually the current directory */
   271 	if (!game_name)
   272 	{
   273 		game_name = ".";
   274 	}
   275 
   276 	return game_name;
   277 }
   278 
   279 MetamodBackend
   280 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   281 {
   282 	/* Check for L4D */
   283 	if (engineFactory("VEngineServer023", NULL) != NULL)
   284 	{
   285 		if (engineFactory("ISERVERPLUGINHELPERS001", NULL) != NULL)
   286 			return MMBackend_CSGO;
   287 		else
   288 			return MMBackend_DOTA;
   289 	}
   290 	else if (engineFactory("VEngineServer022", NULL) != NULL &&
   291 		engineFactory("VEngineCvar007", NULL) != NULL)
   292 	{
   293 		if (strcmp(game_name, "portal2") == 0)
   294 		{
   295 			return MMBackend_Portal2;
   296 		}
   297 		if (engineFactory("EngineTraceServer004", NULL) != NULL)
   298 		{
   299 			return MMBackend_AlienSwarm;
   300 		}
   301 		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   302 		{
   303 			return MMBackend_Left4Dead2;
   304 		}
   305 		return MMBackend_Left4Dead;
   306 	}
   307 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   308 	{
   309 		/* Check for OB */
   310 		if (engineFactory("VEngineCvar004", NULL) != NULL)
   311 		{
   312 			if (engineFactory("VModelInfoServer002", NULL) != NULL)
   313 			{
   314 				/* BGT has same iface version numbers and libs as ep2 */
   315 				if (strcmp(game_name, "pm") == 0)
   316 				{
   317 					return MMBackend_BloodyGoodTime;
   318 				}
   319 				else
   320 				{
   321 					return MMBackend_Episode2;
   322 				}
   323 			}
   324 			else if (engineFactory("VModelInfoServer003", NULL) != NULL)
   325 			{
   326 				if (engineFactory("VFileSystem017", NULL) != NULL)
   327 				{
   328 					return MMBackend_EYE;
   329 				}
   330 				else if (strcmp(game_name, "cstrike") == 0)
   331 				{
   332 					return MMBackend_CSS;
   333 				}
   334 				else
   335 				{
   336 					return MMBackend_Episode2Valve;
   337 				}
   338 			}
   339 		}
   340 		/* Check for Episode One/Old Engine */
   341 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   342 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   343 				  engineFactory("VEngineCvar002", NULL) != NULL))
   344 		{
   345 			/* Check for Dark Messiah which has a weird directory structure */
   346 			if (strcmp(game_name, ".") == 0)
   347 			{
   348 				return MMBackend_DarkMessiah;
   349 			}
   350 			return MMBackend_Episode1;
   351 		}
   352 	}
   353 
   354 	return MMBackend_UNKNOWN;
   355 }
   356