loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Fri Oct 26 15:48:10 2012 -0400 (2012-10-26)
changeset 807 e687c36cc673
parent 805 dcc896d4c552
child 809 6375d8da3383
permissions -rw-r--r--
Updated loader to attempt to guess correct tier0/vstdlib filenames on linux (bug 5522).
dvander@608
     1
/**
ds@669
     2
 * vim: set ts=4 sw=4 tw=99 noet :
dvander@608
     3
 * ======================================================
dvander@608
     4
 * Metamod:Source
ds@730
     5
 * Copyright (C) 2004-2010 AlliedModders LLC and authors.
dvander@608
     6
 * All rights reserved.
dvander@608
     7
 * ======================================================
dvander@608
     8
 *
dvander@608
     9
 * This software is provided 'as-is', without any express or implied warranty.
dvander@608
    10
 * In no event will the authors be held liable for any damages arising from 
dvander@608
    11
 * the use of this software.
dvander@608
    12
 * 
dvander@608
    13
 * Permission is granted to anyone to use this software for any purpose, 
dvander@608
    14
 * including commercial applications, and to alter it and redistribute it 
dvander@608
    15
 * freely, subject to the following restrictions:
dvander@608
    16
 *
dvander@608
    17
 * 1. The origin of this software must not be misrepresented; you must not 
dvander@608
    18
 * claim that you wrote the original software. If you use this software in a 
dvander@608
    19
 * product, an acknowledgment in the product documentation would be 
dvander@608
    20
 * appreciated but is not required.
dvander@608
    21
 * 2. Altered source versions must be plainly marked as such, and must not be
dvander@608
    22
 * misrepresented as being the original software.
dvander@608
    23
 * 3. This notice may not be removed or altered from any source distribution.
dvander@608
    24
 */
dvander@608
    25
dvander@546
    26
#include <time.h>
dvander@579
    27
#include <assert.h>
dvander@579
    28
#include <stdarg.h>
dvander@546
    29
#include <stdio.h>
dvander@546
    30
#include <stdlib.h>
dvander@546
    31
#include <string.h>
dvander@546
    32
#include "loader.h"
dvander@546
    33
#include "serverplugin.h"
dvander@579
    34
#include "gamedll.h"
dvander@579
    35
#include "utility.h"
ds@637
    36
#include "valve_commandline.h"
ds@637
    37
ds@637
    38
#undef GetCommandLine
ds@637
    39
ds@637
    40
typedef ICommandLine *(*GetCommandLine)();
dvander@546
    41
dvander@546
    42
static HMODULE mm_library = NULL;
dvander@546
    43
static char mm_fatal_logfile[PLATFORM_MAX_PATH] = "metamod-fatal.log";
ds@683
    44
MetamodBackend mm_backend = MMBackend_UNKNOWN;
dvander@546
    45
dvander@546
    46
extern void
dvander@546
    47
mm_LogFatal(const char *message, ...)
dvander@546
    48
{
dvander@546
    49
	FILE *fp;
dvander@546
    50
	time_t t;
dvander@546
    51
	va_list ap;
dvander@546
    52
	char header[256];
dvander@546
    53
dvander@546
    54
	fp = fopen(mm_fatal_logfile, "at");
dvander@546
    55
	if (!fp && (fp = fopen("metamod-fatal.log", "at")) == NULL)
dvander@546
    56
		return;
dvander@546
    57
dvander@546
    58
	t = time(NULL);
ds@595
    59
	strftime(header, sizeof(header), "%m/%d/%Y - %H:%M:%S", localtime(&t));
dvander@546
    60
	fprintf(fp, "L %s: ", header);
dvander@546
    61
	
dvander@546
    62
	va_start(ap, message);
dvander@546
    63
	vfprintf(fp, message, ap);
dvander@546
    64
	va_end(ap);
dvander@546
    65
dvander@546
    66
	fprintf(fp, "\n");
dvander@546
    67
dvander@546
    68
	fclose(fp);	
dvander@546
    69
}
dvander@546
    70
ds@637
    71
