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