core/provider/provider_ep2.cpp
author Scott Ehlert <ds@alliedmods.net>
Sat May 22 00:45:44 2010 -0500 (2010-05-22)
changeset 716 6925fbd2b9fa
parent 689 4065ba5e9155
child 725 9eec529c3caa
permissions -rw-r--r--
Added Mac OS X build support to stub/sample plugin makefiles (bug 4391, 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@689
    27
#include <setjmp.h>
ds@658
    28
#include "../metamod_oslink.h"
ds@658
    29
#include <sourcehook.h>
ds@658
    30
#include <convar.h>
ds@658
    31
#include <eiface.h>
ds@658
    32
#include <tier0/icommandline.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 <filesystem.h>
ds@658
    40
#include "metamod.h"
ds@658
    41
ds@658
    42
/* Types */
ds@658
    43
typedef void (*CONPRINTF_FUNC)(const char *, ...);
ds@658
    44
struct UsrMsgInfo
ds@658
    45
{
ds@658
    46
	UsrMsgInfo()
ds@658
    47
	{
ds@658
    48
	}
ds@658
    49
	UsrMsgInfo(int s, const char *t) : size(s), name(t)
ds@658
    50
	{
ds@658
    51
	}
ds@658
    52
	int size;
ds@658
    53
	String name;
ds@658
    54
};
ds@658
    55
ds@658
    56
/* Imports */
ds@658
    57
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@658
    58
#undef CommandLine
ds@658
    59
DLL_IMPORT ICommandLine *CommandLine();
ds@658
    60
#endif
ds@658
    61
ds@658
    62
/* Functions */
ds@689
    63
void CacheUserMessages();
ds@689
    64
void Detour_Error(const tchar *pMsg, ...);
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 BaseProvider g_Ep1Provider;
ds@658
    76
static List<ConCommandBase *> conbases_unreg;
ds@689
    77
static CVector<UsrMsgInfo> usermsgs_list;
ds@689
    78
static jmp_buf usermsg_end;
ds@658
    79
ds@658
    80
ICvar *icvar = NULL;
ds@658
    81
IFileSystem *baseFs = NULL;
ds@658
    82
IServerGameDLL *server = NULL;
ds@658
    83
IVEngineServer *engine = NULL;
ds@658
    84
IServerGameClients *gameclients = NULL;
ds@658
    85
IMetamodSourceProvider *provider = &g_Ep1Provider;
ds@658
    86
ConCommand meta_local_cmd("meta", LocalCommand_Meta, "Metamod:Source control options");
ds@658
    87
ds@658
    88
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
    89
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *, const CCommand &);
ds@658
    90
#else
ds@658
    91
SH_DECL_HOOK1_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, edict_t *);
ds@658
    92
#endif
ds@658
    93
ds@658
    94
void BaseProvider::ConsolePrint(const char *str)
ds@658
    95
{
ds@658
    96
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
    97
	ConMsg("%s", str);
ds@658
    98
#else
ds@658
    99
	Msg("%s", str);
ds@658
   100
#endif
ds@658
   101
}
ds@658
   102
ds@658
   103
void BaseProvider::Notify_DLLInit_Pre(CreateInterfaceFn engineFactory, 
ds@658
   104
									  CreateInterfaceFn serverFactory)
