loader/loader.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Mar 04 10:38:17 2013 -0500 (2013-03-04)
changeset 818 1516c9026fd0
parent 809 6375d8da3383
child 820 a05f33ba5ad4
permissions -rw-r--r--
Added support for Dota 2 (bug 5640).
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@818
    84
	"2.csgo",
skamonkey@818
    85
	"2.dota",
dvander@546
    86
};
dvander@546
    87
dvander@546
    88
#if defined _WIN32
dvander@546
    89
#define LIBRARY_EXT		".dll"
dvander@546
    90
#define LIBRARY_MINEXT	".dll"
ds@705
    91
#elif defined __APPLE__
ds@705
    92
#define LIBRARY_EXT		".dylib"
ds@705
    93
#define LIBRARY_MINEXT	".dylib"
skamonkey@809
    94
#elif defined __linux__
skamonkey@809
    95
#define LIBRARY_EXT		LIB_SUFFIX
skamonkey@809
    96
#define LIBRARY_MINEXT	".so"
dvander@546
    97
#endif
dvander@546
    98
dvander@546
    99
bool
dvander@546
   100
mm_LoadMetamodLibrary(MetamodBackend backend, char *buffer, size_t maxlength)
dvander@546
   101
{
dvander@546
   102
	size_t len, temp_len;
dvander@546
   103
	char mm_path[PLATFORM_MAX_PATH * 2];
dvander@546
   104
dvander@546
   105
	/* Get our path */
dvander@546
   106
	if (!mm_GetFileOfAddress((void*)mm_GetFileOfAddress, mm_path, sizeof(mm_path)))
dvander@546
   107
		return false;
dvander@546
   108
dvander@546
   109
	len = strlen(mm_path);
dvander@546
   110
	temp_len = strlen("server" LIBRARY_EXT);
dvander@546
   111
	if (len < temp_len)
dvander@546
   112
		return false;
dvander@546
   113
dvander@546
   114
	/* Build log file name */
dvander@546
   115
	mm_path[len - temp_len] = '\0';
dvander@546
   116
	mm_Format(mm_fatal_logfile,
dvander@546
   117
			  sizeof(mm_fatal_logfile),
dvander@546
   118
			  "%smetamod-fatal.log",
dvander@546
   119
			  mm_path);
dvander@546
   120
dvander@546
   121
	/* Replace server.dll with the new binary we want */
dvander@546
   122
	mm_Format(&mm_path[len - temp_len],
dvander@546
   123
			  sizeof(mm_path) - (len - temp_len),
dvander@546
   124
			  "metamod.%s" LIBRARY_MINEXT,
dvander@546
   125
			  backend_names[backend]);
dvander@546
   126
dvander@581
   127
	mm_library = (HMODULE)mm_LoadLibrary(mm_path, buffer, maxlength);
dvander@546
   128
dvander@579
   129
	return (mm_library != NULL);
dvander@546
   130
}
dvander@546
   131
dvander@546
   132
void
dvander@546
   133
mm_UnloadMetamodLibrary()
dvander@546
   134
{
dvander@579
   135
	mm_UnloadLibrary(mm_library);
dvander@579
   136
	mm_library = NULL;
dvander@546
   137
}
dvander@546
   138
dvander@546
   139
#if defined _WIN32
dvander@546
   140
#define EXPORT extern "C" __declspec(dllexport)
dvander@546
   141
#elif defined __GNUC__
dvander@546
   142
#if __GNUC__ == 4
dvander@546
   143
#define EXPORT extern "C" __attribute__ ((visibility("default")))
dvander@546
   144
#else
dvander@546
   145
#define EXPORT extern "C"
dvander@546
   146
#endif
dvander@546
   147
#endif
dvander@546
   148
dvander@546
   149
EXPORT void *
dvander@546
   150
CreateInterface(const char *name, int *ret)
dvander@546
   151
{
dvander@579
   152
	/* If we've got a VSP bridge, do nothing. */
dvander@579
   153
	if (vsp_bridge != NULL)
dvander@546
   154
	{
dvander@546
   155
		if (ret != NULL)
dvander@546
   156
			*ret = 1;
dvander@546
   157
		return NULL;
dvander@546
   158
	}
dvander@546
   159
dvander@546
   160
	void *ptr;
dvander@546
   161
	if (strncmp(name, "ISERVERPLUGINCALLBACKS", 22) == 0)
ds@683
   162
	{
ds@683
   163
		/* Either load as VSP or start VSP listener */
dvander@546
   164
		ptr = mm_GetVspCallbacks(atoi(&name[22]));
ds@683
   165
	}
ds@683
   166
	else if (gamedll_bridge == NULL)
ds@683
   167
	{
ds@683
   168
		/* Load as gamedll */
ds@683
   169
		ptr = mm_GameDllRequest(name, ret);
ds@683
   170
	}
dvander@579
   171
	else
ds@683
   172
	{
ds@683
   173
		/* If we've got a gamedll bridge, forward the request. */
ds@683
   174
		return gamedll_bridge->QueryInterface(name, ret);
ds@683
   175
	}
dvander@546
   176
dvander@546
   177
	if (ret != NULL)
dvander@579
   178
		*ret = (ptr != NULL) ? 0 : 1;
dvander@546
   179
dvander@579
   180
	return ptr;
dvander@546
   181
}
dvander@546
   182
