loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Feb 18 01:38:23 2009 -0600 (2009-02-18)
changeset 637 b66489d8aedc
parent 608 30b546cc4e03
child 669 2ae87570fab2
permissions -rw-r--r--
Added support for Dark Messiah engine and game. (no bug, r=me).

The development of this feature would not be possible without the support of the following people from the game's community:
Dylan Riggs, Carl Pettengill, Ed Moreland, and Christian.
     1 /**
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 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  * Version: $Id$
    26  */
    27 
    28 #include <time.h>
    29 #include <assert.h>
    30 #include <stdarg.h>
    31 #include <stdio.h>
    32 #include <stdlib.h>
    33 #include <string.h>
    34 #include "loader.h"
    35 #include "serverplugin.h"
    36 #include "gamedll.h"
    37 #include "utility.h"
    38 #include "valve_commandline.h"
    39 
    40 #undef GetCommandLine
    41 
    42 typedef ICommandLine *(*GetCommandLine)();
    43 
    44 static HMODULE mm_library = NULL;
    45 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    46 
    47 extern void
    48 mm_LogFatal(const char *message, ...)
    49 {
    50 	FILE *fp;
    51 	time_t t;
    52 	va_list ap;
    53 	char header[256];
    54 
    55 	fp = fopen(mm_fatal_logfile, "at");
    56 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    57 		return;
    58 
    59 	t = time(NULL);
    60 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    61 	fprintf(fp, "L %s: ", header);
    62 	
    63 	va_start(ap, message);
    64 	vfprintf(fp, message, ap);
    65 	va_end(ap);
    66 
    67 	fprintf(fp, "\n");
    68 
    69 	fclose(fp);	
    70 }
    71 
    72 static const char *backend_names[] =
    73 {
    74 	"1.ep1",
    75 	"2.ep2",
    76 	"2.l4d",
    77 	"2.darkm"
    78 };
    79 
    80 #if defined _WIN32
    81 #define LIBRARY_EXT		".dll"
    82 #define LIBRARY_MINEXT	".dll"
    83 #elif defined __linux__
    84 #define LIBRARY_EXT		"_i486.so"
    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_i486.so"
   177 #define VSTDLIB_NAME		"bin/vstdlib_i486.so"
   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 		return MMBackend_Left4Dead;
   248 	}
   249 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   250 	{
   251 		/* Check for OB */
   252 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   253 			engineFactory("VModelInfoServer002", NULL) != NULL)
   254 		{
   255 			return MMBackend_Episode2;
   256 		}
   257 		/* Check for Episode One/Old Engine */
   258 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   259 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   260 				  engineFactory("VEngineCvar002", NULL) != NULL))
   261 		{
   262 			/* Check for Dark Messiah which has a weird directory structure */
   263 			if (strcmp(game_name, ".") == 0)
   264 			{
   265 				return MMBackend_DarkMessiah;
   266 			}
   267 			return MMBackend_Episode1;
   268 		}
   269 	}
   270 
   271 	return MMBackend_UNKNOWN;
   272 }
   273