core/provider/provider_ep2.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat Nov 14 00:49:35 2009 -0600 (2009-11-14)
changeset 680 39d0803bdb6e
parent 674 c4ed595f6248
child 682 f125dd3ed7d0
permissions -rw-r--r--
Fixed issue with vsnprintf macro being defined under MSVC 9/2008 (bug 3887, r=dvander).
ds@658
     1
/**
ds@669
     2
 * vim: set ts=4 sw=4 tw=99 noet :
ds@658
     3
 * ======================================================
ds@658
     4
 * Metamod:Source
ds@669
     5
 * Copyright (C) 2004-2009 AlliedModders LLC and authors.
ds@658
     6
 * All rights reserved.
ds@658
     7
 * ======================================================
ds@658
     8
 *
ds@658
     9
 * This software is provided 'as-is', without any express or implied warranty.
ds@658
    10
 * In no event will the authors be held liable for any damages arising from 
ds@658
    11
 * the use of this software.
ds@658
    12
 * 
ds@658
    13
 * Permission is granted to anyone to use this software for any purpose, 
ds@658
    14
 * including commercial applications, and to alter it and redistribute it 
ds@658
    15
 * freely, subject to the following restrictions:
ds@658
    16
 *
ds@658
    17
 * 1. The origin of this software must not be misrepresented; you must not 
ds@658
    18
 * claim that you wrote the original software. If you use this software in a 
ds@658
    19
 * product, an acknowledgment in the product documentation would be 
ds@658
    20
 * appreciated but is not required.
ds@658
    21
 * 2. Altered source versions must be plainly marked as such, and must not be
ds@658
    22
 * misrepresented as being the original software.
ds@658
    23
 * 3. This notice may not be removed or altered from any source distribution.
ds@658
    24
 */
ds@658
    25
ds@658
    26
#include <stdio.h>
ds@658
    27
#include "../metamod_oslink.h"
ds@658
    28
#include <sourcehook.h>
ds@658
    29
#include <convar.h>
ds@658
    30
#include <eiface.h>
ds@658
    31
#include <tier0/icommandline.h>
ds@658
    32
#include <tier1/utldict.h>
ds@658
    33
#include <sh_vector.h>
ds@658
    34
#include <sh_string.h>
ds@658
    35
#include "../metamod_util.h"
ds@658
    36
#include "provider_ep2.h"
ds@658
    37
#include "console.h"
ds@658
    38
#include "metamod_console.h"
ds@658
    39
#include "vsp_listener.h"
ds@658
    40
#include <filesystem.h>
ds@658
    41
#include "metamod.h"
ds@658
    42
ds@658
    43
/* Types */
ds@658
    44
typedef void (*CONPRINTF_FUNC)(const char *, ...);
ds@658
    45
struct UsrMsgInfo
ds@658
    46
{
ds@658
    47
	UsrMsgInfo()
ds@658
    48
	{
ds@658
    49
	}
ds@658
    50
	UsrMsgInfo(int s, const char *t) : size(s), name(t)
ds@658
    51
	{
ds@658
    52
	}
ds@658
    53
	int size;
ds@658
    54
	String name;
ds@658
    55
};
ds@658
    56
ds@658
    57
/* Imports */
ds@658
    58
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@658
    59
#undef CommandLine
ds@658
    60
DLL_IMPORT ICommandLine *CommandLine();
ds@658
    61
#endif
ds@658
    62
ds@658
    63
/* Functions */
ds@658
    64
bool CacheUserMessages();
ds@658
    65
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
    66
void ClientCommand(edict_t *pEdict, const CCommand &args);
ds@658
    67
void LocalCommand_Meta(const CCommand &args);
ds@658
    68
#else
ds@658
    69
void ClientCommand(edict_t *pEdict);
ds@658
    70
void LocalCommand_Meta();
ds@658
    71
#endif
ds@658
    72
ds@658
    73
