loader/loader.cpp
author Scott Ehlert <ds@alliedmods.net>
Mon Nov 24 01:52:07 2008 -0600 (2008-11-24)
changeset 595 b8917bd46c0e
parent 586 ed3320c98dd7
child 608 30b546cc4e03
permissions -rw-r--r--
Fixed timestamp in mm_LogFatal/metamod-fatal.log having the day twice instead of the month.
     1 #include <time.h>
     2 #include <assert.h>
     3 #include <stdarg.h>
     4 #include <stdio.h>
     5 #include <stdlib.h>
     6 #include <string.h>
     7 #include "loader.h"
     8 #include "serverplugin.h"
     9 #include "gamedll.h"
    10 #include "utility.h"
    11 
    12 static HMODULE mm_library = NULL;
    13 static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
    14 
    15 extern void
    16 mm_LogFatal(const char *message, ...)
    17 {
    18 	FILE *fp;
    19 	time_t t;
    20 	va_list ap;
    21 	char header[256];
    22 
    23 	fp = fopen(mm_fatal_logfile, "at");
    24 	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
    25 		return;
    26 
    27 	t = time(NULL);
    28 	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
    29 	fprintf(fp, "L %s: ", header);
    30 	
    31 	va_start(ap, message);
    32 	vfprintf(fp, message, ap);
    33 	va_end(ap);
    34 
    35 	fprintf(fp, "\n");
    36 
    37 	fclose(fp);	
    38 }
    39 
    40 static const char *backend_names[3] =
    41 {
    42 	"1.ep1",
    43 	"2.ep2",
    44 	"2.l4d"
    45 };
    46 
    47 #if defined _WIN32
    48 #define LIBRARY_EXT		".dll"
    49 #define LIBRARY_MINEXT	".dll"
    50 #elif defined __linux__
    51 #define LIBRARY_EXT		"_i486.so"
    52 #define LIBRARY_MINEXT	".so"
    53 #endif
    54 
    55 bool
    56 mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
    57 {
    58 	size_t len, temp_len;
    59 	char mm_path[PLATFORM_MAX_PATH * 2];
    60 
    61 	/* Get our path */
    62 	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
    63 		return false;
    64 
    65 	len = strlen(mm_path);
    66 	temp_len = strlen("server" LIBRARY_EXT);
    67 	if (len < temp_len)
    68 		return false;
    69 
    70 	/* Build log file name */
    71 	mm_path[len - temp_len] = '\0';
    72 	mm_Format(mm_fatal_logfile,
    73 			  sizeof(mm_fatal_logfile),
    74 			  "%smetamod-fatal.log",
    75 			  mm_path);
    76 
    77 	/* Replace server.dll with the new binary we want */
    78 	mm_Format(&mm_path[len - temp_len],
    79 			  sizeof(mm_path) - (len - temp_len),
    80 			  "metamod.%s" LIBRARY_MINEXT,
    81 			  backend_names[backend]);
    82 
    83 	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
    84 
    85 	return (mm_library != NULL);
    86 }
    87 
    88 void
    89 mm_UnloadMetamodLibrary()
    90 {
    91 	mm_UnloadLibrary(mm_library);
    92 	mm_library = NULL;
    93 }
    94 
    95 #if defined _WIN32
    96 #define EXPORT extern "C" __declspec(dllexport)
    97 #elif defined __GNUC__
    98 #if __GNUC__ == 4
    99 #define EXPORT extern "C" __attribute__ ((visibility("default")))
   100 #else
   101 #define EXPORT extern "C"
   102 #endif
   103 #endif
   104 
   105 EXPORT void *
   106 CreateInterface(const char *name, int *ret)
   107 {
   108 	/* If we've got a VSP bridge, do nothing. */
   109 	if (vsp_bridge != NULL)
   110 	{
   111 		if (ret != NULL)
   112 			*ret = 1;
   113 		return NULL;
   114 	}
   115 
   116 	/* If we've got a gamedll bridge, forward the request. */
   117 	if (gamedll_bridge != NULL)
   118 		return gamedll_bridge->QueryInterface(name, ret);
   119 
   120 	/* Otherwise, we're probably trying to load Metamod. */
   121 	void *ptr;
   122 	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
   123 		ptr = mm_GetVspCallbacks(atoi(&name[22]));
   124 	else
   125 		ptr = mm_GameDllRequest(name, ret);
   126 
   127 	if (ret != NULL)
   128 		*ret = (ptr != NULL) ? 0 : 1;
   129 
   130 	return ptr;
   131 }
   132 
   133 void *
   134 mm_GetProcAddress(const char *name)
   135 {
   136 	return mm_GetLibAddress(mm_library, name);
   137 }
   138 
   139 MetamodBackend
   140 mm_DetermineBackend(QueryValveInterface engineFactory)
   141 {
   142 	/* Check for L4D */
   143 	if (engineFactory("VEngineServer022", NULL) != NULL &&
   144 		engineFactory("VEngineCvar007", NULL) != NULL)
   145 	{
   146 		return MMBackend_Left4Dead;
   147 	}
   148 	else if (engineFactory("VEngineServer021", NULL) != NULL)
   149 	{
   150 		/* Check for OB */
   151 		if (engineFactory("VEngineCvar004", NULL) != NULL &&
   152 			engineFactory("VModelInfoServer002", NULL) != NULL)
   153 		{
   154 			return MMBackend_Episode2;
   155 		}
   156 		/* Check for EP1 */
   157 		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
   158 				 (engineFactory("VEngineCvar003", NULL) != NULL ||
   159 				  engineFactory("VEngineCvar002", NULL) != NULL))
   160 		{
   161 			return MMBackend_Episode1;
   162 		}
   163 	}
   164 
   165 	return MMBackend_UNKNOWN;
   166 }
   167