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