void _ServerCommand();
ds@658
    74
/* Variables */
ds@658
    75
static bool usermsgs_extracted = false;
ds@658
    76
static CVector<UsrMsgInfo> usermsgs_list;
ds@658
    77
static VSPListener g_VspListener;
ds@658
    78
static BaseProvider g_Ep1Provider;
ds@658
    79
static List<ConCommandBase *> conbases_unreg;
ds@658
    80
ds@658
    81
ICvar *icvar = NULL;
ds@658
    82
IFileSystem *baseFs = NULL;
ds@658
    83
IServerGameDLL *server = NULL;
ds@658
    84
IVEngineServer *engine = NULL;
ds@658
    85
IServerGameClients *gameclients = NULL;
ds@658
    86
IMetamodSourceProvider *provider = &g_Ep1Provider;
ds@658
    87
ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
ds@658
    88
ds@658
    89
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
    90
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
ds@658
    91
#else
ds@658
    92
SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
ds@658
    93
#endif
ds@658
    94
ds@658
    95
bool AssumeUserMessages()
ds@658
    96
{
ds@658
    97
	int q, size;
ds@658
    98
	char buffer[256];
ds@658
    99
ds@658
   100
	q = 0;
ds@658
   101
ds@658
   102
	while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
ds@658
   103
	{
ds@658
   104
		usermsgs_list.push_back(UsrMsgInfo(size, buffer));
ds@658
   105
		q++;
ds@658
   106
	}
ds@658
   107
ds@658
   108
	return true;
ds@658
   109
}
ds@658
   110
ds@658
   111
void BaseProvider::ConsolePrint(const char *str)
ds@658
   112
{
ds@658
   113
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   114
	ConMsg("%s", str);
ds@658
   115
#else
ds@658
   116
	Msg("%s", str);
ds@658
   117
#endif
ds@658
   118
}
ds@658
   119
ds@658
   120
void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory, 
ds@658
   121
									  CreateInterfaceFn serverFactory)
ds@658
   122
{
ds@658
   123
	engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
ds@658
   124
	if (!engine)
ds@658
   125
	{
ds@658
   126
		DisplayError("Could not find IVEngineServer! Metamod cannot load.");
ds@658
   127
		return;
ds@658
   128
	}
ds@658
   129
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   130
	icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
ds@658
   131
#else
ds@658
   132
	icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
ds@658
   133
#endif
ds@658
   134
	if (!icvar)
ds@658
   135
	{
ds@658
   136
		DisplayError("Could not find ICvar! Metamod cannot load.");
ds@658
   137
		return;
ds@658
   138
	}
ds@658
   139
ds@658
   140
ds@658
   141
	if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
ds@658
   142
		== NULL)
ds@658
   143
	{
ds@658
   144
		gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
ds@658
   145
	}
ds@658
   146
ds@658
   147
	baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
ds@658
   148
	if (baseFs == NULL)
ds@658
   149
	{
ds@658
   150
		mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
ds@658
   151
		return;
ds@658
   152
	}
ds@658
   153
ds@658
   154
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   155
	g_pCVar = icvar;
ds@658
   156
#endif
ds@658
   157
ds@658
   158
	g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
ds@658
   159
ds@658
   160
	if ((usermsgs_extracted = CacheUserMessages()) == false)
ds@658
   161
	{
ds@658
   162
		usermsgs_extracted = AssumeUserMessages();
ds@658
   163
	}
ds@658
   164
ds@658
   165
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@637
   166
	if (!g_SMConVarAccessor.InitConCommandBaseList())
ds@637
   167
	{
ds@637
   168
		/* This is very unlikely considering it's old engine */
ds@637
   169
		mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
ds@637
   170
		mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
ds@658
   171
	}
ds@658
   172
#endif
ds@658
   173
ds@658
   174
	if (gameclients)
ds@658
   175
	{
ds@658
   176
		SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
ds@658
   177
	}