ds@658
   105
{
ds@658
   106
	engine = (IVEngineServer *)((engineFactory)(INTERFACEVERSION_VENGINESERVER, NULL));
ds@658
   107
	if (!engine)
ds@658
   108
	{
ds@658
   109
		DisplayError("Could not find IVEngineServer! Metamod cannot load.");
ds@658
   110
		return;
ds@658
   111
	}
ds@658
   112
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   113
	icvar = (ICvar *)((engineFactory)(CVAR_INTERFACE_VERSION, NULL));
ds@658
   114
#else
ds@658
   115
	icvar = (ICvar *)((engineFactory)(VENGINE_CVAR_INTERFACE_VERSION, NULL));
ds@658
   116
#endif
ds@658
   117
	if (!icvar)
ds@658
   118
	{
ds@658
   119
		DisplayError("Could not find ICvar! Metamod cannot load.");
ds@658
   120
		return;
ds@658
   121
	}
ds@658
   122
ds@658
   123
ds@658
   124
	if ((gameclients = (IServerGameClients *)(serverFactory("ServerGameClients003", NULL)))
ds@658
   125
		== NULL)
ds@658
   126
	{
ds@658
   127
		gameclients = (IServerGameClients *)(serverFactory("ServerGameClients004", NULL));
ds@658
   128
	}
ds@658
   129
ds@658
   130
	baseFs = (IFileSystem *)((engineFactory)(FILESYSTEM_INTERFACE_VERSION, NULL));
ds@658
   131
	if (baseFs == NULL)
ds@658
   132
	{
ds@658
   133
		mm_LogMessage("Unable to find \"%s\": .vdf files will not be parsed", FILESYSTEM_INTERFACE_VERSION);
ds@658
   134
		return;
ds@658
   135
	}
ds@658
   136
ds@658
   137
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   138
	g_pCVar = icvar;
ds@658
   139
#endif
ds@658
   140
ds@658
   141
	g_SMConVarAccessor.RegisterConCommandBase(&meta_local_cmd);
ds@658
   142
ds@689
   143
	CacheUserMessages();
ds@658
   144
ds@658
   145
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@637
   146
	if (!g_SMConVarAccessor.InitConCommandBaseList())
ds@637
   147
	{
ds@637
   148
		/* This is very unlikely considering it's old engine */
ds@637
   149
		mm_LogMessage("[META] Warning: Failed to find ConCommandBase list!");
ds@637
   150
		mm_LogMessage("[META] Warning: ConVars and ConCommands cannot be unregistered properly! Please file a bug report.");
ds@658
   151
	}
ds@658
   152
#endif
ds@658
   153
ds@658
   154
	if (gameclients)
ds@658
   155
	{
ds@658
   156
		SH_ADD_HOOK_STATICFUNC(IServerGameClients, ClientCommand, gameclients, ClientCommand, false);
ds@658
   157
	}
ds@658
   158
}
ds@658
   159
ds@658
   160
void BaseProvider::Notify_DLLShutdown_Pre()
ds@658
   161
{
ds@658
   162
	g_SMConVarAccessor.RemoveMetamodCommands();
ds@658
   163
ds@658
   164
#if SOURCE_ENGINE == SE_DARKMESSIAH
ds@658
   165
	if (g_Metamod.IsLoadedAsGameDLL())
ds@658
   166
	{
ds@658
   167
		icvar->UnlinkVariables(FCVAR_GAMEDLL);
ds@658
   168
	}
ds@658
   169
#endif
ds@658
   170
}
ds@658
   171
ds@658
   172
bool BaseProvider::IsRemotePrintingAvailable()
ds@658
   173
{
ds@658
   174
	return true;
ds@658
   175
}
ds@658
   176
ds@658
   177
void BaseProvider::ClientConsolePrint(edict_t *client, const char *message)
ds@658
   178
{
ds@658
   179
	engine->ClientPrintf(client, message);
ds@658
   180
}
ds@658
   181
ds@658
   182
void BaseProvider::ServerCommand(const char *cmd)
ds@658
   183
{
ds@658
   184
	engine->ServerCommand(cmd);
ds@658
   185
}
ds@658
   186
ds@658
   187
const char *BaseProvider::GetConVarString(ConVar *convar)
ds@658
   188
{
ds@658
   189
	if (convar == NULL)
ds@658
   190
	{
ds@658
   191
		return NULL;
ds@658
   192
	}
ds@658
   193
ds@658
   194
	return convar->GetString();
ds@658
   195
}
ds@658
   196
ds@658
   197
void BaseProvider::SetConVarString(ConVar *convar, const char *str)
ds@658
   198
{
ds@658
   199
	convar->SetValue(str);
ds@658
   200
}
ds@658
   201
ds@658
   202
bool BaseProvider::IsConCommandBaseACommand(ConCommandBase *pCommand)
ds@658
   203
{
ds@658
   204
	return pCommand->IsCommand();
ds@658
   205
}
ds@658
   206
ds@658
   207
ds@658
   208