static const char *backend_names[] =
dvander@546
    72
{
dvander@546
    73
	"1.ep1",
ds@669
    74
	"2.darkm",
dvander@546
    75
	"2.ep2",
skamonkey@740
    76
	"2.bgt",
skamonkey@763
    77
	"2.eye",
skamonkey@791
    78
	"2.css",
ds@674
    79
	"2.ep2v",
ds@637
    80
	"2.l4d",
ds@725
    81
	"2.l4d2",
skamonkey@780
    82
	"2.swarm",
skamonkey@780
    83
	"2.portal2",
skamonkey@780
    84
	"2.csgo"
dvander@546
    85
};
dvander@546
    86
dvander@546
    87
#if defined _WIN32
dvander@546
    88
#define LIBRARY_EXT		".dll"
dvander@546
    89
#define LIBRARY_MINEXT	".dll"
ds@705
    90
#elif defined __APPLE__
ds@705
    91
#define LIBRARY_EXT		".dylib"
ds@705
    92
#define LIBRARY_MINEXT	".dylib"
dvander@546
    93
#endif
dvander@546
    94
dvander@546
    95
bool
dvander@546
    96
mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
dvander@546
    97
{
dvander@546
    98
	size_t len, temp_len;
dvander@546
    99
	char mm_path[PLATFORM_MAX_PATH * 2];
dvander@546
   100
dvander@546
   101
	/* Get our path */
dvander@546
   102
	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
dvander@546
   103
		return false;
dvander@546
   104
dvander@546
   105
	len = strlen(mm_path);
dvander@546
   106
	temp_len = strlen("server" LIBRARY_EXT);
dvander@546
   107
	if (len < temp_len)
dvander@546
   108
		return false;
dvander@546
   109
dvander@546
   110
	/* Build log file name */
dvander@546
   111
	mm_path[len - temp_len] = '\0';
dvander@546
   112
	mm_Format(mm_fatal_logfile,
dvander@546
   113
			  sizeof(mm_fatal_logfile),
dvander@546
   114
			  "%smetamod-fatal.log",
dvander@546
   115
			  mm_path);
dvander@546
   116
dvander@546
   117
	/* Replace server.dll with the new binary we want */
dvander@546
   118
	mm_Format(&mm_path[len - temp_len],
dvander@546
   119
			  sizeof(mm_path) - (len - temp_len),
dvander@546
   120
			  "metamod.%s" LIBRARY_MINEXT,
dvander@546
   121
			  backend_names[backend]);
dvander@546
   122
dvander@581
   123
	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
dvander@546
   124
dvander@579
   125
	return (mm_library != NULL);
dvander@546
   126
}
dvander@546
   127
dvander@546
   128
void
dvander@546
   129
mm_UnloadMetamodLibrary()
dvander@546
   130
{
dvander@579
   131
	mm_UnloadLibrary(mm_library);
dvander@579
   132
	mm_library = NULL;
dvander@546
   133
}
dvander@546
   134
dvander@546
   135
#if defined _WIN32
dvander@546
   136
#define EXPORT extern "C" __declspec(dllexport)
dvander@546
   137
#elif defined __GNUC__
dvander@546
   138
#if __GNUC__ == 4
dvander@546
   139
#define EXPORT extern "C" __attribute__ ((visibility("default")))
dvander@546
   140
#else
dvander@546
   141
#define EXPORT extern "C"
dvander@546
   142
#endif
dvander@546
   143
#endif
dvander@546
   144
dvander@546
   145
EXPORT void *
dvander@546
   146
CreateInterface(const char *name, int *ret)
dvander@546
   147
{
dvander@579
   148
	/* If we've got a VSP bridge, do nothing. */
dvander@579
   149
	if (vsp_bridge != NULL)
dvander@546
   150
	{
dvander@546
   151
		if (ret != NULL)
dvander@546
   152
			*ret = 1;
dvander@546
   153
		return NULL;
dvander@546
   154
	}
dvander@546
   155
dvander@546
   156
	void *ptr;
dvander@546
   157
	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
ds@683
   158
	{
ds@683
   159
		/* Either load as VSP or start VSP listener */
dvander@546
   160
		ptr = mm_GetVspCallbacks(atoi(&name[22]));
ds@683
   161
	}
ds@683
   162
	else if (gamedll_bridge == NULL)
ds@683
   163
	{
ds@683
   164
		/* Load as gamedll */
ds@683
   165
		ptr = mm_GameDllRequest(name, ret);
ds@683
   166
	}
dvander@579
   167
	else
ds@683
   168
	{
ds@683
   169
		/* If we've got a gamedll bridge, forward the request. */
ds@683
   170
		return gamedll_bridge->QueryInterface(name, ret);
ds@683
   171
	}
dvander@546
   172
dvander@546
   173
	if (ret != NULL)
dvander@579
   174
		*ret = (ptr != NULL) ? 0 : 1;
dvander@546
   175
dvander@579
   176
	return ptr;
dvander@546
   177
}
dvander@546
   178