ds@658
   178
}
ds@658
   179
ds@658
   180
void BaseProvider::Notify_DLLShutdown_Pre()
ds@658
   181
{
ds@658
   182
	g_SMConVarAccessor.RemoveMetamodCommands();
ds@658
   183
ds@658
   184
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@658
   185
	if (g_Metamod.IsLoadedAsGameDLL())
ds@658
   186
	{
ds@658
   187
		icvar->UnlinkVariables(FCVAR_GAMEDLL);
ds@658
   188
	}
ds@658
   189
#endif
ds@658
   190
}
ds@658
   191
ds@658
   192
bool BaseProvider::IsRemotePrintingAvailable()
ds@658
   193
{
ds@658
   194
	return true;
ds@658
   195
}
ds@658
   196
ds@658
   197
void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
ds@658
   198
{
ds@658
   199
	engine->ClientPrintf(client, message);
ds@658
   200
}
ds@658
   201
ds@658
   202
void BaseProvider::ServerCommand(const char *cmd)
ds@658
   203
{
ds@658
   204
	engine->ServerCommand(cmd);
ds@658
   205
}
ds@658
   206
ds@658
   207
const char *BaseProvider::GetConVarString(ConVar *convar)
ds@658
   208
{
ds@658
   209
	if (convar == NULL)
ds@658
   210
	{
ds@658
   211
		return NULL;
ds@658
   212
	}
ds@658
   213
ds@658
   214
	return convar->GetString();
ds@658
   215
}
ds@658
   216
ds@658
   217
void BaseProvider::SetConVarString(ConVar *convar, const char *str)
ds@658
   218
{
ds@658
   219
	convar->SetValue(str);
ds@658
   220
}
ds@658
   221
ds@658
   222
bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
ds@658
   223
{
ds@658
   224
	return pCommand->IsCommand();
ds@658
   225
}
ds@658
   226
ds@658
   227
ds@658
   228
bool BaseProvider::IsSourceEngineBuildCompatible(int build)
ds@658
   229
{
ds@658
   230
	return (build == SOURCE_ENGINE_ORIGINAL
ds@658
   231
			|| build == SOURCE_ENGINE_EPISODEONE);
ds@658
   232
}
ds@658
   233
ds@658
   234
const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
ds@658
   235
{
ds@658
   236
	if (key[0] == '-' || key[0] == '+')
ds@658
   237
	{
ds@658
   238
		return CommandLine()->ParmValue(key, defval);
ds@658
   239
	}
ds@658
   240
	else if (icvar)
ds@658
   241
	{
ds@658
   242
		const char *val;
ds@658
   243
		if ((val = icvar->GetCommandLineValue(key)) == NULL)
ds@658
   244
		{
ds@658
   245
			return defval;
ds@658
   246
		}
ds@658
   247
ds@658
   248
		return val;
ds@658
   249
	}
ds@658
   250
ds@658
   251
	return NULL;
ds@658
   252
}
ds@658
   253
ds@658
   254
int BaseProvider::TryServerGameDLL(const char *iface)
ds@658
   255
{
ds@658
   256
	if (strncmp(iface, "ServerGameDLL", 13) != 0)
ds@658
   257
	{
ds@658
   258
		return 0;
ds@658
   259
	}
ds@658
   260
ds@658
   261
	return atoi(&iface[13]);
ds@658
   262
}
ds@658
   263
ds@658
   264
bool BaseProvider::LogMessage(const char *buffer)
ds@658
   265
{
ds@658
   266
	if (!engine)
ds@658
   267
	{
ds@658
   268
		return false;
ds@658
   269
	}
ds@658
   270
ds@658
   271
	engine->LogPrint(buffer);
ds@658
   272
ds@658
   273
	return true;
ds@658
   274
}
ds@658
   275
ds@658
   276
bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
ds@658
   277
{
ds@658
   278
	SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
ds@658
   279
ds@658
   280
	if (hook == ProvidedHook_LevelInit)
ds@658
   281
	{
ds@658
   282
		SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
ds@658
   283
	}
ds@658
   284
	else if (hook == ProvidedHook_LevelShutdown)
ds@658
   285
	{
ds@658
   286
		SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
ds@658
   287
	}
ds@658
   288
	else if (hook == ProvidedHook_GameInit)
ds@658
   289
	{
ds@658
   290
		SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
ds@658
   291
	}
ds@658
   292
ds@658
   293
	*pInfo = mfi;
ds@658
   294
ds@658
   295
	return (mfi.thisptroffs >= 0);
ds@658
   296
}
ds@658
   297
ds@658
   298
void BaseProvider::DisplayError(const char *fmt, ...)
ds@658
   299
{
ds@658
   300
	va_list ap;
ds@658
   301
	char buffer[2048];
ds@658
   302
ds@658
   303
	va_start(ap, fmt);
ds@658
   304
	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
ds@658
   305
	va_end(ap);
ds@658
   306
ds@658
   307
	Error(buffer);
ds@658
   308
}
ds@658
   309
ds@658
   310
void BaseProvider::DisplayWarning(const char *fmt, ...)
ds@658
   311
{
ds@658
   312
	va_list ap;
ds@658
   313
	char buffer[2048];
ds@658
   314
ds@658
   315
	va_start(ap, fmt);
ds@658
   316
	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
ds@658
   317
	va_end(ap);
ds@658
   318
ds@658
   319
	Warning(buffer);
ds@658
   320
}
ds@658
   321
ds@658
   322
IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
ds@658
   323
{
ds@658
   324
	return &g_SMConVarAccessor;
ds@658
   325
}
ds@658
   326
ds@658
   327
bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
ds@658
   328
{
ds@658
   329
	return g_SMConVarAccessor.Register(pCommand);
ds@658
   330
}
ds@658
   331
ds@658
   332
void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
ds@658
   333
{
ds@658
   334
	return g_SMConVarAccessor.Unregister(pCommand);
ds@658
   335
}
ds@658
   336
ds@658
   337
int BaseProvider::GetUserMessageCount()
ds@658
   338
{
ds@658
   339
	if (!usermsgs_extracted)
ds@658
   340
	{
ds@658
   341
		return -1;
ds@658
   342
	}
ds@658
   343
ds@658
   344
	return (int)usermsgs_list.size();
ds@658
   345
}
ds@658
   346
ds@658
   347
int BaseProvider::FindUserMessage(const char *name, int *size)
ds@658
   348
{
ds@658
   349
	for (size_t i = 0; i < usermsgs_list.size(); i++)
ds@658
   350
	{
ds@658
   351
		if (usermsgs_list[i].name.compare(name) == 0)
ds@658
   352
		{
ds@658
   353
			if (size)
ds@658
   354
			{
ds@658
   355
				*size = usermsgs_list[i].size;
ds@658
   356
			}
ds@658
   357
			return (int)i;
ds@658
   358
		}
ds@658
   359
	}
ds@658
   360
	
ds@658
   361
	return -1;
ds@658
   362
}
ds@658
   363
ds@658
   364
const char *BaseProvider::GetUserMessage(int index, int *size)
ds@658
   365
{
ds@658
   366
	if (!usermsgs_extracted || index < 0 || index >= (int)usermsgs_list.size())
ds@658
   367
	{
ds@658
   368
		return NULL;
ds@658
   369
	}
ds@658
   370
ds@658
   371
	if (size)
ds@658
   372
	{
ds@658
   373
		*size = usermsgs_list[index].size;
ds@658
   374
	}
ds@658
   375
ds@658
   376
	return usermsgs_list[index].name.c_str();
ds@658
   377
}
ds@658
   378
ds@658
   379
const char *BaseProvider::GetGameDescription()
ds@658
   380
{
ds@658
   381
	return server->GetGameDescription();
ds@658
   382
}
ds@658
   383
