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