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