ds@658
   384
int BaseProvider::DetermineSourceEngine(const char *game)
ds@658
   385
{
ds@669
   386
#if SOURCE_ENGINE == SE_LEFT4DEAD2
ds@669
   387
	return SOURCE_ENGINE_LEFT4DEAD2;
ds@669
   388
#elif SOURCE_ENGINE == SE_LEFT4DEAD
ds@658
   389
	return SOURCE_ENGINE_LEFT4DEAD;
ds@658
   390
#elif SOURCE_ENGINE == SE_ORANGEBOX
ds@658
   391
	return SOURCE_ENGINE_ORANGEBOX;
ds@674
   392
#elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
ds@674
   393
	return SOURCE_ENGINE_ORANGEBOXVALVE;
ds@669
   394
#elif SOURCE_ENGINE == SE_DARKMESSIAH
ds@669
   395
	return SOURCE_ENGINE_DARKMESSIAH;
ds@658
   396
#else
ds@669
   397
#error "SOURCE_ENGINE not defined to a known value"
ds@658
   398
#endif
ds@658
   399
}
ds@658
   400
ds@658
   401
ConVar *BaseProvider::CreateConVar(const char *name,
ds@658
   402
								   const char *defval,
ds@658
   403
								   const char *help,
ds@658
   404
								   int flags)
ds@658
   405
{
ds@658
   406
	int newflags = 0;
ds@658
   407
	if (flags & ConVarFlag_Notify)
ds@658
   408
	{
ds@658
   409
		newflags |= FCVAR_NOTIFY;
ds@658
   410
	}
ds@658
   411
	if (flags & ConVarFlag_SpOnly)
ds@658
   412
	{
ds@658
   413
		newflags |= FCVAR_SPONLY;
ds@658
   414
	}
ds@658
   415
ds@658
   416
	ConVar *pVar = new ConVar(name, defval, newflags, help);
ds@658
   417
ds@658
   418
	g_SMConVarAccessor.RegisterConCommandBase(pVar);
ds@658
   419
ds@658
   420
	return pVar;
ds@658
   421
}
ds@658
   422
ds@658
   423
IServerPluginCallbacks *BaseProvider::GetVSPCallbacks(int version)
ds@658
   424
{
ds@658
   425
	if (version > 2)
ds@658
   426
	{
ds@658
   427
		return NULL;
ds@658
   428
	}
ds@658
   429
ds@658
   430
	g_VspListener.SetLoadable(true);
ds@658
   431
	return &g_VspListener;
ds@658
   432
}
ds@658
   433
ds@658
   434
bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
ds@658
   435
{
ds@658
   436
	if (baseFs == NULL)
ds@658
   437
	{
ds@658
   438
		return false;
ds@658
   439
	}
ds@658
   440
ds@658
   441
	KeyValues *pValues;
ds@658
   442
	const char *plugin_file, *p_alias;
ds@658
   443
ds@658
   444
	pValues = new KeyValues("Metamod Plugin");
ds@658
   445
ds@658
   446
	if (!pValues->LoadFromFile(baseFs, file))
ds@658
   447
	{
ds@658
   448
		pValues->deleteThis();
ds@658
   449
		return false;
ds@658
   450
	}
ds@658
   451
ds@658
   452
	if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
ds@658
   453
	{
ds@658
   454
		pValues->deleteThis();
ds@658
   455
		return false;
ds@658
   456
	}
ds@658
   457
ds@658
   458
	UTIL_Format(path, path_len, "%s", plugin_file);
ds@658
   459
ds@658
   460
	if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
ds@658
   461
	{
ds@658
   462
		UTIL_Format(alias, alias_len, "%s", p_alias);
ds@658
   463
	}
ds@658
   464
	else
ds@658
   465
	{
ds@658
   466
		UTIL_Format(alias, alias_len, "");
ds@658
   467
	}
ds@658
   468
ds@658
   469
	pValues->deleteThis();
ds@658
   470
ds@658
   471
	return true;
ds@658
   472
}
ds@658
   473