dvander@579
   179
void *
dvander@546
   180
mm_GetProcAddress(const char *name)
dvander@546
   181
{
dvander@579
   182
	return mm_GetLibAddress(mm_library, name);
dvander@546
   183
}
dvander@546
   184
ds@637
   185
#if defined _WIN32
ds@637
   186
#define TIER0_NAME			"bin\\tier0.dll"
ds@637
   187
#define VSTDLIB_NAME		"bin\\vstdlib.dll"
ds@705
   188
#elif defined __APPLE__
ds@705
   189
#define TIER0_NAME			"bin/libtier0.dylib"
ds@705
   190
#define VSTDLIB_NAME		"bin/libvstdlib.dylib"
ds@637
   191
#elif defined __linux__
ds@697
   192
#define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
ds@697
   193
#define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
ds@637
   194
#endif
ds@637
   195
ds@637
   196
const char *
ds@637
   197
mm_GetGameName()
ds@637
   198
{
ds@637
   199
	void *lib;
ds@637
   200
	char error[255];
ds@637
   201
	GetCommandLine valve_cmdline;
ds@637
   202
	char lib_path[PLATFORM_MAX_PATH];
ds@637
   203
	const char *game_name;
ds@637
   204
skamonkey@807
   205
#ifdef __linux__
skamonkey@807
   206
	if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path))
skamonkey@807
   207
		&& !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path))
skamonkey@807
   208
		&& !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path)))
skamonkey@807
   209
#else
skamonkey@807
   210
        if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
skamonkey@807
   211
#endif
ds@637
   212
	{
skamonkey@807
   213
		mm_LogFatal("Could not find path for tier0");
ds@637
   214
		return NULL;
ds@637
   215
	}
ds@637
   216
ds@637
   217
	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
ds@637
   218
	{
ds@637
   219
		mm_LogFatal("Could not load %s: %s", lib_path, error);
ds@637
   220
		return NULL;
ds@637
   221
	}
ds@637
   222
ds@637
   223
	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
ds@725
   224
ds@725
   225
	/* '_Tier0' dropped on Alien Swarm version */
ds@725
   226
	if (valve_cmdline == NULL)
ds@725
   227
	{
ds@725
   228
		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
ds@725
   229
	}
ds@725
   230
ds@637
   231
	if (valve_cmdline == NULL)
ds@637
   232
	{
ds@637
   233
		/* We probably have a Ship engine. */
ds@637
   234
		mm_UnloadLibrary(lib);
skamonkey@807
   235
#ifdef __linux__
skamonkey@807
   236
		if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path))
skamonkey@807
   237
			&& !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path))
skamonkey@807
   238
			&& !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path)))
skamonkey@807
   239
#else
ds@637
   240
		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
skamonkey@807
   241
#endif
ds@637
   242
		{
skamonkey@807
   243
			mm_LogFatal("Could not find path for vstdlib");
ds@637
   244
			return NULL;
ds@637
   245
		}
ds@637
   246
ds@637
   247
		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
ds@637
   248
		{
ds@637
   249
			mm_LogFatal("Could not load %s: %s", lib_path, error);
ds@637
   250
			return NULL;
ds@637
   251
		}
ds@637
   252
ds@637
   253
		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
ds@637
   254
	}
ds@637
   255
ds@637
   256
	if (valve_cmdline == NULL)
ds@637
   257
	{
ds@637
   258
		mm_LogFatal("Could not locate any command line functionality");
ds@637
   259
		return NULL;
ds@637
   260
	}
ds@637
   261
ds@637
   262
	game_name = valve_cmdline()->ParmValue("-game");
ds@637
   263
