loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Jul 22 03:22:27 2010 -0500 (2010-07-22)
changeset 725 9eec529c3caa
parent 721 d799abb69fa7
child 730 145910dd7235
permissions -rw-r--r--
Added support for Alien Swarm (bug 4529, r=dvander).
     1 /**
     2  * vim: set ts=4 sw=4 tw=99 noet :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2009 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.ep2v",
    77 	"2.l4d",
    78 	"2.l4d2",
    79 	"2.swarm"
    80 };
    81 
    82 #if defined _WIN32
    83 #define LIBRARY_EXT		".dll"
    84 #define LIBRARY_MINEXT	".dll"
    85 #elif defined __APPLE__
    86 #define LIBRARY_EXT		".dylib"
    87 #define LIBRARY_MINEXT	".dylib"
    88 #elif defined __linux__
    89 #define LIBRARY_EXT		LIB_SUFFIX
    90 #define LIBRARY_MINEXT	".so"
    91 #endif
    92 
    93 bool
    94 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    95 {
    96 	size_t len, temp_len;
    97 	char mm_path[PLATFORM_MAX_PATH * 2];
    98 
    99 	/* Get our path */
   100 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
   101 		return false;
   102 
   103 	len = strlen(mm_path);
   104 	temp_len = strlen("server" LIBRARY_EXT);
   105 	if (len < temp_len)
   106 		return false;
   107 
   108 	/* Build log file name */
   109 	mm_path[len - temp_len] = '\0';
   110 	mm_Format(mm_fatal_logfile,
   111 			  sizeof(mm_fatal_logfile),
   112 			  "%smetamod-fatal.log",
   113 			  mm_path);
   114 
   115 	/* Replace server.dll with the new binary we want */
   116 	mm_Format(&mm_path[len - temp_len],
   117 			  sizeof(mm_path) - (len - temp_len),
   118 			  "metamod.%s" LIBRARY_MINEXT,
   119 			  backend_names[backend]);
   120 
   121 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   122 
   123 	return (mm_library != NULL);
   124 }
   125 
   126 void
   127 mm_UnloadMetamodLibrary()
   128 {
   129 	mm_UnloadLibrary(mm_library);
   130 	mm_library = NULL;
   131 }
   132 
   133 #if defined _WIN32
   134 #define EXPORT extern "C" __declspec(dllexport)
   135 #elif defined __GNUC__
   136 #if __GNUC__ == 4
   137 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   138 #else
   139 #define EXPORT extern "C"
   140 #endif
   141 #endif
   142 
   143 EXPORT void *
   144 CreateInterface(const char *name, int *ret)
   145 {
   146 	/* If we've got a VSP bridge, do nothing. */
   147 	if (vsp_bridge != NULL)
   148 	{
   149 		if (ret != NULL)
   150 			*ret = 1;
   151 		return NULL;
   152 	}
   153 
   154 	void *ptr;
   155 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   156 	{
   157 		/* Either load as VSP or start VSP listener */
   158 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   159 	}
   160 	else if (gamedll_bridge == NULL)
   161 	{
   162 		/* Load as gamedll */
   163 		ptr = mm_GameDllRequest(name, ret);
   164 	}
   165 	else
   166 	{
   167 		/* If we've got a gamedll bridge, forward the request. */
   168 		return gamedll_bridge->QueryInterface(name, ret);
   169 	}
   170 
   171 	if (ret != NULL)
   172 		*ret = (ptr != NULL) ? 0 : 1;
   173 
   174 	return ptr;
   175 }
   176 
   177 void *
   178 mm_GetProcAddress(const char *name)
   179 {
   180 	return mm_GetLibAddress(mm_library, name);
   181 }
   182 
   183 #if defined _WIN32
   184 #define TIER0_NAME			"bin\\tier0.dll"
   185 #define VSTDLIB_NAME		"bin\\vstdlib.dll"
   186 #elif defined __APPLE__
   187 #define TIER0_NAME			"bin/libtier0.dylib"
   188 #define VSTDLIB_NAME		"bin/libvstdlib.dylib"
   189 #elif defined __linux__
   190 #define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
   191 #define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
   192 #endif
   193 
   194 const char *
   195 mm_GetGameName()
   196 {
   197 	void *lib;
   198 	char error[255];
   199 	GetCommandLine valve_cmdline;
   200 	char lib_path[PLATFORM_MAX_PATH];
   201 	const char *game_name;
   202 
   203 	if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
   204 	{
   205 		mm_LogFatal("Could not find path for: " TIER0_NAME);
   206 		return NULL;
   207 	}
   208 
   209 	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   210 	{
   211 		mm_LogFatal("Could not load %s: %s", lib_path, error);
   212 		return NULL;
   213 	}
   214 
   215 	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
   216 
   217 	/* '_Tier0' dropped on Alien Swarm version */
   218 	if (valve_cmdline == NULL)
   219 	{
   220 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   221 	}
   222 
   223 	if (valve_cmdline == NULL)
   224 	{
   225 		/* We probably have a Ship engine. */
   226 		mm_UnloadLibrary(lib);
   227 		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   228 		{
   229 			mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
   230 			return NULL;
   231 		}
   232 
   233 		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   234 		{
   235 			mm_LogFatal("Could not load %s: %s", lib_path, error);
   236 			return NULL;
   237 		}
   238 
   239 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   240 	}
   241 
   242 	if (valve_cmdline == NULL)
   243 	{
   244 		mm_LogFatal("Could not locate any command line functionality");
   245 		return NULL;
   246 	}
   247 
   248 	game_name = valve_cmdline()->ParmValue("-game");
   249 
   250 	mm_UnloadLibrary(lib);
   251 
   252 	/* This probably means that the game directory is actually the current directory */
   253 	if (!game_name)
   254 	{
   255 		game_name = ".";
   256 	}
   257 
   258 	return game_name;
   259 }
   260 
   261 MetamodBackend
   262 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   263 {
   264 	/* Check for L4D */
   265 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   266 		engineFactory("VEngineCvar007", NULL) != NULL)
   267 	{
   268 		if (engineFactory("EngineTraceServer004", NULL) != NULL)
   269 		{
   270 			return MMBackend_AlienSwarm;
   271 		}
   272 		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   273 		{
   274 			return MMBackend_Left4Dead2;
   275 		}
   276 		return MMBackend_Left4Dead;
   277 	}
   278 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   279 	{
   280 		/* Check for OB */
   281 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   282 			engineFactory("VModelInfoServer002", NULL) != NULL)
   283 		{
   284 			char lib_path[PLATFORM_MAX_PATH];
   285 
   286 			/* These would have failed already if they were going to? */
   287 			mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path));
   288 			void *lib = mm_LoadLibrary(lib_path, NULL, 0);
   289 			void *tier0_ctime = mm_GetLibAddress(lib, "Plat_ctime");
   290 			mm_UnloadLibrary(lib);
   291 
   292 			if (tier0_ctime != NULL)
   293 			{
   294 				return MMBackend_Episode2Valve;
   295 			}
   296 			return MMBackend_Episode2;
   297 		}
   298 		/* Check for Episode One/Old Engine */
   299 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   300 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   301 				  engineFactory("VEngineCvar002", NULL) != NULL))
   302 		{
   303 			/* Check for Dark Messiah which has a weird directory structure */
   304 			if (strcmp(game_name, ".") == 0)
   305 			{
   306 				return MMBackend_DarkMessiah;
   307 			}
   308 			return MMBackend_Episode1;
   309 		}
   310 	}
   311 
   312 	return MMBackend_UNKNOWN;
   313 }
   314