ds@658
   474
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   475
class GlobCommand : public IMetamodSourceCommandInfo
ds@658
   476
{
ds@658
   477
public:
ds@658
   478
	GlobCommand(const CCommand *cmd) : m_cmd(cmd)
ds@658
   479
	{
ds@658
   480
	}
ds@658
   481
public:
ds@658
   482
	unsigned int GetArgCount()
ds@658
   483
	{
ds@658
   484
		return m_cmd->ArgC() - 1;
ds@658
   485
	}
ds@658
   486
ds@658
   487
	const char *GetArg(unsigned int num)
ds@658
   488
	{
ds@658
   489
		return m_cmd->Arg(num);
ds@658
   490
	}
ds@658
   491
ds@658
   492
	const char *GetArgString()
ds@658
   493
	{
ds@658
   494
		return m_cmd->ArgS();
ds@658
   495
	}
ds@658
   496
private:
ds@658
   497
	const CCommand *m_cmd;
ds@658
   498
};
ds@658
   499
#else
ds@658
   500
class GlobCommand : public IMetamodSourceCommandInfo
ds@658
   501
{
ds@658
   502
public:
ds@658
   503
	unsigned int GetArgCount()
ds@658
   504
	{
ds@658
   505
		return engine->Cmd_Argc() - 1;
ds@658
   506
	}
ds@658
   507
ds@658
   508
	const char *GetArg(unsigned int num)
ds@658
   509
	{
ds@658
   510
		return engine->Cmd_Argv(num);
ds@658
   511
	}
ds@658
   512
ds@658
   513
	const char *GetArgString()
ds@658
   514
	{
ds@658
   515
		return engine->Cmd_Args();
ds@658
   516
	}
ds@658
   517
};
ds@658
   518
#endif
ds@658
   519