bool BaseProvider::IsSourceEngineBuildCompatible(int build)
ds@658
   209
{
ds@658
   210
	return (build == SOURCE_ENGINE_ORIGINAL
ds@658
   211
			|| build == SOURCE_ENGINE_EPISODEONE);
ds@658
   212
}
ds@658
   213
ds@658
   214
const char *BaseProvider::GetCommandLineValue(const char *key, const char *defval)
ds@658
   215
{
ds@658
   216
	if (key[0] == '-' || key[0] == '+')
ds@658
   217
	{
ds@658
   218
		return CommandLine()->ParmValue(key, defval);
ds@658
   219
	}
ds@658
   220
	else if (icvar)
ds@658
   221
	{
ds@658
   222
		const char *val;
ds@658
   223
		if ((val = icvar->GetCommandLineValue(key)) == NULL)
ds@658
   224
		{
ds@658
   225
			return defval;
ds@658
   226
		}
ds@658
   227
ds@658
   228
		return val;
ds@658
   229
	}
ds@658
   230
ds@658
   231
	return NULL;
ds@658
   232
}
ds@658
   233
ds@658
   234
int BaseProvider::TryServerGameDLL(const char *iface)
ds@658
   235
{
ds@658
   236
	if (strncmp(iface, "ServerGameDLL", 13) != 0)
ds@658
   237
	{
ds@658
   238
		return 0;
ds@658
   239
	}
ds@658
   240
ds@658
   241
	return atoi(&iface[13]);
ds@658
   242
}
ds@658
   243
ds@658
   244
bool BaseProvider::LogMessage(const char *buffer)
ds@658
   245
{
ds@658
   246
	if (!engine)
ds@658
   247
	{
ds@658
   248
		return false;
ds@658
   249
	}
ds@658
   250
ds@658
   251
	engine->LogPrint(buffer);
ds@658
   252
ds@658
   253
	return true;
ds@658
   254
}
ds@658
   255
ds@658
   256
bool BaseProvider::GetHookInfo(ProvidedHooks hook, SourceHook::MemFuncInfo *pInfo)
ds@658
   257
{
ds@658
   258
	SourceHook::MemFuncInfo mfi = {true, -1, 0, 0};
ds@658
   259
ds@658
   260
	if (hook == ProvidedHook_LevelInit)
ds@658
   261
	{
ds@658
   262
		SourceHook::GetFuncInfo(&IServerGameDLL::LevelInit, mfi);
ds@658
   263
	}
ds@658
   264
	else if (hook == ProvidedHook_LevelShutdown)
ds@658
   265
	{
ds@658
   266
		SourceHook::GetFuncInfo(&IServerGameDLL::LevelShutdown, mfi);
ds@658
   267
	}
ds@658
   268
	else if (hook == ProvidedHook_GameInit)
ds@658
   269
	{
ds@658
   270
		SourceHook::GetFuncInfo(&IServerGameDLL::GameInit, mfi);
ds@658
   271
	}
ds@658
   272
ds@658
   273
	*pInfo = mfi;
ds@658
   274
ds@658
   275
	return (mfi.thisptroffs >= 0);
ds@658
   276
}
ds@658
   277
ds@658
   278
void BaseProvider::DisplayError(const char *fmt, ...)
ds@658
   279
{
ds@658
   280
	va_list ap;
ds@658
   281
	char buffer[2048];
ds@658
   282
ds@658
   283
	va_start(ap, fmt);
ds@658
   284
	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
ds@658
   285
	va_end(ap);
ds@658
   286
ds@658
   287
	Error(buffer);
ds@658
   288
}
ds@658
   289
ds@658
   290
void BaseProvider::DisplayWarning(const char *fmt, ...)
ds@658
   291
{
ds@658
   292
	va_list ap;
ds@658
   293
	char buffer[2048];
ds@658
   294
ds@658
   295
	va_start(ap, fmt);
ds@658
   296
	UTIL_FormatArgs(buffer, sizeof(buffer), fmt, ap);
ds@658
   297
	va_end(ap);
ds@658
   298
ds@658
   299
	Warning(buffer);
ds@658
   300
}
ds@658
   301
ds@658
   302
