loader/loader.cpp
author David Anderson <dvander@alliedmods.net>
Wed Nov 26 00:19:30 2008 -0600 (2008-11-26)
changeset 608 30b546cc4e03
parent 595 b8917bd46c0e
child 637 b66489d8aedc
permissions -rw-r--r--
Updated changelog, added license headers.
     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 
    39 static HMODULE mm_library = NULL;
    40 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    41 
    42 extern void
    43 mm_LogFatal(const char *message, ...)
    44 {
    45 	FILE *fp;
    46 	time_t t;
    47 	va_list ap;
    48 	char header[256];
    49 
    50 	fp = fopen(mm_fatal_logfile, "at");
    51 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    52 		return;
    53 
    54 	t = time(NULL);
    55 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    56 	fprintf(fp, "L %s: ", header);
    57 	
    58 	va_start(ap, message);
    59 	vfprintf(fp, message, ap);
    60 	va_end(ap);
    61 
    62 	fprintf(fp, "\n");
    63 
    64 	fclose(fp);	
    65 }
    66 
    67 static const char *backend_names[3] =
    68 {
    69 	"1.ep1",
    70 	"2.ep2",
    71 	"2.l4d"
    72 };
    73 
    74 #if defined _WIN32
    75 #define LIBRARY_EXT		".dll"
    76 #define LIBRARY_MINEXT	".dll"
    77 #elif defined __linux__
    78 #define LIBRARY_EXT		"_i486.so"
    79 #define LIBRARY_MINEXT	".so"
    80 #endif
    81 
    82 bool
    83 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    84 {
    85 	size_t len, temp_len;
    86 	char mm_path[PLATFORM_MAX_PATH * 2];
    87 
    88 	/* Get our path */
    89 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
    90 		return false;
    91 
    92 	len = strlen(mm_path);
    93 	temp_len = strlen("server" LIBRARY_EXT);
    94 	if (len < temp_len)
    95 		return false;
    96 
    97 	/* Build log file name */
    98 	mm_path[len - temp_len] = '\0';
    99 	mm_Format(mm_fatal_logfile,
   100 			  sizeof(mm_fatal_logfile),
   101 			  "%smetamod-fatal.log",
   102 			  mm_path);
   103 
   104 	/* Replace server.dll with the new binary we want */
   105 	mm_Format(&mm_path[len - temp_len],
   106 			  sizeof(mm_path) - (len - temp_len),
   107 			  "metamod.%s" LIBRARY_MINEXT,
   108 			  backend_names[backend]);
   109 
   110 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
   111 
   112 	return (mm_library != NULL);
   113 }
   114 
   115 void
   116 mm_UnloadMetamodLibrary()
   117 {
   118 	mm_UnloadLibrary(mm_library);
   119 	mm_library = NULL;
   120 }
   121 
   122 #if defined _WIN32
   123 #define EXPORT extern "C" __declspec(dllexport)
   124 #elif defined __GNUC__
   125 #if __GNUC__ == 4
   126 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   127 #else
   128 #define EXPORT extern "C"
   129 #endif
   130 #endif
   131 
   132 EXPORT void *
   133 CreateInterface(const char *name, int *ret)
   134 {
   135 	/* If we've got a VSP bridge, do nothing. */
   136 	if (vsp_bridge != NULL)
   137 	{
   138 		if (ret != NULL)
   139 			*ret = 1;
   140 		return NULL;
   141 	}
   142 
   143 	/* If we've got a gamedll bridge, forward the request. */
   144 	if (gamedll_bridge != NULL)
   145 		return gamedll_bridge->QueryInterface(name, ret);
   146 
   147 	/* Otherwise, we're probably trying to load Metamod. */
   148 	void *ptr;
   149 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   150 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   151 	else
   152 		ptr = mm_GameDllRequest(name, ret);
   153 
   154 	if (ret != NULL)
   155 		*ret = (ptr != NULL) ? 0 : 1;
   156 
   157 	return ptr;
   158 }
   159 
   160 void *
   161 mm_GetProcAddress(const char *name)
   162 {
   163 	return mm_GetLibAddress(mm_library, name);
   164 }
   165 
   166 MetamodBackend
   167 mm_DetermineBackend(QueryValveInterface engineFactory)
   168 {
   169 	/* Check for L4D */
   170 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   171 		engineFactory("VEngineCvar007", NULL) != NULL)
   172 	{
   173 		return MMBackend_Left4Dead;
   174 	}
   175 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   176 	{
   177 		/* Check for OB */
   178 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   179 			engineFactory("VModelInfoServer002", NULL) != NULL)
   180 		{
   181 			return MMBackend_Episode2;
   182 		}
   183 		/* Check for EP1 */
   184 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   185 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   186 				  engineFactory("VEngineCvar002", NULL) != NULL))
   187 		{
   188 			return MMBackend_Episode1;
   189 		}
   190 	}
   191 
   192 	return MMBackend_UNKNOWN;
   193 }
   194