ds@658
   520
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   521
void LocalCommand_Meta(const CCommand &args)
ds@658
   522
{
ds@658
   523
	GlobCommand cmd(&args);
ds@658
   524
#else
ds@658
   525
void LocalCommand_Meta()
ds@658
   526
{
ds@658
   527
	GlobCommand cmd;
ds@658
   528
#endif
ds@658
   529
	Command_Meta(&cmd);
ds@658
   530
}
ds@658
   531
ds@658
   532
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   533
void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
ds@658
   534
{
ds@658
   535
	GlobCommand cmd(&_cmd);
ds@658
   536
#else
ds@658
   537
void ClientCommand(edict_t *pEdict)
ds@658
   538
{
ds@658
   539
	GlobCommand cmd;
ds@658
   540
#endif
ds@658
   541
	if (strcmp(cmd.GetArg(0), "meta") == 0)
ds@658
   542
	{
ds@658
   543
		Command_ClientMeta(pEdict, &cmd);
ds@658
   544
		RETURN_META(MRES_SUPERCEDE);
ds@658
   545
	}
ds@658
   546
ds@658
   547
	RETURN_META(MRES_IGNORED);
ds@658
   548
}
ds@658
   549
ds@658
   550
//////////////////////////////////////////////////////////////////////
ds@658
   551
// EVEN MORE HACKS HERE! YOU HAVE BEEN WARNED!                      //
ds@658
   552
// Signatures necessary in finding the pointer to the CUtlDict that //
ds@658
   553
//   stores user message information.                               //
ds@658
   554
// IServerGameDLL::GetUserMessageInfo() normally crashes with bad   //
ds@658
   555
//   message indices. This is our answer to it. Yuck! <:-(          //
ds@658
   556
//////////////////////////////////////////////////////////////////////
ds@658
   557
#ifdef OS_WIN32
ds@658
   558
	/* General Windows sig */
ds@658
   559
	#define MSGCLASS_SIGLEN		7
ds@658
   560
	#define MSGCLASS_SIG		"\x8B\x0D\x2A\x2A\x2A\x2A\x56"
ds@658
   561
	#define MSGCLASS_OFFS		2
ds@658
   562
ds@658
   563
	/* Dystopia Windows hack */
ds@658
   564
	#define MSGCLASS2_SIGLEN	16
ds@658
   565
	#define MSGCLASS2_SIG		"\x56\x8B\x74\x24\x2A\x85\xF6\x7C\x2A\x3B\x35\x2A\x2A\x2A\x2A\x7D"
ds@658
   566
	#define MSGCLASS2_OFFS		11
ds@658
   567
ds@658
   568
	/* Windows frame pointer sig */
ds@658
   569
	#define MSGCLASS3_SIGLEN	18
ds@658
   570
	#define MSGCLASS3_SIG		"\x55\x8B\xEC\x51\x89\x2A\x2A\x8B\x2A\x2A\x50\x8B\x0D\x2A\x2A\x2A\x2A\xE8"
ds@658
   571
	#define MSGCLASS3_OFFS		13
ds@658
   572
#elif defined OS_LINUX
ds@658
   573
	/* No frame pointer sig */
ds@658
   574
	#define MSGCLASS_SIGLEN		14
ds@658
   575
	#define MSGCLASS_SIG		"\x53\x83\xEC\x2A\x8B\x2A\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
ds@658
   576
	#define MSGCLASS_OFFS		9
ds@658
   577
ds@658
   578
	/* Frame pointer sig */
ds@658
   579
	#define MSGCLASS2_SIGLEN	16
ds@658
   580
	#define MSGCLASS2_SIG		"\x55\x89\xE5\x53\x83\xEC\x2A\x8B\x2A\x2A\xA1\x2A\x2A\x2A\x2A\x89"
ds@658
   581
	#define MSGCLASS2_OFFS		11
ds@658
   582
#endif
ds@658
   583
ds@658
   584
struct UserMessage
ds@658
   585
{
ds@658
   586
	int size;
ds@658
   587
	const char *name;
ds@658
   588
};
ds@658
   589
ds@658
   590
typedef CUtlDict<UserMessage *, int> UserMsgDict;
ds@658
   591
ds@658
   592
/* This is the ugliest function in all of MM:S */
ds@658
   593
bool CacheUserMessages()
ds@658
   594
{
ds@658
   595
	UserMsgDict *dict = NULL;
ds@658
   596
ds@658
   597
	/* Get address of original GetUserMessageInfo() */
ds@658
   598
	char *vfunc = (char *)SH_GET_ORIG_VFNPTR_ENTRY(server, &IServerGameDLL::GetUserMessageInfo);
ds@658
   599
ds@658
   600
	/* Oh dear, we have a relative jump on our hands
ds@658
   601
	 * PVK II on Windows made me do this, but I suppose it doesn't hurt to check this on Linux too...
ds@658
   602
	 */
ds@658
   603
	if (*vfunc == '\xE9')
ds@658
   604
	{
ds@658
   605
		/* Get address from displacement...
ds@658
   606
		 *
ds@658
   607
		 * Add 5 because it's relative to next instruction:
ds@658
   608
		 * Opcode <1 byte> + 32-bit displacement <4 bytes> 
ds@658
   609
		 */
ds@658
   610
		vfunc += *reinterpret_cast<int *>(vfunc + 1) + 5;
ds@658
   611
	}
ds@658
   612
ds@658
   613
	if (UTIL_VerifySignature(vfunc, MSGCLASS_SIG, MSGCLASS_SIGLEN))
ds@658
   614
	{
ds@658
   615
		/* Get address of CUserMessages instance */
ds@658
   616
		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS_OFFS);
ds@658
   617
ds@658
   618
		/* Get address of CUserMessages::m_UserMessages */
ds@658
   619
		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
ds@658
   620
	} 
ds@658
   621
	else if (UTIL_VerifySignature(vfunc, MSGCLASS2_SIG, MSGCLASS2_SIGLEN)) 
ds@658
   622
	{
ds@658
   623
	#ifdef OS_WIN32
ds@658
   624
		/* If we get here, the code is possibly inlined like in Dystopia */
ds@658
   625
ds@658
   626
		/* Get the address of the CUtlRBTree */
ds@658
   627
		char *rbtree = *reinterpret_cast<char **>(vfunc + MSGCLASS2_OFFS);
ds@658
   628
ds@658
   629
		/* CUtlDict should be 8 bytes before the CUtlRBTree (hacktacular!) */
ds@658
   630
		dict = reinterpret_cast<UserMsgDict *>(rbtree - 8);
ds@658
   631
	#elif defined OS_LINUX
ds@658
   632
		/* Get address of CUserMessages instance */
ds@658
   633
		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS2_OFFS);
ds@658
   634
ds@658
   635
		/* Get address of CUserMessages::m_UserMessages */
ds@658
   636
		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
ds@658
   637
	#endif
ds@658
   638
	#ifdef OS_WIN32
ds@658
   639
	} 
ds@658
   640
	else if (UTIL_VerifySignature(vfunc, MSGCLASS3_SIG, MSGCLASS3_SIGLEN)) 
ds@658
   641
	{
ds@658
   642
		/* Get address of CUserMessages instance */
ds@658
   643
		char **userMsgClass = *reinterpret_cast<char ***>(vfunc + MSGCLASS3_OFFS);
ds@658
   644
ds@658
   645
		/* Get address of CUserMessages::m_UserMessages */
ds@658
   646
		dict = reinterpret_cast<UserMsgDict *>(*userMsgClass);
ds@658
   647
	#endif
ds@658
   648
	}
ds@658
   649
ds@658
   650
	#if !defined OS_WIN32
ds@658
   651
	if (dict == NULL)
ds@658
   652
	{
ds@658
   653
		char path[255];
ds@658
   654
		if (GetFileOfAddress(vfunc, path, sizeof(path)))
ds@658
   655
		{
ds@658
   656
			void *handle = dlopen(path, RTLD_NOW);
ds@658
   657
			if (handle != NULL)
ds@658
   658
			{
ds@658
   659
				void *addr = dlsym(handle, "usermessages");
ds@658
   660
				if (addr == NULL)
ds@658
   661
				{
ds@658
   662
					return false;
ds@658
   663
				}
ds@658
   664
				dict = (UserMsgDict *)*(void **)addr;
ds@658
   665
				dlclose(handle);
ds@658
   666
			}
ds@658
   667
		}
ds@658
   668
	}
ds@658
   669
	#endif
ds@658
   670
ds@658
   671
	if (dict != NULL)
ds@658
   672
	{
ds@658
   673
		int msg_count = dict->Count();
ds@658
   674
ds@658
   675
		/* Ensure that count is within bounds of an unsigned byte, because that's what engine supports */
ds@658
   676
		if (msg_count < 0 || msg_count > 255)
ds@658
   677
		{
ds@658
   678
			return false;
ds@658
   679
		}
ds@658
   680
ds@658
   681
		UserMessage *msg;
ds@658
   682
		UsrMsgInfo u_msg;
ds@658
   683
ds@658
   684
		/* Cache messages in our CUtlDict */
ds@658
   685
		for (int i = 0; i < msg_count; i++)
ds@658
   686
		{
ds@658
   687
			msg = dict->Element(i);
ds@658
   688
			u_msg.name = msg->name;
ds@658
   689
			u_msg.size = msg->size;
ds@658
   690
			usermsgs_list.push_back(u_msg);
ds@658
   691
		}
ds@658
   692
ds@658
   693
		return true;
ds@658
   694
	}
ds@658
   695
ds@658
   696
	return false;
ds@658
   697
}
ds@658
   698