dvander@579
   183
void *
dvander@546
   184
mm_GetProcAddress(const char *name)
dvander@546
   185
{
dvander@579
   186
	return mm_GetLibAddress(mm_library, name);
dvander@546
   187
}
dvander@546
   188
ds@637
   189
#if defined _WIN32
ds@637
   190
#define TIER0_NAME			"bin\\tier0.dll"
ds@637
   191
#define VSTDLIB_NAME		"bin\\vstdlib.dll"
ds@705
   192
#elif defined __APPLE__
ds@705
   193
#define TIER0_NAME			"bin/libtier0.dylib"
ds@705
   194
#define VSTDLIB_NAME		"bin/libvstdlib.dylib"
ds@637
   195
#elif defined __linux__
ds@697
   196
#define TIER0_NAME			"bin/" LIB_PREFIX "tier0" LIB_SUFFIX
ds@697
   197
#define VSTDLIB_NAME		"bin/" LIB_PREFIX "vstdlib" LIB_SUFFIX
ds@637
   198
#endif
ds@637
   199
ds@637
   200
const char *
ds@637
   201
mm_GetGameName()
ds@637
   202
{
ds@637
   203
	void *lib;
ds@637
   204
	char error[255];
ds@637
   205
	GetCommandLine valve_cmdline;
ds@637
   206
	char lib_path[PLATFORM_MAX_PATH];
ds@637
   207
	const char *game_name;
ds@637
   208
skamonkey@807
   209
#ifdef __linux__
skamonkey@807
   210
	if (!mm_ResolvePath("bin/libtier0_srv.so", lib_path, sizeof(lib_path))
skamonkey@807
   211
		&& !mm_ResolvePath("bin/libtier0.so", lib_path, sizeof(lib_path))
skamonkey@807
   212
		&& !mm_ResolvePath("bin/tier0_i486.so", lib_path, sizeof(lib_path)))
skamonkey@807
   213
#else
skamonkey@807
   214
        if (!mm_ResolvePath(TIER0_NAME, lib_path, sizeof(lib_path)))
skamonkey@807
   215
#endif
ds@637
   216
	{
skamonkey@807
   217
		mm_LogFatal("Could not find path for tier0");
ds@637
   218
		return NULL;
ds@637
   219
	}
ds@637
   220
ds@637
   221
	if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
ds@637
   222
	{
ds@637
   223
		mm_LogFatal("Could not load %s: %s", lib_path, error);
ds@637
   224
		return NULL;
ds@637
   225
	}
ds@637
   226
ds@637
   227
	valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine_Tier0");
ds@725
   228
ds@725
   229
	/* '_Tier0' dropped on Alien Swarm version */
ds@725
   230
	if (valve_cmdline == NULL)
ds@725
   231
	{
ds@725
   232
		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
ds@725
   233
	}
ds@725
   234
ds@637
   235
	if (valve_cmdline == NULL)
ds@637
   236
	{
ds@637
   237
		/* We probably have a Ship engine. */
ds@637
   238
		mm_UnloadLibrary(lib);
skamonkey@807
   239
#ifdef __linux__
skamonkey@807
   240
		if (!mm_ResolvePath("bin/libvstdlib_srv.so", lib_path, sizeof(lib_path))
skamonkey@807
   241
			&& !mm_ResolvePath("bin/libvstdlib.so", lib_path, sizeof(lib_path))
skamonkey@807
   242
			&& !mm_ResolvePath("bin/vstdlib_i486.so", lib_path, sizeof(lib_path)))
skamonkey@807
   243
#else
ds@637
   244
		if (!mm_ResolvePath(VSTDLIB_NAME, lib_path, sizeof(lib_path)))
skamonkey@807
   245
#endif
ds@637
   246
		{
skamonkey@807
   247
			mm_LogFatal("Could not find path for vstdlib");
ds@637
   248
			return NULL;
ds@637
   249
		}
ds@637
   250
ds@637
   251
		if ((lib = mm_LoadLibrary(lib_path, error, sizeof(error))) == NULL)
ds@637
   252
		{
ds@637
   253
			mm_LogFatal("Could not load %s: %s", lib_path, error);
ds@637
   254
			return NULL;
ds@637
   255
		}
ds@637
   256
ds@637
   257
		valve_cmdline = (GetCommandLine)mm_GetLibAddress(lib, "CommandLine");
ds@637
   258
	}
ds@637
   259
ds@637
   260
	if (valve_cmdline == NULL)
ds@637
   261
	{
ds@637
   262
		mm_LogFatal("Could not locate any command line functionality");
ds@637
   263
		return NULL;
ds@637
   264
	}
ds@637
   265
ds@637
   266
	game_name = valve_cmdline()->ParmValue("-game");
ds@637
   267
ds@721
   268
	mm_UnloadLibrary(lib);
ds@721
   269
ds@637
   270
	/* This probably means that the game directory is actually the current directory */
ds@637
   271
	if (!game_name)
ds@637
   272
	{
ds@637
   273
		game_name = ".";
ds@637
   274
	}
ds@637
   275
ds@637
   276
	return game_name;
ds@637
   277
}
ds@637
   278