ds@721
   264
	mm_UnloadLibrary(lib);
ds@721
   265
ds@637
   266
	/* This probably means that the game directory is actually the current directory */
ds@637
   267
	if (!game_name)
ds@637
   268
	{
ds@637
   269
		game_name = ".";
ds@637
   270
	}
ds@637
   271
ds@637
   272
	return game_name;
ds@637
   273
}
ds@637
   274
dvander@579
   275
MetamodBackend
ds@637
   276
mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
dvander@579
   277
{
dvander@579
   278
	/* Check for L4D */
skamonkey@780
   279
	if (engineFactory("VEngineServer023", NULL) != NULL)
skamonkey@780
   280
	{
skamonkey@780
   281
		return MMBackend_CSGO;
skamonkey@780
   282
	}
skamonkey@780
   283
	else if (engineFactory("VEngineServer022", NULL) != NULL &&
dvander@579
   284
		engineFactory("VEngineCvar007", NULL) != NULL)
dvander@579
   285
	{
skamonkey@780
   286
		if (strcmp(game_name, "portal2") == 0)
skamonkey@780
   287
		{
skamonkey@780
   288
			return MMBackend_Portal2;
skamonkey@780
   289
		}
ds@725
   290
		if (engineFactory("EngineTraceServer004", NULL) != NULL)
ds@725
   291
		{
ds@725
   292
			return MMBackend_AlienSwarm;
ds@725
   293
		}
ds@725
   294
		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
ds@669
   295
		{
ds@669
   296
			return MMBackend_Left4Dead2;
ds@669
   297
		}
dvander@579
   298
		return MMBackend_Left4Dead;
dvander@579
   299
	}
dvander@579
   300
	else if (engineFactory("VEngineServer021", NULL) != NULL)
dvander@579
   301
	{
dvander@579
   302
		/* Check for OB */
psychonic@738
   303
		if (engineFactory("VEngineCvar004", NULL) != NULL)
dvander@579
   304
		{
psychonic@738
   305
			if (engineFactory("VModelInfoServer002", NULL) != NULL)
psychonic@738
   306
			{
skamonkey@740
   307
				/* BGT has same iface version numbers and libs as ep2 */
skamonkey@740
   308
				if (strcmp(game_name, "pm") == 0)
skamonkey@740
   309
				{
skamonkey@740
   310
					return MMBackend_BloodyGoodTime;
skamonkey@740
   311
				}
skamonkey@740
   312
				else
skamonkey@740
   313
				{
skamonkey@740
   314
					return MMBackend_Episode2;
skamonkey@740
   315
				}
psychonic@738
   316
			}
psychonic@738
   317
			else if (engineFactory("VModelInfoServer003", NULL) != NULL)
ds@674
   318
			{
skamonkey@763
   319
				if (engineFactory("VFileSystem017", NULL) != NULL)
skamonkey@763
   320
				{
skamonkey@763
   321
					return MMBackend_EYE;
skamonkey@763
   322
				}
skamonkey@791
   323
				else if (strcmp(game_name, "cstrike") == 0)
skamonkey@791
   324
				{
skamonkey@791
   325
					return MMBackend_CSS;
skamonkey@791
   326
				}
skamonkey@763
   327
				else
skamonkey@763
   328
				{
skamonkey@763
   329
					return MMBackend_Episode2Valve;
skamonkey@763
   330
				}
ds@674
   331
			}
dvander@579
   332
		}
ds@637
   333
		/* Check for Episode One/Old Engine */
dvander@579
   334
		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
dvander@579
   335
				 (engineFactory("VEngineCvar003", NULL) != NULL ||
dvander@579
   336
				  engineFactory("VEngineCvar002", NULL) != NULL))
dvander@579
   337
		{
ds@637
   338
			/* Check for Dark Messiah which has a weird directory structure */
ds@637
   339
			if (strcmp(game_name, ".") == 0)
ds@637
   340
			{
ds@637
   341
				return MMBackend_DarkMessiah;
ds@637
   342
			}
dvander@579
   343
			return MMBackend_Episode1;
dvander@579
   344
		}
dvander@579
   345
	}
dvander@579
   346
dvander@579
   347
	return MMBackend_UNKNOWN;
dvander@579
   348
}
dvander@579
   349