IConCommandBaseAccessor *BaseProvider::GetConCommandBaseAccessor()
ds@658
   303
{
ds@658
   304
	return &g_SMConVarAccessor;
ds@658
   305
}
ds@658
   306
ds@658
   307
bool BaseProvider::RegisterConCommandBase(ConCommandBase *pCommand)
ds@658
   308
{
ds@658
   309
	return g_SMConVarAccessor.Register(pCommand);
ds@658
   310
}
ds@658
   311
ds@658
   312
void BaseProvider::UnregisterConCommandBase(ConCommandBase *pCommand)
ds@658
   313
{
ds@658
   314
	return g_SMConVarAccessor.Unregister(pCommand);
ds@658
   315
}
ds@658
   316
ds@658
   317
int BaseProvider::GetUserMessageCount()
ds@658
   318
{
ds@658
   319
	return (int)usermsgs_list.size();
ds@658
   320
}
ds@658
   321
ds@658
   322
int BaseProvider::FindUserMessage(const char *name, int *size)
ds@658
   323
{
ds@658
   324
	for (size_t i = 0; i < usermsgs_list.size(); i++)
ds@658
   325
	{
ds@658
   326
		if (usermsgs_list[i].name.compare(name) == 0)
ds@658
   327
		{
ds@658
   328
			if (size)
ds@658
   329
			{
ds@658
   330
				*size = usermsgs_list[i].size;
ds@658
   331
			}
ds@658
   332
			return (int)i;
ds@658
   333
		}
ds@658
   334
	}
ds@658
   335
	
ds@658
   336
	return -1;
ds@658
   337
}
ds@658
   338
ds@658
   339
const char *BaseProvider::GetUserMessage(int index, int *size)
ds@658
   340
{
ds@689
   341
	if (index < 0 || index >= (int)usermsgs_list.size())
ds@658
   342
	{
ds@658
   343
		return NULL;
ds@658
   344
	}
ds@658
   345
ds@658
   346
	if (size)
ds@658
   347
	{
ds@658
   348
		*size = usermsgs_list[index].size;
ds@658
   349
	}
ds@658
   350
ds@658
   351
	return usermsgs_list[index].name.c_str();
ds@658
   352
}
ds@658
   353
ds@658
   354
const char *BaseProvider::GetGameDescription()
ds@658
   355
{
ds@658
   356
	return server->GetGameDescription();
ds@658
   357
}
ds@658
   358
ds@658
   359
int BaseProvider::DetermineSourceEngine(const char *game)
ds@658
   360
{
ds@669
   361
#if SOURCE_ENGINE == SE_LEFT4DEAD2
ds@669
   362
	return SOURCE_ENGINE_LEFT4DEAD2;
ds@669
   363
#elif SOURCE_ENGINE == SE_LEFT4DEAD
ds@658
   364
	return SOURCE_ENGINE_LEFT4DEAD;
ds@658
   365
#elif SOURCE_ENGINE == SE_ORANGEBOX
ds@658
   366
	return SOURCE_ENGINE_ORANGEBOX;
ds@674
   367
#elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
ds@674
   368
	return SOURCE_ENGINE_ORANGEBOXVALVE;
ds@669
   369
#elif SOURCE_ENGINE == SE_DARKMESSIAH
ds@669
   370
	return SOURCE_ENGINE_DARKMESSIAH;
ds@658
   371
#else
ds@669
   372
#error "SOURCE_ENGINE not defined to a known value"
ds@658
   373
#endif
ds@658
   374
}
ds@658
   375
ds@658
   376
ConVar *BaseProvider::CreateConVar(const char *name,
ds@658
   377
								   const char *defval,
ds@658
   378
								   const char *help,
ds@658
   379
								   int flags)
ds@658
   380
{
ds@658
   381
	int newflags = 0;
ds@658
   382
	if (flags & ConVarFlag_Notify)
ds@658
   383
	{
ds@658
   384
		newflags |= FCVAR_NOTIFY;
ds@658
   385
	}
ds@658
   386
	if (flags & ConVarFlag_SpOnly)
ds@658
   387
	{
ds@658
   388
		newflags |= FCVAR_SPONLY;
ds@658
   389
	}
ds@658
   390
ds@658
   391
	ConVar *pVar = new ConVar(name, defval, newflags, help);
ds@658
   392
ds@658
   393
	g_SMConVarAccessor.RegisterConCommandBase(pVar);
ds@658
   394
ds@658
   395
	return pVar;
ds@658
   396
}
ds@658
   397
