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