loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Thu Oct 29 16:23:25 2009 -0500 (2009-10-29)
changeset 674 c4ed595f6248
parent 669 2ae87570fab2
child 683 b507295429f9
permissions -rw-r--r--
Added separate build for Orange Box Valve engine (bug 4084, r=dvander).

Also extended MetamodVersionInfo with a game_dir member for getting the game name/directory.
And bumped METAMOD_PLAPI_VERSION to 15 for the above.
     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 
    45 extern void
    46 mm_LogFatal(const char *message, ...)
    47 {
    48 	FILE *fp;
    49 	time_t t;
    50 	va_list ap;
    51 	char header[256];
    52 
    53 	fp = fopen(mm_fatal_logfile, "at");
    54 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    55 		return;
    56 
    57 	t = time(NULL);
    58 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    59 	fprintf(fp, "L %s: ", header);
    60 	
    61 	va_start(ap, message);
    62 	vfprintf(fp, message, ap);
    63 	va_end(ap);
    64 
    65 	fprintf(fp, "\n");
    66 
    67 	fclose(fp);	
    68 }
    69 
    70 static const char *backend_names[] =
    71 {
    72 	"1.ep1",
    73 	"2.darkm",
    74 	"2.ep2",
    75 	"2.ep2v",
    76 	"2.l4d",
    77 	"2.l4d2"
    78 };
    79 
    80 #if defined _WIN32
    81 #define LIBRARY_EXT		".dll"
    82 #define LIBRARY_MINEXT	".dll"
    83 #elif defined __linux__
    84 #define LIBRARY_EXT		BINARY_SUFFIX
    85 #define LIBRARY_MINEXT	".so"
    86 #endif
    87 
    88 bool
    89 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    90 {
    91 	size_t len, temp_len;
    92 	char mm_path[PLATFORM_MAX_PATH * 2];
    93 
    94 	/* Get our path */
    95 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
    96 		return false;
    97 
    98 	len = strlen(mm_path);
    99 	temp_len = strlen("server" LIBRARY_EXT);
   100 	if (len < temp_len)
   101 		return false;
   102 
   103 	/* Build log file name */
   104 	mm_path[len - temp_len] = '\0';
   105 	mm_Format(mm_fatal_logfile,
   106 			  sizeof(mm_fatal_logfile),
   107 			  "%smetamod-fatal.log",
   108 			  mm_path);
   109 
   110 	/* Replace server.dll with the new binary we want */
   111 	mm_Format(&mm_path[len - temp_len],
   112 			  sizeof(mm_path) - (len - temp_len),
   113 			  "metamod.%s" LIBRARY_MINEXT,
   114 			  backend_names[backend]);
   115 
   116 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   117 
   118 	return (mm_library != NULL);
   119 }
   120 
   121 void
   122 mm_UnloadMetamodLibrary()
   123 {
   124 	mm_UnloadLibrary(mm_library);
   125 	mm_library = NULL;
   126 }
   127 
   128 #if defined _WIN32
   129 #define EXPORT extern "C" __declspec(dllexport)
   130 #elif defined __GNUC__
   131 #if __GNUC__ == 4
   132 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   133 #else
   134 #define EXPORT extern "C"
   135 #endif
   136 #endif
   137 
   138 EXPORT void *
   139 CreateInterface(const char *name, int *ret)
   140 {
   141 	/* If we've got a VSP bridge, do nothing. */
   142 	if (vsp_bridge != NULL)
   143 	{
   144 		if (ret != NULL)
   145 			*ret = 1;
   146 		return NULL;
   147 	}
   148 
   149 	/* If we've got a gamedll bridge, forward the request. */
   150 	if (gamedll_bridge != NULL)
   151 		return gamedll_bridge->QueryInterface(name, ret);
   152 
   153 	/* Otherwise, we're probably trying to load Metamod. */
   154 	void *ptr;
   155 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   156 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   157 	else
   158 		ptr = mm_GameDllRequest(name, ret);
   159 
   160 	if (ret != NULL)
   161 		*ret = (ptr != NULL) ? 0 : 1;
   162 
   163 	return ptr;
   164 }
   165 
   166 void *
   167 mm_GetProcAddress(const char *name)
   168 {
   169 	return mm_GetLibAddress(mm_library, name);
   170 }
   171 
   172 #if defined _WIN32
   173 #define TIER0_NAME			"bin\\tier0.dll"
   174 #define VSTDLIB_NAME		"bin\\vstdlib.dll"
   175 #elif defined __linux__
   176 #define TIER0_NAME			"bin/tier0" BINARY_SUFFIX
   177 #define VSTDLIB_NAME		"bin/vstdlib" BINARY_SUFFIX
   178 #endif
   179 
   180 const char *
   181 mm_GetGameName()
   182 {
   183 	void *lib;
   184 	char error[255];
   185 	GetCommandLine valve_cmdline;
   186 	char lib_path[PLATFORM_MAX_PATH];
   187 	const char *game_name;
   188 
   189 	if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
   190 	{
   191 		mm_LogFatal("Could not find path for: " TIER0_NAME);
   192 		return NULL;
   193 	}
   194 
   195 	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   196 	{
   197 		mm_LogFatal("Could not load %s: %s", lib_path, error);
   198 		return NULL;
   199 	}
   200 
   201 	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
   202 	if (valve_cmdline == NULL)
   203 	{
   204 		/* We probably have a Ship engine. */
   205 		mm_UnloadLibrary(lib);
   206 		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
   207 		{
   208 			mm_LogFatal("Could not find path for: " VSTDLIB_NAME);
   209 			return NULL;
   210 		}
   211 
   212 		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
   213 		{
   214 			mm_LogFatal("Could not load %s: %s", lib_path, error);
   215 			return NULL;
   216 		}
   217 
   218 		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
   219 	}
   220 
   221 	mm_UnloadLibrary(lib);
   222 
   223 	if (valve_cmdline == NULL)
   224 	{
   225 		mm_LogFatal("Could not locate any command line functionality");
   226 		return NULL;
   227 	}
   228 
   229 	game_name = valve_cmdline()->ParmValue("-game");
   230 
   231 	/* This probably means that the game directory is actually the current directory */
   232 	if (!game_name)
   233 	{
   234 		game_name = ".";
   235 	}
   236 
   237 	return game_name;
   238 }
   239 
   240 MetamodBackend
   241 mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
   242 {
   243 	/* Check for L4D */
   244 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   245 		engineFactory("VEngineCvar007", NULL) != NULL)
   246 	{
   247 		if (engineFactory("VPrecacheSystem001", NULL) != NULL)
   248 		{
   249 			return MMBackend_Left4Dead2;
   250 		}
   251 		return MMBackend_Left4Dead;
   252 	}
   253 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   254 	{
   255 		/* Check for OB */
   256 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   257 			engineFactory("VModelInfoServer002", NULL) != NULL)
   258 		{
   259 			if (strcmp(game_name, "tf") == 0 || strcmp(game_name, "dod") == 0)
   260 			{
   261 				return MMBackend_Episode2Valve;
   262 			}
   263 			return MMBackend_Episode2;
   264 		}
   265 		/* Check for Episode One/Old Engine */
   266 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   267 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   268 				  engineFactory("VEngineCvar002", NULL) != NULL))
   269 		{
   270 			/* Check for Dark Messiah which has a weird directory structure */
   271 			if (strcmp(game_name, ".") == 0)
   272 			{
   273 				return MMBackend_DarkMessiah;
   274 			}
   275 			return MMBackend_Episode1;
   276 		}
   277 	}
   278 
   279 	return MMBackend_UNKNOWN;
   280 }
   281