ds@658
   398
bool BaseProvider::ProcessVDF(const char *file, char path[], size_t path_len, char alias[], size_t alias_len)
ds@658
   399
{
ds@658
   400
	if (baseFs == NULL)
ds@658
   401
	{
ds@658
   402
		return false;
ds@658
   403
	}
ds@658
   404
ds@658
   405
	KeyValues *pValues;
ds@658
   406
	const char *plugin_file, *p_alias;
ds@658
   407
ds@658
   408
	pValues = new KeyValues("Metamod Plugin");
ds@658
   409
ds@658
   410
	if (!pValues->LoadFromFile(baseFs, file))
ds@658
   411
	{
ds@658
   412
		pValues->deleteThis();
ds@658
   413
		return false;
ds@658
   414
	}
ds@658
   415
ds@658
   416
	if ((plugin_file = pValues->GetString("file", NULL)) == NULL)
ds@658
   417
	{
ds@658
   418
		pValues->deleteThis();
ds@658
   419
		return false;
ds@658
   420
	}
ds@658
   421
ds@658
   422
	UTIL_Format(path, path_len, "%s", plugin_file);
ds@658
   423
ds@658
   424
	if ((p_alias = pValues->GetString("alias", NULL)) != NULL)
ds@658
   425
	{
ds@658
   426
		UTIL_Format(alias, alias_len, "%s", p_alias);
ds@658
   427
	}
ds@658
   428
	else
ds@658
   429
	{
ds@658
   430
		UTIL_Format(alias, alias_len, "");
ds@658
   431
	}
ds@658
   432
ds@658
   433
	pValues->deleteThis();
ds@658
   434
ds@658
   435
	return true;
ds@658
   436
}
ds@658
   437
ds@658
   438
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   439
class GlobCommand : public IMetamodSourceCommandInfo
ds@658
   440
{
ds@658
   441
public:
ds@658
   442
	GlobCommand(const CCommand *cmd) : m_cmd(cmd)
ds@658
   443
	{
ds@658
   444
	}
ds@658
   445
public:
ds@658
   446
	unsigned int GetArgCount()
ds@658
   447
	{
ds@658
   448
		return m_cmd->ArgC() - 1;
ds@658
   449
	}
ds@658
   450
ds@658
   451
	const char *GetArg(unsigned int num)
ds@658
   452
	{
ds@658
   453
		return m_cmd->Arg(num);
ds@658
   454
	}
ds@658
   455
ds@658
   456
	const char *GetArgString()
ds@658
   457
	{
ds@658
   458
		return m_cmd->ArgS();
ds@658
   459
	}
ds@658
   460
private:
ds@658
   461
	const CCommand *m_cmd;
ds@658
   462
};
ds@658
   463
#else
ds@658
   464
class GlobCommand : public IMetamodSourceCommandInfo
ds@658
   465
{
ds@658
   466
public:
ds@658
   467
	unsigned int GetArgCount()
ds@658
   468
	{
ds@658
   469
		return engine->Cmd_Argc() - 1;
ds@658
   470
	}
ds@658
   471
ds@658
   472
	const char *GetArg(unsigned int num)
ds@658
   473
	{
ds@658
   474
		return engine->Cmd_Argv(num);
ds@658
   475
	}
ds@658
   476
ds@658
   477
	const char *GetArgString()
ds@658
   478
	{
ds@658
   479
		return engine->Cmd_Args();
ds@658
   480
	}
ds@658
   481
};
ds@658
   482
#endif
ds@658
   483
