loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Apr 23 16:55:51 2010 -0400 (2010-04-23)
changeset 697 29b113bf1c9e
parent 683 b507295429f9
child 703 ce677af0e3b5
permissions -rw-r--r--
Fixed loading on Left 4 Dead 2 DLC (The Passing) (bug 4358, 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 };
    80 
    81 #if defined _WIN32
    82 #define LIBRARY_EXT		".dll"
    83 #define LIBRARY_MINEXT	".dll"
    84 #elif defined __linux__
    85 #define LIBRARY_EXT		LIB_SUFFIX
    86 #define LIBRARY_MINEXT	".so"
    87 #endif
    88 
    89 bool
    90 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    91 {
    92 	size_t len, temp_len;
    93 	char mm_path[PLATFORM_MAX_PATH * 2];
    94 
    95 	/* Get our path */
    96 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
    97 		return false;
    98 
    99 	len = strlen(mm_path);
   100 	temp_len = strlen("server" LIBRARY_EXT);
   101 	if (len < temp_len)
   102 		return false;
   103 
   104 	/* Build log file name */
   105 	mm_path[len - temp_len] = '\0';
   106 	mm_Format(mm_fatal_logfile,
   107 			  sizeof(mm_fatal_logfile),
   108 			  "%smetamod-fatal.log",
   109 			  mm_path);
   110 
   111 	/* Replace server.dll with the new binary we want */
   112 	mm_Format(&mm_path[len - temp_len],
   113 			  sizeof(mm_path) - (len - temp_len),
   114 			  "metamod.%s" LIBRARY_MINEXT,
   115 			  backend_names[backend]);
   116 
   117 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   118 
   119 	return (mm_library != NULL);
   120 }
   121 
   122 void
   123 mm_UnloadMetamodLibrary()
   124 {
   125 	mm_UnloadLibrary(mm_library);
   126 	mm_library = NULL;
   127 }
   128 
   129 #if defined _WIN32
   130 #define EXPORT extern "C" __declspec(dllexport)
   131 #elif defined __GNUC__
   132 #if __GNUC__ == 4
   133 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   134 #else
   135 #define EXPORT extern "C"
   136 #endif
   137 #endif
   138 
   139 EXPORT void *
   140 CreateInterface(const char *name, int *ret)
   141 {
   142 	/* If we've got a VSP bridge, do nothing. */
   143 	if (vsp_bridge != NULL)
   144 	{
   145 		if (ret != NULL)
   146 			*ret = 1;
   147 		return NULL;
   148 	}
   149 
   150 	void *ptr;
   151 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   152 	{
   153 		/* Either load as VSP or start VSP listener */
   154 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   155 	}
   156 	else if (gamedll_bridge == NULL)
   157 	{
   158 		/* Load as gamedll */
   159 		ptr = mm_GameDllRequest(name, ret);
   160 	}
   161 	else
   162 	{
   163 		/* If we've got a gamedll bridge, forward the request. */
   164 		return gamedll_bridge->QueryInterface(name, ret);
   165 	}
   166 
   167 	if (ret != NULL)
   168 		*ret = (ptr != NULL) ? 0 : 1;
   169 
   170 	return ptr;
   171 }
   172 
   173 void *
   174 mm_GetProcAddress(const char *name)
   175 {
   176 	return mm_GetLibAddress(mm_library, name);
   177 }
   178 
   179 #if defined _WIN32
   180 #define TIER0_NAME			"bin\\tier0.dll"
   181 #define VSTDLIB_NAME		"bin\\vstdlib.dll"
   182 #elif defined __linux__
   183 #define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
   184 #define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
   185 #endif
   186 
   187 const char *
   188 mm_GetGameName()
   189 {
   190 	void *lib;
   191 	char error[255];
   192 	GetCommandLine valve_cmdline;
   193 	char lib_path[PLATFORM_MAX_PATH];
   194 	const char *game_name;
   195 
   196 	if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
   197 	{
   198 		mm_LogFatal("Could not find path for: " TIER0_NAME);
   199 		return NULL;
   200 	}
   201 
   202 	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   203 	{
   204 		mm_LogFatal("Could not load %s: %s", lib_path, error);
   205 		return NULL;
   206 	}
   207 
   208 	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
   209 	if (valve_cmdline == NULL)
   210 	{
   211 		/* We probably have a Ship engine. */
   212 		mm_UnloadLibrary(lib);
   213 		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   214 		{
   215 			mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
   216 			return NULL;
   217 		}
   218 
   219 		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   220 		{
   221 			mm_LogFatal("Could not load %s: %s", lib_path, error);
   222 			return NULL;
   223 		}
   224 
   225 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   226 	}
   227 
   228 	mm_UnloadLibrary(lib);
   229 
   230 	if (valve_cmdline == NULL)
   231 	{
   232 		mm_LogFatal("Could not locate any command line functionality");
   233 		return NULL;
   234 	}
   235 
   236 	game_name = valve_cmdline()->ParmValue("-game");
   237 
   238 	/* This probably means that the game directory is actually the current directory */
   239 	if (!game_name)
   240 	{
   241 		game_name = ".";
   242 	}
   243 
   244 	return game_name;
   245 }
   246 
   247 MetamodBackend
   248 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   249 {
   250 	/* Check for L4D */
   251 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   252 		engineFactory("VEngineCvar007", NULL) != NULL)
   253 	{
   254 		if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   255 		{
   256 			return MMBackend_Left4Dead2;
   257 		}
   258 		return MMBackend_Left4Dead;
   259 	}
   260 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   261 	{
   262 		/* Check for OB */
   263 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   264 			engineFactory("VModelInfoServer002", NULL) != NULL)
   265 		{
   266 			if (strcmp(game_name, "tf") == 0 || strcmp(game_name, "dod") == 0)
   267 			{
   268 				return MMBackend_Episode2Valve;
   269 			}
   270 			return MMBackend_Episode2;
   271 		}
   272 		/* Check for Episode One/Old Engine */
   273 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   274 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   275 				  engineFactory("VEngineCvar002", NULL) != NULL))
   276 		{
   277 			/* Check for Dark Messiah which has a weird directory structure */
   278 			if (strcmp(game_name, ".") == 0)
   279 			{
   280 				return MMBackend_DarkMessiah;
   281 			}
   282 			return MMBackend_Episode1;
   283 		}
   284 	}
   285 
   286 	return MMBackend_UNKNOWN;
   287 }
   288