dvander@579
   279
MetamodBackend
ds@637
   280
mm_DetermineBackend(QueryValveInterface engineFactory, const char *game_name)
dvander@579
   281
{
dvander@579
   282
	/* Check for L4D */
skamonkey@780
   283
	if (engineFactory("VEngineServer023", NULL) != NULL)
skamonkey@780
   284
	{
skamonkey@818
   285
		if (engineFactory("ISERVERPLUGINHELPERS001", NULL) != NULL)
skamonkey@818
   286
			return MMBackend_CSGO;
skamonkey@818
   287
		else
skamonkey@818
   288
			return MMBackend_DOTA;
skamonkey@780
   289
	}
skamonkey@780
   290
	else if (engineFactory("VEngineServer022", NULL) != NULL &&
dvander@579
   291
		engineFactory("VEngineCvar007", NULL) != NULL)
dvander@579
   292
	{
skamonkey@780
   293
		if (strcmp(game_name, "portal2") == 0)
skamonkey@780
   294
		{
skamonkey@780
   295
			return MMBackend_Portal2;
skamonkey@780
   296
		}
ds@725
   297
		if (engineFactory("EngineTraceServer004", NULL) != NULL)
ds@725
   298
		{
ds@725
   299
			return MMBackend_AlienSwarm;
ds@725
   300
		}
ds@725
   301
		else if (engineFactory("VPrecacheSystem001", NULL) != NULL)
ds@669
   302
		{
ds@669
   303
			return MMBackend_Left4Dead2;
ds@669
   304
		}
dvander@579
   305
		return MMBackend_Left4Dead;
dvander@579
   306
	}
dvander@579
   307
	else if (engineFactory("VEngineServer021", NULL) != NULL)
dvander@579
   308
	{
dvander@579
   309
		/* Check for OB */
psychonic@738
   310
		if (engineFactory("VEngineCvar004", NULL) != NULL)
dvander@579
   311
		{
psychonic@738
   312
			if (engineFactory("VModelInfoServer002", NULL) != NULL)
psychonic@738
   313
			{
skamonkey@740
   314
				/* BGT has same iface version numbers and libs as ep2 */
skamonkey@740
   315
				if (strcmp(game_name, "pm") == 0)
skamonkey@740
   316
				{
skamonkey@740
   317
					return MMBackend_BloodyGoodTime;
skamonkey@740
   318
				}
skamonkey@740
   319
				else
skamonkey@740
   320
				{
skamonkey@740
   321
					return MMBackend_Episode2;
skamonkey@740
   322
				}
psychonic@738
   323
			}
psychonic@738
   324
			else if (engineFactory("VModelInfoServer003", NULL) != NULL)
ds@674
   325
			{
skamonkey@763
   326
				if (engineFactory("VFileSystem017", NULL) != NULL)
skamonkey@763
   327
				{
skamonkey@763
   328
					return MMBackend_EYE;
skamonkey@763
   329
				}
skamonkey@791
   330
				else if (strcmp(game_name, "cstrike") == 0)
skamonkey@791
   331
				{
skamonkey@791
   332
					return MMBackend_CSS;
skamonkey@791
   333
				}
skamonkey@763
   334
				else
skamonkey@763
   335
				{
skamonkey@763
   336
					return MMBackend_Episode2Valve;
skamonkey@763
   337
				}
ds@674
   338
			}
dvander@579
   339
		}
ds@637
   340
		/* Check for Episode One/Old Engine */
dvander@579
   341
		else if (engineFactory("VModelInfoServer001", NULL) != NULL &&
dvander@579
   342
				 (engineFactory("VEngineCvar003", NULL) != NULL ||
dvander@579
   343
				  engineFactory("VEngineCvar002", NULL) != NULL))
dvander@579
   344
		{
ds@637
   345
			/* Check for Dark Messiah which has a weird directory structure */
ds@637
   346
			if (strcmp(game_name, ".") == 0)
ds@637
   347
			{
ds@637
   348
				return MMBackend_DarkMessiah;
ds@637
   349
			}
dvander@579
   350
			return MMBackend_Episode1;
dvander@579
   351
		}
dvander@579
   352
	}
dvander@579
   353
dvander@579
   354
	return MMBackend_UNKNOWN;
dvander@579
   355
}
dvander@579
   356