ds@658
   484
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   485
void LocalCommand_Meta(const CCommand &args)
ds@658
   486
{
ds@658
   487
	GlobCommand cmd(&args);
ds@658
   488
#else
ds@658
   489
void LocalCommand_Meta()
ds@658
   490
{
ds@658
   491
	GlobCommand cmd;
ds@658
   492
#endif
ds@658
   493
	Command_Meta(&cmd);
ds@658
   494
}
ds@658
   495
ds@658
   496
#if SOURCE_ENGINE >= SE_ORANGEBOX
ds@658
   497
void ClientCommand(edict_t *pEdict, const CCommand &_cmd)
ds@658
   498
{
ds@658
   499
	GlobCommand cmd(&_cmd);
ds@658
   500
#else
ds@658
   501
void ClientCommand(edict_t *pEdict)
ds@658
   502
{
ds@658
   503
	GlobCommand cmd;
ds@658
   504
#endif
ds@658
   505
	if (strcmp(cmd.GetArg(0), "meta") == 0)
ds@658
   506
	{
ds@658
   507
		Command_ClientMeta(pEdict, &cmd);
ds@658
   508
		RETURN_META(MRES_SUPERCEDE);
ds@658
   509
	}
ds@658
   510
ds@658
   511
	RETURN_META(MRES_IGNORED);
ds@658
   512
}
ds@658
   513
ds@689
   514
/* This only gets called if IServerGameDLL::GetUserMessageInfo() triggers it */
ds@689
   515
void Detour_Error(const tchar *pMsg, ...)
ds@689
   516
{
ds@689
   517
	/* Jump back to setjmp() in CacheUserMessages() */
ds@689
   518
	longjmp(usermsg_end, 1);
ds@689
   519
}
ds@658
   520
ds@689
   521
#define IA32_JMP_IMM32 0xE9
ds@658
   522
ds@689
   523
/* IServerGameDLL::GetUserMessageInfo() crashes on games based on the old engine and
ds@689
   524
 * early Orange Box. This is because Error() from tier0 gets called when a bad index is
ds@689
   525
 * passed. This is all due to a bug in CUtlRBTree::IsValidIndex().
ds@689
   526
 *
ds@689
   527
 * So we detour Error() to fix this. Our detour then jumps back into CacheUserMessages()
ds@689
   528
 * to a point before GetUserMessageInfo() is called. The detour is then removed and we
ds@689
   529
 * exit.
ds@689
   530
 */
ds@689
   531
void CacheUserMessages()
ds@658
   532
{
ds@689
   533
	int q, size;
ds@689
   534
	char buffer[256];
ds@689
   535
	unsigned char *target, *detour;
ds@689
   536
	unsigned char orig_bytes[5];
ds@658
   537
ds@689
   538
	target = (unsigned char *)&Error;
ds@689
   539
	detour = (unsigned char *)&Detour_Error;
ds@689
   540
ds@689
   541
	/* Save bytes from target function */
ds@689
   542
	memcpy(orig_bytes, target, sizeof(orig_bytes));
ds@658
   543
ds@689
   544
	/* Patch in relative jump to our Error() detour */
ds@689
   545
	SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
ds@689
   546
	target[0] = IA32_JMP_IMM32;
ds@689
   547
	*(int32_t *)&target[1] = (int32_t)(detour - (target + 5));
ds@689
   548
ds@689
   549
	/* This is where longjmp() will end up */
ds@689
   550
	if (setjmp(usermsg_end))
ds@658
   551
	{
ds@689
   552
		/* Restore bytes and memory protection */
ds@689
   553
		memcpy(target, orig_bytes, sizeof(orig_bytes));
ds@689
   554
		SetMemAccess(target, sizeof(orig_bytes), SH_MEM_READ|SH_MEM_EXEC);
ds@689
   555
		return;
ds@658
   556
	}
ds@658
   557
ds@689
   558
	q = 0;
ds@658
   559
ds@689
   560
	/* If GetUserMessageInfo() calls Error(), we should end up in our detour */
ds@689
   561
	while (server->GetUserMessageInfo(q, buffer, sizeof(buffer), size))
ds@658
   562
	{
ds@689
   563
		usermsgs_list.push_back(UsrMsgInfo(size, buffer));
ds@689
   564
		q++;
ds@658
   565
	}
ds@658
   566
ds@689
   567
	/* Jump back to setjmp() */
ds@689
   568
	longjmp(usermsg_end, 1);
ds@658
   569
}