loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Oct 29 10:57:27 2009 -0500 (2009-10-29)
changeset 669 2ae87570fab2
parent 637 b66489d8aedc
child 674 c4ed595f6248
permissions -rw-r--r--
Added support for Left 4 Dead 2 (bug 4078, 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 
    45 extern void
    46 mm_LogFatal(const char *message, ...)
    47 {
    48 	FILE *fp;
    49 	time_t t;
    50 	va_list ap;
    51 	char header[256];
    52 
    53 	fp = fopen(mm_fatal_logfile, "at");
    54 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    55 		return;
    56 
    57 	t = time(NULL);
    58 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    59 	fprintf(fp, "L %s: ", header);
    60 	
    61 	va_start(ap, message);
    62 	vfprintf(fp, message, ap);
    63 	va_end(ap);
    64 
    65 	fprintf(fp, "\n");
    66 
    67 	fclose(fp);	
    68 }
    69 
    70 static const char *backend_names[] =
    71 {
    72 	"1.ep1",
    73 	"2.darkm",
    74 	"2.ep2",
    75 	"2.l4d",
    76 	"2.l4d2"
    77 };
    78 
    79 #if defined _WIN32
    80 #define LIBRARY_EXT		".dll"
    81 #define LIBRARY_MINEXT	".dll"
    82 #elif defined __linux__
    83 #define LIBRARY_EXT		BINARY_SUFFIX
    84 #define LIBRARY_MINEXT	".so"
    85 #endif
    86 
    87 bool
    88 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    89 {
    90 	size_t len, temp_len;
    91 	char mm_path[PLATFORM_MAX_PATH * 2];
    92 
    93 	/* Get our path */
    94 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
    95 		return false;
    96 
    97 	len = strlen(mm_path);
    98 	temp_len = strlen("server" LIBRARY_EXT);
    99 	if (len < temp_len)
   100 		return false;
   101 
   102 	/* Build log file name */
   103 	mm_path[len - temp_len] = '\0';
   104 	mm_Format(mm_fatal_logfile,
   105 			  sizeof(mm_fatal_logfile),
   106 			  "%smetamod-fatal.log",
   107 			  mm_path);
   108 
   109 	/* Replace server.dll with the new binary we want */
   110 	mm_Format(&mm_path[len - temp_len],
   111 			  sizeof(mm_path) - (len - temp_len),
   112 			  "metamod.%s" LIBRARY_MINEXT,
   113 			  backend_names[backend]);
   114 
   115 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   116 
   117 	return (mm_library != NULL);
   118 }
   119 
   120 void
   121 mm_UnloadMetamodLibrary()
   122 {
   123 	mm_UnloadLibrary(mm_library);
   124 	mm_library = NULL;
   125 }
   126 
   127 #if defined _WIN32
   128 #define EXPORT extern "C" __declspec(dllexport)
   129 #elif defined __GNUC__
   130 #if __GNUC__ == 4
   131 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   132 #else
   133 #define EXPORT extern "C"
   134 #endif
   135 #endif
   136 
   137 EXPORT void *
   138 CreateInterface(const char *name, int *ret)
   139 {
   140 	/* If we've got a VSP bridge, do nothing. */
   141 	if (vsp_bridge != NULL)
   142 	{
   143 		if (ret != NULL)
   144 			*ret = 1;
   145 		return NULL;
   146 	}
   147 
   148 	/* If we've got a gamedll bridge, forward the request. */
   149 	if (gamedll_bridge != NULL)
   150 		return gamedll_bridge->QueryInterface(name, ret);
   151 
   152 	/* Otherwise, we're probably trying to load Metamod. */
   153 	void *ptr;
   154 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   155 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   156 	else
   157 		ptr = mm_GameDllRequest(name, ret);
   158 
   159 	if (ret != NULL)
   160 		*ret = (ptr != NULL) ? 0 : 1;
   161 
   162 	return ptr;
   163 }
   164 
   165 void *
   166 mm_GetProcAddress(const char *name)
   167 {
   168 	return mm_GetLibAddress(mm_library, name);
   169 }
   170 
   171 #if defined _WIN32
   172 #define TIER0_NAME			"bin\\tier0.dll"
   173 #define VSTDLIB_NAME		"bin\\vstdlib.dll"
   174 #elif defined __linux__
   175 #define TIER0_NAME			"bin/tier0" BINARY_SUFFIX
   176 #define VSTDLIB_NAME		"bin/vstdlib" BINARY_SUFFIX
   177 #endif
   178 
   179 const char *
   180 mm_GetGameName()
   181 {
   182 	void *lib;
   183 	char error[255];
   184 	GetCommandLine valve_cmdline;
   185 	char lib_path[PLATFORM_MAX_PATH];
   186 	const char *game_name;
   187 
   188 	if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
   189 	{
   190 		mm_LogFatal("Could not find path for: " TIER0_NAME);
   191 		return NULL;
   192 	}
   193 
   194 	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   195 	{
   196 		mm_LogFatal("Could not load %s: %s", lib_path, error);
   197 		return NULL;
   198 	}
   199 
   200 	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
   201 	if (valve_cmdline == NULL)
   202 	{
   203 		/* We probably have a Ship engine. */
   204 		mm_UnloadLibrary(lib);
   205 		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   206 		{
   207 			mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
   208 			return NULL;
   209 		}
   210 
   211 		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   212 		{
   213 			mm_LogFatal("Could not load %s: %s", lib_path, error);
   214 			return NULL;
   215 		}
   216 
   217 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   218 	}
   219 
   220 	mm_UnloadLibrary(lib);
   221 
   222 	if (valve_cmdline == NULL)
   223 	{
   224 		mm_LogFatal("Could not locate any command line functionality");
   225 		return NULL;
   226 	}
   227 
   228 	game_name = valve_cmdline()->ParmValue("-game");
   229 
   230 	/* This probably means that the game directory is actually the current directory */
   231 	if (!game_name)
   232 	{
   233 		game_name = ".";
   234 	}
   235 
   236 	return game_name;
   237 }
   238 
   239 MetamodBackend
   240 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   241 {
   242 	/* Check for L4D */
   243 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   244 		engineFactory("VEngineCvar007", NULL) != NULL)
   245 	{
   246 		if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   247 		{
   248 			return MMBackend_Left4Dead2;
   249 		}
   250 		return MMBackend_Left4Dead;
   251 	}
   252 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   253 	{
   254 		/* Check for OB */
   255 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   256 			engineFactory("VModelInfoServer002", NULL) != NULL)
   257 		{
   258 			return MMBackend_Episode2;
   259 		}
   260 		/* Check for Episode One/Old Engine */
   261 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   262 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   263 				  engineFactory("VEngineCvar002", NULL) != NULL))
   264 		{
   265 			/* Check for Dark Messiah which has a weird directory structure */
   266 			if (strcmp(game_name, ".") == 0)
   267 			{
   268 				return MMBackend_DarkMessiah;
   269 			}
   270 			return MMBackend_Episode1;
   271 		}
   272 	}
   273 
   274 	return MMBackend_UNKNOWN;
   275 }
   276